AbstractReturnGeneratedKeyGenerator.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 AbstractReturnGeneratedKeyGenerator implements IKeyGenerator {

  32.     protected Connection connection;
  33.     protected IKeyGeneratorObject type;
  34.     protected long idTraccia;
  35.    
  36.     public AbstractReturnGeneratedKeyGenerator(Connection connection,IKeyGeneratorObject type) throws KeyGeneratorException {
  37.         this.connection = connection;
  38.         this.type = type;
  39.     }
  40.    
  41.     @Override
  42.     public boolean isReturnGeneratedKeySupported(){
  43.         return true;
  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.     @Override
  64.     public long generateKey() throws KeyGeneratorException{
  65.         throw new KeyGeneratorException("ReturnGeneratedKey supported");
  66.     }
  67.    
  68.     @Override
  69.     public long getReturnGeneratedKey(PreparedStatement stmt) throws KeyGeneratorException{
  70.         ResultSet rs = null;
  71.         try{
  72.             rs = stmt.getGeneratedKeys();
  73.             if(rs.next()==false){
  74.                 throw new Exception("ID autoincrementale non ottenuto via JDBC3.0");
  75.             }
  76.             if(this.useReturnGeneratedKeyColumnNameInResultSet()) {
  77.                 this.idTraccia = rs.getLong(this.getColunmKeyName());
  78.             }
  79.             else {
  80.                 this.idTraccia = rs.getLong(1);
  81.             }
  82.             if(this.idTraccia<=0){
  83.                 throw new Exception("ID autoincrementale non ottenuto: is null?");
  84.             }
  85.             return this.idTraccia;
  86.         }catch(Exception e){
  87.             throw new KeyGeneratorException("Errore durante la generazione della chiave via JDBC3.0: "+e.getMessage(),e);
  88.         }finally{
  89.             try{
  90.                 if(rs!=null) {
  91.                     rs.close();
  92.                 }
  93.             }catch(Exception eClose){
  94.                 // close
  95.             }
  96.         }
  97.     }
  98.    
  99. }