JDBCConfigurazioneServizioServiceImpl.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.plugins.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.IJDBCServiceCRUDWithId;
  25. import org.openspcoop2.core.plugins.IdConfigurazioneServizio;
  26. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  27. import org.openspcoop2.generic_project.beans.UpdateField;
  28. import org.openspcoop2.generic_project.beans.UpdateModel;

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

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

  38. import org.openspcoop2.core.plugins.ConfigurazioneServizio;

  39. /**    
  40.  * JDBCConfigurazioneServizioServiceImpl
  41.  *
  42.  * @author Poli Andrea (poli@link.it)
  43.  * @author $Author$
  44.  * @version $Rev$, $Date$
  45.  */
  46. public class JDBCConfigurazioneServizioServiceImpl extends JDBCConfigurazioneServizioServiceSearchImpl
  47.     implements IJDBCServiceCRUDWithId<ConfigurazioneServizio, IdConfigurazioneServizio, JDBCServiceManager> {

  48.     @Override
  49.     public void create(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneServizio configurazioneServizio, 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 configurazioneServizio
  56.         sqlQueryObjectInsert.addInsertTable(this.getConfigurazioneServizioFieldConverter().toTable(ConfigurazioneServizio.model()));
  57.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().ACCORDO,false),"?");
  58.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().TIPO_SOGGETTO_REFERENTE,false),"?");
  59.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().NOME_SOGGETTO_REFERENTE,false),"?");
  60.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().VERSIONE,false),"?");
  61.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().SERVIZIO,false),"?");

  62.         // Insert configurazioneServizio
  63.         org.openspcoop2.utils.jdbc.IKeyGeneratorObject keyGenerator = this.getConfigurazioneServizioFetch().getKeyGeneratorObject(ConfigurazioneServizio.model());
  64.         long id = jdbcUtilities.insertAndReturnGeneratedKey(sqlQueryObjectInsert, keyGenerator, jdbcProperties.isShowSql(),
  65.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneServizio.getAccordo(),ConfigurazioneServizio.model().ACCORDO.getFieldType()),
  66.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneServizio.getTipoSoggettoReferente(),ConfigurazioneServizio.model().TIPO_SOGGETTO_REFERENTE.getFieldType()),
  67.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneServizio.getNomeSoggettoReferente(),ConfigurazioneServizio.model().NOME_SOGGETTO_REFERENTE.getFieldType()),
  68.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneServizio.getVersione(),ConfigurazioneServizio.model().VERSIONE.getFieldType()),
  69.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneServizio.getServizio(),ConfigurazioneServizio.model().SERVIZIO.getFieldType())
  70.         );
  71.         configurazioneServizio.setId(id);

  72.        
  73.     }

  74.     @Override
  75.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneServizio oldId, ConfigurazioneServizio configurazioneServizio, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  76.         ISQLQueryObject sqlQueryObjectUpdate = sqlQueryObject.newSQLQueryObject();
  77.         Long longIdByLogicId = this.findIdConfigurazioneServizio(jdbcProperties, log, connection, sqlQueryObjectUpdate.newSQLQueryObject(), oldId, true);
  78.         Long tableId = configurazioneServizio.getId();
  79.         if(tableId != null && tableId.longValue() > 0) {
  80.             if(tableId.longValue() != longIdByLogicId.longValue()) {
  81.                 throw new Exception("Ambiguous parameter: configurazioneServizio.id ["+tableId+"] does not match logic id ["+longIdByLogicId+"]");
  82.             }
  83.         } else {
  84.             tableId = longIdByLogicId;
  85.             configurazioneServizio.setId(tableId);
  86.         }
  87.         if(tableId==null || tableId<=0){
  88.             throw new Exception("Retrieve tableId failed");
  89.         }

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


  103.         // Object configurazioneServizio
  104.         sqlQueryObjectUpdate.setANDLogicOperator(true);
  105.         sqlQueryObjectUpdate.addUpdateTable(this.getConfigurazioneServizioFieldConverter().toTable(ConfigurazioneServizio.model()));
  106.         boolean isUpdate_configurazioneServizio = true;
  107.         java.util.List<JDBCObject> lstObjects_configurazioneServizio = new java.util.ArrayList<>();
  108.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().ACCORDO,false), "?");
  109.         lstObjects_configurazioneServizio.add(new JDBCObject(configurazioneServizio.getAccordo(), ConfigurazioneServizio.model().ACCORDO.getFieldType()));
  110.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().TIPO_SOGGETTO_REFERENTE,false), "?");
  111.         lstObjects_configurazioneServizio.add(new JDBCObject(configurazioneServizio.getTipoSoggettoReferente(), ConfigurazioneServizio.model().TIPO_SOGGETTO_REFERENTE.getFieldType()));
  112.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().NOME_SOGGETTO_REFERENTE,false), "?");
  113.         lstObjects_configurazioneServizio.add(new JDBCObject(configurazioneServizio.getNomeSoggettoReferente(), ConfigurazioneServizio.model().NOME_SOGGETTO_REFERENTE.getFieldType()));
  114.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().VERSIONE,false), "?");
  115.         lstObjects_configurazioneServizio.add(new JDBCObject(configurazioneServizio.getVersione(), ConfigurazioneServizio.model().VERSIONE.getFieldType()));
  116.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneServizioFieldConverter().toColumn(ConfigurazioneServizio.model().SERVIZIO,false), "?");
  117.         lstObjects_configurazioneServizio.add(new JDBCObject(configurazioneServizio.getServizio(), ConfigurazioneServizio.model().SERVIZIO.getFieldType()));
  118.         sqlQueryObjectUpdate.addWhereCondition("id=?");
  119.         lstObjects_configurazioneServizio.add(new JDBCObject(tableId, Long.class));

  120.         if(isUpdate_configurazioneServizio) {
  121.             // Update configurazioneServizio
  122.             jdbcUtilities.executeUpdate(sqlQueryObjectUpdate.createSQLUpdate(), jdbcProperties.isShowSql(),
  123.                 lstObjects_configurazioneServizio.toArray(new JDBCObject[]{}));
  124.         }


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

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

  240.         // Object configurazioneServizio
  241.         sqlQueryObjectDelete.setANDLogicOperator(true);
  242.         sqlQueryObjectDelete.addDeleteTable(this.getConfigurazioneServizioFieldConverter().toTable(ConfigurazioneServizio.model()));
  243.         if(id != null)
  244.             sqlQueryObjectDelete.addWhereCondition("id=?");

  245.         // Delete configurazioneServizio
  246.         jdbcUtilities.execute(sqlQueryObjectDelete.createSQLDelete(), jdbcProperties.isShowSql(),
  247.             new JDBCObject(id,Long.class));

  248.     }

  249.     @Override
  250.     public void deleteById(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdConfigurazioneServizio idConfigurazioneServizio) throws NotImplementedException,ServiceException,Exception {

  251.         Long id = null;
  252.         try{
  253.             id = this.findIdConfigurazioneServizio(jdbcProperties, log, connection, sqlQueryObject, idConfigurazioneServizio, true);
  254.         }catch(NotFoundException notFound){
  255.             return;
  256.         }
  257.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  258.        
  259.     }
  260.    
  261.     @Override
  262.     public NonNegativeNumber deleteAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject) throws NotImplementedException,ServiceException,Exception {
  263.        
  264.         return this.deleteAll(jdbcProperties, log, connection, sqlQueryObject, new JDBCExpression(this.getConfigurazioneServizioFieldConverter()));

  265.     }

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

  268.         java.util.List<Long> lst = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, new JDBCPaginatedExpression(expression));
  269.        
  270.         for(Long id : lst) {
  271.             this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  272.         }
  273.        
  274.         return new NonNegativeNumber(lst.size());
  275.    
  276.     }



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