AbstractSQLFieldConverter.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 org.openspcoop2.generic_project.beans.AliasField;
  22. import org.openspcoop2.generic_project.beans.ConstantField;
  23. import org.openspcoop2.generic_project.beans.CustomField;
  24. import org.openspcoop2.generic_project.beans.IAliasTableField;
  25. import org.openspcoop2.generic_project.beans.IField;
  26. import org.openspcoop2.generic_project.beans.IModel;
  27. import org.openspcoop2.generic_project.exception.ExpressionException;

  28. /**
  29.  * AbstractSQLFieldConverter
  30.  *
  31.  * @author Poli Andrea (apoli@link.it)
  32.  * @author $Author$
  33.  * @version $Rev$, $Date$
  34.  */
  35. public abstract class AbstractSQLFieldConverter implements ISQLFieldConverter {

  36.    
  37.     @Override
  38.     public String toColumn(IField field,boolean returnAlias,boolean appendTablePrefix) throws ExpressionException {
  39.        
  40.         if(field instanceof CustomField){
  41.             CustomField cf = (CustomField) field;
  42.            
  43.             if(appendTablePrefix){
  44.                 String table =  this.toAliasTable(field);
  45.                 if(table!=null && !"".equals(table)){
  46.                     if(returnAlias){
  47.                         return table+"."+cf.getAliasColumnName();
  48.                     }else{
  49.                         return table+"."+cf.getColumnName();
  50.                     }
  51.                 }
  52.                 else{
  53.                     if(returnAlias){
  54.                         return cf.getAliasColumnName();
  55.                     }else{
  56.                         return cf.getColumnName();
  57.                     }
  58.                 }
  59.             }else{
  60.                 if(returnAlias){
  61.                     return cf.getAliasColumnName();
  62.                 }else{
  63.                     return cf.getColumnName();
  64.                 }
  65.             }
  66.         }
  67.        
  68.         else if(field instanceof ConstantField){
  69.             ConstantField cf = (ConstantField) field;
  70.             if(returnAlias){
  71.                 return cf.getAlias();
  72.             }
  73.             else{
  74.                 return cf.getConstantValue(this.getDatabaseType());
  75.             }
  76.         }
  77.        
  78.         else if(field instanceof AliasField){
  79.             AliasField af = (AliasField) field;
  80. /**         if(returnAlias){
  81. //              return af.getAlias();
  82. //          }
  83. //          else{
  84. //              return this.toColumn(af.getField(), returnAlias, appendTablePrefix);
  85. //          }*/
  86.             return af.getAlias(); // un alias deve usare sempre l'alias
  87.         }
  88.        
  89.         else if(field instanceof IAliasTableField){
  90.            
  91.             IAliasTableField atf = (IAliasTableField) field;
  92.             if(appendTablePrefix){
  93.                 return atf.getAliasTable()+"."+this.toColumn(atf.getField(), returnAlias, false);
  94.             }
  95.             else{
  96.                 return this.toColumn(atf.getField(), returnAlias, false);
  97.             }
  98.            
  99.         }
  100.        
  101.         else{
  102.            
  103.             throw new ExpressionException("Field ["+field.toString()+"] not supported by converter.toColumn: "+this.getClass().getName());
  104.            
  105.         }
  106.        
  107.     }
  108.    
  109.     @Override
  110.     public String toColumn(IField field,boolean appendTablePrefix) throws ExpressionException{
  111.         return this.toColumn(field, false, appendTablePrefix);
  112.     }
  113.    
  114.     @Override
  115.     public String toAliasColumn(IField field,boolean appendTablePrefix) throws ExpressionException{
  116.         return this.toColumn(field, true, appendTablePrefix);
  117.     }
  118.    
  119.    
  120.    
  121.    
  122.    
  123.    
  124.    
  125.     @Override
  126.     public String toTable(IField field,boolean returnAlias) throws ExpressionException {
  127.        
  128.         if(field instanceof CustomField){
  129.             CustomField cf = (CustomField) field;
  130.             if(returnAlias){
  131.                 return cf.getAliasTableName();
  132.             }else{
  133.                 return cf.getTableName();
  134.             }
  135.         }

  136.         else if(field instanceof ConstantField){
  137.             return this.toTable(this.getRootModel(), returnAlias);
  138.         }
  139.        
  140.         else if(field instanceof AliasField){
  141.             AliasField af = (AliasField) field;
  142.             return this.toTable(af.getField(), returnAlias);
  143.         }
  144.        
  145.         else if(field instanceof IAliasTableField){
  146.             IAliasTableField af = (IAliasTableField) field;
  147.             if(returnAlias){
  148.                 return af.getAliasTable();
  149.             }
  150.             else{
  151.                 return this.toTable(af.getField());
  152.             }
  153.         }
  154.        
  155.         else{
  156.             throw new ExpressionException("Field ["+field.toString()+"] not supported by converter.toTable: "+this.getClass().getName());
  157.         }
  158.     }
  159.    
  160.     @Override
  161.     public String toTable(IField field) throws ExpressionException {
  162.         return this.toTable(field, false);
  163.     }

  164.     @Override
  165.     public String toAliasTable(IField field) throws ExpressionException{
  166.         return this.toTable(field, true);
  167.     }
  168.    
  169.    
  170.    
  171.    
  172.    
  173.    
  174.    
  175.     @Override
  176.     public String toTable(IModel<?> model,boolean returnAlias) throws ExpressionException {
  177.         throw new ExpressionException("Model ["+model.toString()+"] not supported by converter.toTable: "+this.getClass().getName());
  178.     }

  179.     @Override
  180.     public String toTable(IModel<?> model) throws ExpressionException {
  181.         return this.toTable(model, false);
  182.     }

  183.     @Override
  184.     public String toAliasTable(IModel<?> model) throws ExpressionException{
  185.         return this.toTable(model, true);
  186.     }
  187.    


  188.    

  189.    

  190. }