FSRecoveryTransazioniImpl.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.monitor.engine.fs_recovery;

  21. import org.openspcoop2.core.transazioni.Transazione;
  22. import org.openspcoop2.core.transazioni.utils.serializer.JaxbDeserializer;
  23. import org.openspcoop2.generic_project.expression.IPaginatedExpression;
  24. import org.openspcoop2.monitor.engine.condition.EsitoUtils;
  25. import org.openspcoop2.monitor.engine.constants.Costanti;
  26. import org.openspcoop2.utils.UtilsException;
  27. import org.openspcoop2.utils.UtilsMultiException;

  28. import java.io.File;
  29. import java.sql.Connection;
  30. import java.util.List;

  31. import org.slf4j.Logger;

  32. /**
  33.  * FSRecoveryTransazioniImpl
  34.  *
  35.  * @author Poli Andrea (apoli@link.it)
  36.  * @author $Author$
  37.  * @version $Rev$, $Date$
  38.  */
  39. public class FSRecoveryTransazioniImpl extends AbstractFSRecovery {
  40.     private org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM;

  41.     public FSRecoveryTransazioniImpl(
  42.             Logger log,
  43.             boolean debug,
  44.             org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM,
  45.             File directory, File directoryDLQ,
  46.             int tentativi,
  47.             long msAttesaProcessingFile) {
  48.         super(log, debug, directory, directoryDLQ, tentativi, msAttesaProcessingFile);
  49.         this.transazioniSM = transazioniSM;
  50.     }

  51.     @Override
  52.     public void process(Connection connection) {
  53.         this.log.info("Recovery Transazioni ...");
  54.         super.process(connection);
  55.         this.log.info("Recovery Transazioni completato");
  56.     }
  57.    
  58.     private static boolean enrichEventi = false;    
  59.     public static boolean isEnrichEventi() {
  60.         return enrichEventi;
  61.     }
  62.     public static void setEnrichEventi(boolean enrichEventi) {
  63.         FSRecoveryTransazioniImpl.enrichEventi = enrichEventi;
  64.     }

  65.     @Override
  66.     public void insertObject(File file, Connection connection) throws UtilsException, UtilsMultiException {
  67.         JaxbDeserializer deserializer = new JaxbDeserializer();
  68.         Transazione transazione = null;
  69.         try {
  70.             transazione = deserializer.readTransazione(file);
  71.         }catch(Exception e) {
  72.             throw new UtilsException(e.getMessage(),e);
  73.         }
  74.        
  75.         // aggiungo evento transazione
  76.         // Non e' più possibile aggiungerlo da quando abbiamo trasformato la colonna in 'eventi'.
  77.         if(enrichEventi) {
  78.             if(transazione.getEventiGestione()==null || "".equals(transazione.getEventiGestione())){
  79.                 transazione.setEventiGestione(Costanti.EVENTO_FILE_SYSTEM_RECOVERY);
  80.             }
  81.             else{
  82.                 transazione.setEventiGestione(transazione.getEventiGestione()+","+Costanti.EVENTO_FILE_SYSTEM_RECOVERY);
  83.             }
  84.         }
  85.        
  86.         try {
  87.             String id = this.transazioniSM.getTransazioneService().convertToId(transazione);
  88.    
  89.             //Se esiste gia' una transazione con questo id, sposto il file direttamente nella DLQ indicando nel file README associato questo motivo
  90.             if(this.transazioniSM.getTransazioneService().exists(id)) {
  91.                
  92.                 // Devo verificare che non abbia uno stato parziale
  93.                 IPaginatedExpression expr = this.transazioniSM.getTransazioneServiceSearch().newPaginatedExpression();
  94.                 expr.limit(1);
  95.                 expr.equals(Transazione.model().ID_TRANSAZIONE, id);
  96.                 List<Object> list = this.transazioniSM.getTransazioneServiceSearch().select(expr, Transazione.model().ESITO_CONTESTO);
  97.                 boolean update = false;
  98.                 String contesto = null;
  99.                 if(list!=null && !list.isEmpty()) {
  100.                     Object o = list.get(0);
  101.                     if(o instanceof String) {
  102.                         contesto = (String) o;
  103.                         if(EsitoUtils.isFaseIntermedia(contesto)) {
  104.                             update = true;
  105.                         }
  106.                     }
  107.                 }
  108.                 /**System.out.println("***************************** File ["+file.getName()+"] gestito tramite un aggiornamento:"+update+", precedente stato: "+contesto);*/
  109.                
  110.                 if(update) {
  111.                     this.transazioniSM.getTransazioneService().update(id,transazione);
  112.                     if(this.debug)
  113.                         this.logDebug("File ["+file.getName()+"] gestito tramite un aggiornamento, precedente stato: "+contesto);
  114.                 }
  115.                 else {
  116.                     String newFileName = FSRecoveryFileUtils.renameToDLQ(this.directoryDLQ, file, "Transazione con id ["+id+"] gia' presente nel database.", this.log);
  117.                     if(this.debug)
  118.                         this.logWarn("File ["+file.getName()+"] rinominato in ["+newFileName+"]");
  119.                 }
  120.             }
  121.             else{
  122.                 this.transazioniSM.getTransazioneService().create(transazione);
  123.             }
  124.         }catch(Exception e) {
  125.             throw new UtilsException(e.getMessage(),e);
  126.         }
  127.     }


  128. }