ExpressionImpl.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.generic_project.expression.impl;

  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import java.util.Map;

  28. import org.openspcoop2.generic_project.beans.ComplexField;
  29. import org.openspcoop2.generic_project.beans.ConstantField;
  30. import org.openspcoop2.generic_project.beans.IField;
  31. import org.openspcoop2.generic_project.beans.IModel;
  32. import org.openspcoop2.generic_project.exception.ExpressionException;
  33. import org.openspcoop2.generic_project.exception.ExpressionNotFoundException;
  34. import org.openspcoop2.generic_project.exception.ExpressionNotImplementedException;
  35. import org.openspcoop2.generic_project.expression.IExpression;
  36. import org.openspcoop2.generic_project.expression.Index;
  37. import org.openspcoop2.generic_project.expression.LikeMode;
  38. import org.openspcoop2.generic_project.expression.SortOrder;
  39. import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;
  40. import org.openspcoop2.generic_project.expression.impl.formatter.ObjectFormatter;
  41. import org.openspcoop2.utils.UtilsRuntimeException;
  42. import org.openspcoop2.utils.sql.DateTimePartEnum;
  43. import org.openspcoop2.utils.sql.DayFormatEnum;

  44. /**
  45.  * ExpressionImpl
  46.  *
  47.  * @author Poli Andrea (apoli@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  */
  51. public class ExpressionImpl implements IExpression {

  52.     private static final String FIELD_IS_NULL = "Field is null";
  53.     private static final String FIELD_TYPE_CONSTANT_CANNOT_BE_USED = "The field type 'ConstantField' can not be used with this method";
  54.    
  55.     private boolean throwExpressionNotInitialized = false;
  56.    
  57.     protected IObjectFormatter objectFormatter;
  58.     public IObjectFormatter getObjectFormatter() {
  59.         return this.objectFormatter;
  60.     }
  61.     public void setObjectFormatter(IObjectFormatter objectFormatter) {
  62.         this.objectFormatter = objectFormatter;
  63.     }
  64.    
  65.     public ExpressionImpl() throws ExpressionException {
  66.         this.objectFormatter = new ObjectFormatter();
  67.     }
  68.     public ExpressionImpl(IObjectFormatter objectFormatter) throws ExpressionException{
  69.         if(objectFormatter==null){
  70.             throw new ExpressionException("IObjectFormatter parameter is null");
  71.         }
  72.         this.objectFormatter = objectFormatter;
  73.     }
  74.     public ExpressionImpl(ExpressionImpl expr){
  75.         this.andLogicOperator = expr.andLogicOperator;
  76.         this.expressionEngine = expr.expressionEngine;
  77.         this.notOperator = expr.notOperator;
  78.         this.objectFormatter = expr.objectFormatter;
  79.         this.orderedFields = expr.orderedFields;
  80.         this.sortOrderEngine = expr.getSortOrder();
  81.         this.groupByFields = expr.groupByFields;
  82.         this.throwExpressionNotInitialized = expr.throwExpressionNotInitialized;
  83.         this.forceIndexes = expr.forceIndexes;
  84.     }
  85.    
  86.     protected boolean andLogicOperator = true;
  87.    
  88.     protected AbstractBaseExpressionImpl expressionEngine;
  89.     public AbstractBaseExpressionImpl getExpressionEngine() {
  90.         return this.expressionEngine;
  91.     }
  92.    
  93.     protected boolean notOperator = false;
  94.    
  95.     private SortOrder sortOrderEngine = SortOrder.UNSORTED;
  96.     protected List<OrderedField> orderedFields = new ArrayList<>();
  97.    
  98.     protected List<IField> groupByFields = new ArrayList<>();
  99.    
  100.     protected List<Index> forceIndexes = new ArrayList<>();
  101.    
  102.     protected Map<String, Object> properties = new HashMap<>();

  103.    
  104.    
  105.    
  106.     /* ************ STATE OF EXPRESSION *********** */
  107.    
  108.     /**
  109.      * Make the negation of the expression
  110.      *
  111.      * @return the instance of itself
  112.      * @throws ExpressionNotImplementedException Method not implemented
  113.      * @throws ExpressionException Error Processing
  114.      */
  115.     @Override
  116.     public IExpression not() throws ExpressionNotImplementedException,ExpressionException{
  117.         /**if(this.expressionEngine==null){
  118.             throw new ExpressionException("You can not use the NOT operation on an uninitialized expression");
  119.         }*/
  120.         this.notOperator = true;
  121.         if(this.expressionEngine!=null){
  122.             this.expressionEngine.setNot(true);
  123.         }
  124.         return this;
  125.     }
  126.    
  127.     /**
  128.      * Use the conjunction "AND" for all expressions
  129.      *
  130.      * @return the instance of itself
  131.      * @throws ExpressionNotImplementedException Method not implemented
  132.      * @throws ExpressionException Error Processing
  133.      */
  134.     @Override
  135.     public IExpression and() throws ExpressionNotImplementedException, ExpressionException{
  136.         this.andLogicOperator = true;
  137.         if(this.expressionEngine instanceof ConjunctionExpressionImpl){
  138.             ((ConjunctionExpressionImpl)this.expressionEngine).setAndConjunction(this.andLogicOperator);
  139.         }
  140.         return this;
  141.     }

  142.     /**
  143.      * Use the conjunction "OR" for all expressions
  144.      *
  145.      * @return the instance of itself
  146.      * @throws ExpressionNotImplementedException Method not implemented
  147.      * @throws ExpressionException Error Processing
  148.      */
  149.     @Override
  150.     public IExpression or() throws ExpressionNotImplementedException, ExpressionException{
  151.         this.andLogicOperator = false;
  152.         if(this.expressionEngine instanceof ConjunctionExpressionImpl){
  153.             ((ConjunctionExpressionImpl)this.expressionEngine).setAndConjunction(this.andLogicOperator);
  154.         }
  155.         return this;
  156.     }
  157.    
  158.    
  159.    
  160.    
  161.     /* ************ COMPARATOR *********** */
  162.    
  163.    
  164.     // Serve nella implementazione sql per poter correggere alcuni comparator dipendenti da database
  165.     protected Comparator getCorrectComparator(Comparator comparator){
  166.         return comparator;
  167.     }
  168.    
  169.     /**
  170.      * Create an expression of equality
  171.      * Example:  (field = value)
  172.      *
  173.      * @param field Resource identifier
  174.      * @param value Value
  175.      * @return Expression that represents the constraint
  176.      * @throws ExpressionNotImplementedException,ExpressionException
  177.      */
  178.     @Override
  179.     public IExpression equals(IField field, Object value)
  180.         throws ExpressionNotImplementedException, ExpressionException {
  181.         checkArgoments(field, value);
  182.         this.buildComparatorExpression(field, value, getCorrectComparator(Comparator.EQUALS));
  183.         return this;
  184.     }
  185.    
  186.     /**
  187.      * Create an expression of inequality
  188.      * Example:  (field &lt;&gt; value)
  189.      *
  190.      * @param field Resource identifier
  191.      * @param value Value
  192.      * @return the instance of itself enriched with expression that represents the constraint
  193.      * @throws ExpressionNotImplementedException,ExpressionException
  194.      */
  195.     @Override
  196.     public IExpression notEquals(IField field, Object value)
  197.     throws ExpressionNotImplementedException, ExpressionException {
  198.         checkArgoments(field, value);
  199.         this.buildComparatorExpression(field, value, getCorrectComparator(Comparator.NOT_EQUALS));
  200.         return this;
  201.     }

  202.     /**
  203.      * Create an expression of equality to each resource in the collection of the keys of the Map
  204.      * Use the conjunction "AND" for all expressions
  205.      * Example:  ( field[0]=values[0] AND field[1]=values[1] ..... AND field[N]=values[N] )
  206.      *
  207.      * @param propertyNameValues Map that contains identifiers and their values
  208.      * @return the instance of itself enriched with expression that represents the constraint
  209.      * @throws ExpressionNotImplementedException,ExpressionException
  210.      */
  211.     @Override
  212.     public IExpression allEquals(Map<IField, Object> propertyNameValues)
  213.     throws ExpressionNotImplementedException, ExpressionException {
  214.         checkArgoments(propertyNameValues);
  215.         buildComparatorExpression(propertyNameValues, getCorrectComparator(Comparator.EQUALS), true);
  216.         return this;
  217.     }

  218.     /**
  219.      * Create an expression of inequality to each resource in the collection of the keys of the Map
  220.      * Use the conjunction "AND" for all expressions
  221.      * Example:  ( field[0]&lt;&gt;values[0] AND field[1]&lt;&gt;values[1] ..... AND field[N]&lt;&gt;values[N] )
  222.      *
  223.      * @param propertyNameValues Map that contains identifiers and their values
  224.      * @return the instance of itself enriched with expression that represents the constraint
  225.      * @throws ExpressionNotImplementedException,ExpressionException
  226.      */
  227.     @Override
  228.     public IExpression allNotEquals(Map<IField, Object> propertyNameValues)
  229.     throws ExpressionNotImplementedException, ExpressionException {
  230.         checkArgoments(propertyNameValues);
  231.         buildComparatorExpression(propertyNameValues, getCorrectComparator(Comparator.NOT_EQUALS), true);
  232.         return this;
  233.     }

  234.     /**
  235.      * Create an expression of equality to each resource in the collection of the keys of the Map
  236.      * Use the conjunction defined by <var>andConjunction</var> for all expressions
  237.      * Example:  ( field[0]=values[0] <var>andConjunction</var> field[1]=values[1] ..... <var>andConjunction</var> field[N]=values[N] )
  238.      *
  239.      * @param propertyNameValues Map that contains identifiers and their values
  240.      * @return the instance of itself enriched with expression that represents the constraint
  241.      * @throws ExpressionNotImplementedException,ExpressionException
  242.      */
  243.     @Override
  244.     public IExpression allEquals(Map<IField, Object> propertyNameValues,boolean andConjunction)
  245.     throws ExpressionNotImplementedException, ExpressionException {
  246.         checkArgoments(propertyNameValues);
  247.         buildComparatorExpression(propertyNameValues, getCorrectComparator(Comparator.EQUALS), andConjunction);
  248.         return this;
  249.     }

  250.     /**
  251.      * Create an expression of inequality to each resource in the collection of the keys of the Map
  252.      * Use the conjunction defined by <var>andConjunction</var> for all expressions
  253.      * Example:  ( field[0]&lt;&gt;values[0] <var>andConjunction</var> field[1]&lt;&gt;values[1] ..... <var>andConjunction</var> field[N]&lt;&gt;values[N] )
  254.      *
  255.      * @param propertyNameValues Map that contains identifiers and their values
  256.      * @return the instance of itself enriched with expression that represents the constraint
  257.      * @throws ExpressionNotImplementedException,ExpressionException
  258.      */
  259.     @Override
  260.     public IExpression allNotEquals(Map<IField, Object> propertyNameValues,boolean andConjunction)
  261.     throws ExpressionNotImplementedException, ExpressionException {
  262.         checkArgoments(propertyNameValues);
  263.         buildComparatorExpression(propertyNameValues, getCorrectComparator(Comparator.NOT_EQUALS), andConjunction);
  264.         return this;
  265.     }
  266.    
  267.     /**
  268.      * Create an expression "greaterThan"
  269.      * Example:  (field &gt; value)
  270.      *
  271.      * @param field Resource identifier
  272.      * @param value Value
  273.      * @return the instance of itself enriched with expression that represents the constraint
  274.      * @throws ExpressionNotImplementedException,ExpressionException
  275.      */
  276.     @Override
  277.     public IExpression greaterThan(IField field, Object value)
  278.     throws ExpressionNotImplementedException, ExpressionException {
  279.         checkArgoments(field, value);
  280.         this.buildComparatorExpression(field, value, getCorrectComparator(Comparator.GREATER_THAN));
  281.         return this;
  282.     }

  283.     /**
  284.      * Create an expression "greaterEquals"
  285.      * Example:  (field &gt;= value)
  286.      *
  287.      * @param field Resource identifier
  288.      * @param value Value
  289.      * @return the instance of itself enriched with expression that represents the constraint
  290.      * @throws ExpressionNotImplementedException,ExpressionException
  291.      */
  292.     @Override
  293.     public IExpression greaterEquals(IField field, Object value)
  294.     throws ExpressionNotImplementedException, ExpressionException {
  295.         checkArgoments(field, value);
  296.         this.buildComparatorExpression(field, value, getCorrectComparator(Comparator.GREATER_EQUALS));
  297.         return this;
  298.     }

  299.     /**
  300.      * Create an expression "lessThan"
  301.      * Example:  (field &lt; value)
  302.      *
  303.      * @param field Resource identifier
  304.      * @param value Value
  305.      * @return the instance of itself enriched with expression that represents the constraint
  306.      * @throws ExpressionNotImplementedException,ExpressionException
  307.      */
  308.     @Override
  309.     public IExpression lessThan(IField field, Object value)
  310.     throws ExpressionNotImplementedException, ExpressionException {
  311.         checkArgoments(field, value);
  312.         this.buildComparatorExpression(field, value, getCorrectComparator(Comparator.LESS_THAN));
  313.         return this;
  314.     }

  315.     /**
  316.      * Create an expression "lessEquals"
  317.      * Example:  (field &lt;= value)
  318.      *
  319.      * @param field Resource identifier
  320.      * @param value Value
  321.      * @return the instance of itself enriched with expression that represents the constraint
  322.      * @throws ExpressionNotImplementedException,ExpressionException
  323.      */
  324.     @Override
  325.     public IExpression lessEquals(IField field, Object value)
  326.     throws ExpressionNotImplementedException, ExpressionException {
  327.         checkArgoments(field, value);
  328.         this.buildComparatorExpression(field, value, getCorrectComparator(Comparator.LESS_EQUALS));
  329.         return this;
  330.     }

  331.    
  332.    
  333.    
  334.     /* ************ SPECIAL COMPARATOR *********** */
  335.    
  336.     /**
  337.      * Create an expression "is null"
  338.      * Example:  ( field is null )
  339.      *
  340.      * @param field Resource identifier
  341.      * @return the instance of itself enriched with expression that represents the constraint
  342.      * @throws ExpressionNotImplementedException,ExpressionException
  343.      */
  344.     @Override
  345.     public IExpression isNull(IField field) throws ExpressionNotImplementedException,ExpressionException{
  346.         checkArgoments(field,false);
  347.         this.buildComparatorExpression(field, null, getCorrectComparator(Comparator.IS_NULL));
  348.         return this;
  349.     }
  350.    
  351.     /**
  352.      * Create an expression "is not null"
  353.      * Example:  ( field is not null )
  354.      *
  355.      * @param field Resource identifier
  356.      * @return the instance of itself enriched with expression that represents the constraint
  357.      * @throws ExpressionNotImplementedException,ExpressionException
  358.      */
  359.     @Override
  360.     public IExpression isNotNull(IField field) throws ExpressionNotImplementedException,ExpressionException{
  361.         checkArgoments(field,false);
  362.         this.buildComparatorExpression(field, null, getCorrectComparator(Comparator.IS_NOT_NULL));
  363.         return this;
  364.     }
  365.    
  366.     /**
  367.      * Create an expression "is empty"
  368.      * Example:  ( field = '' )
  369.      *
  370.      * @param field Resource identifier
  371.      * @return the instance of itself enriched with expression that represents the constraint
  372.      * @throws ExpressionNotImplementedException,ExpressionException
  373.      */
  374.     @Override
  375.     public IExpression isEmpty(IField field) throws ExpressionNotImplementedException,ExpressionException{
  376.         checkArgoments(field,false);
  377.         this.buildComparatorExpression(field, null, getCorrectComparator(Comparator.IS_EMPTY));
  378.         return this;
  379.     }

  380.     /**
  381.      * Create an expression "is not empty"
  382.      * Example:  ( field &lt;&gt; '' )
  383.      *
  384.      * @param field Resource identifier
  385.      * @return the instance of itself enriched with expression that represents the constraint
  386.      * @throws ExpressionNotImplementedException,ExpressionException
  387.      */
  388.     @Override
  389.     public IExpression isNotEmpty(IField field) throws ExpressionNotImplementedException,ExpressionException{
  390.         checkArgoments(field,false);
  391.         this.buildComparatorExpression(field, null, getCorrectComparator(Comparator.IS_NOT_EMPTY));
  392.         return this;
  393.     }

  394.    
  395.    
  396.    
  397.    
  398.     /* ************ BETWEEN *********** */
  399.    
  400.     /**
  401.      * Create an expression "between"
  402.      * Example:  ( field BEETWEEN lower AND high )
  403.      *
  404.      * @param field Resource identifier
  405.      * @param lower Lower limit value to be applied to the constraint
  406.      * @param high Higher limit value to be applied to the constraint
  407.      * @return the instance of itself enriched with expression that represents the constraint
  408.      * @throws ExpressionNotImplementedException,ExpressionException
  409.      */
  410.     @Override
  411.     public IExpression between(IField field, Object lower, Object high)
  412.     throws ExpressionNotImplementedException, ExpressionException {
  413.         checkArgoments(field, lower, high);
  414.         buildBeetweenExpression(field, lower, high);
  415.         return this;
  416.     }

  417.    
  418.    
  419.    
  420.     /* ************ LIKE *********** */
  421.    
  422.     /**
  423.      * Create an expression "like"
  424.      * Example:  ( field like '%value%' )
  425.      *
  426.      * @param field Resource identifier
  427.      * @param value Value
  428.      * @return the instance of itself enriched with expression that represents the constraint
  429.      * @throws ExpressionNotImplementedException,ExpressionException
  430.      */
  431.     @Override
  432.     public IExpression like(IField field, String value)
  433.     throws ExpressionNotImplementedException, ExpressionException {
  434.         checkArgoments(field, value);
  435.         buildLikeExpression(field, value, LikeMode.ANYWHERE, false);
  436.         return this;
  437.     }

  438.     /**
  439.      * Create an expression "like"
  440.      * Example:  
  441.      *   LikeMode.ANYWHERE -&gt; ( field like '%value%' )
  442.      *   LikeMode.EXACT -&gt; ( field like 'value' )
  443.      *   LikeMode.END -&gt; ( field like '%value' )
  444.      *   LikeMode.START -&gt; ( field like 'value%' )
  445.      *
  446.      * @param field Resource identifier
  447.      * @param value Value
  448.      * @param mode LikeMode
  449.      * @return the instance of itself enriched with expression that represents the constraint
  450.      * @throws ExpressionNotImplementedException,ExpressionException
  451.      */
  452.     @Override
  453.     public IExpression like(IField field, String value, LikeMode mode)
  454.     throws ExpressionNotImplementedException, ExpressionException {
  455.         checkArgoments(field, value);
  456.         buildLikeExpression(field, value, mode, false);
  457.         return this;
  458.     }

  459.     /**
  460.      * Create an expression "like" case-insensitive
  461.      * Example:  ( toLower(field) like '%toLower(value)%' )  
  462.      *
  463.      * @param field Resource identifier
  464.      * @param value Value
  465.      * @return the instance of itself enriched with expression that represents the constraint
  466.      * @throws ExpressionNotImplementedException,ExpressionException
  467.      */
  468.     @Override
  469.     public IExpression ilike(IField field, String value)
  470.     throws ExpressionNotImplementedException, ExpressionException {
  471.         checkArgoments(field, value);
  472.         buildLikeExpression(field, value, LikeMode.ANYWHERE, true);
  473.         return this;
  474.     }

  475.     /**
  476.      * Create an expression "like" case-insensitive
  477.      * Example:  
  478.      *   LikeMode.ANYWHERE -&gt; ( toLower(field) like '%toLower(value)%' )
  479.      *   LikeMode.EXACT -&gt; ( toLower(field) like 'toLower(value)' )
  480.      *   LikeMode.END -&gt; ( toLower(field) like '%toLower(value)' )
  481.      *   LikeMode.START -&gt; ( toLower(field) like 'toLower(value)%' )
  482.      *
  483.      * @param field Resource identifier
  484.      * @param value Value
  485.      * @param mode LikeMode
  486.      * @return the instance of itself enriched with expression that represents the constraint
  487.      * @throws ExpressionNotImplementedException,ExpressionException
  488.      */
  489.     @Override
  490.     public IExpression ilike(IField field, String value, LikeMode mode)
  491.     throws ExpressionNotImplementedException, ExpressionException {
  492.         checkArgoments(field, value);
  493.         buildLikeExpression(field, value, mode, true);
  494.         return this;
  495.     }

  496.    
  497.    
  498.    
  499.     /* ************ DATE PART *********** */
  500.    
  501.     /**
  502.      * Create an expression che verifica l'anno di una data
  503.      * Example:  ( EXTRACT(YEAR FROM field) = 'value' )  
  504.      *
  505.      * @param field Resource identifier
  506.      * @param value Value
  507.      * @return the instance of itself enriched with expression that represents the constraint
  508.      * @throws ExpressionNotImplementedException,ExpressionException
  509.      */
  510.     @Override
  511.     public IExpression isYear(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  512.         checkArgoments(field, value);
  513.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.YEAR);
  514.         return this;
  515.     }
  516.     @Override
  517.     public IExpression isYear(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  518.         checkArgoments(field, value);
  519.         buildDateTimePartExpression(field, value, DateTimePartEnum.YEAR);
  520.         return this;
  521.     }
  522.    
  523.     /**
  524.      * Create an expression che verifica il mese (numerico) di una data
  525.      * Example:  ( EXTRACT(MONTH FROM field) = 'value' )  
  526.      *
  527.      * @param field Resource identifier
  528.      * @param value Value
  529.      * @return the instance of itself enriched with expression that represents the constraint
  530.      * @throws ExpressionNotImplementedException,ExpressionException
  531.      */
  532.     @Override
  533.     public IExpression isMonth(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  534.         checkArgoments(field, value);
  535.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.MONTH);
  536.         return this;
  537.     }
  538.     @Override
  539.     public IExpression isMonth(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  540.         checkArgoments(field, value);
  541.         buildDateTimePartExpression(field, value, DateTimePartEnum.MONTH);
  542.         return this;
  543.     }
  544.    
  545.     /**
  546.      * Create an expression che verifica il giorno del mese di una data
  547.      * Example:  ( EXTRACT(DAY FROM field) = 'value' )  
  548.      *
  549.      * @param field Resource identifier
  550.      * @param value Value
  551.      * @return the instance of itself enriched with expression that represents the constraint
  552.      * @throws ExpressionNotImplementedException,ExpressionException
  553.      */
  554.     @Override
  555.     public IExpression isDayOfMonth(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  556.         checkArgoments(field, value);
  557.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.DAY);
  558.         return this;
  559.     }
  560.     @Override
  561.     public IExpression isDayOfMonth(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  562.         checkArgoments(field, value);
  563.         buildDateTimePartExpression(field, value, DateTimePartEnum.DAY);
  564.         return this;
  565.     }
  566.    
  567.     /**
  568.      * Create an expression che verifica il giorno dell'anno di una data
  569.      * Example:  ( TO_CHAR(data_ingresso_richiesta, 'DDD') = 'value' )  
  570.      *
  571.      * @param field Resource identifier
  572.      * @param value Value
  573.      * @return the instance of itself enriched with expression that represents the constraint
  574.      * @throws ExpressionNotImplementedException,ExpressionException
  575.      */
  576.     @Override
  577.     public IExpression isDayOfYear(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  578.         checkArgoments(field, value);
  579.         buildDayFormatExpression(field, value+"", DayFormatEnum.DAY_OF_YEAR);
  580.         return this;
  581.     }
  582.     @Override
  583.     public IExpression isDayOfYear(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  584.         checkArgoments(field, value);
  585.         buildDayFormatExpression(field, value, DayFormatEnum.DAY_OF_YEAR);
  586.         return this;
  587.     }
  588.    
  589.     /**
  590.      * Create an expression che verifica il giorno della settimana di una data
  591.      * Example:  ( TO_CHAR(data_ingresso_richiesta, 'D') = 'value' )  
  592.      *
  593.      * @param field Resource identifier
  594.      * @param value Value
  595.      * @return the instance of itself enriched with expression that represents the constraint
  596.      * @throws ExpressionNotImplementedException,ExpressionException
  597.      */
  598.     @Override
  599.     public IExpression isDayOfWeek(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  600.         checkArgoments(field, value);
  601.         buildDayFormatExpression(field, value+"", DayFormatEnum.DAY_OF_WEEK);
  602.         return this;
  603.     }
  604.     @Override
  605.     public IExpression isDayOfWeek(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  606.         checkArgoments(field, value);
  607.         buildDayFormatExpression(field, value, DayFormatEnum.DAY_OF_WEEK);
  608.         return this;
  609.     }
  610.    
  611.     /**
  612.      * Create an expression che verifica l'ora di una data
  613.      * Example:  ( EXTRACT(HOUR FROM field) = 'value' )  
  614.      *
  615.      * @param field Resource identifier
  616.      * @param value Value
  617.      * @return the instance of itself enriched with expression that represents the constraint
  618.      * @throws ExpressionNotImplementedException,ExpressionException
  619.      */
  620.     @Override
  621.     public IExpression isHour(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  622.         checkArgoments(field, value);
  623.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.HOUR);
  624.         return this;
  625.     }
  626.     @Override
  627.     public IExpression isHour(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  628.         checkArgoments(field, value);
  629.         buildDateTimePartExpression(field, value, DateTimePartEnum.HOUR);
  630.         return this;
  631.     }
  632.    
  633.     /**
  634.      * Create an expression che verifica i minuti di una data
  635.      * Example:  ( EXTRACT(MINUTE FROM field) = 'value' )  
  636.      *
  637.      * @param field Resource identifier
  638.      * @param value Value
  639.      * @return the instance of itself enriched with expression that represents the constraint
  640.      * @throws ExpressionNotImplementedException,ExpressionException
  641.      */
  642.     @Override
  643.     public IExpression isMinute(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  644.         checkArgoments(field, value);
  645.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.MINUTE);
  646.         return this;
  647.     }
  648.     @Override
  649.     public IExpression isMinute(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  650.         checkArgoments(field, value);
  651.         buildDateTimePartExpression(field, value, DateTimePartEnum.MINUTE);
  652.         return this;
  653.     }
  654.    
  655.     /**
  656.      * Create an expression che verifica i secondi di una data
  657.      * Example:  ( EXTRACT(SECOND FROM field) = 'value' )  
  658.      *
  659.      * @param field Resource identifier
  660.      * @param value Value
  661.      * @return the instance of itself enriched with expression that represents the constraint
  662.      * @throws ExpressionNotImplementedException,ExpressionException
  663.      */
  664.     @Override
  665.     public IExpression isSecond(IField field, int value) throws ExpressionNotImplementedException,ExpressionException{
  666.         checkArgoments(field, value);
  667.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.SECOND);
  668.         return this;
  669.     }
  670.     @Override
  671.     public IExpression isSecond(IField field, double value) throws ExpressionNotImplementedException,ExpressionException{
  672.         checkArgoments(field, value);
  673.         buildDateTimePartExpression(field, value+"", DateTimePartEnum.SECOND);
  674.         return this;
  675.     }
  676.     @Override
  677.     public IExpression isSecond(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  678.         checkArgoments(field, value);
  679.         buildDateTimePartExpression(field, value, DateTimePartEnum.SECOND);
  680.         return this;
  681.     }
  682.    
  683.     /**
  684.      * Create an expression che verifica il giorno di una data, nel formato umano (es. Friday)
  685.      * Example:  ( TO_CHAR(data_ingresso_richiesta, 'DAY') = 'value' )  
  686.      *
  687.      * @param field Resource identifier
  688.      * @param value Value
  689.      * @return the instance of itself enriched with expression that represents the constraint
  690.      * @throws ExpressionNotImplementedException,ExpressionException
  691.      */
  692.     @Override
  693.     public IExpression isFullDayName(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  694.         checkArgoments(field, value);
  695.         buildDayFormatExpression(field, value, DayFormatEnum.FULL_DAY_NAME);
  696.         return this;
  697.     }
  698.    
  699.     /**
  700.      * Create an expression che verifica il giorno di una data, nel formato compatto umano (es. Fri)
  701.      * Example:  ( TO_CHAR(data_ingresso_richiesta, 'DY') = 'value' )  
  702.      *
  703.      * @param field Resource identifier
  704.      * @param value Value
  705.      * @return the instance of itself enriched with expression that represents the constraint
  706.      * @throws ExpressionNotImplementedException,ExpressionException
  707.      */
  708.     @Override
  709.     public IExpression isShortDayName(IField field, String value) throws ExpressionNotImplementedException,ExpressionException{
  710.         checkArgoments(field, value);
  711.         buildDayFormatExpression(field, value, DayFormatEnum.SHORT_DAY_NAME);
  712.         return this;
  713.     }

  714.    
  715.    
  716.    
  717.    
  718.    
  719.     /* ************ IN *********** */
  720.    
  721.     /**
  722.      * Create an expression "in" verifying that the value of <var>field</var> is one of those given in the param <var>values</var>
  723.      * Example:  ( field IN ('values[0]', 'values[1]', ... ,'values[n]')  )
  724.      *
  725.      * @param field Resource identifier
  726.      * @param values Values
  727.      * @return the instance of itself enriched with expression that represents the constraint
  728.      * @throws ExpressionNotImplementedException,ExpressionException
  729.      */
  730.     @Override
  731.     public IExpression in(IField field, Object... values)
  732.     throws ExpressionNotImplementedException, ExpressionException {
  733.         checkArgoments(field, values);
  734.         buildInExpression(field, values);
  735.         return this;
  736.     }

  737.     /**
  738.      * Create an expression "in" verifying that the value of <var>field</var> is one of those given in the param <var>values</var>
  739.      * Example:  ( field IN ('values[0]', 'values[1]', ... ,'values[n]')  )
  740.      *
  741.      * @param field Resource identifier
  742.      * @param values Values
  743.      * @return the instance of itself enriched with expression that represents the constraint
  744.      * @throws ExpressionNotImplementedException,ExpressionException
  745.      */
  746.     @Override
  747.     public IExpression in(IField field, Collection<?> values)
  748.     throws ExpressionNotImplementedException, ExpressionException {
  749.         if(values==null){
  750.             throw new ExpressionException("Values is null");
  751.         }
  752.         checkArgoments(field,  values.toArray(new Object[1]));
  753.         buildInExpression(field, values.toArray(new Object[1]));
  754.         return this;
  755.     }



  756.    
  757.    
  758.    
  759.     /* ************ CONJUNCTION *********** */
  760.    
  761.     /**
  762.      *  Adds to the expression in the conjunction "AND" of the espressions given as a parameters
  763.      *  Example: ( exp1 AND exp2  )
  764.      *  
  765.      * @param exp1 Filter combined in AND with <var>exp1</var>
  766.      * @param exp2 Filter combined in AND with <var>exp2</var>
  767.      * @return the instance of itself enriched with expression that represents the constraint
  768.      * @throws ExpressionNotImplementedException,ExpressionException
  769.      */
  770.     @Override
  771.     public IExpression and(IExpression exp1, IExpression exp2)
  772.     throws ExpressionNotImplementedException, ExpressionException {
  773.         IExpression [] expr = new IExpression[2];
  774.         expr[0] = exp1;
  775.         expr[1] = exp2;
  776.         return this.and(expr);
  777.     }

  778.     /**
  779.      *  Adds to the expression in the conjunction "AND" of the espressions given as a parameters
  780.      *  Example: ( expressions[1] AND expressions[2] AND ... expressions[N] )
  781.      *  
  782.      * @param expressions Expressions combined in AND
  783.      * @return the instance of itself enriched with expression that represents the constraint
  784.      * @throws ExpressionNotImplementedException,ExpressionException
  785.      */
  786.     @Override
  787.     public IExpression and(IExpression... expressions)
  788.         throws ExpressionNotImplementedException, ExpressionException {
  789.         checkConjunctionOperation(expressions);
  790.         buildConjunctionExpression(true, expressions);
  791.         return this;
  792.     }

  793.     /**
  794.      *  Adds to the expression in the conjunction "OR" of the espressions given as a parameters
  795.      *  Example: ( exp1 OR exp2  )
  796.      *  
  797.      * @param exp1 Filter combined in OR with <var>exp1</var>
  798.      * @param exp2 Filter combined in OR with <var>exp2</var>
  799.      * @return the instance of itself enriched with expression that represents the constraint
  800.      * @throws ExpressionNotImplementedException,ExpressionException
  801.      */
  802.     @Override
  803.     public IExpression or(IExpression exp1, IExpression exp2)
  804.     throws ExpressionNotImplementedException, ExpressionException {
  805.         IExpression [] expr = new IExpression[2];
  806.         expr[0] = exp1;
  807.         expr[1] = exp2;
  808.         return this.or(expr);
  809.     }

  810.     /**
  811.      *  Adds to the expression in the conjunction "OR" of the espressions given as a parameters
  812.      *  Example: ( expressions[1] OR expressions[2] OR ... expressions[N] )
  813.      *  
  814.      * @param expressions Expressions combined in OR
  815.      * @return the instance of itself enriched with expression that represents the constraint
  816.      * @throws ExpressionNotImplementedException,ExpressionException
  817.      */
  818.     @Override
  819.     public IExpression or(IExpression... expressions)
  820.     throws ExpressionNotImplementedException, ExpressionException {
  821.         checkConjunctionOperation(expressions);
  822.         buildConjunctionExpression(false, expressions);
  823.         return this;
  824.     }


  825.    
  826.    
  827.    
  828.     /* ************ NEGATION *********** */
  829.    
  830.     /**
  831.      * Adding to the negation of the expression passed as parameter
  832.      * Example: ( NOT expression )
  833.      *
  834.      * @param expression Expression
  835.      * @return the instance of itself enriched with expression that represents the constraint
  836.      * @throws ExpressionNotImplementedException,ExpressionException
  837.      */
  838.     @Override
  839.     public IExpression not(IExpression expression)
  840.     throws ExpressionNotImplementedException, ExpressionException {
  841.         if(! (expression instanceof ExpressionImpl) ){
  842.             throw new ExpressionException("Expression has wrong type expect: "+ExpressionImpl.class.getName()+"  found: "+expression.getClass().getName());
  843.         }
  844.         buildNotExpression(((ExpressionImpl)expression).expressionEngine);
  845.         return this;
  846.     }
  847.    
  848.    
  849.    
  850.    
  851.    
  852.    
  853.     /* ************ ORDER *********** */
  854.    
  855.     /**
  856.      * Sets the sort of research
  857.      *
  858.      * @param sortOrder sort of research
  859.      * @return the instance of itself
  860.      * @throws ExpressionNotImplementedException Method not implemented
  861.      * @throws ExpressionException Error Processing
  862.      */
  863.     @Override
  864.     public IExpression sortOrder(SortOrder sortOrder)
  865.             throws ExpressionNotImplementedException, ExpressionException {
  866.         this.sortOrderEngine = sortOrder;
  867.         return this;
  868.     }

  869.     /**
  870.      * Adds a sort field order
  871.      *
  872.      * @param field sort field order
  873.      * @return the instance of itself
  874.      * @throws ExpressionNotImplementedException Method not implemented
  875.      * @throws ExpressionException Error Processing
  876.      */
  877.     @Override
  878.     public IExpression addOrder(IField field)
  879.             throws ExpressionNotImplementedException, ExpressionException {
  880.         this.checkArgoments(field,false);
  881.         if(this.sortOrderEngine==null || SortOrder.UNSORTED.equals(this.sortOrderEngine)){
  882.             throw new ExpressionException("To add order by conditions must first be defined the sort order (by sortOrder method)");
  883.         }
  884.         this.orderedFields.add(new OrderedField(field, this.sortOrderEngine));
  885.         return this;
  886.     }
  887.    
  888.     /**
  889.      * Adds a sort field order
  890.      *
  891.      * @param field sort field order
  892.      * @param sortOrder sort of research
  893.      * @return the instance of itself
  894.      * @throws ExpressionNotImplementedException Method not implemented
  895.      * @throws ExpressionException Error Processing
  896.      */
  897.     @Override
  898.     public IExpression addOrder(IField field, SortOrder sortOrder) throws ExpressionNotImplementedException,ExpressionException{
  899.         this.checkArgoments(field,false);
  900.         if(sortOrder==null){
  901.             throw new ExpressionException("Sort order parameter undefined");
  902.         }
  903.         if(SortOrder.UNSORTED.equals(sortOrder)){
  904.             throw new ExpressionException("Sort order parameter not valid (use ASC or DESC)");
  905.         }
  906.         this.orderedFields.add(new OrderedField(field, sortOrder));
  907.         return this;
  908.     }
  909.    
  910.     public List<OrderedField> getOrderedFields() {
  911.         return this.orderedFields;
  912.     }
  913.    
  914.     public SortOrder getSortOrder(){
  915.         if(
  916.             ( this.sortOrderEngine==null || SortOrder.UNSORTED.equals(this.sortOrderEngine) )
  917.                 &&
  918.             ( this.orderedFields!=null && !this.orderedFields.isEmpty() )
  919.         ){
  920.             // ritorno un sortOrder a caso tra i orderedFields, tanto poi viene usato sempre quello indicato per ogni field.
  921.             return this.orderedFields.get(0).getSortOrder();
  922.         }
  923.         return this.sortOrderEngine;
  924.     }
  925.    
  926.    
  927.    
  928.    
  929.    
  930.    
  931.     /* ************ GROUP BY *********** */
  932.    
  933.     @Override
  934.     public IExpression addGroupBy(IField field)
  935.             throws ExpressionNotImplementedException, ExpressionException {
  936.         this.checkArgoments(field,false);
  937.         this.groupByFields.add(field);
  938.         return this;
  939.     }
  940.    
  941.     public List<IField> getGroupByFields() {
  942.         return this.groupByFields;
  943.     }
  944.    
  945.    
  946.    
  947.    
  948.    
  949.    
  950.    
  951.    
  952.        
  953.     /* ************ IN USE MODEL/FIELD EXPRESSION *********** */
  954.    
  955.     /**
  956.      * Return the information as the <var>field</var> is used in the expression
  957.      *
  958.      * @param field field test
  959.      * @return information as the <var>field</var> is used in the expression
  960.      */
  961.     @Override
  962.     public boolean inUseField(IField field,boolean checkOnlyWhereCondition) throws ExpressionNotImplementedException, ExpressionException {
  963.         if(this.expressionEngine==null &&
  964.             checkOnlyWhereCondition) {
  965.             return false;
  966.         }
  967.         if( this.expressionEngine!=null && this.expressionEngine.inUseField(field) ){
  968.             return true;
  969.         }
  970.         if(!checkOnlyWhereCondition){
  971.             boolean check = checkUseField(field);
  972.             if(check) {
  973.                 return true;
  974.             }
  975.         }
  976.         return false;
  977.     }
  978.     private boolean checkUseField(IField field) {
  979.         if(this.orderedFields!=null){
  980.             for (int i = 0; i < this.orderedFields.size(); i++) {
  981.                 if(field.equals(this.orderedFields.get(i).getField())){
  982.                     return true;
  983.                 }
  984.             }
  985.         }
  986.         if(this.groupByFields!=null){
  987.             for (int i = 0; i < this.groupByFields.size(); i++) {
  988.                 if(field.equals(this.groupByFields.get(i))){
  989.                     return true;
  990.                 }
  991.             }
  992.         }
  993.         return false;
  994.     }
  995.        
  996.     /**
  997.      * Return the information as there are few fields in the expression belonging to the <var>model</var>
  998.      *
  999.      * @param model model test
  1000.      * @return information as there are few fields in the expression belonging to the <var>model</var>
  1001.      */
  1002.     @Override
  1003.     public boolean inUseModel(IModel<?> model,boolean checkOnlyWhereCondition) throws ExpressionNotImplementedException, ExpressionException {
  1004.         if(this.expressionEngine==null &&
  1005.             checkOnlyWhereCondition) {
  1006.             return false;
  1007.         }
  1008.         if( this.expressionEngine!=null && this.expressionEngine.inUseModel(model) ){
  1009.             return true;
  1010.         }
  1011.         if(!checkOnlyWhereCondition){
  1012.             if(this.orderedFields!=null){
  1013.                 for (int i = 0; i < this.orderedFields.size(); i++) {
  1014.                     IField field = this.orderedFields.get(i).getField();
  1015.                     boolean inUseModel = false;
  1016.                     if(model.getBaseField()!=null){
  1017.                         // Modello di un elemento non radice
  1018.                         if(field instanceof ComplexField){
  1019.                             ComplexField c = (ComplexField) field;
  1020.                             inUseModel = c.getFather().equals(model.getBaseField());
  1021.                         }else{
  1022.                             String modeClassName = model.getModeledClass().getName() + "";
  1023.                             inUseModel = modeClassName.equals(field.getClassType().getName());
  1024.                         }
  1025.                     }
  1026.                     else{
  1027.                         String modeClassName = model.getModeledClass().getName() + "";
  1028.                         inUseModel = modeClassName.equals(field.getClassType().getName());
  1029.                     }
  1030.                     if(inUseModel){
  1031.                         return true;
  1032.                     }
  1033.                 }
  1034.             }
  1035.             if(this.groupByFields!=null){
  1036.                 for (int i = 0; i < this.groupByFields.size(); i++) {
  1037.                     IField field = this.groupByFields.get(i);
  1038.                     boolean inUseModel = false;
  1039.                     if(model.getBaseField()!=null){
  1040.                         // Modello di un elemento non radice
  1041.                         if(field instanceof ComplexField){
  1042.                             ComplexField c = (ComplexField) field;
  1043.                             inUseModel = c.getFather().equals(model.getBaseField());
  1044.                         }else{
  1045.                             String modeClassName = model.getModeledClass().getName() + "";
  1046.                             inUseModel = modeClassName.equals(field.getClassType().getName());
  1047.                         }
  1048.                     }
  1049.                     else{
  1050.                         String modeClassName = model.getModeledClass().getName() + "";
  1051.                         inUseModel = modeClassName.equals(field.getClassType().getName());
  1052.                     }
  1053.                     if(inUseModel){
  1054.                         return true;
  1055.                     }
  1056.                 }
  1057.             }
  1058.         }
  1059.         return false;
  1060.     }
  1061.    
  1062.    
  1063.    
  1064.    
  1065.    
  1066.    
  1067.    
  1068.    
  1069.    
  1070.    
  1071.     /* ************ GET FIELDS INTO EXPRESSION *********** */
  1072.    
  1073.     /**
  1074.      * Return the fields userd in the expression
  1075.      *
  1076.      * @return Return the fields userd in the expression
  1077.      * @throws ExpressionNotImplementedException
  1078.      * @throws ExpressionException
  1079.      */
  1080.     @Override
  1081.     public List<IField> getFields(boolean onlyWhereCondition) throws ExpressionNotImplementedException, ExpressionException{
  1082.         List<IField> o = this.expressionEngine!=null ? this.expressionEngine.getFields() : null;
  1083.         if(!onlyWhereCondition){
  1084.             o = addFieldByOrderBy(o);
  1085.             o = addFieldByGroupBy(o);
  1086.         }
  1087.         return o;
  1088.     }
  1089.     private List<IField> addFieldByOrderBy(List<IField> o) {
  1090.         if(this.orderedFields!=null){
  1091.             for (int i = 0; i < this.orderedFields.size(); i++) {
  1092.                 IField field = this.orderedFields.get(i).getField();
  1093.                 if(o==null){
  1094.                     o = new ArrayList<>();
  1095.                 }
  1096.                 if(!o.contains(field)){
  1097.                     o.add(field);
  1098.                 }
  1099.             }
  1100.         }
  1101.         return o;
  1102.     }
  1103.     private List<IField> addFieldByGroupBy(List<IField> o) {
  1104.         if(this.groupByFields!=null){
  1105.             for (int i = 0; i < this.groupByFields.size(); i++) {
  1106.                 IField field = this.groupByFields.get(i);
  1107.                 if(o==null){
  1108.                     o = new ArrayList<>();
  1109.                 }
  1110.                 if(!o.contains(field)){
  1111.                     o.add(field);
  1112.                 }
  1113.             }
  1114.         }
  1115.         return o;
  1116.     }
  1117.    
  1118.     /**
  1119.      * Return the object associated with the <var>field</var> present in the expression
  1120.      *
  1121.      * @param field field test
  1122.      * @return the object associated with the <var>field</var> present in the expression
  1123.      */
  1124.     @Override
  1125.     public List<Object> getWhereConditionFieldValues(IField field) throws ExpressionNotImplementedException, ExpressionNotFoundException, ExpressionException{
  1126.         if(this.expressionEngine==null){
  1127.             throw new ExpressionNotFoundException("Field is not used in expression (Expression is empty)");
  1128.         }
  1129.         if(!this.inUseField(field,true)){
  1130.             throw new ExpressionNotFoundException("Field is not used in expression ["+field.toString()+"]");
  1131.         }
  1132.         List<Object> o = this.expressionEngine.getFieldValues(field);
  1133.         if(o==null){
  1134.             // non dovrei mai entrarci
  1135.             throw new ExpressionNotFoundException("Field is not used in expression ["+field.toString()+"] (null??)");
  1136.         }
  1137.         return o;
  1138.     }
  1139.    
  1140.    
  1141.    
  1142.    
  1143.    
  1144.    
  1145.    
  1146.     /* ************ EXPRESSION STATE *********** */
  1147.    
  1148.     /**
  1149.      * Return an indication if the expression contains 'where conditions'
  1150.      *
  1151.      * @return indication if the expression contains 'where conditions'
  1152.      * @throws ExpressionNotImplementedException Method not implemented
  1153.      * @throws ExpressionException Error Processing
  1154.      */
  1155.     @Override
  1156.     public boolean isWhereConditionsPresent() throws ExpressionNotImplementedException, ExpressionException{
  1157.         return this.expressionEngine==null;
  1158.     }
  1159.    
  1160.    
  1161.    
  1162.    
  1163.    
  1164.     /* ************ INDEX *********** */
  1165.    
  1166.     /**
  1167.      * Force use index
  1168.      *
  1169.      * @param index index
  1170.      * @return the instance of itself
  1171.      * @throws ExpressionNotImplementedException Method not implemented
  1172.      * @throws ExpressionException Error Processing
  1173.      */
  1174.     @Override
  1175.     public IExpression addForceIndex(Index index) throws ExpressionNotImplementedException,ExpressionException{
  1176.         this.forceIndexes.add(index);
  1177.         return this;
  1178.     }
  1179.    
  1180.     /**
  1181.      * Return the force indexes
  1182.      *
  1183.      * @return list force indexes
  1184.      * @throws ExpressionNotImplementedException Method not implemented
  1185.      * @throws ExpressionException Error Processing
  1186.      */
  1187.     public List<Index> getForceIndexes() {
  1188.         return this.forceIndexes;
  1189.     }
  1190.    
  1191.    
  1192.    
  1193.    
  1194.     /* ************ PROPERTY *********** */
  1195.    
  1196.     @Override
  1197.     public void addProperty(String name, Object value) {
  1198.         this.properties.put(name, value);
  1199.     }
  1200.     @Override
  1201.     public Object getProperty(String name) {
  1202.         return this.properties.get(name);
  1203.     }
  1204.    
  1205.    
  1206.    
  1207.    

  1208.     /* ************ TO STRING *********** */
  1209.    
  1210.     @Override
  1211.     public String toString(){
  1212.         return this.toString(true);
  1213.     }
  1214.     protected String toString(boolean printForceIndex){
  1215.         String s = super.toString();
  1216.         if(s==null){
  1217.             return s;
  1218.         }
  1219.         else{
  1220.            
  1221.             if(this.throwExpressionNotInitialized){
  1222.                 throw new UtilsRuntimeException("Expression is not initialized");
  1223.             }
  1224.            
  1225.             StringBuilder bf = new StringBuilder();
  1226.             if(this.expressionEngine==null){
  1227.                 bf.append("");
  1228.             }else{
  1229.                 bf.append(this.expressionEngine.toString());
  1230.             }
  1231.            
  1232.            
  1233.             if(!this.groupByFields.isEmpty()){
  1234.                
  1235.                 bf.append(" GROUP BY ");
  1236.                
  1237.                 int index = 0;
  1238.                 for (Iterator<IField> iterator = this.groupByFields.iterator(); iterator.hasNext();) {
  1239.                     IField field = iterator.next();
  1240.                     if(index>0){
  1241.                         bf.append(" , ");
  1242.                     }
  1243.                     if(field instanceof ComplexField){
  1244.                         ComplexField cf = (ComplexField) field;
  1245.                         if(cf.getFather()!=null){
  1246.                             bf.append(cf.getFather().getFieldName());
  1247.                         }else{
  1248.                             bf.append(field.getClassName());
  1249.                         }
  1250.                     }else{
  1251.                         bf.append(field.getClassName());
  1252.                     }
  1253.                     bf.append(".");
  1254.                     bf.append(field.getFieldName());
  1255.                     index++;
  1256.                 }
  1257.             }
  1258.            
  1259.            
  1260.             if(!SortOrder.UNSORTED.equals(this.getSortOrder())){
  1261.                
  1262.                 bf.append(" ORDER BY ");
  1263.                 if(!this.orderedFields.isEmpty()){
  1264.                     int index = 0;
  1265.                     for (Iterator<OrderedField> iterator = this.orderedFields.iterator(); iterator.hasNext();) {
  1266.                         OrderedField orderedField = iterator.next();
  1267.                         if(index>0){
  1268.                             bf.append(" , ");
  1269.                         }
  1270.                         IField field = orderedField.getField();
  1271.                         if(field instanceof ComplexField){
  1272.                             ComplexField cf = (ComplexField) field;
  1273.                             if(cf.getFather()!=null){
  1274.                                 bf.append(cf.getFather().getFieldName());
  1275.                             }else{
  1276.                                 bf.append(field.getClassName());
  1277.                             }
  1278.                         }else{
  1279.                             bf.append(field.getClassName());
  1280.                         }
  1281.                         bf.append(".");
  1282.                         bf.append(field.getFieldName());
  1283.                        
  1284.                         bf.append(" ");
  1285.                         bf.append(orderedField.getSortOrder().name());
  1286.                         index++;
  1287.                     }
  1288.                 }else{
  1289.                     bf.append(" DEFAULT_ORDER_FIELD");
  1290.                    
  1291.                     bf.append(" ");
  1292.                     bf.append(this.getSortOrder().name());
  1293.                 }
  1294.             }
  1295.                
  1296.             if(printForceIndex){
  1297.                 this.printForceIndex(bf);
  1298.             }
  1299.            
  1300.             return bf.toString();
  1301.         }
  1302.     }
  1303.     protected void printForceIndex(StringBuilder bf){
  1304.         if(!this.forceIndexes.isEmpty()){
  1305.             for (Iterator<Index> iterator =this.forceIndexes.iterator(); iterator.hasNext();) {
  1306.                 Index forceIndex = iterator.next();        
  1307.                 String forceIndexSql = "/*+ index("+forceIndex.getModel().getModeledClass().getSimpleName()+" "+forceIndex.getName()+") */";
  1308.                 bf.append(" ");
  1309.                 bf.append(forceIndexSql);
  1310.             }
  1311.         }
  1312.     }
  1313.    
  1314.    
  1315.    
  1316.    
  1317.    
  1318.     /* ************ OBJECTS ************ */
  1319.     protected ComparatorExpressionImpl getComparatorExpression(IField field, Object value, Comparator c) {
  1320.         return new ComparatorExpressionImpl(this.objectFormatter,field,value,c);
  1321.     }
  1322.     protected BetweenExpressionImpl getBetweenExpression(IField field, Object lower, Object high) {
  1323.         return new BetweenExpressionImpl(this.objectFormatter,field,lower,high);
  1324.     }
  1325.     protected InExpressionImpl getInExpression(IField field, Object... values) {
  1326.         List<Object> lista = new ArrayList<>();
  1327.         if(values!=null && values.length>0){
  1328.             lista.addAll(Arrays.asList(values));
  1329.         }
  1330.         return new InExpressionImpl(this.objectFormatter,field, lista);
  1331.     }
  1332.     protected LikeExpressionImpl getLikeExpression(IField field, String value, LikeMode mode, boolean caseInsensitive) {
  1333.         return new LikeExpressionImpl(this.objectFormatter,field, value, mode, caseInsensitive);
  1334.     }
  1335.     protected DateTimePartExpressionImpl getDateTimePartExpression(IField field, String value, DateTimePartEnum dateTimePartEnum) {
  1336.         return new DateTimePartExpressionImpl(this.objectFormatter,field, value, dateTimePartEnum);
  1337.     }
  1338.     protected DayFormatExpressionImpl getDayFormatExpression(IField field, String value, DayFormatEnum dayFormatEnum) {
  1339.         return new DayFormatExpressionImpl(this.objectFormatter,field, value, dayFormatEnum);
  1340.     }
  1341.     protected ConjunctionExpressionImpl getConjunctionExpression() {
  1342.         return new ConjunctionExpressionImpl(this.objectFormatter);
  1343.     }
  1344.        
  1345.    
  1346.    
  1347.     /* ************ UTILITY - BUILD ************ */
  1348.     protected void buildComparatorExpression(IField field, Object value, Comparator c) {
  1349.         ComparatorExpressionImpl cExp = getComparatorExpression(field,value,c);
  1350.         if(this.expressionEngine==null){
  1351.             this.expressionEngine = cExp;
  1352.         }else{
  1353.             conjunctionWithInternalInstance(cExp);
  1354.         }
  1355.         this.expressionEngine.setNot(this.notOperator);
  1356.     }
  1357.     protected void buildComparatorExpression(Map<IField, Object> propertyNameValues,Comparator c,boolean and) {
  1358.         Iterator<IField> fieldsIt = propertyNameValues.keySet().iterator();
  1359.         ConjunctionExpressionImpl newConjunctionExpr = getConjunctionExpression();
  1360.         newConjunctionExpr.setAndConjunction(and);
  1361.         while (fieldsIt.hasNext()) {
  1362.             IField field = fieldsIt.next();
  1363.             Object value = propertyNameValues.get(field);
  1364.             newConjunctionExpr.addExpression(getComparatorExpression(field,value,c));
  1365.         }
  1366.         this.conjunctionWithInternalInstance(newConjunctionExpr);
  1367.         this.expressionEngine.setNot(this.notOperator);
  1368.     }
  1369.     protected void buildBeetweenExpression(IField field, Object lower, Object high) {
  1370.         BetweenExpressionImpl cExp = getBetweenExpression(field,lower,high);
  1371.         if(this.expressionEngine==null){
  1372.             this.expressionEngine = cExp;
  1373.         }else{
  1374.             conjunctionWithInternalInstance(cExp);
  1375.         }
  1376.         this.expressionEngine.setNot(this.notOperator);
  1377.     }
  1378.     protected void buildInExpression(IField field, Object... values) {
  1379.         InExpressionImpl cExp = getInExpression(field, values);
  1380.         if(this.expressionEngine==null){
  1381.             this.expressionEngine = cExp;
  1382.         }else{
  1383.             conjunctionWithInternalInstance(cExp);
  1384.         }
  1385.         this.expressionEngine.setNot(this.notOperator);
  1386.     }
  1387.     protected void buildLikeExpression(IField field, String value, LikeMode mode, boolean caseInsensitive) {
  1388.         LikeExpressionImpl cExp = getLikeExpression(field, value, mode, caseInsensitive);
  1389.         if(this.expressionEngine==null){
  1390.             this.expressionEngine = cExp;
  1391.         }else{
  1392.             conjunctionWithInternalInstance(cExp);
  1393.         }
  1394.         this.expressionEngine.setNot(this.notOperator);
  1395.     }
  1396.     protected void buildDateTimePartExpression(IField field, String value, DateTimePartEnum dateTimePartEnum) {
  1397.         DateTimePartExpressionImpl cExp = getDateTimePartExpression(field, value, dateTimePartEnum);
  1398.         if(this.expressionEngine==null){
  1399.             this.expressionEngine = cExp;
  1400.         }else{
  1401.             conjunctionWithInternalInstance(cExp);
  1402.         }
  1403.         this.expressionEngine.setNot(this.notOperator);
  1404.     }
  1405.     protected void buildDayFormatExpression(IField field, String value, DayFormatEnum dayFormatEnum) {
  1406.         DayFormatExpressionImpl cExp = getDayFormatExpression(field, value, dayFormatEnum);
  1407.         if(this.expressionEngine==null){
  1408.             this.expressionEngine = cExp;
  1409.         }else{
  1410.             conjunctionWithInternalInstance(cExp);
  1411.         }
  1412.         this.expressionEngine.setNot(this.notOperator);
  1413.     }
  1414.     protected void buildNotExpression(AbstractBaseExpressionImpl expr) {
  1415.         expr.setNot(true);
  1416.         if(this.expressionEngine==null){
  1417.             this.expressionEngine = expr;
  1418.         }else{
  1419.             String s1 = this.expressionEngine.toString();
  1420.             String s2 = expr.toString();
  1421.             if(s1.equals(s2)){
  1422.                 // same
  1423.                 this.expressionEngine = expr;
  1424.             }else{
  1425.                 conjunctionWithInternalInstance(expr);
  1426.             }
  1427.         }
  1428.     }
  1429.     protected void buildConjunctionExpression(boolean and,IExpression... expressions) {
  1430.         ConjunctionExpressionImpl cExp = getConjunctionExpression();
  1431.         cExp.setAndConjunction(and);
  1432.         boolean add = false;
  1433.         for (int i = 0; i < expressions.length; i++) {
  1434.             ExpressionImpl xmlExpression = (ExpressionImpl) expressions[i];
  1435.             if(xmlExpression.expressionEngine!=null){
  1436.                 cExp.addExpression(xmlExpression.expressionEngine);
  1437.                 add = true;
  1438.             }
  1439.         }
  1440.         if(add){
  1441.             if(this.expressionEngine==null){
  1442.                 this.expressionEngine = cExp;
  1443.             }else{
  1444.                 conjunctionWithInternalInstance(cExp);
  1445.             }
  1446.             this.expressionEngine.setNot(this.notOperator);
  1447.         }
  1448.     }
  1449.     protected void conjunctionWithInternalInstance(AbstractBaseExpressionImpl newExpr) {
  1450.         ConjunctionExpressionImpl newConjunctionExpr = getConjunctionExpression();
  1451.         newConjunctionExpr.setAndConjunction(this.andLogicOperator);
  1452.         if(this.expressionEngine!=null){
  1453.             if(! (this.expressionEngine instanceof ConjunctionExpressionImpl) ){
  1454.                 // se non e' una congiunzione e' una singola operazione.
  1455.                 newConjunctionExpr.addExpression(this.expressionEngine);
  1456.             }else{
  1457.                 // se e' una congiunzione verifico che al suo interno vi siano solo semplici operazioni e non altre congiunzioni e che l'operatore utilizzato sia quello di default.
  1458.                 // se cosi' e' evito di creare una struttura simile alla seguente: ((A and B) and C)
  1459.                 // Preferisco invece ricreare un nuovo oggetto equivalente: A and B and C
  1460.                 boolean simpleConjunction = true;
  1461.                 ConjunctionExpressionImpl cTest = (ConjunctionExpressionImpl) this.expressionEngine;
  1462.                 if(cTest.isAndConjunction() != this.andLogicOperator){
  1463.                     simpleConjunction = false;
  1464.                 }
  1465.                 if(simpleConjunction){
  1466.                     for (Iterator<AbstractBaseExpressionImpl> iterator = cTest.getLista().iterator(); iterator.hasNext();) {
  1467.                         AbstractBaseExpressionImpl expr = iterator.next();
  1468.                         if( expr instanceof ConjunctionExpressionImpl ){
  1469.                             simpleConjunction = false;
  1470.                             break;
  1471.                         }
  1472.                     }
  1473.                 }
  1474.                 if(simpleConjunction){
  1475.                     for (Iterator<AbstractBaseExpressionImpl> iterator = cTest.getLista().iterator(); iterator.hasNext();) {
  1476.                         AbstractBaseExpressionImpl expr = iterator.next();
  1477.                         // Perchè ??? expr.setNot(false); Se si setta sempre a false una operazione scritta come and().not(expr).and().equals(a,2) resetta il not.
  1478.                         newConjunctionExpr.addExpression(expr);
  1479.                     }
  1480.                 }else{
  1481.                     newConjunctionExpr.addExpression(cTest);
  1482.                 }
  1483.             }
  1484.         }
  1485.         newConjunctionExpr.addExpression(newExpr);
  1486.         this.expressionEngine = newConjunctionExpr;
  1487.         this.expressionEngine.setNot(this.notOperator);
  1488.     }
  1489.    
  1490.    
  1491.    
  1492.    
  1493.    
  1494.     /* ************ UTILITY - CHECK ************ */
  1495.     protected void checkArgoments(IField field) throws ExpressionException {
  1496.         this.checkArgoments(field, true);
  1497.     }
  1498.     protected void checkArgoments(IField field, boolean constantPermit) throws ExpressionException {
  1499.         if(field==null){
  1500.             throw new ExpressionException(FIELD_IS_NULL);
  1501.         }
  1502.         if(!constantPermit &&
  1503.             (field instanceof ConstantField)
  1504.             ){
  1505.             throw new ExpressionException(FIELD_TYPE_CONSTANT_CANNOT_BE_USED);
  1506.         }
  1507.     }
  1508.    
  1509.     protected void checkArgoments(IField field, Object value) throws ExpressionException {
  1510.         this.checkArgoments(field, value, true);
  1511.     }
  1512.     protected void checkArgoments(IField field, Object value, boolean constantPermit) throws ExpressionException {
  1513.         if(field==null){
  1514.             throw new ExpressionException(FIELD_IS_NULL);
  1515.         }
  1516.         if(value==null){
  1517.             throw new ExpressionException("Value is null for field: "+field.toString());
  1518.         }
  1519.         try{
  1520.             this.objectFormatter.isSupported(value);
  1521.         }catch(Exception e){
  1522.             throw new ExpressionException("[Value] "+e.getMessage(),e);
  1523.         }
  1524.         if(!constantPermit &&
  1525.             (field instanceof ConstantField)
  1526.             ){
  1527.             throw new ExpressionException(FIELD_TYPE_CONSTANT_CANNOT_BE_USED);
  1528.         }
  1529.     }
  1530.    
  1531.     protected void checkArgoments(IField field, Object [] values) throws ExpressionException {
  1532.         this.checkArgoments(field, values, true);
  1533.     }
  1534.     protected void checkArgoments(IField field, Object [] values, boolean constantPermit) throws ExpressionException {
  1535.         if(field==null){
  1536.             throw new ExpressionException(FIELD_IS_NULL);
  1537.         }
  1538.         if(values==null){
  1539.             throw new ExpressionException("Values is null");
  1540.         }
  1541.         for (int i = 0; i < values.length; i++) {
  1542.             if(values[i]==null){
  1543.                 throw new ExpressionException("Values["+i+"] is null for field: "+field.toString());
  1544.             }
  1545.             try{
  1546.                 this.objectFormatter.isSupported(values[i]);
  1547.             }catch(Exception e){
  1548.                 throw new ExpressionException("[Value["+i+"]] "+e.getMessage(),e);
  1549.             }
  1550.         }
  1551.         if(!constantPermit &&
  1552.             (field instanceof ConstantField)
  1553.             ){
  1554.             throw new ExpressionException(FIELD_TYPE_CONSTANT_CANNOT_BE_USED);
  1555.         }
  1556.     }
  1557.    
  1558.     protected void checkArgoments(IField field, Object lower, Object high) throws ExpressionException {
  1559.         this.checkArgoments(field, lower, high, true);
  1560.     }
  1561.     protected void checkArgoments(IField field, Object lower, Object high, boolean constantPermit) throws ExpressionException {
  1562.         if(field==null){
  1563.             throw new ExpressionException(FIELD_IS_NULL);
  1564.         }
  1565.         if(lower==null){
  1566.             throw new ExpressionException("Lower is null for field: "+field.toString());
  1567.         }
  1568.         if(high==null){
  1569.             throw new ExpressionException("High is null for field: "+field.toString());
  1570.         }
  1571.         try{
  1572.             this.objectFormatter.isSupported(lower);
  1573.         }catch(Exception e){
  1574.             throw new ExpressionException("[Lower] "+e.getMessage(),e);
  1575.         }
  1576.         try{
  1577.             this.objectFormatter.isSupported(high);
  1578.         }catch(Exception e){
  1579.             throw new ExpressionException("[High] "+e.getMessage(),e);
  1580.         }
  1581.         if(!constantPermit &&
  1582.             (field instanceof ConstantField)
  1583.             ){
  1584.             throw new ExpressionException(FIELD_TYPE_CONSTANT_CANNOT_BE_USED);
  1585.         }
  1586.     }
  1587.    
  1588.     protected void checkArgoments(Map<IField, Object> propertyNameValues) throws ExpressionException {
  1589.         this.checkArgoments(propertyNameValues, true);
  1590.     }
  1591.     protected void checkArgoments(Map<IField, Object> propertyNameValues, boolean constantPermit) throws ExpressionException {
  1592.         if(propertyNameValues == null){
  1593.             throw new ExpressionException("PropertyNameValues is null");
  1594.         }
  1595.         Iterator<IField> fieldsIt = propertyNameValues.keySet().iterator();
  1596.         while (fieldsIt.hasNext()) {
  1597.             IField iField = fieldsIt.next();
  1598.             if(iField==null){
  1599.                 throw new ExpressionException(FIELD_IS_NULL);
  1600.             }
  1601.             if(propertyNameValues.get(iField)==null){
  1602.                 throw new ExpressionException("Value for Field["+iField+"] is null");  
  1603.             }
  1604.             try{
  1605.                 this.objectFormatter.isSupported(propertyNameValues.get(iField));
  1606.             }catch(Exception e){
  1607.                 throw new ExpressionException("[Value for Field["+iField+"]] "+e.getMessage(),e);
  1608.             }
  1609.             if(!constantPermit &&
  1610.                 (iField instanceof ConstantField)
  1611.                 ){
  1612.                 throw new ExpressionException(FIELD_TYPE_CONSTANT_CANNOT_BE_USED);
  1613.             }
  1614.         }
  1615.     }
  1616.    
  1617.     protected void checkConjunctionOperation(IExpression... expressions) throws ExpressionException {
  1618.         if(expressions==null || expressions.length<=0){
  1619.             throw new ExpressionException("Expressions is null or is empty");
  1620.         }
  1621.         for (int i = 0; i < expressions.length; i++) {
  1622.             if(expressions[i] == null){
  1623.                 throw new ExpressionException("Expression["+i+"] is null");
  1624.             }
  1625.             if( ! (expressions[i] instanceof ExpressionImpl)){
  1626.                 throw new ExpressionException("Expression["+i+"] has wrong type expect: "+ExpressionImpl.class.getName()+"  found: "+expressions[i].getClass().getName());
  1627.             }
  1628.         }
  1629.     }
  1630.    

  1631. }