JDBCEventoServiceImpl.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.eventi.dao.jdbc;

  21. import java.sql.Connection;

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

  23. import org.slf4j.Logger;

  24. import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceCRUDWithoutId;
  25. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  26. import org.openspcoop2.generic_project.beans.UpdateField;
  27. import org.openspcoop2.generic_project.beans.UpdateModel;

  28. import org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities;
  29. import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
  30. import org.openspcoop2.generic_project.exception.NotFoundException;
  31. import org.openspcoop2.generic_project.exception.NotImplementedException;
  32. import org.openspcoop2.generic_project.exception.ServiceException;
  33. import org.openspcoop2.generic_project.expression.IExpression;
  34. import org.openspcoop2.generic_project.dao.jdbc.JDBCExpression;
  35. import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;

  36. import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;

  37. import org.openspcoop2.core.eventi.Evento;

  38. /**    
  39.  * JDBCEventoServiceImpl
  40.  *
  41.  * @author Poli Andrea (poli@link.it)
  42.  * @author $Author$
  43.  * @version $Rev$, $Date$
  44.  */
  45. public class JDBCEventoServiceImpl extends JDBCEventoServiceSearchImpl
  46.     implements IJDBCServiceCRUDWithoutId<Evento, JDBCServiceManager> {

  47.     @Override
  48.     public void create(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {

  49.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  50.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  51.        
  52.         ISQLQueryObject sqlQueryObjectInsert = sqlQueryObject.newSQLQueryObject();
  53.                


  54.         // Object evento
  55.         sqlQueryObjectInsert.addInsertTable(this.getEventoFieldConverter().toTable(Evento.model()));
  56.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().TIPO,false),"?");
  57.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().CODICE,false),"?");
  58.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().SEVERITA,false),"?");
  59.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().ORA_REGISTRAZIONE,false),"?");
  60.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().DESCRIZIONE,false),"?");
  61.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().ID_TRANSAZIONE,false),"?");
  62.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().ID_CONFIGURAZIONE,false),"?");
  63.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().CONFIGURAZIONE,false),"?");
  64.         sqlQueryObjectInsert.addInsertField(this.getEventoFieldConverter().toColumn(Evento.model().CLUSTER_ID,false),"?");

  65.         // Insert evento
  66.         org.openspcoop2.utils.jdbc.IKeyGeneratorObject keyGenerator = this.getEventoFetch().getKeyGeneratorObject(Evento.model());
  67.         long id = jdbcUtilities.insertAndReturnGeneratedKey(sqlQueryObjectInsert, keyGenerator, jdbcProperties.isShowSql(),
  68.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getTipo(),Evento.model().TIPO.getFieldType()),
  69.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getCodice(),Evento.model().CODICE.getFieldType()),
  70.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getSeverita(),Evento.model().SEVERITA.getFieldType()),
  71.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getOraRegistrazione(),Evento.model().ORA_REGISTRAZIONE.getFieldType()),
  72.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getDescrizione(),Evento.model().DESCRIZIONE.getFieldType()),
  73.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getIdTransazione(),Evento.model().ID_TRANSAZIONE.getFieldType()),
  74.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getIdConfigurazione(),Evento.model().ID_CONFIGURAZIONE.getFieldType()),
  75.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getConfigurazione(),Evento.model().CONFIGURAZIONE.getFieldType()),
  76.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(evento.getClusterId(),Evento.model().CLUSTER_ID.getFieldType())
  77.         );
  78.         evento.setId(id);

  79.        
  80.     }

  81.     @Override
  82.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  83.        
  84.         Long tableId = evento.getId();
  85.         if(tableId==null || tableId<=0){
  86.             throw new Exception("Retrieve tableId failed");
  87.         }

  88.         this.update(jdbcProperties, log, connection, sqlQueryObject, tableId, evento, idMappingResolutionBehaviour);
  89.     }
  90.     @Override
  91.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, Evento evento, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  92.    
  93.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  94.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  95.        
  96.         ISQLQueryObject sqlQueryObjectInsert = sqlQueryObject.newSQLQueryObject();
  97.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObjectInsert.newSQLQueryObject();
  98.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObjectDelete.newSQLQueryObject();
  99.         ISQLQueryObject sqlQueryObjectUpdate = sqlQueryObjectGet.newSQLQueryObject();
  100.        


  101.         // Object evento
  102.         sqlQueryObjectUpdate.setANDLogicOperator(true);
  103.         sqlQueryObjectUpdate.addUpdateTable(this.getEventoFieldConverter().toTable(Evento.model()));
  104.         boolean isUpdate_evento = true;
  105.         java.util.List<JDBCObject> lstObjects_evento = new java.util.ArrayList<>();
  106.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().TIPO,false), "?");
  107.         lstObjects_evento.add(new JDBCObject(evento.getTipo(), Evento.model().TIPO.getFieldType()));
  108.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().CODICE,false), "?");
  109.         lstObjects_evento.add(new JDBCObject(evento.getCodice(), Evento.model().CODICE.getFieldType()));
  110.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().SEVERITA,false), "?");
  111.         lstObjects_evento.add(new JDBCObject(evento.getSeverita(), Evento.model().SEVERITA.getFieldType()));
  112.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().ORA_REGISTRAZIONE,false), "?");
  113.         lstObjects_evento.add(new JDBCObject(evento.getOraRegistrazione(), Evento.model().ORA_REGISTRAZIONE.getFieldType()));
  114.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().DESCRIZIONE,false), "?");
  115.         lstObjects_evento.add(new JDBCObject(evento.getDescrizione(), Evento.model().DESCRIZIONE.getFieldType()));
  116.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().ID_TRANSAZIONE,false), "?");
  117.         lstObjects_evento.add(new JDBCObject(evento.getIdTransazione(), Evento.model().ID_TRANSAZIONE.getFieldType()));
  118.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().ID_CONFIGURAZIONE,false), "?");
  119.         lstObjects_evento.add(new JDBCObject(evento.getIdConfigurazione(), Evento.model().ID_CONFIGURAZIONE.getFieldType()));
  120.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().CONFIGURAZIONE,false), "?");
  121.         lstObjects_evento.add(new JDBCObject(evento.getConfigurazione(), Evento.model().CONFIGURAZIONE.getFieldType()));
  122.         sqlQueryObjectUpdate.addUpdateField(this.getEventoFieldConverter().toColumn(Evento.model().CLUSTER_ID,false), "?");
  123.         lstObjects_evento.add(new JDBCObject(evento.getClusterId(), Evento.model().CLUSTER_ID.getFieldType()));
  124.         sqlQueryObjectUpdate.addWhereCondition("id=?");
  125.         lstObjects_evento.add(new JDBCObject(tableId, Long.class));

  126.         if(isUpdate_evento) {
  127.             // Update evento
  128.             jdbcUtilities.executeUpdate(sqlQueryObjectUpdate.createSQLUpdate(), jdbcProperties.isShowSql(),
  129.                 lstObjects_evento.toArray(new JDBCObject[]{}));
  130.         }


  131.     }
  132.    
  133.     @Override
  134.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  135.        
  136.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  137.                 this.getEventoFieldConverter().toTable(Evento.model()),
  138.                 this.getMapTableToPKColumnEngine(),
  139.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, evento),
  140.                 this.getEventoFieldConverter(), this, null, updateFields);
  141.     }
  142.    
  143.     @Override
  144.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento, IExpression condition, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  145.        
  146.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  147.                 this.getEventoFieldConverter().toTable(Evento.model()),
  148.                 this.getMapTableToPKColumnEngine(),
  149.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, evento),
  150.                 this.getEventoFieldConverter(), this, condition, updateFields);
  151.     }
  152.    
  153.     @Override
  154.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento, UpdateModel ... updateModels) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  155.        
  156.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  157.                 this.getEventoFieldConverter().toTable(Evento.model()),
  158.                 this.getMapTableToPKColumnEngine(),
  159.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, evento),
  160.                 this.getEventoFieldConverter(), this, updateModels);
  161.     }  
  162.    
  163.     @Override
  164.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  165.         java.util.List<Object> ids = new java.util.ArrayList<>();
  166.         ids.add(tableId);
  167.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  168.                 this.getEventoFieldConverter().toTable(Evento.model()),
  169.                 this.getMapTableToPKColumnEngine(),
  170.                 ids,
  171.                 this.getEventoFieldConverter(), this, null, updateFields);
  172.     }
  173.    
  174.     @Override
  175.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, IExpression condition, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  176.         java.util.List<Object> ids = new java.util.ArrayList<>();
  177.         ids.add(tableId);
  178.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  179.                 this.getEventoFieldConverter().toTable(Evento.model()),
  180.                 this.getMapTableToPKColumnEngine(),
  181.                 ids,
  182.                 this.getEventoFieldConverter(), this, condition, updateFields);
  183.     }
  184.    
  185.     @Override
  186.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, UpdateModel ... updateModels) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  187.         java.util.List<Object> ids = new java.util.ArrayList<>();
  188.         ids.add(tableId);
  189.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  190.                 this.getEventoFieldConverter().toTable(Evento.model()),
  191.                 this.getMapTableToPKColumnEngine(),
  192.                 ids,
  193.                 this.getEventoFieldConverter(), this, updateModels);
  194.     }
  195.    
  196.     @Override
  197.     public void updateOrCreate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {
  198.    
  199.         Long id = evento.getId();
  200.         if(id != null && this.exists(jdbcProperties, log, connection, sqlQueryObject, id)) {
  201.             this.update(jdbcProperties, log, connection, sqlQueryObject, evento,idMappingResolutionBehaviour);      
  202.         } else {
  203.             this.create(jdbcProperties, log, connection, sqlQueryObject, evento,idMappingResolutionBehaviour);
  204.         }
  205.        
  206.     }
  207.    
  208.     @Override
  209.     public void updateOrCreate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, Evento evento, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {
  210.         if(this.exists(jdbcProperties, log, connection, sqlQueryObject, tableId)) {
  211.             this.update(jdbcProperties, log, connection, sqlQueryObject, tableId, evento,idMappingResolutionBehaviour);
  212.         } else {
  213.             this.create(jdbcProperties, log, connection, sqlQueryObject, evento,idMappingResolutionBehaviour);
  214.         }
  215.     }
  216.    
  217.     @Override
  218.     public void delete(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Evento evento) throws NotImplementedException,ServiceException,Exception {
  219.        
  220.        
  221.         Long longId = null;
  222.         if(evento.getId()==null){
  223.             throw new Exception("Parameter "+evento.getClass().getName()+".id is null");
  224.         }
  225.         if(evento.getId()<=0){
  226.             throw new Exception("Parameter "+evento.getClass().getName()+".id is less equals 0");
  227.         }
  228.         longId = evento.getId();
  229.        
  230.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, longId);
  231.        
  232.     }

  233.     private void deleteEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long id) throws NotImplementedException,ServiceException,Exception {
  234.    
  235.         if(id!=null && id.longValue()<=0){
  236.             throw new ServiceException("Id is less equals 0");
  237.         }
  238.        
  239.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  240.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  241.        
  242.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObject.newSQLQueryObject();
  243.        

  244.         // Object evento
  245.         sqlQueryObjectDelete.setANDLogicOperator(true);
  246.         sqlQueryObjectDelete.addDeleteTable(this.getEventoFieldConverter().toTable(Evento.model()));
  247.         if(id != null)
  248.             sqlQueryObjectDelete.addWhereCondition("id=?");

  249.         // Delete evento
  250.         jdbcUtilities.execute(sqlQueryObjectDelete.createSQLDelete(), jdbcProperties.isShowSql(),
  251.             new JDBCObject(id,Long.class));

  252.     }

  253.    
  254.     @Override
  255.     public NonNegativeNumber deleteAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject) throws NotImplementedException,ServiceException,Exception {
  256.        
  257.         return this.deleteAll(jdbcProperties, log, connection, sqlQueryObject, new JDBCExpression(this.getEventoFieldConverter()));

  258.     }

  259.     @Override
  260.     public NonNegativeNumber deleteAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, JDBCExpression expression) throws NotImplementedException, ServiceException,Exception {

  261.         java.util.List<Long> lst = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, new JDBCPaginatedExpression(expression));
  262.        
  263.         for(Long id : lst) {
  264.             this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  265.         }
  266.        
  267.         return new NonNegativeNumber(lst.size());
  268.    
  269.     }



  270.     // -- DB
  271.    
  272.     @Override
  273.     public void deleteById(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws ServiceException, NotImplementedException, Exception {
  274.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  275.     }
  276.    
  277.     @Override
  278.     public int nativeUpdate(JDBCServiceManagerProperties jdbcProperties, Logger log,Connection connection,ISQLQueryObject sqlObject, String sql,Object ... param) throws ServiceException,NotImplementedException, Exception {
  279.    
  280.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeUpdate(jdbcProperties, log, connection, sqlObject,
  281.                                                                                             sql,param);
  282.    
  283.     }
  284. }