ConjunctionExpressionSQL.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.sql;

  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.openspcoop2.generic_project.exception.ExpressionException;
  25. import org.openspcoop2.generic_project.expression.impl.AbstractBaseExpressionImpl;
  26. import org.openspcoop2.generic_project.expression.impl.ConjunctionExpressionImpl;
  27. import org.openspcoop2.generic_project.expression.impl.formatter.IObjectFormatter;
  28. import org.openspcoop2.utils.sql.ISQLQueryObject;

  29. /**
  30.  * ConjunctionExpressionSQL
  31.  *
  32.  * @author Poli Andrea (apoli@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  */
  36. public class ConjunctionExpressionSQL extends ConjunctionExpressionImpl implements ISQLExpression {

  37.     @SuppressWarnings("unused")
  38.     private ISQLFieldConverter sqlFieldConverter;
  39.    
  40.     public ConjunctionExpressionSQL(ISQLFieldConverter sqlFieldConverter,IObjectFormatter objectFormatter) {
  41.         super(objectFormatter);
  42.         this.sqlFieldConverter = sqlFieldConverter;
  43.     }

  44.     private String toSqlEngine(SQLMode mode,ISQLQueryObject sqlQueryObject, List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  45.         StringBuilder bf = new StringBuilder();
  46.         if(isNot()){
  47.             bf.append("( NOT ");
  48.         }
  49.         bf.append("( ");
  50.         int index = 0;
  51.         for (Iterator<AbstractBaseExpressionImpl> iterator = this.getLista().iterator(); iterator.hasNext();) {
  52.             AbstractBaseExpressionImpl exp = iterator.next();
  53.             if(index>0){
  54.                 if(this.isAndConjunction())
  55.                     bf.append(" AND ");
  56.                 else
  57.                     bf.append(" OR ");
  58.             }
  59.             if(exp instanceof ISQLExpression){
  60.                 if(sqlQueryObject!=null){
  61.                     ISQLQueryObject sqlQ = null;
  62.                     try{
  63.                         sqlQ = sqlQueryObject.newSQLQueryObject();
  64.                    
  65.                         switch (mode) {
  66.                         case STANDARD:
  67.                             ((ISQLExpression)exp).toSql(sqlQ);
  68.                             bf.append(sqlQ.createSQLConditions());
  69.                             break;
  70.                         case PREPARED_STATEMENT:
  71.                             ((ISQLExpression)exp).toSqlPreparedStatement(sqlQ,oggettiPreparedStatement);
  72.                             bf.append(sqlQ.createSQLConditions());
  73.                             break;
  74.                         case JPA:
  75.                             ((ISQLExpression)exp).toSqlJPA(sqlQ,oggettiJPA);
  76.                             bf.append(sqlQ.createSQLConditions());
  77.                             break;
  78.                         }
  79.                     }catch(Exception e){
  80.                         throw new ExpressionException("Expression["+index+"] (type:"+exp.getClass().getName()+")  sqlQueryObject error: "+e.getMessage(),e);
  81.                     }
  82.                 }
  83.                 else{
  84.                     switch (mode) {
  85.                     case STANDARD:
  86.                         bf.append(((ISQLExpression)exp).toSql());
  87.                         break;
  88.                     case PREPARED_STATEMENT:
  89.                         bf.append(((ISQLExpression)exp).toSqlPreparedStatement(oggettiPreparedStatement));
  90.                         break;
  91.                     case JPA:
  92.                         bf.append(((ISQLExpression)exp).toSqlJPA(oggettiJPA));
  93.                         break;
  94.                     }
  95.                 }
  96.             }else{
  97.                 throw new ExpressionException("Expression["+index+"] (type:"+exp.getClass().getName()+") is not as cast with "+ISQLExpression.class.getName());
  98.             }
  99.             index++;
  100.         }
  101.         bf.append(" )");
  102.         if(isNot()){
  103.             bf.append(" )");
  104.         }
  105.         return bf.toString();
  106.     }
  107.    
  108.     private void toSqlEngine(ISQLQueryObject sqlQueryObject,SQLMode mode,List<Object> oggettiPreparedStatement,Map<String, Object> oggettiJPA)throws ExpressionException{
  109.         try{
  110.             String s = toSqlEngine(mode, sqlQueryObject, oggettiPreparedStatement, oggettiJPA);
  111.             s = s.substring(1,s.length()-2);
  112.             sqlQueryObject.addWhereCondition(s);
  113.         }catch(Exception e){
  114.             throw new ExpressionException(e);
  115.         }  
  116.     }
  117.    
  118.     @Override
  119.     public String toSql() throws ExpressionException {
  120.         return toSqlEngine(SQLMode.STANDARD, null, null, null);
  121.     }

  122.     @Override
  123.     public String toSqlPreparedStatement(List<Object> oggetti)
  124.             throws ExpressionException {
  125.         return toSqlEngine(SQLMode.PREPARED_STATEMENT, null, oggetti, null);
  126.     }

  127.     @Override
  128.     public String toSqlJPA(Map<String, Object> oggetti)
  129.             throws ExpressionException {
  130.         return toSqlEngine(SQLMode.JPA, null, null, oggetti);
  131.     }
  132.    
  133.     @Override
  134.     public void toSql(ISQLQueryObject sqlQueryObject) throws ExpressionException {
  135.         toSqlEngine(sqlQueryObject,SQLMode.STANDARD, null, null);
  136.     }

  137.     @Override
  138.     public void toSqlPreparedStatement(ISQLQueryObject sqlQueryObject,List<Object> oggetti)
  139.             throws ExpressionException {
  140.         toSqlEngine(sqlQueryObject,SQLMode.PREPARED_STATEMENT, oggetti, null);
  141.     }

  142.     @Override
  143.     public void toSqlJPA(ISQLQueryObject sqlQueryObject,Map<String, Object> oggetti)
  144.             throws ExpressionException {
  145.         toSqlEngine(sqlQueryObject,SQLMode.JPA, null, oggetti);
  146.     }
  147. }