JDBCResourceServiceSearchImpl.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.commons.search.dao.jdbc;

  21. import java.sql.Connection;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.commons.search.AccordoServizioParteComune;
  26. import org.openspcoop2.core.commons.search.IdAccordoServizioParteComune;
  27. import org.openspcoop2.core.commons.search.IdResource;
  28. import org.openspcoop2.core.commons.search.Resource;
  29. import org.openspcoop2.core.commons.search.dao.IDBAccordoServizioParteComuneServiceSearch;
  30. import org.openspcoop2.core.commons.search.dao.jdbc.converter.ResourceFieldConverter;
  31. import org.openspcoop2.core.commons.search.dao.jdbc.fetch.ResourceFetch;
  32. import org.openspcoop2.generic_project.beans.CustomField;
  33. import org.openspcoop2.generic_project.beans.FunctionField;
  34. import org.openspcoop2.generic_project.beans.IField;
  35. import org.openspcoop2.generic_project.beans.InUse;
  36. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  37. import org.openspcoop2.generic_project.beans.Union;
  38. import org.openspcoop2.generic_project.beans.UnionExpression;
  39. import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceSearchWithId;
  40. import org.openspcoop2.generic_project.dao.jdbc.JDBCExpression;
  41. import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;
  42. import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;
  43. import org.openspcoop2.generic_project.dao.jdbc.utils.IJDBCFetch;
  44. import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
  45. import org.openspcoop2.generic_project.exception.MultipleResultException;
  46. import org.openspcoop2.generic_project.exception.NotFoundException;
  47. import org.openspcoop2.generic_project.exception.NotImplementedException;
  48. import org.openspcoop2.generic_project.exception.ServiceException;
  49. import org.openspcoop2.generic_project.expression.IExpression;
  50. import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
  51. import org.openspcoop2.generic_project.utils.UtilsTemplate;
  52. import org.openspcoop2.utils.sql.ISQLQueryObject;
  53. import org.slf4j.Logger;

  54. /**    
  55.  * JDBCResourceServiceSearchImpl
  56.  *
  57.  * @author Poli Andrea (poli@link.it)
  58.  * @author $Author$
  59.  * @version $Rev$, $Date$
  60.  */
  61. public class JDBCResourceServiceSearchImpl implements IJDBCServiceSearchWithId<Resource, IdResource, JDBCServiceManager> {

  62.     private ResourceFieldConverter _resourceFieldConverter = null;
  63.     public ResourceFieldConverter getResourceFieldConverter() {
  64.         if(this._resourceFieldConverter==null){
  65.             this._resourceFieldConverter = new ResourceFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  66.         }      
  67.         return this._resourceFieldConverter;
  68.     }
  69.     @Override
  70.     public ISQLFieldConverter getFieldConverter() {
  71.         return this.getResourceFieldConverter();
  72.     }
  73.    
  74.     private ResourceFetch resourceFetch = new ResourceFetch();
  75.     public ResourceFetch getResourceFetch() {
  76.         return this.resourceFetch;
  77.     }
  78.     @Override
  79.     public IJDBCFetch getFetch() {
  80.         return getResourceFetch();
  81.     }
  82.    
  83.    
  84.     private JDBCServiceManager jdbcServiceManager = null;

  85.     @Override
  86.     public void setServiceManager(JDBCServiceManager serviceManager) throws ServiceException{
  87.         this.jdbcServiceManager = serviceManager;
  88.     }
  89.    
  90.     @Override
  91.     public JDBCServiceManager getServiceManager() throws ServiceException{
  92.         return this.jdbcServiceManager;
  93.     }
  94.     public JDBCServiceManager getServiceManager(Connection connection, JDBCServiceManagerProperties jdbcProperties, Logger log) throws ServiceException{
  95.         return new JDBCServiceManager(connection, jdbcProperties, log);
  96.     }
  97.    

  98.     @Override
  99.     public IdResource convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Resource resource) throws NotImplementedException, ServiceException, Exception{
  100.    
  101.         IdResource idResource = new IdResource();
  102.         idResource.setNome(resource.getNome());
  103.         idResource.setHttpMethod(resource.getHttpMethod());
  104.         idResource.setPath(resource.getPath());
  105.         idResource.setIdAccordoServizioParteComune(resource.getIdAccordoServizioParteComune());
  106.         return idResource;
  107.     }
  108.    
  109.     @Override
  110.     public Resource get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {
  111.         Long id_resource = ( (id!=null && id.getId()!=null && id.getId()>0) ? id.getId() : this.findIdResource(jdbcProperties, log, connection, sqlQueryObject, id, true));
  112.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, id_resource,idMappingResolutionBehaviour);
  113.        
  114.        
  115.     }
  116.    
  117.     @Override
  118.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id) throws MultipleResultException, NotImplementedException, ServiceException,Exception {

  119.         Long id_resource = this.findIdResource(jdbcProperties, log, connection, sqlQueryObject, id, false);
  120.         return id_resource != null && id_resource > 0;
  121.        
  122.     }
  123.    
  124.     @Override
  125.     public List<IdResource> findAllIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

  126.         List<IdResource> list = new ArrayList<IdResource>();

  127.         // TODO: implementazione non efficiente.
  128.         // Per ottenere una implementazione efficiente:
  129.         // 1. Usare metodo select di questa classe indirizzando esattamente i field necessari a create l'ID logico
  130.         // 2. Usare metodo getResourceFetch() sul risultato della select per ottenere un oggetto Resource
  131.         //    La fetch con la map inserirĂ  nell'oggetto solo i valori estratti
  132.         // 3. Usare metodo convertToId per ottenere l'id

  133.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  134.        
  135.         for(Long id: ids) {
  136.             Resource resource = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  137.             IdResource idResource = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,resource);
  138.             list.add(idResource);
  139.         }

  140.         return list;
  141.        
  142.     }
  143.    
  144.     @Override
  145.     public List<Resource> findAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

  146.         List<Resource> list = new ArrayList<Resource>();
  147.        
  148.         // TODO: implementazione non efficiente.
  149.         // Per ottenere una implementazione efficiente:
  150.         // 1. Usare metodo select di questa classe indirizzando esattamente i field necessari
  151.         // 2. Usare metodo getResourceFetch() sul risultato della select per ottenere un oggetto Resource
  152.         //    La fetch con la map inserirĂ  nell'oggetto solo i valori estratti

  153.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  154.        
  155.         for(Long id: ids) {
  156.             list.add(this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour));
  157.         }

  158.         return list;      
  159.        
  160.     }
  161.    
  162.     @Override
  163.     public Resource find(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour)
  164.         throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {

  165.         long id = this.findTableId(jdbcProperties, log, connection, sqlQueryObject, expression);
  166.         if(id>0){
  167.             return this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  168.         }else{
  169.             throw new NotFoundException("Entry with id["+id+"] not found");
  170.         }
  171.        
  172.     }
  173.    
  174.     @Override
  175.     public NonNegativeNumber count(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws NotImplementedException, ServiceException,Exception {
  176.        
  177.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareCount(jdbcProperties, log, connection, sqlQueryObject, expression,
  178.                                                 this.getResourceFieldConverter(), Resource.model());
  179.        
  180.         sqlQueryObject.addSelectCountField(this.getResourceFieldConverter().toTable(Resource.model())+".id","tot",true);
  181.        
  182.         joinEngine(expression,sqlQueryObject);
  183.        
  184.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.count(jdbcProperties, log, connection, sqlQueryObject, expression,
  185.                                                                             this.getResourceFieldConverter(), Resource.model(),listaQuery);
  186.     }

  187.     @Override
  188.     public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id) throws NotFoundException, NotImplementedException, ServiceException,Exception {
  189.        
  190.         Long id_resource = this.findIdResource(jdbcProperties, log, connection, sqlQueryObject, id, true);
  191.         return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, id_resource);
  192.        
  193.     }

  194.     @Override
  195.     public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  196.                                                     JDBCPaginatedExpression paginatedExpression, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  197.         return this.select(jdbcProperties, log, connection, sqlQueryObject,
  198.                                 paginatedExpression, false, field);
  199.     }
  200.    
  201.     @Override
  202.     public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  203.                                                     JDBCPaginatedExpression paginatedExpression, boolean distinct, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  204.         List<Map<String,Object>> map =
  205.             this.select(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, distinct, new IField[]{field});
  206.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectSingleObject(map);
  207.     }
  208.    
  209.     @Override
  210.     public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  211.                                                     JDBCPaginatedExpression paginatedExpression, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  212.         return this.select(jdbcProperties, log, connection, sqlQueryObject,
  213.                                 paginatedExpression, false, field);
  214.     }
  215.    
  216.     @Override
  217.     public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  218.                                                     JDBCPaginatedExpression paginatedExpression, boolean distinct, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  219.        
  220.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,field);
  221.         try{
  222.        
  223.             ISQLQueryObject sqlQueryObjectDistinct =
  224.                         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(distinct,sqlQueryObject, paginatedExpression, log,
  225.                                                 this.getResourceFieldConverter(), field);

  226.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, sqlQueryObjectDistinct);
  227.            
  228.         }finally{
  229.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,field);
  230.         }
  231.     }

  232.     @Override
  233.     public Object aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  234.                                                     JDBCExpression expression, FunctionField functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  235.         Map<String,Object> map =
  236.             this.aggregate(jdbcProperties, log, connection, sqlQueryObject, expression, new FunctionField[]{functionField});
  237.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectAggregateObject(map,functionField);
  238.     }
  239.    
  240.     @Override
  241.     public Map<String,Object> aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  242.                                                     JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {                                                  
  243.        
  244.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
  245.         try{
  246.             List<Map<String,Object>> list = selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
  247.             return list.get(0);
  248.         }finally{
  249.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
  250.         }
  251.     }

  252.     @Override
  253.     public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  254.                                                     JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  255.        
  256.         if(expression.getGroupByFields().isEmpty()){
  257.             throw new ServiceException("GroupBy conditions not found in expression");
  258.         }
  259.        
  260.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
  261.         try{
  262.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
  263.         }finally{
  264.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
  265.         }
  266.     }
  267.    

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



  368.     // -- ConstructorExpression

  369.     @Override
  370.     public JDBCExpression newExpression(Logger log) throws NotImplementedException, ServiceException {
  371.         try{
  372.             return new JDBCExpression(this.getResourceFieldConverter());
  373.         }catch(Exception e){
  374.             throw new ServiceException(e);
  375.         }
  376.     }


  377.     @Override
  378.     public JDBCPaginatedExpression newPaginatedExpression(Logger log) throws NotImplementedException, ServiceException {
  379.         try{
  380.             return new JDBCPaginatedExpression(this.getResourceFieldConverter());
  381.         }catch(Exception e){
  382.             throw new ServiceException(e);
  383.         }
  384.     }
  385.    
  386.     @Override
  387.     public JDBCExpression toExpression(JDBCPaginatedExpression paginatedExpression, Logger log) throws NotImplementedException, ServiceException {
  388.         try{
  389.             return new JDBCExpression(paginatedExpression);
  390.         }catch(Exception e){
  391.             throw new ServiceException(e);
  392.         }
  393.     }

  394.     @Override
  395.     public JDBCPaginatedExpression toPaginatedExpression(JDBCExpression expression, Logger log) throws NotImplementedException, ServiceException {
  396.         try{
  397.             return new JDBCPaginatedExpression(expression);
  398.         }catch(Exception e){
  399.             throw new ServiceException(e);
  400.         }
  401.     }
  402.    
  403.    
  404.    
  405.     // -- DB

  406.     @Override
  407.     public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id, Resource obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  408.         _mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
  409.                 this.get(jdbcProperties,log,connection,sqlQueryObject,id,null));
  410.     }
  411.    
  412.     @Override
  413.     public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, Resource obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  414.         _mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
  415.                 this.get(jdbcProperties,log,connection,sqlQueryObject,tableId,null));
  416.     }
  417.     private void _mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Resource obj, Resource imgSaved) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  418.         if(imgSaved==null){
  419.             return;
  420.         }
  421.         obj.setId(imgSaved.getId());
  422.         if(obj.getIdAccordoServizioParteComune()!=null &&
  423.                 imgSaved.getIdAccordoServizioParteComune()!=null){
  424.             obj.getIdAccordoServizioParteComune().setId(imgSaved.getIdAccordoServizioParteComune().getId());
  425.             if(obj.getIdAccordoServizioParteComune().getIdSoggetto()!=null &&
  426.                     imgSaved.getIdAccordoServizioParteComune().getIdSoggetto()!=null){
  427.                 obj.getIdAccordoServizioParteComune().getIdSoggetto().setId(imgSaved.getIdAccordoServizioParteComune().getIdSoggetto().getId());
  428.             }
  429.         }

  430.     }
  431.    
  432.     @Override
  433.     public Resource get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  434.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId), idMappingResolutionBehaviour);
  435.     }
  436.    
  437.     private Resource getEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  438.    
  439.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  440.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  441.        
  442.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();
  443.                
  444.         Resource resource = new Resource();
  445.        

  446.         // Object resource
  447.         ISQLQueryObject sqlQueryObjectGet_resource = sqlQueryObjectGet.newSQLQueryObject();
  448.         sqlQueryObjectGet_resource.setANDLogicOperator(true);
  449.         sqlQueryObjectGet_resource.addFromTable(this.getResourceFieldConverter().toTable(Resource.model()));
  450.         sqlQueryObjectGet_resource.addSelectField("id");
  451.         sqlQueryObjectGet_resource.addSelectField(this.getResourceFieldConverter().toColumn(Resource.model().NOME,true));
  452.         sqlQueryObjectGet_resource.addSelectField(this.getResourceFieldConverter().toColumn(Resource.model().HTTP_METHOD,true));
  453.         sqlQueryObjectGet_resource.addSelectField(this.getResourceFieldConverter().toColumn(Resource.model().PATH,true));
  454.         sqlQueryObjectGet_resource.addWhereCondition("id=?");

  455.         // Get resource
  456.         resource = (Resource) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_resource.createSQLQuery(), jdbcProperties.isShowSql(), Resource.model(), this.getResourceFetch(),
  457.             new JDBCObject(tableId,Long.class));


  458.         // Recupero idAccordo
  459.         ISQLQueryObject sqlQueryObjectGet_accordoServizioParteComune = sqlQueryObjectGet.newSQLQueryObject();
  460.         sqlQueryObjectGet_accordoServizioParteComune.addFromTable(this.getResourceFieldConverter().toTable(Resource.model()));
  461.         sqlQueryObjectGet_accordoServizioParteComune.addSelectField("id_accordo");
  462.         sqlQueryObjectGet_accordoServizioParteComune.setANDLogicOperator(true);
  463.         sqlQueryObjectGet_accordoServizioParteComune.addWhereCondition("id=?");
  464.        
  465.         // Recupero _accordoServizioParteComune_soggetto
  466.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_accordoServizioParteComune = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  467.                 new JDBCObject(resource.getId(), Long.class)
  468.         };
  469.         Long id_accordoServizioParteComune =
  470.             (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_accordoServizioParteComune.createSQLQuery(), jdbcProperties.isShowSql(),
  471.             Long.class, searchParams_accordoServizioParteComune);
  472.        
  473.         IDBAccordoServizioParteComuneServiceSearch search = ((IDBAccordoServizioParteComuneServiceSearch)this.getServiceManager(connection, jdbcProperties, log).getAccordoServizioParteComuneServiceSearch());
  474.         AccordoServizioParteComune as = search.get(id_accordoServizioParteComune);
  475.         IdAccordoServizioParteComune idAccordo = search.convertToId(as);
  476.         resource.setIdAccordoServizioParteComune(idAccordo);

  477.        
  478.         return resource;  
  479.    
  480.     }
  481.    
  482.     @Override
  483.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  484.         return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  485.     }
  486.    
  487.     private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  488.    
  489.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  490.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  491.                
  492.         boolean existsResource = false;

  493.         sqlQueryObject = sqlQueryObject.newSQLQueryObject();
  494.         sqlQueryObject.setANDLogicOperator(true);

  495.         sqlQueryObject.addFromTable(this.getResourceFieldConverter().toTable(Resource.model()));
  496.         sqlQueryObject.addSelectField(this.getResourceFieldConverter().toColumn(Resource.model().NOME,true));
  497.         sqlQueryObject.addWhereCondition("id=?");


  498.         // Exists resource
  499.         existsResource = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  500.             new JDBCObject(tableId,Long.class));

  501.        
  502.         return existsResource;
  503.    
  504.     }
  505.    
  506.     private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{
  507.    
  508.         if(expression.inUseModel(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE,false)){
  509.             String tableName1 = this.getResourceFieldConverter().toAliasTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE);
  510.             String tableName2 = this.getResourceFieldConverter().toAliasTable(Resource.model());
  511.             sqlQueryObject.addWhereCondition(tableName1+".id="+tableName2+".id_accordo");
  512.         }
  513.         if(expression.inUseModel(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE.ID_SOGGETTO,false)){
  514.             String tableName1 = this.getResourceFieldConverter().toAliasTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE);
  515.             String tableName2 = this.getResourceFieldConverter().toAliasTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE.ID_SOGGETTO);
  516.             sqlQueryObject.addWhereCondition(tableName1+".id_referente="+tableName2+".id");
  517.         }
  518.        
  519.         if(expression.inUseModel(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE.ID_SOGGETTO,false)){
  520.             if(expression.inUseModel(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE,false)==false){
  521.                 sqlQueryObject.addFromTable(this.getResourceFieldConverter().toTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE));
  522.             }
  523.         }
  524.        
  525.     }
  526.    
  527.     protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id) throws NotFoundException, ServiceException, NotImplementedException, Exception{
  528.         // Identificativi
  529.         java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
  530.         Long longId = this.findIdResource(jdbcProperties, log, connection, sqlQueryObject.newSQLQueryObject(), id, true);
  531.         rootTableIdValues.add(longId);
  532.         return rootTableIdValues;
  533.     }
  534.    
  535.     protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
  536.    
  537.         ResourceFieldConverter converter = this.getResourceFieldConverter();
  538.         Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
  539.         UtilsTemplate<IField> utilities = new UtilsTemplate<>();

  540.         // Resource.model()
  541.         mapTableToPKColumn.put(converter.toTable(Resource.model()),
  542.             utilities.newList(
  543.                 new CustomField("id", Long.class, "id", converter.toTable(Resource.model()))
  544.             ));

  545.         // Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE
  546.         mapTableToPKColumn.put(converter.toTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE),
  547.             utilities.newList(
  548.                 new CustomField("id", Long.class, "id", converter.toTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE))
  549.             ));

  550.         // Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE.ID_SOGGETTO
  551.         mapTableToPKColumn.put(converter.toTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE.ID_SOGGETTO),
  552.             utilities.newList(
  553.                 new CustomField("id", Long.class, "id", converter.toTable(Resource.model().ID_ACCORDO_SERVIZIO_PARTE_COMUNE.ID_SOGGETTO))
  554.             ));
  555.        
  556.         return mapTableToPKColumn;      
  557.     }
  558.    
  559.     @Override
  560.     public List<Long> findAllTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression paginatedExpression) throws ServiceException, NotImplementedException, Exception {
  561.        
  562.         List<Long> list = new ArrayList<Long>();

  563.         sqlQueryObject.setSelectDistinct(true);
  564.         sqlQueryObject.setANDLogicOperator(true);
  565.         sqlQueryObject.addSelectField(this.getResourceFieldConverter().toTable(Resource.model())+".id");
  566.         Class<?> objectIdClass = Long.class;
  567.        
  568.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFindAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  569.                                                 this.getResourceFieldConverter(), Resource.model());
  570.        
  571.         joinEngine(paginatedExpression,sqlQueryObject);
  572.        
  573.         List<Object> listObjects = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.findAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  574.                                                                             this.getResourceFieldConverter(), Resource.model(), objectIdClass, listaQuery);
  575.         for(Object object: listObjects) {
  576.             list.add((Long)object);
  577.         }

  578.         return list;
  579.        
  580.     }
  581.    
  582.     @Override
  583.     public long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws ServiceException, NotFoundException, MultipleResultException, NotImplementedException, Exception {
  584.    
  585.         sqlQueryObject.setSelectDistinct(true);
  586.         sqlQueryObject.setANDLogicOperator(true);
  587.         sqlQueryObject.addSelectField(this.getResourceFieldConverter().toTable(Resource.model())+".id");
  588.         Class<?> objectIdClass = Long.class;
  589.        
  590.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFind(jdbcProperties, log, connection, sqlQueryObject, expression,
  591.                                                 this.getResourceFieldConverter(), Resource.model());
  592.        
  593.         joinEngine(expression,sqlQueryObject);

  594.         Object res = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.find(jdbcProperties, log, connection, sqlQueryObject, expression,
  595.                                                         this.getResourceFieldConverter(), Resource.model(), objectIdClass, listaQuery);
  596.         if(res!=null && (((Long) res).longValue()>0) ){
  597.             return ((Long) res).longValue();
  598.         }
  599.         else{
  600.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  601.         }
  602.        
  603.     }

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

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

  609.         InUse inUse = new InUse();
  610.         inUse.setInUse(false);
  611.        
  612.         /*
  613.          * TODO: implement code that checks whether the object identified by the id parameter is used by other objects
  614.         */
  615.        
  616.         // Delete this line when you have implemented the method
  617.         int throwNotImplemented = 1;
  618.         if(throwNotImplemented==1){
  619.                 throw new NotImplementedException("NotImplemented");
  620.         }
  621.         // Delete this line when you have implemented the method

  622.         return inUse;

  623.     }
  624.    
  625.     @Override
  626.     public IdResource findId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, boolean throwNotFound)
  627.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  628.        
  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.         // Object _resource
  633.         //TODO Implementare la ricerca dell'id
  634.         sqlQueryObjectGet.addFromTable(this.getResourceFieldConverter().toTable(Resource.model()));
  635.         sqlQueryObjectGet.addSelectField(this.getResourceFieldConverter().toColumn(Resource.model().NOME,true));
  636.         sqlQueryObjectGet.addSelectField("id_accordo");
  637.         sqlQueryObjectGet.setANDLogicOperator(true);
  638.         sqlQueryObjectGet.addWhereCondition("id=?");

  639.         // Recupero _resource
  640.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_resource = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  641.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(tableId,Long.class)
  642.         };
  643.         List<Class<?>> listaFieldIdReturnType_resource = new ArrayList<Class<?>>();
  644.         listaFieldIdReturnType_resource.add(String.class);
  645.         listaFieldIdReturnType_resource.add(Long.class);
  646.         org.openspcoop2.core.commons.search.IdResource id_resource = null;
  647.         List<Object> listaFieldId_resource = jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  648.                 listaFieldIdReturnType_resource, searchParams_resource);
  649.         if(listaFieldId_resource==null || listaFieldId_resource.size()<=0){
  650.             if(throwNotFound){
  651.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  652.             }
  653.         }
  654.         else{
  655.             // set _resource
  656.             id_resource = new org.openspcoop2.core.commons.search.IdResource();
  657.             id_resource.setNome((String)listaFieldId_resource.get(0));
  658.             Long idAccordoFK = (Long) listaFieldId_resource.get(1);
  659.             id_resource.
  660.                 setIdAccordoServizioParteComune(((IDBAccordoServizioParteComuneServiceSearch)this.getServiceManager(connection, jdbcProperties, log).
  661.                         getAccordoServizioParteComuneServiceSearch()).findId(idAccordoFK, true));
  662.         }
  663.        
  664.         return id_resource;
  665.        
  666.     }

  667.     @Override
  668.     public Long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id, boolean throwNotFound)
  669.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  670.    
  671.         return this.findIdResource(jdbcProperties,log,connection,sqlQueryObject,id,throwNotFound);
  672.            
  673.     }
  674.    
  675.     @Override
  676.     public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  677.                                             String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
  678.        
  679.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
  680.                                                                                             sql,returnClassTypes,param);
  681.                                                        
  682.     }
  683.    
  684.     protected Long findIdResource(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdResource id, boolean throwNotFound) throws NotFoundException, ServiceException, NotImplementedException, Exception {

  685.         if(jdbcProperties==null) {
  686.             throw new ServiceException("Param jdbcProperties is null");
  687.         }
  688.         if(sqlQueryObject==null) {
  689.             throw new ServiceException("Param sqlQueryObject is null");
  690.         }
  691.         if(id==null) {
  692.             throw new ServiceException("Param id is null");
  693.         }
  694.        
  695.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  696.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  697.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

  698.         if(id.getIdAccordoServizioParteComune()==null){
  699.             throw new ServiceException("IdAccordoServizioParteComune non fornito");
  700.         }
  701.         if(id.getIdAccordoServizioParteComune().getIdSoggetto()==null){
  702.             throw new ServiceException("IdAccordoServizioParteComune.getIdSoggetto non fornito");
  703.         }
  704.        
  705.         // Recupero idAccordo
  706.         AccordoServizioParteComune as = this.getServiceManager(connection, jdbcProperties, log).getAccordoServizioParteComuneServiceSearch().get(id.getIdAccordoServizioParteComune());
  707.        

  708.         // Object _resource
  709.         sqlQueryObjectGet.addFromTable(this.getResourceFieldConverter().toTable(Resource.model()));
  710.         sqlQueryObjectGet.addSelectField("id");
  711.         // Devono essere mappati nella where condition i metodi dell'oggetto id.getXXX
  712.         sqlQueryObjectGet.setANDLogicOperator(true);
  713.         sqlQueryObjectGet.setSelectDistinct(true);
  714.         sqlQueryObjectGet.addWhereCondition(this.getResourceFieldConverter().toColumn(Resource.model().NOME,true)+"=?");
  715.         sqlQueryObjectGet.addWhereCondition("id_accordo=?");

  716.         // Recupero _resource
  717.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_resource = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  718.                 new JDBCObject(id.getNome(), String.class),
  719.                 new JDBCObject(as.getId(), Long.class)
  720.         };
  721.         Long id_resource = null;
  722.         try{
  723.             id_resource = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  724.                         Long.class, searchParams_resource);
  725.         }catch(NotFoundException notFound){
  726.             if(throwNotFound){
  727.                 throw new NotFoundException(notFound);
  728.             }
  729.         }
  730.         if(id_resource==null || id_resource<=0){
  731.             if(throwNotFound){
  732.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  733.             }
  734.         }
  735.        
  736.         return id_resource;
  737.     }
  738. }