JDBCConfigurazioneFiltroServiceSearchImpl.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.core.plugins.dao.jdbc;

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

  24. import java.sql.Connection;

  25. import org.slf4j.Logger;

  26. import org.openspcoop2.utils.sql.ISQLQueryObject;

  27. import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
  28. import org.openspcoop2.generic_project.dao.jdbc.utils.IJDBCFetch;
  29. import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
  30. import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceSearchWithId;
  31. import org.openspcoop2.core.plugins.IdConfigurazioneFiltro;
  32. import org.openspcoop2.generic_project.utils.UtilsTemplate;
  33. import org.openspcoop2.generic_project.beans.CustomField;
  34. import org.openspcoop2.generic_project.beans.InUse;
  35. import org.openspcoop2.generic_project.beans.IField;
  36. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  37. import org.openspcoop2.generic_project.beans.UnionExpression;
  38. import org.openspcoop2.generic_project.beans.Union;
  39. import org.openspcoop2.generic_project.beans.FunctionField;
  40. import org.openspcoop2.generic_project.exception.MultipleResultException;
  41. import org.openspcoop2.generic_project.exception.NotFoundException;
  42. import org.openspcoop2.generic_project.exception.NotImplementedException;
  43. import org.openspcoop2.generic_project.exception.ServiceException;
  44. import org.openspcoop2.generic_project.expression.IExpression;
  45. import org.openspcoop2.generic_project.dao.jdbc.JDBCExpression;
  46. import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;

  47. import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;
  48. import org.openspcoop2.core.plugins.dao.jdbc.converter.ConfigurazioneFiltroFieldConverter;
  49. import org.openspcoop2.core.plugins.dao.jdbc.fetch.ConfigurazioneFiltroFetch;
  50. import org.openspcoop2.core.plugins.ConfigurazioneFiltro;

  51. /**    
  52.  * JDBCConfigurazioneFiltroServiceSearchImpl
  53.  *
  54.  * @author Poli Andrea (poli@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  */
  58. public class JDBCConfigurazioneFiltroServiceSearchImpl implements IJDBCServiceSearchWithId<ConfigurazioneFiltro, IdConfigurazioneFiltro, JDBCServiceManager> {

  59.     private ConfigurazioneFiltroFieldConverter _configurazioneFiltroFieldConverter = null;
  60.     public ConfigurazioneFiltroFieldConverter getConfigurazioneFiltroFieldConverter() {
  61.         if(this._configurazioneFiltroFieldConverter==null){
  62.             this._configurazioneFiltroFieldConverter = new ConfigurazioneFiltroFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  63.         }      
  64.         return this._configurazioneFiltroFieldConverter;
  65.     }
  66.     @Override
  67.     public ISQLFieldConverter getFieldConverter() {
  68.         return this.getConfigurazioneFiltroFieldConverter();
  69.     }
  70.    
  71.     private ConfigurazioneFiltroFetch configurazioneFiltroFetch = new ConfigurazioneFiltroFetch();
  72.     public ConfigurazioneFiltroFetch getConfigurazioneFiltroFetch() {
  73.         return this.configurazioneFiltroFetch;
  74.     }
  75.     @Override
  76.     public IJDBCFetch getFetch() {
  77.         return getConfigurazioneFiltroFetch();
  78.     }
  79.    
  80.    
  81.     private JDBCServiceManager jdbcServiceManager = null;

  82.     @Override
  83.     public void setServiceManager(JDBCServiceManager serviceManager) throws ServiceException{
  84.         this.jdbcServiceManager = serviceManager;
  85.     }
  86.    
  87.     @Override
  88.     public JDBCServiceManager getServiceManager() throws ServiceException{
  89.         return this.jdbcServiceManager;
  90.     }
  91.    

  92.     @Override
  93.     public IdConfigurazioneFiltro convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneFiltro configurazioneFiltro) throws NotImplementedException, ServiceException, Exception{
  94.    
  95.         IdConfigurazioneFiltro idConfigurazioneFiltro = new IdConfigurazioneFiltro();
  96.         idConfigurazioneFiltro.setNome(configurazioneFiltro.getNome());
  97.         return idConfigurazioneFiltro;
  98.     }
  99.    
  100.     @Override
  101.     public ConfigurazioneFiltro get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {
  102.         Long id_configurazioneFiltro = ( (id!=null && id.getId()!=null && id.getId()>0) ? id.getId() : this.findIdConfigurazioneFiltro(jdbcProperties, log, connection, sqlQueryObject, id, true));
  103.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, id_configurazioneFiltro,idMappingResolutionBehaviour);
  104.        
  105.        
  106.     }
  107.    
  108.     @Override
  109.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id) throws MultipleResultException, NotImplementedException, ServiceException,Exception {

  110.         Long id_configurazioneFiltro = this.findIdConfigurazioneFiltro(jdbcProperties, log, connection, sqlQueryObject, id, false);
  111.         return id_configurazioneFiltro != null && id_configurazioneFiltro > 0;
  112.        
  113.     }
  114.    
  115.     @Override
  116.     public List<IdConfigurazioneFiltro> findAllIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

  117.         List<IdConfigurazioneFiltro> list = new ArrayList<IdConfigurazioneFiltro>();

  118.         // TODO: implementazione non efficiente.
  119.         // Per ottenere una implementazione efficiente:
  120.         // 1. Usare metodo select di questa classe indirizzando esattamente i field necessari a create l'ID logico
  121.         // 2. Usare metodo getConfigurazioneFiltroFetch() sul risultato della select per ottenere un oggetto ConfigurazioneFiltro
  122.         //    La fetch con la map inserirĂ  nell'oggetto solo i valori estratti
  123.         // 3. Usare metodo convertToId per ottenere l'id

  124.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  125.        
  126.         for(Long id: ids) {
  127.             ConfigurazioneFiltro configurazioneFiltro = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  128.             IdConfigurazioneFiltro idConfigurazioneFiltro = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,configurazioneFiltro);
  129.             list.add(idConfigurazioneFiltro);
  130.         }

  131.         return list;
  132.        
  133.     }
  134.    
  135.     @Override
  136.     public List<ConfigurazioneFiltro> findAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

  137.         List<ConfigurazioneFiltro> list = new ArrayList<ConfigurazioneFiltro>();
  138.        
  139.         // TODO: implementazione non efficiente.
  140.         // Per ottenere una implementazione efficiente:
  141.         // 1. Usare metodo select di questa classe indirizzando esattamente i field necessari
  142.         // 2. Usare metodo getConfigurazioneFiltroFetch() sul risultato della select per ottenere un oggetto ConfigurazioneFiltro
  143.         //    La fetch con la map inserirĂ  nell'oggetto solo i valori estratti

  144.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  145.        
  146.         for(Long id: ids) {
  147.             list.add(this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour));
  148.         }

  149.         return list;      
  150.        
  151.     }
  152.    
  153.     @Override
  154.     public ConfigurazioneFiltro find(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour)
  155.         throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {

  156.         long id = this.findTableId(jdbcProperties, log, connection, sqlQueryObject, expression);
  157.         if(id>0){
  158.             return this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  159.         }else{
  160.             throw new NotFoundException("Entry with id["+id+"] not found");
  161.         }
  162.        
  163.     }
  164.    
  165.     @Override
  166.     public NonNegativeNumber count(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws NotImplementedException, ServiceException,Exception {
  167.        
  168.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareCount(jdbcProperties, log, connection, sqlQueryObject, expression,
  169.                                                 this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model());
  170.        
  171.         sqlQueryObject.addSelectCountField(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model())+".id","tot",true);
  172.        
  173.         joinEngine(expression,sqlQueryObject);
  174.        
  175.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.count(jdbcProperties, log, connection, sqlQueryObject, expression,
  176.                                                                             this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),listaQuery);
  177.     }

  178.     @Override
  179.     public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id) throws NotFoundException, NotImplementedException, ServiceException,Exception {
  180.        
  181.         Long id_configurazioneFiltro = this.findIdConfigurazioneFiltro(jdbcProperties, log, connection, sqlQueryObject, id, true);
  182.         return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, id_configurazioneFiltro);
  183.        
  184.     }

  185.     @Override
  186.     public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  187.                                                     JDBCPaginatedExpression paginatedExpression, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  188.         return this.select(jdbcProperties, log, connection, sqlQueryObject,
  189.                                 paginatedExpression, false, field);
  190.     }
  191.    
  192.     @Override
  193.     public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  194.                                                     JDBCPaginatedExpression paginatedExpression, boolean distinct, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  195.         List<Map<String,Object>> map =
  196.             this.select(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, distinct, new IField[]{field});
  197.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectSingleObject(map);
  198.     }
  199.    
  200.     @Override
  201.     public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  202.                                                     JDBCPaginatedExpression paginatedExpression, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  203.         return this.select(jdbcProperties, log, connection, sqlQueryObject,
  204.                                 paginatedExpression, false, field);
  205.     }
  206.    
  207.     @Override
  208.     public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  209.                                                     JDBCPaginatedExpression paginatedExpression, boolean distinct, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  210.        
  211.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,field);
  212.         try{
  213.        
  214.             ISQLQueryObject sqlQueryObjectDistinct =
  215.                         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(distinct,sqlQueryObject, paginatedExpression, log,
  216.                                                 this.getConfigurazioneFiltroFieldConverter(), field);

  217.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, sqlQueryObjectDistinct);
  218.            
  219.         }finally{
  220.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,field);
  221.         }
  222.     }

  223.     @Override
  224.     public Object aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  225.                                                     JDBCExpression expression, FunctionField functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  226.         Map<String,Object> map =
  227.             this.aggregate(jdbcProperties, log, connection, sqlQueryObject, expression, new FunctionField[]{functionField});
  228.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectAggregateObject(map,functionField);
  229.     }
  230.    
  231.     @Override
  232.     public Map<String,Object> aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  233.                                                     JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {                                                  
  234.        
  235.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
  236.         try{
  237.             List<Map<String,Object>> list = selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
  238.             return list.get(0);
  239.         }finally{
  240.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
  241.         }
  242.     }

  243.     @Override
  244.     public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  245.                                                     JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  246.        
  247.         if(expression.getGroupByFields().isEmpty()){
  248.             throw new ServiceException("GroupBy conditions not found in expression");
  249.         }
  250.        
  251.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
  252.         try{
  253.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
  254.         }finally{
  255.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
  256.         }
  257.     }
  258.    

  259.     @Override
  260.     public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  261.                                                     JDBCPaginatedExpression paginatedExpression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  262.        
  263.         if(paginatedExpression.getGroupByFields().isEmpty()){
  264.             throw new ServiceException("GroupBy conditions not found in expression");
  265.         }
  266.        
  267.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,functionField);
  268.         try{
  269.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression);
  270.         }finally{
  271.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,functionField);
  272.         }
  273.     }
  274.    
  275.     protected List<Map<String,Object>> selectEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  276.                                                 IExpression expression) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  277.         return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression, null);
  278.     }
  279.     protected List<Map<String,Object>> selectEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  280.                                                 IExpression expression, ISQLQueryObject sqlQueryObjectDistinct) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  281.        
  282.         List<Object> listaQuery = new ArrayList<>();
  283.         List<JDBCObject> listaParams = new ArrayList<>();
  284.         List<Object> returnField = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSelect(jdbcProperties, log, connection, sqlQueryObject,
  285.                                 expression, this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),
  286.                                 listaQuery,listaParams);
  287.        
  288.         joinEngine(expression,sqlQueryObject);
  289.        
  290.         List<Map<String,Object>> list = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.select(jdbcProperties, log, connection,
  291.                                         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(sqlQueryObject,sqlQueryObjectDistinct),
  292.                                         expression, this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),
  293.                                         listaQuery,listaParams,returnField);
  294.         if(list!=null && !list.isEmpty()){
  295.             return list;
  296.         }
  297.         else{
  298.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  299.         }
  300.     }
  301.    
  302.     @Override
  303.     public List<Map<String,Object>> union(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  304.                                                 Union union, UnionExpression ... unionExpression) throws ServiceException,NotFoundException,NotImplementedException,Exception {    
  305.        
  306.         List<ISQLQueryObject> sqlQueryObjectInnerList = new ArrayList<>();
  307.         List<JDBCObject> jdbcObjects = new ArrayList<>();
  308.         List<Class<?>> returnClassTypes = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareUnion(jdbcProperties, log, connection, sqlQueryObject,
  309.                                 this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),
  310.                                 sqlQueryObjectInnerList, jdbcObjects, union, unionExpression);
  311.        
  312.         if(unionExpression!=null){
  313.             for (int i = 0; i < unionExpression.length; i++) {
  314.                 UnionExpression ue = unionExpression[i];
  315.                 IExpression expression = ue.getExpression();
  316.                 joinEngine(expression,sqlQueryObjectInnerList.get(i));
  317.             }
  318.         }
  319.        
  320.         List<Map<String,Object>> list = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.union(jdbcProperties, log, connection, sqlQueryObject,
  321.                                         this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),
  322.                                         sqlQueryObjectInnerList, jdbcObjects, returnClassTypes, union, unionExpression);
  323.         if(list!=null && !list.isEmpty()){
  324.             return list;
  325.         }
  326.         else{
  327.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  328.         }                              
  329.     }
  330.    
  331.     @Override
  332.     public NonNegativeNumber unionCount(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  333.                                                 Union union, UnionExpression ... unionExpression) throws ServiceException,NotFoundException,NotImplementedException,Exception {    
  334.        
  335.         List<ISQLQueryObject> sqlQueryObjectInnerList = new ArrayList<>();
  336.         List<JDBCObject> jdbcObjects = new ArrayList<>();
  337.         List<Class<?>> returnClassTypes = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareUnionCount(jdbcProperties, log, connection, sqlQueryObject,
  338.                                 this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),
  339.                                 sqlQueryObjectInnerList, jdbcObjects, union, unionExpression);
  340.        
  341.         if(unionExpression!=null){
  342.             for (int i = 0; i < unionExpression.length; i++) {
  343.                 UnionExpression ue = unionExpression[i];
  344.                 IExpression expression = ue.getExpression();
  345.                 joinEngine(expression,sqlQueryObjectInnerList.get(i));
  346.             }
  347.         }
  348.        
  349.         NonNegativeNumber number = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.unionCount(jdbcProperties, log, connection, sqlQueryObject,
  350.                                         this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(),
  351.                                         sqlQueryObjectInnerList, jdbcObjects, returnClassTypes, union, unionExpression);
  352.         if(number!=null && number.longValue()>=0){
  353.             return number;
  354.         }
  355.         else{
  356.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  357.         }
  358.     }



  359.     // -- ConstructorExpression

  360.     @Override
  361.     public JDBCExpression newExpression(Logger log) throws NotImplementedException, ServiceException {
  362.         try{
  363.             return new JDBCExpression(this.getConfigurazioneFiltroFieldConverter());
  364.         }catch(Exception e){
  365.             throw new ServiceException(e);
  366.         }
  367.     }


  368.     @Override
  369.     public JDBCPaginatedExpression newPaginatedExpression(Logger log) throws NotImplementedException, ServiceException {
  370.         try{
  371.             return new JDBCPaginatedExpression(this.getConfigurazioneFiltroFieldConverter());
  372.         }catch(Exception e){
  373.             throw new ServiceException(e);
  374.         }
  375.     }
  376.    
  377.     @Override
  378.     public JDBCExpression toExpression(JDBCPaginatedExpression paginatedExpression, Logger log) throws NotImplementedException, ServiceException {
  379.         try{
  380.             return new JDBCExpression(paginatedExpression);
  381.         }catch(Exception e){
  382.             throw new ServiceException(e);
  383.         }
  384.     }

  385.     @Override
  386.     public JDBCPaginatedExpression toPaginatedExpression(JDBCExpression expression, Logger log) throws NotImplementedException, ServiceException {
  387.         try{
  388.             return new JDBCPaginatedExpression(expression);
  389.         }catch(Exception e){
  390.             throw new ServiceException(e);
  391.         }
  392.     }
  393.    
  394.    
  395.    
  396.     // -- DB

  397.     @Override
  398.     public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id, ConfigurazioneFiltro obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  399.         _mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
  400.                 this.get(jdbcProperties,log,connection,sqlQueryObject,id,null));
  401.     }
  402.    
  403.     @Override
  404.     public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, ConfigurazioneFiltro obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  405.         _mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
  406.                 this.get(jdbcProperties,log,connection,sqlQueryObject,tableId,null));
  407.     }
  408.     private void _mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneFiltro obj, ConfigurazioneFiltro imgSaved) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  409.         if(imgSaved==null){
  410.             return;
  411.         }
  412.         obj.setId(imgSaved.getId());

  413.     }
  414.    
  415.     @Override
  416.     public ConfigurazioneFiltro get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  417.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId), idMappingResolutionBehaviour);
  418.     }
  419.    
  420.     private ConfigurazioneFiltro getEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  421.    
  422.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  423.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  424.        
  425.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();
  426.                
  427.         ConfigurazioneFiltro configurazioneFiltro = new ConfigurazioneFiltro();
  428.        

  429.         // Object configurazioneFiltro
  430.         ISQLQueryObject sqlQueryObjectGet_configurazioneFiltro = sqlQueryObjectGet.newSQLQueryObject();
  431.         sqlQueryObjectGet_configurazioneFiltro.setANDLogicOperator(true);
  432.         sqlQueryObjectGet_configurazioneFiltro.addFromTable(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model()));
  433.         sqlQueryObjectGet_configurazioneFiltro.addSelectField("id");
  434.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME,true));
  435.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().DESCRIZIONE,true));
  436.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().TIPO_MITTENTE,true));
  437.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME_MITTENTE,true));
  438.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().IDPORTA_MITTENTE,true));
  439.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().TIPO_DESTINATARIO,true));
  440.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME_DESTINATARIO,true));
  441.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().IDPORTA_DESTINATARIO,true));
  442.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().TIPO_SERVIZIO,true));
  443.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME_SERVIZIO,true));
  444.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().VERSIONE_SERVIZIO,true));
  445.         sqlQueryObjectGet_configurazioneFiltro.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().AZIONE,true));
  446.         sqlQueryObjectGet_configurazioneFiltro.addWhereCondition("id=?");

  447.         // Get configurazioneFiltro
  448.         configurazioneFiltro = (ConfigurazioneFiltro) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_configurazioneFiltro.createSQLQuery(), jdbcProperties.isShowSql(), ConfigurazioneFiltro.model(), this.getConfigurazioneFiltroFetch(),
  449.             new JDBCObject(tableId,Long.class));



  450.        
  451.         return configurazioneFiltro;  
  452.    
  453.     }
  454.    
  455.     @Override
  456.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  457.         return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  458.     }
  459.    
  460.     private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  461.    
  462.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  463.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  464.                
  465.         boolean existsConfigurazioneFiltro = false;

  466.         sqlQueryObject = sqlQueryObject.newSQLQueryObject();
  467.         sqlQueryObject.setANDLogicOperator(true);

  468.         sqlQueryObject.addFromTable(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model()));
  469.         sqlQueryObject.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME,true));
  470.         sqlQueryObject.addWhereCondition("id=?");


  471.         // Exists configurazioneFiltro
  472.         existsConfigurazioneFiltro = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  473.             new JDBCObject(tableId,Long.class));

  474.        
  475.         return existsConfigurazioneFiltro;
  476.    
  477.     }
  478.    
  479.     private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{
  480.    
  481.         // nop
  482.        
  483.     }
  484.    
  485.     protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id) throws NotFoundException, ServiceException, NotImplementedException, Exception{
  486.         // Identificativi
  487.         java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
  488.         Long longId = this.findIdConfigurazioneFiltro(jdbcProperties, log, connection, sqlQueryObject.newSQLQueryObject(), id, true);
  489.         rootTableIdValues.add(longId);
  490.        
  491.         return rootTableIdValues;
  492.     }
  493.    
  494.     protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
  495.    
  496.         ConfigurazioneFiltroFieldConverter converter = this.getConfigurazioneFiltroFieldConverter();
  497.         Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
  498.         UtilsTemplate<IField> utilities = new UtilsTemplate<>();
  499.        
  500.         // ConfigurazioneFiltro.model()
  501.         mapTableToPKColumn.put(converter.toTable(ConfigurazioneFiltro.model()),
  502.             utilities.newList(
  503.                 new CustomField("id", Long.class, "id", converter.toTable(ConfigurazioneFiltro.model()))
  504.             ));
  505.        
  506.         return mapTableToPKColumn;      
  507.     }
  508.    
  509.     @Override
  510.     public List<Long> findAllTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression paginatedExpression) throws ServiceException, NotImplementedException, Exception {
  511.        
  512.         List<Long> list = new ArrayList<Long>();

  513.         sqlQueryObject.setSelectDistinct(true);
  514.         sqlQueryObject.setANDLogicOperator(true);
  515.         sqlQueryObject.addSelectField(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model())+".id");
  516.         Class<?> objectIdClass = Long.class;
  517.        
  518.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFindAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  519.                                                 this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model());
  520.        
  521.         joinEngine(paginatedExpression,sqlQueryObject);
  522.        
  523.         List<Object> listObjects = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.findAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  524.                                                                             this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(), objectIdClass, listaQuery);
  525.         for(Object object: listObjects) {
  526.             list.add((Long)object);
  527.         }

  528.         return list;
  529.        
  530.     }
  531.    
  532.     @Override
  533.     public long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws ServiceException, NotFoundException, MultipleResultException, NotImplementedException, Exception {
  534.    
  535.         sqlQueryObject.setSelectDistinct(true);
  536.         sqlQueryObject.setANDLogicOperator(true);
  537.         sqlQueryObject.addSelectField(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model())+".id");
  538.         Class<?> objectIdClass = Long.class;
  539.        
  540.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFind(jdbcProperties, log, connection, sqlQueryObject, expression,
  541.                                                 this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model());
  542.        
  543.         joinEngine(expression,sqlQueryObject);

  544.         Object res = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.find(jdbcProperties, log, connection, sqlQueryObject, expression,
  545.                                                         this.getConfigurazioneFiltroFieldConverter(), ConfigurazioneFiltro.model(), objectIdClass, listaQuery);
  546.         if(res!=null && (((Long) res).longValue()>0) ){
  547.             return ((Long) res).longValue();
  548.         }
  549.         else{
  550.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  551.         }
  552.        
  553.     }

  554.     @Override
  555.     public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws ServiceException, NotFoundException, NotImplementedException, Exception {
  556.         return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  557.     }

  558.     private InUse inUseEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws ServiceException, NotFoundException, NotImplementedException, Exception {

  559.         InUse inUse = new InUse();
  560.         inUse.setInUse(false);
  561.        
  562.         /*
  563.          * TODO: implement code that checks whether the object identified by the id parameter is used by other objects
  564.         */
  565.        
  566.         // Delete this line when you have implemented the method
  567.         int throwNotImplemented = 1;
  568.         if(throwNotImplemented==1){
  569.                 throw new NotImplementedException("NotImplemented");
  570.         }
  571.         // Delete this line when you have implemented the method

  572.         return inUse;

  573.     }
  574.    
  575.     @Override
  576.     public IdConfigurazioneFiltro findId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, boolean throwNotFound)
  577.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  578.        
  579.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  580.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  581.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

  582.          
  583.         // Object _configurazioneFiltro
  584.         sqlQueryObjectGet.addFromTable(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model()));
  585.         sqlQueryObjectGet.addSelectField(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME,true));
  586.         sqlQueryObjectGet.setANDLogicOperator(true);
  587.         sqlQueryObjectGet.addWhereCondition("id=?");

  588.         // Recupero _configurazioneFiltro
  589.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_configurazioneFiltro = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  590.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(tableId,Long.class)
  591.         };
  592.         List<Class<?>> listaFieldIdReturnType_configurazioneFiltro = new ArrayList<Class<?>>();
  593.         listaFieldIdReturnType_configurazioneFiltro.add(String.class);
  594.         org.openspcoop2.core.plugins.IdConfigurazioneFiltro id_configurazioneFiltro = null;
  595.         List<Object> listaFieldId_configurazioneFiltro = jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  596.                 listaFieldIdReturnType_configurazioneFiltro, searchParams_configurazioneFiltro);
  597.         if(listaFieldId_configurazioneFiltro==null || listaFieldId_configurazioneFiltro.size()<=0){
  598.             if(throwNotFound){
  599.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  600.             }
  601.         }
  602.         else{
  603.             // set _configurazioneFiltro
  604.             id_configurazioneFiltro = new org.openspcoop2.core.plugins.IdConfigurazioneFiltro();
  605.             id_configurazioneFiltro.setNome((String)listaFieldId_configurazioneFiltro.get(0));
  606.         }
  607.        
  608.         return id_configurazioneFiltro;
  609.        
  610.     }

  611.     @Override
  612.     public Long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id, boolean throwNotFound)
  613.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  614.    
  615.         return this.findIdConfigurazioneFiltro(jdbcProperties,log,connection,sqlQueryObject,id,throwNotFound);
  616.            
  617.     }
  618.    
  619.     @Override
  620.     public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  621.                                             String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
  622.        
  623.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
  624.                                                                                             sql,returnClassTypes,param);
  625.                                                        
  626.     }
  627.    
  628.     protected Long findIdConfigurazioneFiltro(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneFiltro id, boolean throwNotFound) throws NotFoundException, ServiceException, NotImplementedException, Exception {

  629.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  630.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  631.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

  632.         if(id==null){
  633.             throw new ServiceException("Id not defined");
  634.         }
  635.         if(id.getNome()==null){
  636.             throw new ServiceException("Id.nome not defined");
  637.         }
  638.                      

  639.         // Object _configurazioneFiltro
  640.         sqlQueryObjectGet.addFromTable(this.getConfigurazioneFiltroFieldConverter().toTable(ConfigurazioneFiltro.model()));
  641.         sqlQueryObjectGet.addSelectField("id");
  642.         // Devono essere mappati nella where condition i metodi dell'oggetto id.getXXX
  643.         sqlQueryObjectGet.setANDLogicOperator(true);
  644.         sqlQueryObjectGet.setSelectDistinct(true);
  645.         sqlQueryObjectGet.addWhereCondition(this.getConfigurazioneFiltroFieldConverter().toColumn(ConfigurazioneFiltro.model().NOME,true)+"=?");

  646.         // Recupero _configurazioneFiltro
  647.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_configurazioneFiltro = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  648.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(id.getNome(),String.class)
  649.         };
  650.         Long id_configurazioneFiltro = null;
  651.         try{
  652.             id_configurazioneFiltro = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  653.                         Long.class, searchParams_configurazioneFiltro);
  654.         }catch(NotFoundException notFound){
  655.             if(throwNotFound){
  656.                 throw new NotFoundException(notFound);
  657.             }
  658.         }
  659.         if(id_configurazioneFiltro==null || id_configurazioneFiltro<=0){
  660.             if(throwNotFound){
  661.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  662.             }
  663.         }
  664.        
  665.         return id_configurazioneFiltro;
  666.     }
  667. }