JDBCAllarmeNotificaServiceSearchImpl.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.AllarmeNotifica;
  27. import org.openspcoop2.core.allarmi.dao.jdbc.converter.AllarmeNotificaFieldConverter;
  28. import org.openspcoop2.core.allarmi.dao.jdbc.fetch.AllarmeNotificaFetch;
  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.IJDBCServiceSearchWithoutId;
  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.  * JDBCAllarmeNotificaServiceSearchImpl
  53.  *
  54.  * @author Poli Andrea (poli@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  */
  58. public class JDBCAllarmeNotificaServiceSearchImpl implements IJDBCServiceSearchWithoutId<AllarmeNotifica, JDBCServiceManager> {

  59.     private AllarmeNotificaFieldConverter _allarmeNotificaFieldConverter = null;
  60.     public AllarmeNotificaFieldConverter getAllarmeNotificaFieldConverter() {
  61.         if(this._allarmeNotificaFieldConverter==null){
  62.             this._allarmeNotificaFieldConverter = new AllarmeNotificaFieldConverter(this.jdbcServiceManager.getJdbcProperties().getDatabaseType());
  63.         }      
  64.         return this._allarmeNotificaFieldConverter;
  65.     }
  66.     @Override
  67.     public ISQLFieldConverter getFieldConverter() {
  68.         return this.getAllarmeNotificaFieldConverter();
  69.     }
  70.    
  71.     private AllarmeNotificaFetch allarmeNotificaFetch = new AllarmeNotificaFetch();
  72.     public AllarmeNotificaFetch getAllarmeNotificaFetch() {
  73.         return this.allarmeNotificaFetch;
  74.     }
  75.     @Override
  76.     public IJDBCFetch getFetch() {
  77.         return getAllarmeNotificaFetch();
  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.    

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

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

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

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

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


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

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

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

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

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



  313.     // -- ConstructorExpression

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


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

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

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

  366.         // Object allarmeNotifica
  367.         sqlQueryObjectGet.setANDLogicOperator(true);
  368.         sqlQueryObjectGet.addFromTable(this.getAllarmeNotificaFieldConverter().toTable(AllarmeNotifica.model()));
  369.         sqlQueryObjectGet.addSelectField("id");
  370.         sqlQueryObjectGet.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().DATA_NOTIFICA,true));
  371.         sqlQueryObjectGet.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().OLD_STATO,true));
  372.         sqlQueryObjectGet.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().OLD_DETTAGLIO_STATO,true));
  373.         sqlQueryObjectGet.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().NUOVO_STATO,true));
  374.         sqlQueryObjectGet.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().NUOVO_DETTAGLIO_STATO,true));
  375.         sqlQueryObjectGet.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().HISTORY_ENTRY,true));
  376.         sqlQueryObjectGet.addWhereCondition("id=?");

  377.         // Get allarmeNotifica
  378.         allarmeNotifica = (AllarmeNotifica) jdbcUtilities.executeQuerySingleResult(sqlQueryObjectGet.createSQLQuery(), jdbcProperties.isShowSql(), AllarmeNotifica.model(), this.getAllarmeNotificaFetch(),
  379.             new JDBCObject(tableId,Long.class));


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

  417.         sqlQueryObject = sqlQueryObject.newSQLQueryObject();
  418.         sqlQueryObject.setANDLogicOperator(true);

  419.         sqlQueryObject.addFromTable(this.getAllarmeNotificaFieldConverter().toTable(AllarmeNotifica.model()));
  420.         sqlQueryObject.addSelectField(this.getAllarmeNotificaFieldConverter().toColumn(AllarmeNotifica.model().DATA_NOTIFICA,true));
  421.         sqlQueryObject.addWhereCondition("id=?");


  422.         // Exists allarmeNotifica
  423.         existsAllarmeNotifica = jdbcUtilities.exists(sqlQueryObject.createSQLQuery(), jdbcProperties.isShowSql(),
  424.             new JDBCObject(tableId,Long.class));

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

  453.         // AllarmeNotifica.model()
  454.         mapTableToPKColumn.put(converter.toTable(AllarmeNotifica.model()),
  455.             utilities.newList(
  456.                 new CustomField("id", Long.class, "id", converter.toTable(AllarmeNotifica.model()))
  457.             ));

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

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

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

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

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

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

  517.         InUse inUse = new InUse();
  518.         inUse.setInUse(false);
  519.        
  520.         /*
  521.          * TODO: implement code that checks whether the object identified by the id parameter is used by other objects
  522.         */
  523.        
  524.         // Delete this line when you have implemented the method
  525.         int throwNotImplemented = 1;
  526.         if(throwNotImplemented==1){
  527.                 throw new NotImplementedException("NotImplemented");
  528.         }
  529.         // Delete this line when you have implemented the method

  530.         return inUse;

  531.     }
  532.    

  533.    
  534.     @Override
  535.     public List<List<Object>> nativeQuery(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject,
  536.                                             String sql,List<Class<?>> returnClassTypes,Object ... param) throws ServiceException,NotFoundException,NotImplementedException,Exception{
  537.        
  538.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeQuery(jdbcProperties, log, connection, sqlQueryObject,
  539.                                                                                             sql,returnClassTypes,param);
  540.                                                        
  541.     }
  542.    
  543. }