JDBCSoggettoServiceSearchImpl.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.IdSoggetto;
  26. import org.openspcoop2.core.commons.search.Soggetto;
  27. import org.openspcoop2.core.commons.search.SoggettoRuolo;
  28. import org.openspcoop2.core.commons.search.dao.jdbc.converter.SoggettoFieldConverter;
  29. import org.openspcoop2.core.commons.search.dao.jdbc.fetch.SoggettoFetch;
  30. import org.openspcoop2.generic_project.beans.CustomField;
  31. import org.openspcoop2.generic_project.beans.FunctionField;
  32. import org.openspcoop2.generic_project.beans.IField;
  33. import org.openspcoop2.generic_project.beans.InUse;
  34. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  35. import org.openspcoop2.generic_project.beans.Union;
  36. import org.openspcoop2.generic_project.beans.UnionExpression;
  37. import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceSearchWithId;
  38. import org.openspcoop2.generic_project.dao.jdbc.JDBCExpression;
  39. import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;
  40. import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;
  41. import org.openspcoop2.generic_project.dao.jdbc.utils.IJDBCFetch;
  42. import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
  43. import org.openspcoop2.generic_project.exception.MultipleResultException;
  44. import org.openspcoop2.generic_project.exception.NotFoundException;
  45. import org.openspcoop2.generic_project.exception.NotImplementedException;
  46. import org.openspcoop2.generic_project.exception.ServiceException;
  47. import org.openspcoop2.generic_project.expression.IExpression;
  48. import org.openspcoop2.generic_project.expression.impl.sql.ISQLFieldConverter;
  49. import org.openspcoop2.generic_project.utils.UtilsTemplate;
  50. import org.openspcoop2.utils.sql.ISQLQueryObject;
  51. import org.slf4j.Logger;

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

  60.     private SoggettoFieldConverter _soggettoFieldConverter = null;
  61.     public SoggettoFieldConverter getSoggettoFieldConverter() {
  62.         if(this._soggettoFieldConverter==null){
  63.             this._soggettoFieldConverter = new SoggettoFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  64.         }      
  65.         return this._soggettoFieldConverter;
  66.     }
  67.     @Override
  68.     public ISQLFieldConverter getFieldConverter() {
  69.         return this.getSoggettoFieldConverter();
  70.     }
  71.    
  72.     private SoggettoFetch soggettoFetch = new SoggettoFetch();
  73.     public SoggettoFetch getSoggettoFetch() {
  74.         return this.soggettoFetch;
  75.     }
  76.     @Override
  77.     public IJDBCFetch getFetch() {
  78.         return getSoggettoFetch();
  79.     }
  80.    
  81.    
  82.     private JDBCServiceManager jdbcServiceManager = null;

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

  96.     @Override
  97.     public IdSoggetto convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Soggetto soggetto) throws NotImplementedException, ServiceException, Exception{
  98.    
  99.         IdSoggetto idSoggetto = new IdSoggetto();
  100.         idSoggetto.setNome(soggetto.getNomeSoggetto());
  101.         idSoggetto.setTipo(soggetto.getTipoSoggetto());
  102.         return idSoggetto;

  103.     }
  104.    
  105.     @Override
  106.     public Soggetto get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdSoggetto id, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {
  107.         Long id_soggetto = ( (id!=null && id.getId()!=null && id.getId()>0) ? id.getId() : this.findIdSoggetto(jdbcProperties, log, connection, sqlQueryObject, id, true));
  108.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, id_soggetto,idMappingResolutionBehaviour);
  109.        
  110.        
  111.     }
  112.    
  113.     @Override
  114.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdSoggetto id) throws MultipleResultException, NotImplementedException, ServiceException,Exception {

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

  122.         List<IdSoggetto> list = new ArrayList<IdSoggetto>();

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

  129.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  130.        
  131.         for(Long id: ids) {
  132.             Soggetto soggetto = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  133.             IdSoggetto idSoggetto = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,soggetto);
  134.             list.add(idSoggetto);
  135.         }

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

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

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

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

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

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

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

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

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

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

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



  364.     // -- ConstructorExpression

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


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

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

  402.     @Override
  403.     public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdSoggetto id, Soggetto obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  404.         _mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
  405.                 this.get(jdbcProperties,log,connection,sqlQueryObject,id,null));
  406.     }
  407.    
  408.     @Override
  409.     public void mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, Soggetto obj) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  410.         _mappingTableIds(jdbcProperties,log,connection,sqlQueryObject,obj,
  411.                 this.get(jdbcProperties,log,connection,sqlQueryObject,tableId,null));
  412.     }
  413.     private void _mappingTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Soggetto obj, Soggetto imgSaved) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  414.         if(imgSaved==null){
  415.             return;
  416.         }
  417.         obj.setId(imgSaved.getId());
  418.         if(obj.getSoggettoRuoloList()!=null){
  419.             List<org.openspcoop2.core.commons.search.SoggettoRuolo> listObj_ = obj.getSoggettoRuoloList();
  420.             for(org.openspcoop2.core.commons.search.SoggettoRuolo itemObj_ : listObj_){
  421.                 org.openspcoop2.core.commons.search.SoggettoRuolo itemAlreadySaved_ = null;
  422.                 if(imgSaved.getSoggettoRuoloList()!=null){
  423.                     List<org.openspcoop2.core.commons.search.SoggettoRuolo> listImgSaved_ = imgSaved.getSoggettoRuoloList();
  424.                     for(org.openspcoop2.core.commons.search.SoggettoRuolo itemImgSaved_ : listImgSaved_){
  425.                         boolean objEqualsToImgSaved_ = false;
  426.                         objEqualsToImgSaved_ = org.openspcoop2.generic_project.utils.Utilities.equals(itemObj_.getIdRuolo().getNome(),itemImgSaved_.getIdRuolo().getNome());
  427.                         if(objEqualsToImgSaved_){
  428.                             itemAlreadySaved_=itemImgSaved_;
  429.                             break;
  430.                         }
  431.                     }
  432.                 }
  433.                 if(itemAlreadySaved_!=null){
  434.                     itemObj_.setId(itemAlreadySaved_.getId());
  435.                     if(itemObj_.getIdRuolo()!=null &&
  436.                             itemAlreadySaved_.getIdRuolo()!=null){
  437.                         itemObj_.getIdRuolo().setId(itemAlreadySaved_.getIdRuolo().getId());
  438.                     }
  439.                     if(itemObj_.getIdSoggetto()!=null &&
  440.                             itemAlreadySaved_.getIdSoggetto()!=null){
  441.                         itemObj_.getIdSoggetto().setId(itemAlreadySaved_.getIdSoggetto().getId());
  442.                     }
  443.                 }
  444.             }
  445.         }

  446.     }
  447.    
  448.     @Override
  449.     public Soggetto get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  450.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId), idMappingResolutionBehaviour);
  451.     }
  452.    
  453.     private Soggetto getEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  454.    
  455.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  456.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  457.        
  458.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();
  459.                
  460.         Soggetto soggetto = new Soggetto();
  461.        

  462.         // Object soggetto
  463.         ISQLQueryObject sqlQueryObjectGet_soggetto = sqlQueryObjectGet.newSQLQueryObject();
  464.         sqlQueryObjectGet_soggetto.setANDLogicOperator(true);
  465.         sqlQueryObjectGet_soggetto.addFromTable(this.getSoggettoFieldConverter().toTable(Soggetto.model()));
  466.         sqlQueryObjectGet_soggetto.addSelectField("id");
  467.         sqlQueryObjectGet_soggetto.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().NOME_SOGGETTO,true));
  468.         sqlQueryObjectGet_soggetto.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().TIPO_SOGGETTO,true));
  469.         sqlQueryObjectGet_soggetto.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().SERVER,true));
  470.         sqlQueryObjectGet_soggetto.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().IDENTIFICATIVO_PORTA,true));
  471.         sqlQueryObjectGet_soggetto.addWhereCondition("id=?");

  472.         // Get soggetto
  473.         soggetto = (Soggetto) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_soggetto.createSQLQuery(), jdbcProperties.isShowSql(), Soggetto.model(), this.getSoggettoFetch(),
  474.             new JDBCObject(tableId,Long.class));



  475.         // Object soggetto_soggettoRuolo
  476.         ISQLQueryObject sqlQueryObjectGet_soggetto_soggettoRuolo = sqlQueryObjectGet.newSQLQueryObject();
  477.         sqlQueryObjectGet_soggetto_soggettoRuolo.setANDLogicOperator(true);
  478.         sqlQueryObjectGet_soggetto_soggettoRuolo.addFromTable(this.getSoggettoFieldConverter().toTable(Soggetto.model().SOGGETTO_RUOLO));
  479.         sqlQueryObjectGet_soggetto_soggettoRuolo.addSelectField("id");
  480.         sqlQueryObjectGet_soggetto_soggettoRuolo.addWhereCondition("id_soggetto=?");

  481.         // Get soggetto_soggettoRuolo
  482.         java.util.List<Object> soggetto_soggettoRuolo_list = (java.util.List<Object>) jdbcUtilities.executeQuery(sqlQueryObjectGet_soggetto_soggettoRuolo.createSQLQuery(), jdbcProperties.isShowSql(), Soggetto.model().SOGGETTO_RUOLO, this.getSoggettoFetch(),
  483.             new JDBCObject(soggetto.getId(),Long.class));

  484.         if(soggetto_soggettoRuolo_list != null) {
  485.             for (Object soggetto_soggettoRuolo_object: soggetto_soggettoRuolo_list) {
  486.                 SoggettoRuolo soggetto_soggettoRuolo = (SoggettoRuolo) soggetto_soggettoRuolo_object;


  487.                 if(idMappingResolutionBehaviour==null ||
  488.                     (org.openspcoop2.generic_project.beans.IDMappingBehaviour.ENABLED.equals(idMappingResolutionBehaviour) || org.openspcoop2.generic_project.beans.IDMappingBehaviour.USE_TABLE_ID.equals(idMappingResolutionBehaviour))
  489.                 ){
  490.                     // Object _soggetto_soggettoRuolo_ruolo (recupero id)
  491.                     ISQLQueryObject sqlQueryObjectGet_soggetto_soggettoRuolo_ruolo_readFkId = sqlQueryObjectGet.newSQLQueryObject();
  492.                     sqlQueryObjectGet_soggetto_soggettoRuolo_ruolo_readFkId.addFromTable(this.getSoggettoFieldConverter().toTable(org.openspcoop2.core.commons.search.Soggetto.model().SOGGETTO_RUOLO));
  493.                     sqlQueryObjectGet_soggetto_soggettoRuolo_ruolo_readFkId.addSelectField("id_ruolo");
  494.                     sqlQueryObjectGet_soggetto_soggettoRuolo_ruolo_readFkId.addWhereCondition("id=?");
  495.                     sqlQueryObjectGet_soggetto_soggettoRuolo_ruolo_readFkId.setANDLogicOperator(true);
  496.                     Long idFK_soggetto_soggettoRuolo_ruolo = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_soggetto_soggettoRuolo_ruolo_readFkId.createSQLQuery(), jdbcProperties.isShowSql(),Long.class,
  497.                             new JDBCObject(soggetto_soggettoRuolo.getId(),Long.class));
  498.                    
  499.                     org.openspcoop2.core.commons.search.IdRuolo id_soggetto_soggettoRuolo_ruolo = null;
  500.                     if(idMappingResolutionBehaviour==null || org.openspcoop2.generic_project.beans.IDMappingBehaviour.ENABLED.equals(idMappingResolutionBehaviour)){
  501.                         id_soggetto_soggettoRuolo_ruolo = ((JDBCRuoloServiceSearch)(this.getServiceManager(connection, jdbcProperties, log).getRuoloServiceSearch())).findId(idFK_soggetto_soggettoRuolo_ruolo, false);
  502.                     }else{
  503.                         id_soggetto_soggettoRuolo_ruolo = new org.openspcoop2.core.commons.search.IdRuolo();
  504.                     }
  505.                     id_soggetto_soggettoRuolo_ruolo.setId(idFK_soggetto_soggettoRuolo_ruolo);
  506.                     soggetto_soggettoRuolo.setIdRuolo(id_soggetto_soggettoRuolo_ruolo);
  507.                 }

  508. //              if(idMappingResolutionBehaviour==null ||
  509. //                  (org.openspcoop2.generic_project.beans.IDMappingBehaviour.ENABLED.equals(idMappingResolutionBehaviour) || org.openspcoop2.generic_project.beans.IDMappingBehaviour.USE_TABLE_ID.equals(idMappingResolutionBehaviour))
  510. //              ){
  511. //                  // Object _soggetto_soggettoRuolo_soggetto (recupero id)
  512. //                  ISQLQueryObject sqlQueryObjectGet_soggetto_soggettoRuolo_soggetto_readFkId = sqlQueryObjectGet.newSQLQueryObject();
  513. //                  sqlQueryObjectGet_soggetto_soggettoRuolo_soggetto_readFkId.addFromTable(this.getSoggettoFieldConverter().toTable(org.openspcoop2.core.commons.search.Soggetto.model().SOGGETTO_RUOLO));
  514. //                  sqlQueryObjectGet_soggetto_soggettoRuolo_soggetto_readFkId.addSelectField("id_soggetto");
  515. //                  sqlQueryObjectGet_soggetto_soggettoRuolo_soggetto_readFkId.addWhereCondition("id=?");
  516. //                  sqlQueryObjectGet_soggetto_soggettoRuolo_soggetto_readFkId.setANDLogicOperator(true);
  517. //                  Long idFK_soggetto_soggettoRuolo_soggetto = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_soggetto_soggettoRuolo_soggetto_readFkId.createSQLQuery(), jdbcProperties.isShowSql(),Long.class,
  518. //                          new JDBCObject(soggetto_soggettoRuolo.getId(),Long.class));
  519. //                  
  520. //                  org.openspcoop2.core.commons.search.IdSoggetto id_soggetto_soggettoRuolo_soggetto = null;
  521. //                  if(idMappingResolutionBehaviour==null || org.openspcoop2.generic_project.beans.IDMappingBehaviour.ENABLED.equals(idMappingResolutionBehaviour)){
  522. //                      id_soggetto_soggettoRuolo_soggetto = ((JDBCSoggettoServiceSearch)(this.getServiceManager(connection, jdbcProperties, log).getSoggettoServiceSearch())).findId(idFK_soggetto_soggettoRuolo_soggetto, false);
  523. //                  }else{
  524. //                      id_soggetto_soggettoRuolo_soggetto = new org.openspcoop2.core.commons.search.IdSoggetto();
  525. //                  }
  526. //                  id_soggetto_soggettoRuolo_soggetto.setId(idFK_soggetto_soggettoRuolo_soggetto);
  527. //                  soggetto_soggettoRuolo.setIdSoggetto(id_soggetto_soggettoRuolo_soggetto);
  528. //              }
  529.                 IdSoggetto idSoggetto = new IdSoggetto();
  530.                 idSoggetto.setTipo(soggetto.getTipoSoggetto());
  531.                 idSoggetto.setNome(soggetto.getNomeSoggetto());
  532.                 soggetto_soggettoRuolo.setIdSoggetto(idSoggetto);

  533.                 soggetto.addSoggettoRuolo(soggetto_soggettoRuolo);
  534.             }
  535.         }
  536.                
  537.        
  538.         return soggetto;  
  539.    
  540.     }
  541.    
  542.     @Override
  543.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  544.         return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  545.     }
  546.    
  547.     private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  548.    
  549.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  550.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  551.                
  552.         boolean existsSoggetto = false;

  553.         sqlQueryObject = sqlQueryObject.newSQLQueryObject();
  554.         sqlQueryObject.setANDLogicOperator(true);

  555.         sqlQueryObject.addFromTable(this.getSoggettoFieldConverter().toTable(Soggetto.model()));
  556.         sqlQueryObject.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().NOME_SOGGETTO,true));
  557.         sqlQueryObject.addWhereCondition("id=?");


  558.         // Exists soggetto
  559.         existsSoggetto = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  560.             new JDBCObject(tableId,Long.class));

  561.        
  562.         return existsSoggetto;
  563.    
  564.     }
  565.    
  566.     private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{
  567.    
  568.         if(expression.inUseModel(Soggetto.model().SOGGETTO_RUOLO,false) || expression.inUseModel(Soggetto.model().SOGGETTO_RUOLO.ID_RUOLO,false)){
  569.             String tableName1 = this.getSoggettoFieldConverter().toAliasTable(Soggetto.model());
  570.             String tableName2 = this.getSoggettoFieldConverter().toAliasTable(Soggetto.model().SOGGETTO_RUOLO);
  571.             sqlQueryObject.addWhereCondition(tableName1+".id="+tableName2+".id_soggetto");
  572.            
  573.             tableName1 = this.getSoggettoFieldConverter().toAliasTable(Soggetto.model().SOGGETTO_RUOLO.ID_RUOLO);
  574.             tableName2 = this.getSoggettoFieldConverter().toAliasTable(Soggetto.model().SOGGETTO_RUOLO);
  575.             sqlQueryObject.addWhereCondition(tableName1+".id="+tableName2+".id_ruolo");
  576.            
  577.             if(expression.inUseModel(Soggetto.model().SOGGETTO_RUOLO.ID_RUOLO,false)){
  578.                 if(expression.inUseModel(Soggetto.model().SOGGETTO_RUOLO,false)==false){
  579.                     sqlQueryObject.addFromTable(this.getSoggettoFieldConverter().toTable(Soggetto.model().SOGGETTO_RUOLO));
  580.                 }
  581.             }
  582.         }
  583.        
  584.        
  585.     }
  586.    
  587.     protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdSoggetto id) throws NotFoundException, ServiceException, NotImplementedException, Exception{
  588.         // Identificativi
  589.         java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
  590.         Long longId = this.findIdSoggetto(jdbcProperties, log, connection, sqlQueryObject.newSQLQueryObject(), id, true);
  591.         rootTableIdValues.add(longId);
  592.         return rootTableIdValues;
  593.     }
  594.    
  595.     protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
  596.    
  597.         SoggettoFieldConverter converter = this.getSoggettoFieldConverter();
  598.         Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
  599.         UtilsTemplate<IField> utilities = new UtilsTemplate<>();

  600.         // Soggetto.model()
  601.         mapTableToPKColumn.put(converter.toTable(Soggetto.model()),
  602.             utilities.newList(
  603.                 new CustomField("id", Long.class, "id", converter.toTable(Soggetto.model()))
  604.             ));

  605.         // Soggetto.model().SOGGETTO_RUOLO
  606.         mapTableToPKColumn.put(converter.toTable(Soggetto.model().SOGGETTO_RUOLO),
  607.             utilities.newList(
  608.                 new CustomField("id", Long.class, "id", converter.toTable(Soggetto.model().SOGGETTO_RUOLO))
  609.             ));

  610.         // Soggetto.model().SOGGETTO_RUOLO.ID_RUOLO
  611.         mapTableToPKColumn.put(converter.toTable(Soggetto.model().SOGGETTO_RUOLO.ID_RUOLO),
  612.             utilities.newList(
  613.                 new CustomField("id", Long.class, "id", converter.toTable(Soggetto.model().SOGGETTO_RUOLO.ID_RUOLO))
  614.             ));

  615.         // Soggetto.model().SOGGETTO_RUOLO.ID_SOGGETTO
  616.         mapTableToPKColumn.put(converter.toTable(Soggetto.model().SOGGETTO_RUOLO.ID_SOGGETTO),
  617.             utilities.newList(
  618.                 new CustomField("id", Long.class, "id", converter.toTable(Soggetto.model().SOGGETTO_RUOLO.ID_SOGGETTO))
  619.             ));

  620.         return mapTableToPKColumn;      
  621.     }
  622.    
  623.     @Override
  624.     public List<Long> findAllTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression paginatedExpression) throws ServiceException, NotImplementedException, Exception {
  625.        
  626.         List<Long> list = new ArrayList<Long>();

  627.         sqlQueryObject.setSelectDistinct(true);
  628.         sqlQueryObject.setANDLogicOperator(true);
  629.         sqlQueryObject.addSelectField(this.getSoggettoFieldConverter().toTable(Soggetto.model())+".id");
  630.         Class<?> objectIdClass = Long.class;
  631.        
  632.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFindAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  633.                                                 this.getSoggettoFieldConverter(), Soggetto.model());
  634.        
  635.         joinEngine(paginatedExpression,sqlQueryObject);
  636.        
  637.         List<Object> listObjects = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.findAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  638.                                                                             this.getSoggettoFieldConverter(), Soggetto.model(), objectIdClass, listaQuery);
  639.         for(Object object: listObjects) {
  640.             list.add((Long)object);
  641.         }

  642.         return list;
  643.        
  644.     }
  645.    
  646.     @Override
  647.     public long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws ServiceException, NotFoundException, MultipleResultException, NotImplementedException, Exception {
  648.    
  649.         sqlQueryObject.setSelectDistinct(true);
  650.         sqlQueryObject.setANDLogicOperator(true);
  651.         sqlQueryObject.addSelectField(this.getSoggettoFieldConverter().toTable(Soggetto.model())+".id");
  652.         Class<?> objectIdClass = Long.class;
  653.        
  654.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFind(jdbcProperties, log, connection, sqlQueryObject, expression,
  655.                                                 this.getSoggettoFieldConverter(), Soggetto.model());
  656.        
  657.         joinEngine(expression,sqlQueryObject);

  658.         Object res = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.find(jdbcProperties, log, connection, sqlQueryObject, expression,
  659.                                                         this.getSoggettoFieldConverter(), Soggetto.model(), objectIdClass, listaQuery);
  660.         if(res!=null && (((Long) res).longValue()>0) ){
  661.             return ((Long) res).longValue();
  662.         }
  663.         else{
  664.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  665.         }
  666.        
  667.     }

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

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

  673.         InUse inUse = new InUse();
  674.         inUse.setInUse(false);
  675.        
  676.         /*
  677.          * TODO: implement code that checks whether the object identified by the id parameter is used by other objects
  678.         */
  679.        
  680.         // Delete this line when you have implemented the method
  681.         int throwNotImplemented = 1;
  682.         if(throwNotImplemented==1){
  683.                 throw new NotImplementedException("NotImplemented");
  684.         }
  685.         // Delete this line when you have implemented the method

  686.         return inUse;

  687.     }
  688.    
  689.     @Override
  690.     public IdSoggetto findId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, boolean throwNotFound)
  691.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  692.        
  693.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  694.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  695.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();              

  696.         // Object _soggetto
  697.         sqlQueryObjectGet.addFromTable(this.getSoggettoFieldConverter().toTable(Soggetto.model()));
  698.         sqlQueryObjectGet.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().TIPO_SOGGETTO,true));
  699.         sqlQueryObjectGet.addSelectField(this.getSoggettoFieldConverter().toColumn(Soggetto.model().NOME_SOGGETTO,true));
  700.         sqlQueryObjectGet.setANDLogicOperator(true);
  701.         sqlQueryObjectGet.addWhereCondition("id=?");

  702.         // Recupero _soggetto
  703.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_soggetto = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  704.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(tableId,Long.class)
  705.         };
  706.         List<Class<?>> listaFieldIdReturnType_soggetto = new ArrayList<Class<?>>();
  707.         listaFieldIdReturnType_soggetto.add(String.class);
  708.         listaFieldIdReturnType_soggetto.add(String.class);
  709.         org.openspcoop2.core.commons.search.IdSoggetto id_soggetto = null;
  710.         List<Object> listaFieldId_soggetto = jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  711.                 listaFieldIdReturnType_soggetto, searchParams_soggetto);
  712.         if(listaFieldId_soggetto==null || listaFieldId_soggetto.size()<=0){
  713.             if(throwNotFound){
  714.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  715.             }
  716.         }
  717.         else{
  718.             // set _soggetto
  719.             id_soggetto = new org.openspcoop2.core.commons.search.IdSoggetto();
  720.             id_soggetto.setTipo((String)listaFieldId_soggetto.get(0));
  721.             id_soggetto.setNome((String)listaFieldId_soggetto.get(1));
  722.         }
  723.        
  724.         return id_soggetto;
  725.        
  726.     }

  727.     @Override
  728.     public Long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdSoggetto id, boolean throwNotFound)
  729.             throws NotFoundException, ServiceException, NotImplementedException, Exception {
  730.    
  731.         return this.findIdSoggetto(jdbcProperties,log,connection,sqlQueryObject,id,throwNotFound);
  732.            
  733.     }
  734.    
  735.     @Override
  736.     public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  737.                                             String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
  738.        
  739.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
  740.                                                                                             sql,returnClassTypes,param);
  741.                                                        
  742.     }
  743.    
  744.     protected Long findIdSoggetto(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdSoggetto id, boolean throwNotFound) throws NotFoundException, ServiceException, NotImplementedException, Exception {

  745.         if(jdbcProperties==null) {
  746.             throw new ServiceException("Param jdbcProperties is null");
  747.         }
  748.         if(sqlQueryObject==null) {
  749.             throw new ServiceException("Param sqlQueryObject is null");
  750.         }
  751.         if(id==null) {
  752.             throw new ServiceException("Param id is null");
  753.         }
  754.        
  755.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  756.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  757.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

  758.         // Object _soggetto
  759.         sqlQueryObjectGet.addFromTable(this.getSoggettoFieldConverter().toTable(Soggetto.model()));
  760.         sqlQueryObjectGet.addSelectField("id");
  761.         sqlQueryObjectGet.addWhereCondition(this.getSoggettoFieldConverter().toColumn(Soggetto.model().NOME_SOGGETTO,true)+"=?");
  762.         sqlQueryObjectGet.addWhereCondition(this.getSoggettoFieldConverter().toColumn(Soggetto.model().TIPO_SOGGETTO,true)+"=?");
  763.         sqlQueryObjectGet.setANDLogicOperator(true);
  764.         sqlQueryObjectGet.setSelectDistinct(true);
  765.        
  766.         // Recupero _soggetto
  767.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_soggetto = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  768.                 new JDBCObject(id.getNome(), id.getNome().getClass()),
  769.                 new JDBCObject(id.getTipo(), id.getTipo().getClass()),
  770.         };
  771.         Long id_soggetto = null;
  772.         try{
  773.             id_soggetto = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  774.                         Long.class, searchParams_soggetto);
  775.         }catch(NotFoundException notFound){
  776.             if(throwNotFound){
  777.                 throw new NotFoundException(notFound);
  778.             }
  779.         }
  780.         if(id_soggetto==null || id_soggetto<=0){
  781.             if(throwNotFound){
  782.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  783.             }
  784.         }
  785.        
  786.         return id_soggetto;
  787.     }
  788. }