IDSerialGenerator_mysql.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.id.serial;

  21. import java.lang.reflect.Method;
  22. import java.sql.Connection;
  23. import java.sql.Statement;

  24. import org.openspcoop2.utils.UtilsException;
  25. import org.slf4j.Logger;

  26. /**
  27.  * IDSerialGenerator_mysql
  28.  *
  29.  * @author Andrea Poli (apoli@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class IDSerialGenerator_mysql {

  34.     public static String generate(Connection conDB,IDSerialGeneratorParameter param, Logger log, InfoStatistics infoStatistics) throws UtilsException{
  35.        
  36.         Statement stmtUpdate = null;
  37.         String identificativoUnivoco = null;
  38.         try{
  39.             if(conDB==null) {
  40.                 throw new Exception("Connection is null");
  41.             }
  42.            
  43.             String protocollo = param.getProtocollo();
  44.            
  45.             // Incremento
  46.             stmtUpdate= conDB.createStatement();
  47.            
  48.             String table = param.getTableName();
  49.             if(table==null){
  50.                 if(param.getInformazioneAssociataAlProgressivo()!=null){
  51.                     table = Constants.TABELLA_ID_RELATIVO_AS_LONG;
  52.                 }
  53.                 else{
  54.                     table = Constants.TABELLA_ID_AS_LONG;
  55.                 }
  56.             }
  57.            
  58.             String columnInfoAssociata = param.getColumnRelativeInfo();
  59.             if(columnInfoAssociata==null){
  60.                 columnInfoAssociata = Constants.TABELLA_ID_COLONNA_INFO_ASSOCIATA;
  61.             }
  62.            
  63.             String columnPrg = param.getColumnPrg();
  64.             if(columnPrg==null){
  65.                 columnPrg = Constants.TABELLA_ID_COLONNA_COUNTER;
  66.             }
  67.            
  68.             String columnProtocollo = param.getColumnProtocol();
  69.             if(columnProtocollo==null){
  70.                 columnProtocollo = Constants.TABELLA_ID_COLONNA_PROTOCOLLO;
  71.             }

  72.             String condition = " WHERE "+columnProtocollo+"='"+protocollo+"'";
  73.             if(param.getInformazioneAssociataAlProgressivo()!=null){
  74.                 condition = " AND "+columnInfoAssociata+"='"+param.getInformazioneAssociataAlProgressivo()+"'";
  75.             }
  76.            
  77.             String query = "UPDATE " + table  + " SET "+columnPrg+
  78.                     " = LAST_INSERT_ID("+columnPrg+"+1)"+condition;
  79.            
  80.             stmtUpdate.executeUpdate(query );
  81.                            
  82.             /*          
  83.             if (stmtUpdate instanceof com.mysql.jdbc.Statement){
  84.             com.mysql.jdbc.Statement temp=(com.mysql.jdbc.Statement) stmtUpdate;
  85.             //counter = ((com.mysql.jdbc.Statement) stmtUpdate).getLastInsertID();
  86.             counter=temp.getLastInsertID();
  87.             } else {
  88.             org.jboss.resource.adapter.jdbc.WrappedStatement jbossStatement=(org.jboss.resource.adapter.jdbc.WrappedStatement)stmtUpdate;
  89.             com.mysql.jdbc.Statement temp=(com.mysql.jdbc.Statement)jbossStatement.getUnderlyingStatement();
  90.             counter=temp.getLastInsertID();
  91.             }
  92.              */
  93.        
  94.             // TODO REFLECTION VERIFY
  95.             Class<?> cMysqlStatement = Class.forName("com.mysql.jdbc.Statement");
  96.             Method m = cMysqlStatement.getClass().getMethod("getLastInsertID");
  97.             if(stmtUpdate.getClass().isAssignableFrom(cMysqlStatement)){
  98.                 Object oReturn = m.invoke(stmtUpdate);
  99.                 if(oReturn!=null){
  100.                     Long counterAsLong = (Long) oReturn;
  101.                     identificativoUnivoco = counterAsLong.longValue()+"";
  102.                 }
  103.             }
  104.             else{
  105.                 Method mUnder = cMysqlStatement.getClass().getMethod("getUnderlyingStatement");
  106.                 Object stmtUnder = mUnder.invoke(stmtUpdate);
  107.                 Object oReturn = m.invoke(stmtUnder);
  108.                 if(oReturn!=null){
  109.                     Long counterAsLong = (Long) oReturn;
  110.                     identificativoUnivoco = counterAsLong.longValue()+"";
  111.                 }
  112.             }
  113.            
  114.             stmtUpdate.close();
  115.         } catch(Exception e) {
  116.             try{
  117.                 if( stmtUpdate != null )
  118.                     stmtUpdate.close();
  119.             } catch(Exception er) {}
  120.             try{
  121.                 if(conDB!=null) {
  122.                     conDB.rollback();
  123.                 }
  124.             } catch(Exception er) {
  125.                 // close
  126.             }
  127.         }
  128.        
  129.         return identificativoUnivoco;
  130.     }
  131.    
  132. }