JDBCConfigurazioneRateLimitingProprietaServiceImpl.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.controllo_traffico.dao.jdbc;

  21. import java.sql.Connection;

  22. import org.openspcoop2.core.controllo_traffico.ConfigurazioneRateLimitingProprieta;
  23. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  24. import org.openspcoop2.generic_project.beans.UpdateField;
  25. import org.openspcoop2.generic_project.beans.UpdateModel;
  26. import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceCRUDWithoutId;
  27. import org.openspcoop2.generic_project.dao.jdbc.JDBCExpression;
  28. import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;
  29. import org.openspcoop2.generic_project.dao.jdbc.JDBCServiceManagerProperties;
  30. import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
  31. import org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities;
  32. import org.openspcoop2.generic_project.exception.NotFoundException;
  33. import org.openspcoop2.generic_project.exception.NotImplementedException;
  34. import org.openspcoop2.generic_project.exception.ServiceException;
  35. import org.openspcoop2.generic_project.expression.IExpression;
  36. import org.openspcoop2.utils.sql.ISQLQueryObject;
  37. import org.slf4j.Logger;

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

  47.     @Override
  48.     public void create(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, 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 configurazioneRateLimitingProprieta
  55.         sqlQueryObjectInsert.addInsertTable(this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()));
  56.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneRateLimitingProprietaFieldConverter().toColumn(ConfigurazioneRateLimitingProprieta.model().NOME,false),"?");
  57.         sqlQueryObjectInsert.addInsertField(this.getConfigurazioneRateLimitingProprietaFieldConverter().toColumn(ConfigurazioneRateLimitingProprieta.model().VALORE,false),"?");

  58.         // Insert configurazioneRateLimitingProprieta
  59.         org.openspcoop2.utils.jdbc.IKeyGeneratorObject keyGenerator = this.getConfigurazioneRateLimitingProprietaFetch().getKeyGeneratorObject(ConfigurazioneRateLimitingProprieta.model());
  60.         long id = jdbcUtilities.insertAndReturnGeneratedKey(sqlQueryObjectInsert, keyGenerator, jdbcProperties.isShowSql(),
  61.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneRateLimitingProprieta.getNome(),ConfigurazioneRateLimitingProprieta.model().NOME.getFieldType()),
  62.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(configurazioneRateLimitingProprieta.getValore(),ConfigurazioneRateLimitingProprieta.model().VALORE.getFieldType())
  63.         );
  64.         configurazioneRateLimitingProprieta.setId(id);

  65.        
  66.     }

  67.     @Override
  68.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  69.        
  70.         Long tableId = configurazioneRateLimitingProprieta.getId();
  71.         if(tableId==null || tableId<=0){
  72.             throw new Exception("Retrieve tableId failed");
  73.         }

  74.         this.update(jdbcProperties, log, connection, sqlQueryObject, tableId, configurazioneRateLimitingProprieta, idMappingResolutionBehaviour);
  75.     }
  76.     @Override
  77.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  78.    
  79.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  80.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  81.        
  82.         ISQLQueryObject sqlQueryObjectInsert = sqlQueryObject.newSQLQueryObject();
  83.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObjectInsert.newSQLQueryObject();
  84.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObjectDelete.newSQLQueryObject();
  85.         ISQLQueryObject sqlQueryObjectUpdate = sqlQueryObjectGet.newSQLQueryObject();
  86.        


  87.         // Object configurazioneRateLimitingProprieta
  88.         sqlQueryObjectUpdate.setANDLogicOperator(true);
  89.         sqlQueryObjectUpdate.addUpdateTable(this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()));
  90.         boolean isUpdate = true;
  91.         java.util.List<JDBCObject> lstObjects = new java.util.ArrayList<>();
  92.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneRateLimitingProprietaFieldConverter().toColumn(ConfigurazioneRateLimitingProprieta.model().NOME,false), "?");
  93.         lstObjects.add(new JDBCObject(configurazioneRateLimitingProprieta.getNome(), ConfigurazioneRateLimitingProprieta.model().NOME.getFieldType()));
  94.         sqlQueryObjectUpdate.addUpdateField(this.getConfigurazioneRateLimitingProprietaFieldConverter().toColumn(ConfigurazioneRateLimitingProprieta.model().VALORE,false), "?");
  95.         lstObjects.add(new JDBCObject(configurazioneRateLimitingProprieta.getValore(), ConfigurazioneRateLimitingProprieta.model().VALORE.getFieldType()));
  96.         sqlQueryObjectUpdate.addWhereCondition("id=?");
  97.         lstObjects.add(new JDBCObject(tableId, Long.class));

  98.         if(isUpdate) {
  99.             // Update configurazioneRateLimitingProprieta
  100.             jdbcUtilities.executeUpdate(sqlQueryObjectUpdate.createSQLUpdate(), jdbcProperties.isShowSql(),
  101.                 lstObjects.toArray(new JDBCObject[]{}));
  102.         }


  103.     }
  104.    
  105.     @Override
  106.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  107.        
  108.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  109.                 this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()),
  110.                 this.getMapTableToPKColumnEngine(),
  111.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, configurazioneRateLimitingProprieta),
  112.                 this.getConfigurazioneRateLimitingProprietaFieldConverter(), this, null, updateFields);
  113.     }
  114.    
  115.     @Override
  116.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, IExpression condition, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  117.        
  118.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  119.                 this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()),
  120.                 this.getMapTableToPKColumnEngine(),
  121.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, configurazioneRateLimitingProprieta),
  122.                 this.getConfigurazioneRateLimitingProprietaFieldConverter(), this, condition, updateFields);
  123.     }
  124.    
  125.     @Override
  126.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, UpdateModel ... updateModels) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  127.        
  128.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  129.                 this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()),
  130.                 this.getMapTableToPKColumnEngine(),
  131.                 this.getRootTablePrimaryKeyValuesEngine(jdbcProperties, log, connection, sqlQueryObject, configurazioneRateLimitingProprieta),
  132.                 this.getConfigurazioneRateLimitingProprietaFieldConverter(), this, updateModels);
  133.     }  
  134.    
  135.     @Override
  136.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  137.         java.util.List<Object> ids = new java.util.ArrayList<>();
  138.         ids.add(tableId);
  139.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  140.                 this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()),
  141.                 this.getMapTableToPKColumnEngine(),
  142.                 ids,
  143.                 this.getConfigurazioneRateLimitingProprietaFieldConverter(), this, null, updateFields);
  144.     }
  145.    
  146.     @Override
  147.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, IExpression condition, UpdateField ... updateFields) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  148.         java.util.List<Object> ids = new java.util.ArrayList<>();
  149.         ids.add(tableId);
  150.         GenericJDBCUtilities.updateFields(jdbcProperties, log, connection, sqlQueryObject,
  151.                 this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()),
  152.                 this.getMapTableToPKColumnEngine(),
  153.                 ids,
  154.                 this.getConfigurazioneRateLimitingProprietaFieldConverter(), this, condition, updateFields);
  155.     }
  156.    
  157.     @Override
  158.     public void updateFields(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, UpdateModel ... updateModels) 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.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()),
  163.                 this.getMapTableToPKColumnEngine(),
  164.                 ids,
  165.                 this.getConfigurazioneRateLimitingProprietaFieldConverter(), this, updateModels);
  166.     }
  167.    
  168.     @Override
  169.     public void updateOrCreate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {
  170.    
  171.         Long id = configurazioneRateLimitingProprieta.getId();
  172.         if(id != null && this.exists(jdbcProperties, log, connection, sqlQueryObject, id)) {
  173.             this.update(jdbcProperties, log, connection, sqlQueryObject, configurazioneRateLimitingProprieta,idMappingResolutionBehaviour);    
  174.         } else {
  175.             this.create(jdbcProperties, log, connection, sqlQueryObject, configurazioneRateLimitingProprieta,idMappingResolutionBehaviour);
  176.         }
  177.        
  178.     }
  179.    
  180.     @Override
  181.     public void updateOrCreate(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotImplementedException,ServiceException,Exception {
  182.         if(this.exists(jdbcProperties, log, connection, sqlQueryObject, tableId)) {
  183.             this.update(jdbcProperties, log, connection, sqlQueryObject, tableId, configurazioneRateLimitingProprieta,idMappingResolutionBehaviour);
  184.         } else {
  185.             this.create(jdbcProperties, log, connection, sqlQueryObject, configurazioneRateLimitingProprieta,idMappingResolutionBehaviour);
  186.         }
  187.     }
  188.    
  189.     @Override
  190.     public void delete(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, ConfigurazioneRateLimitingProprieta configurazioneRateLimitingProprieta) throws NotImplementedException,ServiceException,Exception {
  191.        
  192.        
  193.         Long longId = null;
  194.         if(configurazioneRateLimitingProprieta.getId()==null){
  195.             throw new Exception("Parameter "+configurazioneRateLimitingProprieta.getClass().getName()+".id is null");
  196.         }
  197.         if(configurazioneRateLimitingProprieta.getId()<=0){
  198.             throw new Exception("Parameter "+configurazioneRateLimitingProprieta.getClass().getName()+".id is less equals 0");
  199.         }
  200.         longId = configurazioneRateLimitingProprieta.getId();
  201.        
  202.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, longId);
  203.        
  204.     }

  205.     private void deleteEngine(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, Long id) throws NotImplementedException,ServiceException,Exception {
  206.    
  207.         if(id!=null && id.longValue()<=0){
  208.             throw new ServiceException("Id is less equals 0");
  209.         }
  210.        
  211.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  212.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  213.        
  214.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObject.newSQLQueryObject();
  215.        

  216.         // Object configurazioneRateLimitingProprieta
  217.         sqlQueryObjectDelete.setANDLogicOperator(true);
  218.         sqlQueryObjectDelete.addDeleteTable(this.getConfigurazioneRateLimitingProprietaFieldConverter().toTable(ConfigurazioneRateLimitingProprieta.model()));
  219.         if(id != null)
  220.             sqlQueryObjectDelete.addWhereCondition("id=?");

  221.         // Delete configurazioneRateLimitingProprieta
  222.         jdbcUtilities.execute(sqlQueryObjectDelete.createSQLDelete(), jdbcProperties.isShowSql(),
  223.             new JDBCObject(id,Long.class));

  224.     }

  225.    
  226.     @Override
  227.     public NonNegativeNumber deleteAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject) throws NotImplementedException,ServiceException,Exception {
  228.        
  229.         return this.deleteAll(jdbcProperties, log, connection, sqlQueryObject, new JDBCExpression(this.getConfigurazioneRateLimitingProprietaFieldConverter()));

  230.     }

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

  233.         java.util.List<Long> lst = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, new JDBCPaginatedExpression(expression));
  234.        
  235.         for(Long id : lst) {
  236.             this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  237.         }
  238.        
  239.         return new NonNegativeNumber(lst.size());
  240.    
  241.     }



  242.     // -- DB
  243.    
  244.     @Override
  245.     public void deleteById(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId) throws ServiceException, NotImplementedException, Exception {
  246.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, Long.valueOf(tableId));
  247.     }
  248.    
  249.     @Override
  250.     public int nativeUpdate(JDBCServiceManagerProperties jdbcProperties, Logger log,Connection connection,ISQLQueryObject sqlObject, String sql,Object ... param) throws ServiceException,NotImplementedException, Exception {
  251.    
  252.         return org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities.nativeUpdate(jdbcProperties, log, connection, sqlObject,
  253.                                                                                             sql,param);
  254.    
  255.     }
  256. }