JDBCGruppoServiceSearchImpl.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.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.commons.search.IdGruppo;
  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.commons.search.dao.jdbc.converter.GruppoFieldConverter;
  49. import org.openspcoop2.core.commons.search.dao.jdbc.fetch.GruppoFetch;
  50. import org.openspcoop2.core.commons.search.Gruppo;

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

  59.     private GruppoFieldConverter _gruppoFieldConverter = null;
  60.     public GruppoFieldConverter getGruppoFieldConverter() {
  61.         if(this._gruppoFieldConverter==null){
  62.             this._gruppoFieldConverter = new GruppoFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  63.         }      
  64.         return this._gruppoFieldConverter;
  65.     }
  66.     @Override
  67.     public ISQLFieldConverter getFieldConverter() {
  68.         return this.getGruppoFieldConverter();
  69.     }
  70.    
  71.     private GruppoFetch gruppoFetch = new GruppoFetch();
  72.     public GruppoFetch getGruppoFetch() {
  73.         return this.gruppoFetch;
  74.     }
  75.     @Override
  76.     public IJDBCFetch getFetch() {
  77.         return getGruppoFetch();
  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.     public JDBCServiceManager getServiceManager(Connection connection, JDBCServiceManagerProperties jdbcProperties, Logger log) throws ServiceException{
  92.         return new JDBCServiceManager(connection, jdbcProperties, log);
  93.     }
  94.    

  95.     @Override
  96.     public IdGruppo convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Gruppo gruppo) throws NotImplementedException, ServiceException, Exception{
  97.    
  98.         IdGruppo idGruppo = new IdGruppo();
  99.         idGruppo.setNome(gruppo.getNome());
  100.         return idGruppo;
  101.        
  102.     }
  103.    
  104.     @Override
  105.     public Gruppo get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdGruppo id, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {
  106.         Long id_gruppo = ( (id!=null && id.getId()!=null && id.getId()>0) ? id.getId() : this.findIdGruppo(jdbcProperties, log, connection, sqlQueryObject, id, true));
  107.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, id_gruppo,idMappingResolutionBehaviour);
  108.        
  109.        
  110.     }
  111.    
  112.     @Override
  113.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdGruppo id) throws MultipleResultException, NotImplementedException, ServiceException,Exception {

  114.         Long id_gruppo = this.findIdGruppo(jdbcProperties, log, connection, sqlQueryObject, id, false);
  115.         return id_gruppo != null && id_gruppo > 0;
  116.        
  117.     }
  118.    
  119.     @Override
  120.     public List<IdGruppo> findAllIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

  121.         List<IdGruppo> list = new ArrayList<IdGruppo>();

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

  128.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  129.        
  130.         for(Long id: ids) {
  131.             Gruppo gruppo = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  132.             IdGruppo idGruppo = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,gruppo);
  133.             list.add(idGruppo);
  134.         }

  135.         return list;
  136.        
  137.     }
  138.    
  139.     @Override
  140.     public List<Gruppo> findAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

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

  148.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  149.        
  150.         for(Long id: ids) {
  151.             list.add(this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour));
  152.         }

  153.         return list;      
  154.        
  155.     }
  156.    
  157.     @Override
  158.     public Gruppo find(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour)
  159.         throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {

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

  182.     @Override
  183.     public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdGruppo id) throws NotFoundException, NotImplementedException, ServiceException,Exception {
  184.        
  185.         Long id_gruppo = this.findIdGruppo(jdbcProperties, log, connection, sqlQueryObject, id, true);
  186.         return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, id_gruppo);
  187.        
  188.     }

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

  221.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, sqlQueryObjectDistinct);
  222.            
  223.         }finally{
  224.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,field);
  225.         }
  226.     }

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

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

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



  363.     // -- ConstructorExpression

  364.     @Override
  365.     public JDBCExpression newExpression(Logger log) throws NotImplementedException, ServiceException {
  366.         try{
  367.             return new JDBCExpression(this.getGruppoFieldConverter());
  368.         }catch(Exception e){
  369.             throw new ServiceException(e);
  370.         }
  371.     }


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

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

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

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

  433.         // Object gruppo
  434.         ISQLQueryObject sqlQueryObjectGet_gruppo = sqlQueryObjectGet.newSQLQueryObject();
  435.         sqlQueryObjectGet_gruppo.setANDLogicOperator(true);
  436.         sqlQueryObjectGet_gruppo.addFromTable(this.getGruppoFieldConverter().toTable(Gruppo.model()));
  437.         sqlQueryObjectGet_gruppo.addSelectField("id");
  438.         sqlQueryObjectGet_gruppo.addSelectField(this.getGruppoFieldConverter().toColumn(Gruppo.model().NOME,true));
  439.         sqlQueryObjectGet_gruppo.addSelectField(this.getGruppoFieldConverter().toColumn(Gruppo.model().SERVICE_BINDING,true));
  440.         sqlQueryObjectGet_gruppo.addWhereCondition("id=?");

  441.         // Get gruppo
  442.         gruppo = (Gruppo) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_gruppo.createSQLQuery(), jdbcProperties.isShowSql(), Gruppo.model(), this.getGruppoFetch(),
  443.             new JDBCObject(tableId,Long.class));



  444.        
  445.         return gruppo;  
  446.    
  447.     }
  448.    
  449.     @Override
  450.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  451.         return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  452.     }
  453.    
  454.     private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  455.    
  456.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  457.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  458.                
  459.         boolean existsGruppo = false;

  460.         sqlQueryObject = sqlQueryObject.newSQLQueryObject();
  461.         sqlQueryObject.setANDLogicOperator(true);

  462.         sqlQueryObject.addFromTable(this.getGruppoFieldConverter().toTable(Gruppo.model()));
  463.         sqlQueryObject.addSelectField(this.getGruppoFieldConverter().toColumn(Gruppo.model().NOME,true));
  464.         sqlQueryObject.addWhereCondition("id=?");


  465.         // Exists gruppo
  466.         existsGruppo = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  467.             new JDBCObject(tableId,Long.class));

  468.        
  469.         return existsGruppo;
  470.    
  471.     }
  472.    
  473.     private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{

  474.         // nop

  475.     }
  476.    
  477.     protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdGruppo id) throws NotFoundException, ServiceException, NotImplementedException, Exception{
  478.         // Identificativi
  479.         java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
  480.         Long longId = this.findIdGruppo(jdbcProperties, log, connection, sqlQueryObject.newSQLQueryObject(), id, true);
  481.         rootTableIdValues.add(longId);
  482.        
  483.         return rootTableIdValues;
  484.     }
  485.    
  486.     protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
  487.    
  488.         GruppoFieldConverter converter = this.getGruppoFieldConverter();
  489.         Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
  490.         UtilsTemplate<IField> utilities = new UtilsTemplate<>();

  491.         // Gruppo.model()
  492.         mapTableToPKColumn.put(converter.toTable(Gruppo.model()),
  493.             utilities.newList(
  494.                 new CustomField("id", Long.class, "id", converter.toTable(Gruppo.model()))
  495.             ));

  496.         return mapTableToPKColumn;      
  497.     }
  498.    
  499.     @Override
  500.     public List<Long> findAllTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression paginatedExpression) throws ServiceException, NotImplementedException, Exception {
  501.        
  502.         List<Long> list = new ArrayList<Long>();

  503.         sqlQueryObject.setSelectDistinct(true);
  504.         sqlQueryObject.setANDLogicOperator(true);
  505.         sqlQueryObject.addSelectField(this.getGruppoFieldConverter().toTable(Gruppo.model())+".id");
  506.         Class<?> objectIdClass = Long.class;
  507.        
  508.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFindAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  509.                                                 this.getGruppoFieldConverter(), Gruppo.model());
  510.        
  511.         joinEngine(paginatedExpression,sqlQueryObject);
  512.        
  513.         List<Object> listObjects = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.findAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  514.                                                                             this.getGruppoFieldConverter(), Gruppo.model(), objectIdClass, listaQuery);
  515.         for(Object object: listObjects) {
  516.             list.add((Long)object);
  517.         }

  518.         return list;
  519.        
  520.     }
  521.    
  522.     @Override
  523.     public long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws ServiceException, NotFoundException, MultipleResultException, NotImplementedException, Exception {
  524.    
  525.         sqlQueryObject.setSelectDistinct(true);
  526.         sqlQueryObject.setANDLogicOperator(true);
  527.         sqlQueryObject.addSelectField(this.getGruppoFieldConverter().toTable(Gruppo.model())+".id");
  528.         Class<?> objectIdClass = Long.class;
  529.        
  530.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFind(jdbcProperties, log, connection, sqlQueryObject, expression,
  531.                                                 this.getGruppoFieldConverter(), Gruppo.model());
  532.        
  533.         joinEngine(expression,sqlQueryObject);

  534.         Object res = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.find(jdbcProperties, log, connection, sqlQueryObject, expression,
  535.                                                         this.getGruppoFieldConverter(), Gruppo.model(), objectIdClass, listaQuery);
  536.         if(res!=null && (((Long) res).longValue()>0) ){
  537.             return ((Long) res).longValue();
  538.         }
  539.         else{
  540.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  541.         }
  542.        
  543.     }

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

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

  549.         InUse inUse = new InUse();
  550.         inUse.setInUse(false);
  551.        
  552.         /*
  553.          * TODO: implement code that checks whether the object identified by the id parameter is used by other objects
  554.         */
  555.        
  556.         // Delete this line when you have implemented the method
  557.         int throwNotImplemented = 1;
  558.         if(throwNotImplemented==1){
  559.                 throw new NotImplementedException("NotImplemented");
  560.         }
  561.         // Delete this line when you have implemented the method

  562.         return inUse;

  563.     }
  564.    
  565.     @Override
  566.     public IdGruppo findId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, boolean throwNotFound)
  567.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  568.        
  569.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  570.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  571.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();
  572.          

  573.         // Object _gruppo
  574.         sqlQueryObjectGet.addFromTable(this.getGruppoFieldConverter().toTable(Gruppo.model()));
  575.         sqlQueryObjectGet.addSelectField(this.getGruppoFieldConverter().toColumn(Gruppo.model().NOME,true));
  576.         sqlQueryObjectGet.setANDLogicOperator(true);
  577.         sqlQueryObjectGet.addWhereCondition("id=?");

  578.         // Recupero _gruppo
  579.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_gruppo = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  580.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(tableId,Long.class)
  581.         };
  582.         List<Class<?>> listaFieldIdReturnType_gruppo = new ArrayList<Class<?>>();
  583.         listaFieldIdReturnType_gruppo.add(String.class);
  584.         org.openspcoop2.core.commons.search.IdGruppo id_gruppo = null;
  585.         List<Object> listaFieldId_gruppo = jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  586.                 listaFieldIdReturnType_gruppo, searchParams_gruppo);
  587.         if(listaFieldId_gruppo==null || listaFieldId_gruppo.size()<=0){
  588.             if(throwNotFound){
  589.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  590.             }
  591.         }
  592.         else{
  593.             // set _gruppo
  594.             id_gruppo = new org.openspcoop2.core.commons.search.IdGruppo();
  595.             id_gruppo.setNome((String) listaFieldId_gruppo.get(0));
  596.         }
  597.        
  598.         return id_gruppo;
  599.        
  600.     }

  601.     @Override
  602.     public Long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdGruppo id, boolean throwNotFound)
  603.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  604.    
  605.         return this.findIdGruppo(jdbcProperties,log,connection,sqlQueryObject,id,throwNotFound);
  606.            
  607.     }
  608.    
  609.     @Override
  610.     public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  611.                                             String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
  612.        
  613.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
  614.                                                                                             sql,returnClassTypes,param);
  615.                                                        
  616.     }
  617.    
  618.     protected Long findIdGruppo(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdGruppo id, boolean throwNotFound) throws NotFoundException, ServiceException, NotImplementedException, Exception {

  619.         if(jdbcProperties==null) {
  620.             throw new ServiceException("Param jdbcProperties is null");
  621.         }
  622.         if(sqlQueryObject==null) {
  623.             throw new ServiceException("Param sqlQueryObject is null");
  624.         }
  625.         if(id==null) {
  626.             throw new ServiceException("Param id is null");
  627.         }
  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 _gruppo
  633.         sqlQueryObjectGet.addFromTable(this.getGruppoFieldConverter().toTable(Gruppo.model()));
  634.         sqlQueryObjectGet.addSelectField("id");
  635.         // Devono essere mappati nella where condition i metodi dell'oggetto id.getXXX
  636.         sqlQueryObjectGet.setANDLogicOperator(true);
  637.         sqlQueryObjectGet.setSelectDistinct(true);
  638.         sqlQueryObjectGet.addWhereCondition(this.getGruppoFieldConverter().toColumn(Gruppo.model().NOME,true)+"=?");
  639.        
  640.         // Recupero _gruppo
  641.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_gruppo = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  642.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(id.getNome(),String.class),
  643.         };
  644.         Long id_gruppo = null;
  645.         try{
  646.             id_gruppo = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  647.                         Long.class, searchParams_gruppo);
  648.         }catch(NotFoundException notFound){
  649.             if(throwNotFound){
  650.                 throw new NotFoundException(notFound);
  651.             }
  652.         }
  653.         if(id_gruppo==null || id_gruppo<=0){
  654.             if(throwNotFound){
  655.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  656.             }
  657.         }
  658.        
  659.         return id_gruppo;
  660.     }
  661. }