Transaction.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.sdk.transaction;

  21. import org.openspcoop2.core.commons.dao.DAOFactory;
  22. import org.openspcoop2.monitor.sdk.constants.TransactionExceptionCode;
  23. import org.openspcoop2.monitor.sdk.exceptions.TransactionException;

  24. import java.util.ArrayList;
  25. import java.util.LinkedList;
  26. import java.util.List;

  27. import org.slf4j.Logger;
  28. import org.openspcoop2.core.transazioni.Transazione;
  29. import org.openspcoop2.protocol.sdk.ProtocolException;
  30. import org.openspcoop2.protocol.sdk.builder.EsitoTransazione;
  31. import org.openspcoop2.protocol.sdk.diagnostica.MsgDiagnostico;
  32. import org.openspcoop2.protocol.sdk.tracciamento.Traccia;
  33. import org.openspcoop2.protocol.utils.EsitiProperties;
  34. import org.openspcoop2.utils.LoggerWrapperFactory;

  35. /**
  36.  * Transaction
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class Transaction {

  43.     public Transaction(Logger log,DAOFactory daoFactory,Transazione transaction) throws ProtocolException{
  44.         this(log,daoFactory,transaction,null,null,null);
  45.     }
  46.     public Transaction(Logger log,DAOFactory daoFactory,Transazione transaction,Traccia requestTrace,Traccia responseTrace,List<MsgDiagnostico> msgdiagnosticiList) throws ProtocolException{
  47.         this.log = log;
  48.         this.daoFactory = daoFactory;
  49.         this.idTransazione = transaction.getIdTransazione();
  50.         this.transactionResult = EsitiProperties.getInstanceFromProtocolName(this.log,transaction.getProtocollo()).convertToEsitoTransazione(transaction.getEsito(), transaction.getEsitoContesto());
  51.         this.transaction = transaction;
  52.         this.requestTrace = requestTrace;
  53.         this.responseTrace = responseTrace;
  54.         this.msgdiagnosticiList = msgdiagnosticiList;
  55.     }
  56.    
  57.     /** Logger */
  58.     private Logger log = LoggerWrapperFactory.getLogger(Transaction.class);
  59.     public Logger getLogger(){
  60.         return this.log;
  61.     }
  62.    
  63.     /** DAOFactory */
  64.     private DAOFactory daoFactory = null;
  65.     public DAOFactory getDAOFactory(){
  66.         return this.daoFactory;
  67.     }
  68.    
  69.    
  70.     /** Identificatore unico di transazione */
  71.     private Transazione transaction;
  72.     private String idTransazione;
  73.     private EsitoTransazione transactionResult;
  74.     public String getIdTransazione() {
  75.         return this.idTransazione;
  76.     }
  77.     public EsitoTransazione getTransactionResult() {
  78.         return this.transactionResult;
  79.     }
  80.     public Transazione getTransaction() {
  81.         return this.transaction;
  82.     }
  83.    
  84.    
  85.     /** Messaggi diagnostici */
  86.     private List<MsgDiagnostico> msgdiagnosticiList = new ArrayList<MsgDiagnostico>();
  87.     public void setMsgdiagnosticiList(List<MsgDiagnostico> msgdiagnosticiList) {
  88.         this.msgdiagnosticiList = msgdiagnosticiList;
  89.     }
  90.     public List<MsgDiagnostico> getMsgDiagnostici(){
  91.         return this.msgdiagnosticiList;
  92.     }
  93.     public void addMsgDiagnostico(MsgDiagnostico msgDiag){
  94.         this.msgdiagnosticiList.add(msgDiag);
  95.     }
  96.     public void removeDiagnosticMessages(){
  97.         this.msgdiagnosticiList.clear();
  98.     }
  99.     public MsgDiagnostico getDiagnosticMessage(int index){
  100.         return this.msgdiagnosticiList.get(index);
  101.     }
  102.     public void removeDiagnosticMessage(int index){
  103.         this.msgdiagnosticiList.remove(index);
  104.     }
  105.     public int countDiagnosticMessages(){
  106.         return this.msgdiagnosticiList.size();
  107.     }
  108.    
  109.     /** Tracce */
  110.     private Traccia requestTrace;
  111.     private Traccia responseTrace;
  112.     public Traccia getRequestTrace() {
  113.         return this.requestTrace;
  114.     }
  115.     public void setRequestTrace(Traccia requestTrace) {
  116.         this.requestTrace = requestTrace;
  117.     }
  118.     public Traccia getResponseTrace() {
  119.         return this.responseTrace;
  120.     }
  121.     public void setResponseTrace(Traccia responseTrace) {
  122.         this.responseTrace = responseTrace;
  123.     }

  124.     /** Risorse di Contenuto */
  125.     private List<AbstractContentResource> risorseContenuto = new ArrayList<AbstractContentResource>();
  126.     public List<AbstractContentResource> getContentResources(){
  127.         return this.risorseContenuto;
  128.     }
  129.     @SuppressWarnings("unchecked")
  130.     public <ContentResourceType> List<ContentResourceType> getContentResourcesByType(Class<ContentResourceType> type){
  131.         List<ContentResourceType> retList = new LinkedList<ContentResourceType>();
  132.         if (this.risorseContenuto != null) {
  133.             for (int i=0; i<this.risorseContenuto.size(); i++) {
  134.                 if (type.equals(this.risorseContenuto.get(i).getClass())) {
  135.                     retList.add((ContentResourceType)this.risorseContenuto.get(i));
  136.                 }
  137.             }
  138.         }
  139.         return retList;
  140.     }
  141.     public AbstractContentResource getContentResourceByName(String name){
  142.         AbstractContentResource retResource = null;
  143.         if (this.risorseContenuto!=null) {
  144.             for (int i=0; i<this.risorseContenuto.size(); i++) {
  145.                 if (name.equals(this.risorseContenuto.get(i).getName())) {
  146.                     retResource = this.risorseContenuto.get(i);
  147.                 }
  148.             }
  149.         }
  150.         return retResource;
  151.     }
  152.     public void addContentResource(AbstractContentResource resource) throws TransactionException{
  153.         // controlla che la risorsa che si vuole aggiungere non esista giĆ 
  154.         for (int i=0; i<this.risorseContenuto.size(); i++) {
  155.             if (resource.getName().equals(this.risorseContenuto.get(i).getName()))
  156.                 throw new TransactionException(TransactionExceptionCode.ADD_RES_EXIST);
  157.         }
  158.         this.risorseContenuto.add(resource);
  159.     }
  160.     public void updateContentResource(AbstractContentResource resource) throws TransactionException{
  161.         int index = 0;
  162.         boolean found = false;
  163.         while (!found && this.risorseContenuto!=null && index<this.risorseContenuto.size()) {
  164.             if (resource.getName().equals(this.risorseContenuto.get(index).getName()))
  165.                 found = true;
  166.             else
  167.                 index++;
  168.         }
  169.         if (found)
  170.             this.risorseContenuto.set(index, resource);
  171.         else
  172.             throw new TransactionException(TransactionExceptionCode.UPD_RES_NOT_EXIST);
  173.     }
  174.     public void removeContentResource(AbstractContentResource resource) throws TransactionException{
  175.         int index = 0;
  176.         boolean found = false;
  177.         while (!found && this.risorseContenuto!=null && index<this.risorseContenuto.size()) {
  178.             if (resource.getName().equals(this.risorseContenuto.get(index).getName()))
  179.                 found = true;
  180.             else
  181.                 index++;
  182.         }
  183.         if (found)
  184.             this.risorseContenuto.remove(index);
  185.         else
  186.             throw new TransactionException(TransactionExceptionCode.DEL_RES_NOT_EXIST);
  187.     }
  188.    
  189.    


  190.     /** Dati SLA */
  191.     private SLATrace slaTrace;
  192.     public SLATrace getSlaTrace() {
  193.         return this.slaTrace;
  194.     }
  195.     public void setSlaTrace(SLATrace slaTrace) {
  196.         this.slaTrace = slaTrace;
  197.     }

  198.    
  199. }