AbstractNoReturnGeneratedKeyGenerator.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.utils.jdbc;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;

  24. /**
  25.  * Contiene la definizione di un KeyGenerator
  26.  *
  27.  * @author Poli Andrea (apoli@link.it)
  28.  * @author $Author$
  29.  * @version $Rev$, $Date$
  30.  */

  31. public abstract class AbstractNoReturnGeneratedKeyGenerator implements IKeyGenerator {

  32.     protected Connection connection;
  33.     protected IKeyGeneratorObject type;
  34.     protected long idTable;
  35.    
  36.     public AbstractNoReturnGeneratedKeyGenerator(Connection connection,IKeyGeneratorObject type) throws KeyGeneratorException {
  37.         this.connection = connection;
  38.         this.type = type;
  39.     }
  40.    
  41.     @Override
  42.     public boolean isReturnGeneratedKeySupported(){
  43.         return false;
  44.     }
  45.    
  46.     @Override
  47.     public boolean useReturnGeneratedKeyColumnNameInResultSet() {
  48.         return false;
  49.     }
  50.    
  51.     @Override
  52.     public String getColunmKeyName() throws KeyGeneratorException {
  53.         switch (this.type.getType()) {
  54.             case DEFAULT:
  55.                 return "id";
  56.             case CUSTOM:
  57.                 return ((CustomKeyGeneratorObject)this.type).getColumnNameId();
  58.             default:
  59.                 throw new KeyGeneratorException("Tipo di KeyGeneratorObjects non gestito: "+this.type);
  60.         }
  61.     }
  62.    
  63.     public abstract String getSequenceQuery() throws KeyGeneratorException;
  64.    
  65.     @Override
  66.     public long generateKey() throws KeyGeneratorException{
  67.        
  68.         PreparedStatement stmt = null;
  69.         ResultSet rs = null;
  70.         try{
  71.             String sequenceQuery =  getSequenceQuery();
  72.             stmt = this.connection.prepareStatement(sequenceQuery);
  73.             rs = stmt.executeQuery();
  74.             if(rs.next()==false){
  75.                 throw new Exception("ID autoincrementale non ottenuto via Sequence");
  76.             }
  77.             this.idTable = rs.getLong("nextval");
  78.             if(this.idTable<=0){
  79.                 throw new Exception("ID autoincrementale non ottenuto: is null?");
  80.             }
  81.             return this.idTable;
  82.         }catch(Exception e){
  83.             throw new KeyGeneratorException("Errore durante la generazione della chiave: "+e.getMessage(),e);
  84.         }finally{
  85.             try{
  86.                 if(rs!=null) {
  87.                     rs.close();
  88.                 }
  89.             }catch(Exception eClose){}
  90.             try{
  91.                 if(stmt!=null) {
  92.                     stmt.close();
  93.                 }
  94.             }catch(Exception eClose){
  95.                 // close
  96.             }
  97.         }
  98.     }
  99.    
  100.     @Override
  101.     public long getReturnGeneratedKey(PreparedStatement stmt) throws KeyGeneratorException{
  102.         return this.idTable;
  103.     }
  104.    
  105. }