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

  50. /**    
  51.  * JDBCAllarmeHistoryServiceSearchImpl
  52.  *
  53.  * @author Poli Andrea (poli@link.it)
  54.  * @author $Author$
  55.  * @version $Rev$, $Date$
  56.  */
  57. public class JDBCAllarmeHistoryServiceSearchImpl implements IJDBCServiceSearchWithoutId<AllarmeHistory, JDBCServiceManager> {

  58.     private AllarmeHistoryFieldConverter allarmeHistoryFieldConverterInternal = null;
  59.     public AllarmeHistoryFieldConverter getAllarmeHistoryFieldConverter() {
  60.         if(this.allarmeHistoryFieldConverterInternal==null){
  61.             this.allarmeHistoryFieldConverterInternal = new AllarmeHistoryFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  62.         }      
  63.         return this.allarmeHistoryFieldConverterInternal;
  64.     }
  65.     @Override
  66.     public ISQLFieldConverter getFieldConverter() {
  67.         return this.getAllarmeHistoryFieldConverter();
  68.     }
  69.    
  70.     private AllarmeHistoryFetch allarmeHistoryFetch = new AllarmeHistoryFetch();
  71.     public AllarmeHistoryFetch getAllarmeHistoryFetch() {
  72.         return this.allarmeHistoryFetch;
  73.     }
  74.     @Override
  75.     public IJDBCFetch getFetch() {
  76.         return getAllarmeHistoryFetch();
  77.     }
  78.    
  79.    
  80.     private JDBCServiceManager jdbcServiceManager = null;

  81.     @Override
  82.     public void setServiceManager(JDBCServiceManager serviceManager) throws ServiceException{
  83.         this.jdbcServiceManager = serviceManager;
  84.     }
  85.    
  86.     @Override
  87.     public JDBCServiceManager getServiceManager() throws ServiceException{
  88.         return this.jdbcServiceManager;
  89.     }
  90.    

  91.    
  92.    
  93.    
  94.    
  95.     @Override
  96.     public List<AllarmeHistory> findAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException, ServiceException,Exception {

  97.         List<AllarmeHistory> list = new ArrayList<>();
  98.        
  99.         /** TODO: implementazione non efficiente.
  100.         // Per ottenere una implementazione efficiente:
  101.         // 1. Usare metodo select di questa classe indirizzando esattamente i field necessari
  102.         // 2. Usare metodo getAllarmeHistoryFetch() sul risultato della select per ottenere un oggetto AllarmeHistory
  103.         //    La fetch con la map inserirĂ  nell'oggetto solo i valori estratti*/

  104.         List<Long> ids = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, expression);
  105.        
  106.         for(Long id: ids) {
  107.             list.add(this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour));
  108.         }

  109.         return list;      
  110.        
  111.     }
  112.    
  113.     @Override
  114.     public AllarmeHistory find(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour)
  115.         throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException,Exception {

  116.         long id = this.findTableId(jdbcProperties, log, connection, sqlQueryObject, expression);
  117.         if(id>0){
  118.             return this.get(jdbcProperties, log, connection, sqlQueryObject, id, idMappingResolutionBehaviour);
  119.         }else{
  120.             throw new NotFoundException("Entry with id["+id+"] not found");
  121.         }
  122.        
  123.     }
  124.    
  125.     @Override
  126.     public NonNegativeNumber count(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws NotImplementedException, ServiceException,Exception {
  127.        
  128.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareCount(jdbcProperties, log, connection, sqlQueryObject, expression,
  129.                                                 this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model());
  130.        
  131.         sqlQueryObject.addSelectCountField(this.getAllarmeHistoryFieldConverter().toTable(AllarmeHistory.model())+".id","tot",true);
  132.        
  133.         joinEngine(expression,sqlQueryObject);
  134.        
  135.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.count(jdbcProperties, log, connection, sqlQueryObject, expression,
  136.                                                                             this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),listaQuery);
  137.     }


  138.     @Override
  139.     public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  140.                                                     JDBCPaginatedExpression paginatedExpression, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  141.         return this.select(jdbcProperties, log, connection, sqlQueryObject,
  142.                                 paginatedExpression, false, field);
  143.     }
  144.    
  145.     @Override
  146.     public List<Object> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  147.                                                     JDBCPaginatedExpression paginatedExpression, boolean distinct, IField field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  148.         List<Map<String,Object>> map =
  149.             this.select(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, distinct, new IField[]{field});
  150.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectSingleObject(map);
  151.     }
  152.    
  153.     @Override
  154.     public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  155.                                                     JDBCPaginatedExpression paginatedExpression, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  156.         return this.select(jdbcProperties, log, connection, sqlQueryObject,
  157.                                 paginatedExpression, false, field);
  158.     }
  159.    
  160.     @Override
  161.     public List<Map<String,Object>> select(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  162.                                                     JDBCPaginatedExpression paginatedExpression, boolean distinct, IField ... field) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  163.        
  164.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,field);
  165.         try{
  166.        
  167.             ISQLQueryObject sqlQueryObjectDistinct =
  168.                         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(distinct,sqlQueryObject, paginatedExpression, log,
  169.                                                 this.getAllarmeHistoryFieldConverter(), field);

  170.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression, sqlQueryObjectDistinct);
  171.            
  172.         }finally{
  173.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,field);
  174.         }
  175.     }

  176.     @Override
  177.     public Object aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  178.                                                     JDBCExpression expression, FunctionField functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  179.         Map<String,Object> map =
  180.             this.aggregate(jdbcProperties, log, connection, sqlQueryObject, expression, new FunctionField[]{functionField});
  181.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.selectAggregateObject(map,functionField);
  182.     }
  183.    
  184.     @Override
  185.     public Map<String,Object> aggregate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  186.                                                     JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {                                                  
  187.        
  188.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
  189.         try{
  190.             List<Map<String,Object>> list = selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
  191.             return list.get(0);
  192.         }finally{
  193.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
  194.         }
  195.     }

  196.     @Override
  197.     public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  198.                                                     JDBCExpression expression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  199.        
  200.         if(expression.getGroupByFields().isEmpty()){
  201.             throw new ServiceException("GroupBy conditions not found in expression");
  202.         }
  203.        
  204.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,expression,functionField);
  205.         try{
  206.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression);
  207.         }finally{
  208.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,expression,functionField);
  209.         }
  210.     }
  211.    

  212.     @Override
  213.     public List<Map<String,Object>> groupBy(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  214.                                                     JDBCPaginatedExpression paginatedExpression, FunctionField ... functionField) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  215.        
  216.         if(paginatedExpression.getGroupByFields().isEmpty()){
  217.             throw new ServiceException("GroupBy conditions not found in expression");
  218.         }
  219.        
  220.         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.setFields(sqlQueryObject,paginatedExpression,functionField);
  221.         try{
  222.             return selectEngine(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression);
  223.         }finally{
  224.             org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.removeFields(sqlQueryObject,paginatedExpression,functionField);
  225.         }
  226.     }
  227.    
  228.     protected List<Map<String,Object>> selectEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  229.                                                 IExpression expression) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  230.         return selectEngine(jdbcProperties, log, connection, sqlQueryObject, expression, null);
  231.     }
  232.     protected List<Map<String,Object>> selectEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  233.                                                 IExpression expression, ISQLQueryObject sqlQueryObjectDistinct) throws ServiceException,NotFoundException,NotImplementedException,Exception {
  234.        
  235.         List<Object> listaQuery = new ArrayList<>();
  236.         List<JDBCObject> listaParams = new ArrayList<>();
  237.         List<Object> returnField = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSelect(jdbcProperties, log, connection, sqlQueryObject,
  238.                                 expression, this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),
  239.                                 listaQuery,listaParams);
  240.        
  241.         joinEngine(expression,sqlQueryObject);
  242.        
  243.         List<Map<String,Object>> list = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.select(jdbcProperties, log, connection,
  244.                                         org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareSqlQueryObjectForSelectDistinct(sqlQueryObject,sqlQueryObjectDistinct),
  245.                                         expression, this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),
  246.                                         listaQuery,listaParams,returnField);
  247.         if(list!=null && !list.isEmpty()){
  248.             return list;
  249.         }
  250.         else{
  251.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  252.         }
  253.     }
  254.    
  255.     @Override
  256.     public List<Map<String,Object>> union(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  257.                                                 Union union, UnionExpression ... unionExpression) throws ServiceException,NotFoundException,NotImplementedException,Exception {    
  258.        
  259.         List<ISQLQueryObject> sqlQueryObjectInnerList = new ArrayList<>();
  260.         List<JDBCObject> jdbcObjects = new ArrayList<>();
  261.         List<Class<?>> returnClassTypes = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareUnion(jdbcProperties, log, connection, sqlQueryObject,
  262.                                 this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),
  263.                                 sqlQueryObjectInnerList, jdbcObjects, union, unionExpression);
  264.        
  265.         if(unionExpression!=null){
  266.             for (int i = 0; i < unionExpression.length; i++) {
  267.                 UnionExpression ue = unionExpression[i];
  268.                 IExpression expression = ue.getExpression();
  269.                 joinEngine(expression,sqlQueryObjectInnerList.get(i));
  270.             }
  271.         }
  272.        
  273.         List<Map<String,Object>> list = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.union(jdbcProperties, log, connection, sqlQueryObject,
  274.                                         this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),
  275.                                         sqlQueryObjectInnerList, jdbcObjects, returnClassTypes, union, unionExpression);
  276.         if(list!=null && !list.isEmpty()){
  277.             return list;
  278.         }
  279.         else{
  280.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  281.         }                              
  282.     }
  283.    
  284.     @Override
  285.     public NonNegativeNumber unionCount(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  286.                                                 Union union, UnionExpression ... unionExpression) throws ServiceException,NotFoundException,NotImplementedException,Exception {    
  287.        
  288.         List<ISQLQueryObject> sqlQueryObjectInnerList = new ArrayList<>();
  289.         List<JDBCObject> jdbcObjects = new ArrayList<>();
  290.         List<Class<?>> returnClassTypes = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareUnionCount(jdbcProperties, log, connection, sqlQueryObject,
  291.                                 this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),
  292.                                 sqlQueryObjectInnerList, jdbcObjects, union, unionExpression);
  293.        
  294.         if(unionExpression!=null){
  295.             for (int i = 0; i < unionExpression.length; i++) {
  296.                 UnionExpression ue = unionExpression[i];
  297.                 IExpression expression = ue.getExpression();
  298.                 joinEngine(expression,sqlQueryObjectInnerList.get(i));
  299.             }
  300.         }
  301.        
  302.         NonNegativeNumber number = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.unionCount(jdbcProperties, log, connection, sqlQueryObject,
  303.                                         this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(),
  304.                                         sqlQueryObjectInnerList, jdbcObjects, returnClassTypes, union, unionExpression);
  305.         if(number!=null && number.longValue()>=0){
  306.             return number;
  307.         }
  308.         else{
  309.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  310.         }
  311.     }



  312.     // -- ConstructorExpression

  313.     @Override
  314.     public JDBCExpression newExpression(Logger log) throws NotImplementedException, ServiceException {
  315.         try{
  316.             return new JDBCExpression(this.getAllarmeHistoryFieldConverter());
  317.         }catch(Exception e){
  318.             throw new ServiceException(e);
  319.         }
  320.     }


  321.     @Override
  322.     public JDBCPaginatedExpression newPaginatedExpression(Logger log) throws NotImplementedException, ServiceException {
  323.         try{
  324.             return new JDBCPaginatedExpression(this.getAllarmeHistoryFieldConverter());
  325.         }catch(Exception e){
  326.             throw new ServiceException(e);
  327.         }
  328.     }
  329.    
  330.     @Override
  331.     public JDBCExpression toExpression(JDBCPaginatedExpression paginatedExpression, Logger log) throws NotImplementedException, ServiceException {
  332.         try{
  333.             return new JDBCExpression(paginatedExpression);
  334.         }catch(Exception e){
  335.             throw new ServiceException(e);
  336.         }
  337.     }

  338.     @Override
  339.     public JDBCPaginatedExpression toPaginatedExpression(JDBCExpression expression, Logger log) throws NotImplementedException, ServiceException {
  340.         try{
  341.             return new JDBCPaginatedExpression(expression);
  342.         }catch(Exception e){
  343.             throw new ServiceException(e);
  344.         }
  345.     }
  346.    
  347.    
  348.    
  349.     // -- DB

  350.    
  351.     @Override
  352.     public AllarmeHistory get(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  353.         return this.getEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId), idMappingResolutionBehaviour);
  354.     }
  355.    
  356.     private AllarmeHistory getEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, MultipleResultException, NotImplementedException, ServiceException, Exception {
  357.    
  358.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  359.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  360.        
  361.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObject.newSQLQueryObject();
  362.        

  363.         // Object allarmeHistory
  364.         sqlQueryObjectGet.setANDLogicOperator(true);
  365.         sqlQueryObjectGet.addFromTable(this.getAllarmeHistoryFieldConverter().toTable(AllarmeHistory.model()));
  366.         sqlQueryObjectGet.addSelectField("id");
  367.         sqlQueryObjectGet.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().ENABLED,true));
  368.         sqlQueryObjectGet.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().STATO,true));
  369.         sqlQueryObjectGet.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().DETTAGLIO_STATO,true));
  370.         sqlQueryObjectGet.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().ACKNOWLEDGED,true));
  371.         sqlQueryObjectGet.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().TIMESTAMP_UPDATE,true));
  372.         sqlQueryObjectGet.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().UTENTE,true));
  373.         sqlQueryObjectGet.addWhereCondition("id=?");

  374.         // Get allarmeHistory
  375.         AllarmeHistory allarmeHistory = (AllarmeHistory) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(), AllarmeHistory.model(), this.getAllarmeHistoryFetch(),
  376.             new JDBCObject(tableId,Long.class));


  377.         if(idMappingResolutionBehaviour==null ||
  378.             (org.openspcoop2.generic_project.beans.IDMappingBehaviour.ENABLED.equals(idMappingResolutionBehaviour) || org.openspcoop2.generic_project.beans.IDMappingBehaviour.USE_TABLE_ID.equals(idMappingResolutionBehaviour))
  379.         ){
  380.             // Object _allarme (recupero id)
  381.             ISQLQueryObject sqlQueryObjectGetAllarmeReadFkId = sqlQueryObjectGet.newSQLQueryObject();
  382.             sqlQueryObjectGetAllarmeReadFkId.addFromTable(this.getAllarmeHistoryFieldConverter().toTable(org.openspcoop2.core.allarmi.AllarmeHistory.model()));
  383.             sqlQueryObjectGetAllarmeReadFkId.addSelectField("id_allarme");
  384.             sqlQueryObjectGetAllarmeReadFkId.addWhereCondition("id=?");
  385.             sqlQueryObjectGetAllarmeReadFkId.setANDLogicOperator(true);
  386.             Long idFKAllarme = (Long) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGetAllarmeReadFkId.createSQLQuery(), jdbcProperties.isShowSql(),Long.class,
  387.                     new JDBCObject(allarmeHistory.getId(),Long.class));
  388.            
  389.             org.openspcoop2.core.allarmi.IdAllarme idAllarme = null;
  390.             if(idMappingResolutionBehaviour==null || org.openspcoop2.generic_project.beans.IDMappingBehaviour.ENABLED.equals(idMappingResolutionBehaviour)){
  391.                 idAllarme = ((JDBCAllarmeServiceSearch)(this.getServiceManager().getAllarmeServiceSearch())).findId(idFKAllarme, false);
  392.             }else{
  393.                 idAllarme = new org.openspcoop2.core.allarmi.IdAllarme();
  394.             }
  395.             idAllarme.setId(idFKAllarme);
  396.             allarmeHistory.setIdAllarme(idAllarme);
  397.         }              
  398.        
  399.         return allarmeHistory;  
  400.    
  401.     }
  402.    
  403.     @Override
  404.     public boolean exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  405.         return this._exists(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  406.     }
  407.    
  408.     private boolean _exists(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long tableId) throws MultipleResultException, NotImplementedException, ServiceException, Exception {
  409.    
  410.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  411.                     new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  412.                
  413.         boolean existsAllarmeHistory = false;

  414.         sqlQueryObject = sqlQueryObject.newSQLQueryObject();
  415.         sqlQueryObject.setANDLogicOperator(true);

  416.         sqlQueryObject.addFromTable(this.getAllarmeHistoryFieldConverter().toTable(AllarmeHistory.model()));
  417.         sqlQueryObject.addSelectField(this.getAllarmeHistoryFieldConverter().toColumn(AllarmeHistory.model().ENABLED,true));
  418.         sqlQueryObject.addWhereCondition("id=?");


  419.         // Exists allarmeHistory
  420.         existsAllarmeHistory = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  421.             new JDBCObject(tableId,Long.class));

  422.        
  423.         return existsAllarmeHistory;
  424.    
  425.     }
  426.    
  427.     private void joinEngine(IExpression expression, ISQLQueryObject sqlQueryObject) throws NotImplementedException, ServiceException, Exception{
  428.    
  429.         if(expression.inUseModel(AllarmeHistory.model().ID_ALLARME,false)){
  430.             String tableName1 = this.getAllarmeHistoryFieldConverter().toAliasTable(AllarmeHistory.model());
  431.             String tableName2 = this.getAllarmeHistoryFieldConverter().toAliasTable(AllarmeHistory.model().ID_ALLARME);
  432.             sqlQueryObject.addWhereCondition(tableName1+".id_allarme="+tableName2+".id");
  433.         }
  434.        
  435.     }
  436.    
  437.     protected java.util.List<Object> getRootTablePrimaryKeyValuesEngine(Logger log, Connection connection, ISQLQueryObject sqlQueryObject, AllarmeHistory allarmeHistory) throws NotFoundException, ServiceException, NotImplementedException, Exception{
  438.         // Identificativi
  439.         java.util.List<Object> rootTableIdValues = new java.util.ArrayList<>();
  440.         rootTableIdValues.add(allarmeHistory.getId());
  441.        
  442.         return rootTableIdValues;
  443.     }
  444.    
  445.     protected Map<String, List<IField>> getMapTableToPKColumnEngine() throws NotImplementedException, Exception{
  446.    
  447.         AllarmeHistoryFieldConverter converter = this.getAllarmeHistoryFieldConverter();
  448.         Map<String, List<IField>> mapTableToPKColumn = new java.util.HashMap<>();
  449.         UtilsTemplate<IField> utilities = new UtilsTemplate<>();

  450.         // AllarmeHistory.model()
  451.         mapTableToPKColumn.put(converter.toTable(AllarmeHistory.model()),
  452.             utilities.newList(
  453.                 new CustomField("id", Long.class, "id", converter.toTable(AllarmeHistory.model()))
  454.             ));

  455.         // AllarmeHistory.model().ID_ALLARME
  456.         mapTableToPKColumn.put(converter.toTable(AllarmeHistory.model().ID_ALLARME),
  457.             utilities.newList(
  458.                 new CustomField("id", Long.class, "id", converter.toTable(AllarmeHistory.model().ID_ALLARME))
  459.             ));
  460.        
  461.         return mapTableToPKColumn;      
  462.     }
  463.    
  464.     @Override
  465.     public List<Long> findAllTableIds(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCPaginatedExpression paginatedExpression) throws ServiceException, NotImplementedException, Exception {
  466.        
  467.         List<Long> list = new ArrayList<Long>();

  468.         sqlQueryObject.setSelectDistinct(true);
  469.         sqlQueryObject.setANDLogicOperator(true);
  470.         sqlQueryObject.addSelectField(this.getAllarmeHistoryFieldConverter().toTable(AllarmeHistory.model())+".id");
  471.         Class<?> objectIdClass = Long.class;
  472.        
  473.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFindAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  474.                                                 this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model());
  475.        
  476.         joinEngine(paginatedExpression,sqlQueryObject);
  477.        
  478.         List<Object> listObjects = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.findAll(jdbcProperties, log, connection, sqlQueryObject, paginatedExpression,
  479.                                                                             this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(), objectIdClass, listaQuery);
  480.         for(Object object: listObjects) {
  481.             list.add((Long)object);
  482.         }

  483.         return list;
  484.        
  485.     }
  486.    
  487.     @Override
  488.     public long findTableId(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws ServiceException, NotFoundException, MultipleResultException, NotImplementedException, Exception {
  489.    
  490.         sqlQueryObject.setSelectDistinct(true);
  491.         sqlQueryObject.setANDLogicOperator(true);
  492.         sqlQueryObject.addSelectField(this.getAllarmeHistoryFieldConverter().toTable(AllarmeHistory.model())+".id");
  493.         Class<?> objectIdClass = Long.class;
  494.        
  495.         List<Object> listaQuery = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.prepareFind(jdbcProperties, log, connection, sqlQueryObject, expression,
  496.                                                 this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model());
  497.        
  498.         joinEngine(expression,sqlQueryObject);

  499.         Object res = org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.find(jdbcProperties, log, connection, sqlQueryObject, expression,
  500.                                                         this.getAllarmeHistoryFieldConverter(), AllarmeHistory.model(), objectIdClass, listaQuery);
  501.         if(res!=null && (((Long) res).longValue()>0) ){
  502.             return ((Long) res).longValue();
  503.         }
  504.         else{
  505.             throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotFoundException();
  506.         }
  507.        
  508.     }

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

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

  514.         throw org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.newNotImplementedException();

  515.     }
  516.    

  517.    
  518.     @Override
  519.     public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  520.                                             String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
  521.        
  522.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
  523.                                                                                             sql,returnClassTypes,param);
  524.                                                        
  525.     }
  526.    
  527. }