JDBCCredenzialeMittenteServiceImpl.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.transazioni.dao.jdbc;

  21. import java.sql.Connection;

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

  24. import org.slf4j.Logger;

  25. import org.openspcoop2.generic_project.dao.jdbc.IJDBCServiceCRUDWithId;
  26. import org.openspcoop2.core.transazioni.IdCredenzialeMittente;
  27. import org.openspcoop2.generic_project.beans.NonNegativeNumber;
  28. import org.openspcoop2.generic_project.beans.UpdateField;
  29. import org.openspcoop2.generic_project.beans.UpdateModel;

  30. import org.openspcoop2.generic_project.dao.jdbc.utils.GenericJDBCUtilities;
  31. import org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject;
  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.generic_project.dao.jdbc.JDBCExpression;
  37. import org.openspcoop2.generic_project.dao.jdbc.JDBCPaginatedExpression;

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

  39. import org.openspcoop2.core.transazioni.CredenzialeMittente;

  40. /**    
  41.  * JDBCCredenzialeMittenteServiceImpl
  42.  *
  43.  * @author Poli Andrea (poli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public class JDBCCredenzialeMittenteServiceImpl extends JDBCCredenzialeMittenteServiceSearchImpl
  48.     implements IJDBCServiceCRUDWithId<CredenzialeMittente, IdCredenzialeMittente, JDBCServiceManager> {

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

  51.         if(TipiDatabase.POSTGRESQL.equals(jdbcProperties.getDatabase()) && org.openspcoop2.utils.jdbc.PostgreSQLUtilities.containsNullByteSequence(credenzialeMittente.getCredenziale())){
  52.             credenzialeMittente.setCredenziale(org.openspcoop2.utils.jdbc.PostgreSQLUtilities.normalizeString(credenzialeMittente.getCredenziale()));
  53.         }
  54.        
  55.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  56.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  57.        
  58.         ISQLQueryObject sqlQueryObjectInsert = sqlQueryObject.newSQLQueryObject();
  59.                


  60.         // Object credenzialeMittente
  61.         sqlQueryObjectInsert.addInsertTable(this.getCredenzialeMittenteFieldConverter().toTable(CredenzialeMittente.model()));
  62.         sqlQueryObjectInsert.addInsertField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().TIPO,false),"?");
  63.         sqlQueryObjectInsert.addInsertField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().CREDENZIALE,false),"?");
  64.         sqlQueryObjectInsert.addInsertField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().ORA_REGISTRAZIONE,false),"?");
  65.         sqlQueryObjectInsert.addInsertField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().REF_CREDENZIALE,false),"?");

  66.         // Insert credenzialeMittente
  67.         org.openspcoop2.utils.jdbc.IKeyGeneratorObject keyGenerator = this.getCredenzialeMittenteFetch().getKeyGeneratorObject(CredenzialeMittente.model());
  68.         long id = jdbcUtilities.insertAndReturnGeneratedKey(sqlQueryObjectInsert, keyGenerator, jdbcProperties.isShowSql(),
  69.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(credenzialeMittente.getTipo(),CredenzialeMittente.model().TIPO.getFieldType()),
  70.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(credenzialeMittente.getCredenziale(),CredenzialeMittente.model().CREDENZIALE.getFieldType()),
  71.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(credenzialeMittente.getOraRegistrazione(),CredenzialeMittente.model().ORA_REGISTRAZIONE.getFieldType()),
  72.             new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCObject(credenzialeMittente.getRefCredenziale(),CredenzialeMittente.model().REF_CREDENZIALE.getFieldType())
  73.         );
  74.         credenzialeMittente.setId(id);

  75.        
  76.     }

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

  93.         this.update(jdbcProperties, log, connection, sqlQueryObject, tableId, credenzialeMittente, idMappingResolutionBehaviour);
  94.     }
  95.     @Override
  96.     public void update(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, long tableId, CredenzialeMittente credenzialeMittente, org.openspcoop2.generic_project.beans.IDMappingBehaviour idMappingResolutionBehaviour) throws NotFoundException, NotImplementedException, ServiceException, Exception {
  97.    
  98.         if(TipiDatabase.POSTGRESQL.equals(jdbcProperties.getDatabase()) && org.openspcoop2.utils.jdbc.PostgreSQLUtilities.containsNullByteSequence(credenzialeMittente.getCredenziale())){
  99.             credenzialeMittente.setCredenziale(org.openspcoop2.utils.jdbc.PostgreSQLUtilities.normalizeString(credenzialeMittente.getCredenziale()));
  100.         }
  101.        
  102.         org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities jdbcUtilities =
  103.                 new org.openspcoop2.generic_project.dao.jdbc.utils.JDBCPreparedStatementUtilities(sqlQueryObject.getTipoDatabaseOpenSPCoop2(), log, connection);
  104.        
  105.         ISQLQueryObject sqlQueryObjectInsert = sqlQueryObject.newSQLQueryObject();
  106.         ISQLQueryObject sqlQueryObjectDelete = sqlQueryObjectInsert.newSQLQueryObject();
  107.         ISQLQueryObject sqlQueryObjectGet = sqlQueryObjectDelete.newSQLQueryObject();
  108.         ISQLQueryObject sqlQueryObjectUpdate = sqlQueryObjectGet.newSQLQueryObject();
  109.        


  110.         // Object credenzialeMittente
  111.         sqlQueryObjectUpdate.setANDLogicOperator(true);
  112.         sqlQueryObjectUpdate.addUpdateTable(this.getCredenzialeMittenteFieldConverter().toTable(CredenzialeMittente.model()));
  113.         boolean isUpdate_credenzialeMittente = true;
  114.         java.util.List<JDBCObject> lstObjects_credenzialeMittente = new java.util.ArrayList<>();
  115.         sqlQueryObjectUpdate.addUpdateField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().TIPO,false), "?");
  116.         lstObjects_credenzialeMittente.add(new JDBCObject(credenzialeMittente.getTipo(), CredenzialeMittente.model().TIPO.getFieldType()));
  117.         sqlQueryObjectUpdate.addUpdateField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().CREDENZIALE,false), "?");
  118.         lstObjects_credenzialeMittente.add(new JDBCObject(credenzialeMittente.getCredenziale(), CredenzialeMittente.model().CREDENZIALE.getFieldType()));
  119.         sqlQueryObjectUpdate.addUpdateField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().ORA_REGISTRAZIONE,false), "?");
  120.         lstObjects_credenzialeMittente.add(new JDBCObject(credenzialeMittente.getOraRegistrazione(), CredenzialeMittente.model().ORA_REGISTRAZIONE.getFieldType()));
  121.         sqlQueryObjectUpdate.addUpdateField(this.getCredenzialeMittenteFieldConverter().toColumn(CredenzialeMittente.model().REF_CREDENZIALE,false), "?");
  122.         lstObjects_credenzialeMittente.add(new JDBCObject(credenzialeMittente.getRefCredenziale(), CredenzialeMittente.model().REF_CREDENZIALE.getFieldType()));
  123.         sqlQueryObjectUpdate.addWhereCondition("id=?");
  124.         lstObjects_credenzialeMittente.add(new JDBCObject(tableId, Long.class));

  125.         if(isUpdate_credenzialeMittente) {
  126.             // Update credenzialeMittente
  127.             jdbcUtilities.executeUpdate(sqlQueryObjectUpdate.createSQLUpdate(), jdbcProperties.isShowSql(),
  128.                 lstObjects_credenzialeMittente.toArray(new JDBCObject[]{}));
  129.         }


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

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

  245.         // Object credenzialeMittente
  246.         sqlQueryObjectDelete.setANDLogicOperator(true);
  247.         sqlQueryObjectDelete.addDeleteTable(this.getCredenzialeMittenteFieldConverter().toTable(CredenzialeMittente.model()));
  248.         if(id != null)
  249.             sqlQueryObjectDelete.addWhereCondition("id=?");

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

  253.     }

  254.     @Override
  255.     public void deleteById(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject, IdCredenzialeMittente idCredenzialeMittente) throws NotImplementedException,ServiceException,Exception {

  256.         Long id = null;
  257.         try{
  258.             id = this.findIdCredenzialeMittente(jdbcProperties, log, connection, sqlQueryObject, idCredenzialeMittente, true);
  259.         }catch(NotFoundException notFound){
  260.             return;
  261.         }
  262.         this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  263.        
  264.     }
  265.    
  266.     @Override
  267.     public NonNegativeNumber deleteAll(JDBCServiceManagerProperties jdbcProperties, Logger log, Connection connection, ISQLQueryObject sqlQueryObject) throws NotImplementedException,ServiceException,Exception {
  268.        
  269.         return this.deleteAll(jdbcProperties, log, connection, sqlQueryObject, new JDBCExpression(this.getCredenzialeMittenteFieldConverter()));

  270.     }

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

  273.         java.util.List<Long> lst = this.findAllTableIds(jdbcProperties, log, connection, sqlQueryObject, new JDBCPaginatedExpression(expression));
  274.        
  275.         for(Long id : lst) {
  276.             this.deleteEngine(jdbcProperties, log, connection, sqlQueryObject, id);
  277.         }
  278.        
  279.         return new NonNegativeNumber(lst.size());
  280.    
  281.     }



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