JDBCStatisticaInfoServiceImpl.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.statistiche.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.statistiche.StatisticaInfo;
  38. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;

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

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

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


  55.         // Object statisticaInfo
  56.         sqlQueryObjectInsert.addInsertTable(this.getStatisticaInfoFieldConverter().toTable(StatisticaInfo.model()));
  57.         sqlQueryObjectInsert.addInsertField(this.getStatisticaInfoFieldConverter().toColumn(StatisticaInfo.model().TIPO_STATISTICA,false),"?");
  58.         sqlQueryObjectInsert.addInsertField(this.getStatisticaInfoFieldConverter().toColumn(StatisticaInfo.model().DATA_ULTIMA_GENERAZIONE,false),"?");

  59.         // Insert statisticaInfo
  60.         String insertSql = sqlQueryObjectInsert.createSQLInsert();
  61.         jdbcUtilities.execute(insertSql, jdbcProperties.isShowSql(),
  62.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(statisticaInfo.getTipoStatistica(),StatisticaInfo.model().TIPO_STATISTICA.getFieldType()),
  63.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(statisticaInfo.getDataUltimaGenerazione(),StatisticaInfo.model().DATA_ULTIMA_GENERAZIONE.getFieldType())
  64.         );

  65.        
  66.     }

  67.     @Override
  68.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, StatisticaInfo statisticaInfo, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  69.        

  70.    
  71.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  72.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  73.        
  74.         ISQLQueryObject sqlQueryObjectInsert = sqlQueryObject.newSQLQueryObject();
  75.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObjectInsert.newSQLQueryObject();
  76.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObjectDelete.newSQLQueryObject();
  77.         ISQLQueryObject sqlQueryObjectUpdate = sqlQueryObjectGet.newSQLQueryObject();
  78.        


  79.         // Object statisticaInfo
  80.         sqlQueryObjectUpdate.setANDLogicOperator(true);
  81.         sqlQueryObjectUpdate.addUpdateTable(this.getStatisticaInfoFieldConverter().toTable(StatisticaInfo.model()));
  82.         boolean isUpdate = true;
  83.         java.util.List<JDBCObject> lstObjects = new java.util.ArrayList<>();
  84.         sqlQueryObjectUpdate.addUpdateField(this.getStatisticaInfoFieldConverter().toColumn(StatisticaInfo.model().TIPO_STATISTICA,false), "?");
  85.         lstObjects.add(new JDBCObject(statisticaInfo.getTipoStatistica(), StatisticaInfo.model().TIPO_STATISTICA.getFieldType()));
  86.         sqlQueryObjectUpdate.addUpdateField(this.getStatisticaInfoFieldConverter().toColumn(StatisticaInfo.model().DATA_ULTIMA_GENERAZIONE,false), "?");
  87.         lstObjects.add(new JDBCObject(statisticaInfo.getDataUltimaGenerazione(), StatisticaInfo.model().DATA_ULTIMA_GENERAZIONE.getFieldType()));
  88.         sqlQueryObjectUpdate.addWhereCondition(this.getStatisticaInfoFieldConverter().toColumn(StatisticaInfo.model().TIPO_STATISTICA,false)+"=?");
  89.         lstObjects.add(new JDBCObject(statisticaInfo.getTipoStatisticaRawEnumValue(), String.class));

  90.         if(isUpdate) {
  91.             // Update statisticaInfo
  92.             jdbcUtilities.executeUpdate(sqlQueryObjectUpdate.createSQLUpdate(), jdbcProperties.isShowSql(),
  93.                 lstObjects.toArray(new JDBCObject[]{}));
  94.         }

  95.     }
  96.     @Override
  97.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, StatisticaInfo statisticaInfo, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  98.         throw new NotImplementedException("Table without long id column PK");
  99.     }
  100.    
  101.     @Override
  102.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, StatisticaInfo statisticaInfo, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  103.        
  104.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  105.                 this.getStatisticaInfoFieldConverter().toTable(StatisticaInfo.model()),
  106.                 this.getMapTableToPKColumnEngine(),
  107.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, statisticaInfo),
  108.                 this.getStatisticaInfoFieldConverter(), this, null, updateFields);
  109.     }
  110.    
  111.     @Override
  112.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, StatisticaInfo statisticaInfo, IExpression condition, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  113.        
  114.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  115.                 this.getStatisticaInfoFieldConverter().toTable(StatisticaInfo.model()),
  116.                 this.getMapTableToPKColumnEngine(),
  117.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, statisticaInfo),
  118.                 this.getStatisticaInfoFieldConverter(), this, condition, updateFields);
  119.     }
  120.    
  121.     @Override
  122.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, StatisticaInfo statisticaInfo, UpdateModel ... updateModels) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  123.        
  124.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  125.                 this.getStatisticaInfoFieldConverter().toTable(StatisticaInfo.model()),
  126.                 this.getMapTableToPKColumnEngine(),
  127.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, statisticaInfo),
  128.                 this.getStatisticaInfoFieldConverter(), this, updateModels);
  129.     }  
  130.    
  131.     @Override
  132.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  133.         throw new NotImplementedException("Table without long id column PK");
  134.     }
  135.    
  136.     @Override
  137.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, IExpression condition, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  138.         throw new NotImplementedException("Table without long id column PK");
  139.     }
  140.    
  141.     @Override
  142.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, UpdateModel ... updateModels) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  143.         throw new NotImplementedException("Table without long id column PK");
  144.     }
  145.    
  146.     @Override
  147.     public void updateOrCreate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, StatisticaInfo statisticaInfo, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {
  148.    
  149. //      Long id = statisticaInfo.getId();
  150. //      if(id != null && this.exists(jdbcProperties, log, connection, sqlQueryObject, id)) {
  151.         this.update(jdbcProperties, log, connection, sqlQueryObject, statisticaInfo,idMappingResolutionBehaviour);
  152. //      } else {
  153. //          this.create(jdbcProperties, log, connection, sqlQueryObject, statisticaInfo,idMappingResolutionBehaviour);
  154. //      }
  155.        
  156.     }
  157.    
  158.     @Override
  159.     public void updateOrCreate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, StatisticaInfo statisticaInfo, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {
  160.         throw new NotImplementedException("Table without long id column PK");
  161.     }
  162.    
  163.     @Override
  164.     public void delete(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, StatisticaInfo statisticaInfo) throws NotImplementedException,ServiceException,Exception {
  165.        
  166.         TipoIntervalloStatistico idObject = statisticaInfo.getTipoStatistica();
  167.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, idObject);
  168.                

  169.     }

  170.     private void deleteEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Object id) throws NotImplementedException,ServiceException,Exception {
  171.        
  172.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  173.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  174.        
  175.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObject.newSQLQueryObject();
  176.        
  177.         if(id == null ){
  178.             throw new ServiceException("ID Logico non trovato");
  179.         }
  180.         if(id instanceof TipoIntervalloStatistico == false){
  181.             throw new ServiceException("Tipo dell'id non valido, atteso["+TipoIntervalloStatistico.class.getName()+"] trovato["+id.getClass().getName()+"]");
  182.         }
  183.         TipoIntervalloStatistico tipoStatistica = (TipoIntervalloStatistico) id;

  184.         // Object statisticaInfo
  185.         sqlQueryObjectDelete.setANDLogicOperator(true);
  186.         sqlQueryObjectDelete.addDeleteTable(this.getStatisticaInfoFieldConverter().toTable(StatisticaInfo.model()));
  187.         sqlQueryObjectDelete.addWhereCondition(this.getStatisticaInfoFieldConverter().toColumn(StatisticaInfo.model().TIPO_STATISTICA,false)+"=?");

  188.         // Delete statisticaInfo
  189.         jdbcUtilities.execute(sqlQueryObjectDelete.createSQLDelete(), jdbcProperties.isShowSql(),
  190.             new JDBCObject(tipoStatistica.getValue(),String.class));

  191.     }

  192.    
  193.     @Override
  194.     public NonNegativeNumber deleteAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject) throws NotImplementedException,ServiceException,Exception {
  195.        
  196.         return this.deleteAll(jdbcProperties, log, connection, sqlQueryObject, new JDBCExpression(this.getStatisticaInfoFieldConverter()));

  197.     }

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

  200.         java.util.List<Object> lst = this._findAllObjectIds(jdbcProperties, log, connection, sqlQueryObject, new JDBCPaginatedExpression(expression));
  201.        
  202.         for(Object id : lst) {
  203.             this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  204.         }
  205.        
  206.         return new NonNegativeNumber(lst.size());
  207.    
  208.     }



  209.     // -- DB
  210.    
  211.     @Override
  212.     public void deleteById(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws ServiceException, NotImplementedException, Exception {
  213.         throw new NotImplementedException("Table without long id column PK");
  214.     }
  215.    
  216.     @Override
  217.     public int nativeUpdate(JDBCServiceManagerProperties jdbcProperties, Logger log,Connection connection,ISQLQueryObject sqlObject, String sql,Object ... param) throws ServiceException,NotImplementedException, Exception {
  218.    
  219.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeUpdate(jdbcProperties, log, connection, sqlObject,
  220.                                                                                             sql,param);
  221.    
  222.     }
  223. }