JDBCPortaDominioServiceSearchImpl.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.core.commons.search.dao.jdbc;

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

  24. import java.sql.Connection;

  25. import org.slf4j.Logger;

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

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

  47. import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;
  48. import org.openspcoop2.core.commons.search.dao.jdbc.converter.PortaDominioFieldConverter;
  49. import org.openspcoop2.core.commons.search.dao.jdbc.fetch.PortaDominioFetch;
  50. import org.openspcoop2.core.commons.search.PortaDominio;

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

  59.     private PortaDominioFieldConverter _portaDominioFieldConverter = null;
  60.     public PortaDominioFieldConverter getPortaDominioFieldConverter() {
  61.         if(this._portaDominioFieldConverter==null){
  62.             this._portaDominioFieldConverter = new PortaDominioFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  63.         }      
  64.         return this._portaDominioFieldConverter;
  65.     }
  66.     @Override
  67.     public ISQLFieldConverter getFieldConverter() {
  68.         return this.getPortaDominioFieldConverter();
  69.     }
  70.    
  71.     private PortaDominioFetch portaDominioFetch = new PortaDominioFetch();
  72.     public PortaDominioFetch getPortaDominioFetch() {
  73.         return this.portaDominioFetch;
  74.     }
  75.     @Override
  76.     public IJDBCFetch getFetch() {
  77.         return getPortaDominioFetch();
  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 IdPortaDominio convertToId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, PortaDominio portaDominio) throws NotImplementedException, ServiceException, Exception{
  97.    
  98.         IdPortaDominio idPortaDominio = new IdPortaDominio();
  99.         idPortaDominio.setNome(portaDominio.getNome());
  100.         return idPortaDominio;

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

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

  120.         List<IdPortaDominio> list = new ArrayList<IdPortaDominio>();

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

  127.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  128.        
  129.         for(Long id: ids) {
  130.             PortaDominio portaDominio = this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  131.             IdPortaDominio idPortaDominio = this.convertToId(jdbcProperties,log,connection,sqlQueryObject,portaDominio);
  132.             list.add(idPortaDominio);
  133.         }

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

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

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

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

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

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

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

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

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

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

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



  362.     // -- ConstructorExpression

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


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

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

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

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

  432.         // Object portaDominio
  433.         ISQLQueryObject sqlQueryObjectGet_portaDominio = sqlQueryObjectGet.newSQLQueryObject();
  434.         sqlQueryObjectGet_portaDominio.setANDLogicOperator(true);
  435.         sqlQueryObjectGet_portaDominio.addFromTable(this.getPortaDominioFieldConverter().toTable(PortaDominio.model()));
  436.         sqlQueryObjectGet_portaDominio.addSelectField("id");
  437.         sqlQueryObjectGet_portaDominio.addSelectField(this.getPortaDominioFieldConverter().toColumn(PortaDominio.model().NOME,true));
  438.         sqlQueryObjectGet_portaDominio.addSelectField(this.getPortaDominioFieldConverter().toColumn(PortaDominio.model().TIPO,true));
  439.         sqlQueryObjectGet_portaDominio.addWhereCondition("id=?");

  440.         // Get portaDominio
  441.         portaDominio = (PortaDominio) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet_portaDominio.createSQLQuery(), jdbcProperties.isShowSql(), PortaDominio.model(), this.getPortaDominioFetch(),
  442.             new JDBCObject(tableId,Long.class));



  443.        
  444.         return portaDominio;  
  445.    
  446.     }
  447.    
  448.     @Override
  449.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  450.         return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  451.     }
  452.    
  453.     private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws 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.         boolean existsPortaDominio = false;

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

  461.         sqlQueryObject.addFromTable(this.getPortaDominioFieldConverter().toTable(PortaDominio.model()));
  462.         sqlQueryObject.addSelectField(this.getPortaDominioFieldConverter().toColumn(PortaDominio.model().NOME,true));
  463.         sqlQueryObject.addWhereCondition("id=?");


  464.         // Exists portaDominio
  465.         existsPortaDominio = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  466.             new JDBCObject(tableId,Long.class));

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

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

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

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

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

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

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

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

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

  562.         return inUse;

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

  571.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();            

  572.         // Object _portaDominio
  573.         sqlQueryObjectGet.addFromTable(this.getPortaDominioFieldConverter().toTable(PortaDominio.model()));
  574.         sqlQueryObjectGet.addSelectField(this.getPortaDominioFieldConverter().toColumn(PortaDominio.model().NOME,true));
  575.         sqlQueryObjectGet.setANDLogicOperator(true);
  576.         sqlQueryObjectGet.addWhereCondition("id=?");

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

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

  618.         if(jdbcProperties==null) {
  619.             throw new ServiceException("Param jdbcProperties is null");
  620.         }
  621.         if(sqlQueryObject==null) {
  622.             throw new ServiceException("Param sqlQueryObject is null");
  623.         }
  624.         if(id==null) {
  625.             throw new ServiceException("Param id is null");
  626.         }
  627.        
  628.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  629.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);

  630.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();

  631.         sqlQueryObjectGet.addFromTable(this.getPortaDominioFieldConverter().toTable(PortaDominio.model()));
  632.         sqlQueryObjectGet.addSelectField("id");
  633.         sqlQueryObjectGet.addWhereCondition(this.getPortaDominioFieldConverter().toColumn(PortaDominio.model().NOME,true)+"=?");
  634.         sqlQueryObjectGet.setANDLogicOperator(true);
  635.         sqlQueryObjectGet.setSelectDistinct(true);
  636.        
  637.         // Recupero _porta-dominio
  638.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] searchParams_portaDominio = new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject [] {
  639.                 new JDBCObject(id.getNome(), id.getNome().getClass())
  640.         };
  641.         Long id_portaDominio = null;
  642.         try{
  643.             id_portaDominio = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(),
  644.                         Long.class, searchParams_portaDominio);
  645.         }catch(NotFoundException notFound){
  646.             if(throwNotFound){
  647.                 throw new NotFoundException(notFound);
  648.             }
  649.         }
  650.         if(id_portaDominio==null || id_portaDominio<=0){
  651.             if(throwNotFound){
  652.                 throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  653.             }
  654.         }
  655.        
  656.         return id_portaDominio;
  657.     }
  658. }