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

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

  59.     private RuoloFieldConverter _ruoloFieldConverter = null;
  60.     public RuoloFieldConverter getRuoloFieldConverter() {
  61.         if(this._ruoloFieldConverter==null){
  62.             this._ruoloFieldConverter = new RuoloFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  63.         }      
  64.         return this._ruoloFieldConverter;
  65.     }
  66.     @Override
  67.     public ISQLFieldConverter getFieldConverter() {
  68.         return this.getRuoloFieldConverter();
  69.     }
  70.    
  71.     private RuoloFetch ruoloFetch = new RuoloFetch();
  72.     public RuoloFetch getRuoloFetch() {
  73.         return this.ruoloFetch;
  74.     }
  75.     @Override
  76.     public IJDBCFetch getFetch() {
  77.         return getRuoloFetch();
  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 IdRuolo convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Ruolo ruolo) throws NotImplementedException, ServiceException, Exception{
  97.    
  98.         IdRuolo idRuolo = new IdRuolo();
  99.         idRuolo.setNome(ruolo.getNome());
  100.        
  101.         return idRuolo;
  102.     }
  103.    
  104.     @Override
  105.     public Ruolo get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdRuolo id, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {
  106.         Long id_ruolo = ( (id!=null && id.getId()!=null && id.getId()>0) ? id.getId() : this.findIdRuolo(jdbcProperties, log, connection, sqlQueryObject, id, true));
  107.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, id_ruolo,idMappingResolutionBehaviour);
  108.        
  109.        
  110.     }
  111.    
  112.     @Override
  113.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdRuolo id) throws MultipleResultException, NotImplementedException, ServiceException,Exception {

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

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

  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 getRuoloFetch() sul risultato della select per ottenere un oggetto Ruolo
  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.             Ruolo ruolo = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  132.             IdRuolo idRuolo = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,ruolo);
  133.             list.add(idRuolo);
  134.         }

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

  141.         List<Ruolo> list = new ArrayList<Ruolo>();
  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 getRuoloFetch() sul risultato della select per ottenere un oggetto Ruolo
  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 Ruolo 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.getRuoloFieldConverter(), Ruolo.model());
  174.        
  175.         sqlQueryObject.addSelectCountField(this.getRuoloFieldConverter().toTable(Ruolo.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.getRuoloFieldConverter(), Ruolo.model(),listaQuery);
  181.     }

  182.     @Override
  183.     public InUse inUse(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdRuolo id) throws NotFoundException, NotImplementedException, ServiceException,Exception {
  184.        
  185.         Long id_ruolo = this.findIdRuolo(jdbcProperties, log, connection, sqlQueryObject, id, true);
  186.         return this.inUseEngine(jdbcProperties, log, connection, sqlQueryObject, id_ruolo);
  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.getRuoloFieldConverter(), 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.getRuoloFieldConverter(), Ruolo.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.getRuoloFieldConverter(), Ruolo.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.getRuoloFieldConverter(), Ruolo.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.getRuoloFieldConverter(), Ruolo.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.getRuoloFieldConverter(), Ruolo.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.getRuoloFieldConverter(), Ruolo.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.getRuoloFieldConverter());
  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.getRuoloFieldConverter());
  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, IdRuolo id, Ruolo 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, Ruolo 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, Ruolo obj, Ruolo imgSaved) throws NotFoundException,NotImplementedException,ServiceException,Exception{
  413.         if(imgSaved==null){
  414.             return;
  415.         }
  416.         obj.setId(imgSaved.getId());

  417.     }
  418.    
  419.     @Override
  420.     public Ruolo 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 Ruolo 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.         Ruolo ruolo = new Ruolo();
  432.        

  433.         // Object ruolo
  434.         ISQLQueryObject sqlQueryObjectGet_ruolo = sqlQueryObjectGet.newSQLQueryObject();
  435.         sqlQueryObjectGet_ruolo.setANDLogicOperator(true);
  436.         sqlQueryObjectGet_ruolo.addFromTable(this.getRuoloFieldConverter().toTable(Ruolo.model()));
  437.         sqlQueryObjectGet_ruolo.addSelectField("id");
  438.         sqlQueryObjectGet_ruolo.addSelectField(this.getRuoloFieldConverter().toColumn(Ruolo.model().NOME,true));
  439.         sqlQueryObjectGet_ruolo.addSelectField(this.getRuoloFieldConverter().toColumn(Ruolo.model().NOME_ESTERNO,true));
  440.         sqlQueryObjectGet_ruolo.addWhereCondition("id=?");

  441.         // Get ruolo
  442.         ruolo = (Ruolo) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_ruolo.createSQLQuery(), jdbcProperties.isShowSql(), Ruolo.model(), this.getRuoloFetch(),
  443.             new JDBCObject(tableId,Long.class));



  444.        
  445.         return ruolo;  
  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 existsRuolo = false;

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

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


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

  468.        
  469.         return existsRuolo;
  470.    
  471.     }
  472.    
  473.     private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{
  474.    
  475.         // nop
  476.        
  477.     }
  478.    
  479.     protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdRuolo id) throws NotFoundException, ServiceException, NotImplementedException, Exception{
  480.         // Identificativi
  481.         java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
  482.         Long longId = this.findIdRuolo(jdbcProperties, log, connection, sqlQueryObject.newSQLQueryObject(), id, true);
  483.         rootTableIdValues.add(longId);
  484.         return rootTableIdValues;
  485.     }
  486.    
  487.     protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
  488.    
  489.         RuoloFieldConverter converter = this.getRuoloFieldConverter();
  490.         Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
  491.         UtilsTemplate<IField> utilities = new UtilsTemplate<>();

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

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

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

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

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

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

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

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

  563.         return inUse;

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

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

  574.         // Object _ruolo
  575.         sqlQueryObjectGet.addFromTable(this.getRuoloFieldConverter().toTable(Ruolo.model()));
  576.         sqlQueryObjectGet.addSelectField(this.getRuoloFieldConverter().toColumn(Ruolo.model().NOME,true));
  577.         sqlQueryObjectGet.setANDLogicOperator(true);
  578.         sqlQueryObjectGet.addWhereCondition("id=?");

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

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

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

  622.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

  623.         if(id==null || id.getNome()==null) {
  624.             throw new ServiceException("Id ruolo non fornito");
  625.         }
  626.        
  627.         // Object _ruolo
  628.         sqlQueryObjectGet.addFromTable(this.getRuoloFieldConverter().toTable(Ruolo.model()));
  629.         sqlQueryObjectGet.addSelectField("id");
  630.         // Devono essere mappati nella where condition i metodi dell'oggetto id.getXXX
  631.         sqlQueryObjectGet.setANDLogicOperator(true);
  632.         sqlQueryObjectGet.setSelectDistinct(true);
  633.         sqlQueryObjectGet.addWhereCondition(this.getRuoloFieldConverter().toColumn(Ruolo.model().NOME,true)+"=?");
  634.        
  635.         // Recupero _ruolo
  636.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_ruolo = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  637.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(id.getNome(),String.class)
  638.         };
  639.         Long id_ruolo = null;
  640.         try{
  641.             id_ruolo = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  642.                         Long.class, searchParams_ruolo);
  643.         }catch(NotFoundException notFound){
  644.             if(throwNotFound){
  645.                 throw new NotFoundException(notFound);
  646.             }
  647.         }
  648.         if(id_ruolo==null || id_ruolo<=0){
  649.             if(throwNotFound){
  650.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  651.             }
  652.         }
  653.        
  654.         return id_ruolo;
  655.     }
  656. }