Traccia.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.protocol.sdk.tracciamento;

  21. import java.io.ByteArrayOutputStream;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;

  27. import org.openspcoop2.core.constants.Costanti;
  28. import org.openspcoop2.core.constants.TipoPdD;
  29. import org.openspcoop2.core.id.IDSoggetto;
  30. import org.openspcoop2.core.tracciamento.Allegati;
  31. import org.openspcoop2.core.tracciamento.Dominio;
  32. import org.openspcoop2.core.tracciamento.DominioSoggetto;
  33. import org.openspcoop2.core.tracciamento.Proprieta;
  34. import org.openspcoop2.core.tracciamento.Protocollo;
  35. import org.openspcoop2.core.tracciamento.TracciaEsitoElaborazione;
  36. import org.openspcoop2.core.tracciamento.constants.TipoEsitoElaborazione;
  37. import org.openspcoop2.protocol.sdk.Allegato;
  38. import org.openspcoop2.protocol.sdk.Busta;
  39. import org.openspcoop2.protocol.sdk.BustaRawContent;
  40. import org.openspcoop2.protocol.sdk.constants.EsitoElaborazioneMessaggioTracciatura;
  41. import org.openspcoop2.protocol.sdk.constants.RuoloMessaggio;

  42. /**
  43.  * Bean Contenente le informazioni relative alle tracce
  44.  *
  45.  * @author Stefano Corallo (corallo@link.it)
  46.  * @author Nardi Lorenzo
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */

  50. public class Traccia implements java.io.Serializable {

  51.     /**
  52.      * serialVersionUID
  53.      */
  54.     private static final long serialVersionUID = 1L;
  55.    
  56.     // indicazione store
  57.     private boolean stored = false;
  58.    
  59.     // Busta
  60.     protected Busta busta;
  61.     private byte[] bustaInByte;
  62.     private BustaRawContent<?> bustaRawContent;

  63.     // properties
  64.     protected Map<String, String> properties = new HashMap<>();
  65.    
  66.     // protocollo
  67.     private String protocollo;
  68.    
  69.     // traccia
  70.     private org.openspcoop2.core.tracciamento.Traccia traccia;
  71.    
  72.     // lista allegati
  73.     private List<Allegato> allegati = new ArrayList<>();
  74.    
  75.    
  76.     public Traccia() {
  77.         this.traccia =  new org.openspcoop2.core.tracciamento.Traccia();
  78.     }
  79.     public Traccia(org.openspcoop2.core.tracciamento.Traccia traccia) {
  80.        
  81.         this.traccia = traccia;
  82.        
  83.         // protocollo
  84.         if(traccia.getBusta()!=null && traccia.getBusta().getProtocollo()!=null){
  85.             this.protocollo = traccia.getBusta().getProtocollo().getIdentificativo();
  86.         }
  87.        
  88.         // busta
  89.         if(traccia.getBusta()!=null){
  90.             this.busta = new Busta(traccia.getBusta());
  91.         }
  92.        
  93.         // allegati
  94.         if(traccia.getAllegati()!=null && traccia.getAllegati().sizeAllegatoList()>0)
  95.         for (org.openspcoop2.core.tracciamento.Allegato allegato : traccia.getAllegati().getAllegatoList()) {
  96.             this.addAllegato(new Allegato(allegato),false);
  97.         }
  98.     }
  99.    
  100.    
  101.    
  102.     // stored
  103.    
  104.     public boolean isStored() {
  105.         return this.stored;
  106.     }
  107.     public void setStored(boolean stored) {
  108.         this.stored = stored;
  109.     }
  110.    
  111.    
  112.    
  113.     // base
  114.    
  115.     public org.openspcoop2.core.tracciamento.Traccia getTraccia() {
  116.         return this.traccia;
  117.     }
  118.     public void setTraccia(org.openspcoop2.core.tracciamento.Traccia traccia) {
  119.         this.traccia = traccia;
  120.        
  121.         // protocollo
  122.         if(traccia.getBusta()!=null && traccia.getBusta().getProtocollo()!=null){
  123.             this.protocollo = traccia.getBusta().getProtocollo().getIdentificativo();
  124.         }
  125.        
  126.         // allegati
  127.         if(traccia.getAllegati()!=null && traccia.getAllegati().sizeAllegatoList()>0)
  128.         for (org.openspcoop2.core.tracciamento.Allegato allegato : traccia.getAllegati().getAllegatoList()) {
  129.             this.addAllegato(new Allegato(allegato),false);
  130.         }
  131.     }
  132.    
  133.    
  134.    
  135.     // id  [Wrapper]
  136.    
  137.     public Long getId() {
  138.         return this.traccia.getId();
  139.     }
  140.     public void setId(Long id) {
  141.         this.traccia.setId(id);
  142.     }
  143.    
  144.    
  145.    
  146.     // idTransazione [Wrapper]
  147.    
  148.     public String getIdTransazione() {
  149.         return this.traccia.getIdTransazione();
  150.     }
  151.     public void setIdTransazione(String idTransazione) {
  152.         this.traccia.setIdTransazione(idTransazione);
  153.     }
  154.    
  155.    
  156.    
  157.     // data [Wrapper]
  158.    
  159.     public Date getGdo() {
  160.         return this.traccia.getOraRegistrazione();
  161.     }
  162.     public void setGdo(Date value) {
  163.         this.traccia.setOraRegistrazione(value);
  164.     }
  165.    
  166.    
  167.     // dominio [wrapper]
  168.    
  169.     public IDSoggetto getIdSoggetto() {
  170.         IDSoggetto idSoggetto = null;
  171.         if(this.traccia.getDominio()!=null){
  172.             if(this.traccia.getDominio().getSoggetto()!=null){
  173.                 if(idSoggetto==null){
  174.                     idSoggetto = new IDSoggetto();
  175.                 }
  176.                 idSoggetto.setTipo(this.traccia.getDominio().getSoggetto().getTipo());
  177.                 idSoggetto.setNome(this.traccia.getDominio().getSoggetto().getBase());
  178.             }
  179.             if(this.traccia.getDominio().getIdentificativoPorta()!=null){
  180.                 if(idSoggetto==null){
  181.                     idSoggetto = new IDSoggetto();
  182.                 }
  183.                 idSoggetto.setCodicePorta(this.traccia.getDominio().getIdentificativoPorta());
  184.             }
  185.         }
  186.         return idSoggetto;
  187.     }
  188.     public void setIdSoggetto(IDSoggetto value) {
  189.         if(value!=null && (value.getTipo()!=null || value.getNome()!=null || value.getCodicePorta()!=null)){
  190.             if(value.getTipo()!=null || value.getNome()!=null){
  191.                 if(this.traccia.getDominio()==null){
  192.                     this.traccia.setDominio(new Dominio());
  193.                 }
  194.                 if(this.traccia.getDominio().getSoggetto()==null){
  195.                     this.traccia.getDominio().setSoggetto(new DominioSoggetto());
  196.                 }
  197.                 this.traccia.getDominio().getSoggetto().setTipo(value.getTipo());
  198.                 this.traccia.getDominio().getSoggetto().setBase(value.getNome());
  199.             }
  200.             else{
  201.                 if(this.traccia.getDominio()!=null){
  202.                     this.traccia.getDominio().setSoggetto(null);
  203.                 }
  204.             }
  205.            
  206.             if(value.getCodicePorta()!=null){
  207.                 if(this.traccia.getDominio()==null){
  208.                     this.traccia.setDominio(new Dominio());
  209.                 }
  210.                 this.traccia.getDominio().setIdentificativoPorta(value.getCodicePorta());
  211.             }
  212.             else{
  213.                 if(this.traccia.getDominio()!=null){
  214.                     this.traccia.getDominio().setIdentificativoPorta(null);
  215.                 }
  216.             }
  217.         }
  218.         else{
  219.             if(this.traccia.getDominio()!=null){
  220.                 if(this.traccia.getDominio().getFunzione()==null){
  221.                     this.traccia.setDominio(null);
  222.                 }
  223.                 else{
  224.                     this.traccia.getDominio().setIdentificativoPorta(null);
  225.                     this.traccia.getDominio().setSoggetto(null);
  226.                 }
  227.             }
  228.         }
  229.     }
  230.     public TipoPdD getTipoPdD() {
  231.         if(this.traccia.getDominio()!=null && this.traccia.getDominio().getFunzione()!=null){
  232.             switch (this.traccia.getDominio().getFunzione()) {
  233.             case PORTA_DELEGATA:
  234.                 return TipoPdD.DELEGATA;
  235.             case PORTA_APPLICATIVA:
  236.                 return TipoPdD.APPLICATIVA;
  237.             case INTEGRATION_MANAGER:
  238.                 return TipoPdD.INTEGRATION_MANAGER;
  239.             case ROUTER:
  240.                 return TipoPdD.ROUTER;
  241.             }
  242.         }
  243.         return null;
  244.     }
  245.     public void setTipoPdD(TipoPdD tipoPdD) {
  246.         if(tipoPdD!=null){
  247.             if(this.traccia.getDominio()==null){
  248.                 this.traccia.setDominio(new Dominio());
  249.             }
  250.             switch (tipoPdD) {
  251.             case DELEGATA:
  252.                 this.traccia.getDominio().setFunzione(org.openspcoop2.core.tracciamento.constants.TipoPdD.PORTA_DELEGATA);
  253.                 break;
  254.             case APPLICATIVA:
  255.                 this.traccia.getDominio().setFunzione(org.openspcoop2.core.tracciamento.constants.TipoPdD.PORTA_APPLICATIVA);
  256.                 break;
  257.             case INTEGRATION_MANAGER:
  258.                 this.traccia.getDominio().setFunzione(org.openspcoop2.core.tracciamento.constants.TipoPdD.INTEGRATION_MANAGER);
  259.                 break;
  260.             case ROUTER:
  261.                 this.traccia.getDominio().setFunzione(org.openspcoop2.core.tracciamento.constants.TipoPdD.ROUTER);
  262.                 break;
  263.             }
  264.         }
  265.         else{
  266.             if(this.traccia.getDominio()!=null){
  267.                 if(this.traccia.getDominio().getIdentificativoPorta()==null && this.traccia.getDominio().getSoggetto()==null){
  268.                     this.traccia.setDominio(null);
  269.                 }
  270.                 else{
  271.                     this.traccia.getDominio().setFunzione(null);
  272.                 }
  273.             }
  274.         }
  275.     }
  276.    
  277.    
  278.     // tipoTraccia [wrapper]
  279.    
  280.     public RuoloMessaggio getTipoMessaggio() {
  281.         if(this.traccia.getTipo()!=null){
  282.             switch (this.traccia.getTipo()) {
  283.             case RICHIESTA:
  284.                 return RuoloMessaggio.RICHIESTA;
  285.             case RISPOSTA:
  286.                 return RuoloMessaggio.RISPOSTA;
  287.             }
  288.         }
  289.         return null;
  290.     }
  291.     public void setTipoMessaggio(RuoloMessaggio value) {
  292.         if(value!=null){
  293.             switch (value) {
  294.             case RICHIESTA:
  295.                 this.traccia.setTipo(org.openspcoop2.core.tracciamento.constants.TipoTraccia.RICHIESTA);
  296.                 break;
  297.             case RISPOSTA:
  298.                 this.traccia.setTipo(org.openspcoop2.core.tracciamento.constants.TipoTraccia.RISPOSTA);
  299.                 break;
  300.             }
  301.         }
  302.     }

  303.    
  304.    
  305.     // Busta
  306.    
  307.     public Busta getBusta() {
  308.         return this.busta;
  309.     }
  310.     public void setBusta(Busta value) {
  311.         this.busta = value!=null ? value.newInstance() : null;
  312.         if(value!=null)
  313.             this.traccia.setBusta(value.getBusta());
  314.         else
  315.             this.traccia.setBusta(null);
  316.     }
  317.     public byte[] getBustaAsByteArray() {
  318.         return this.bustaInByte;
  319.     }
  320.     public void setBustaAsByteArray(byte[] bustaInByte) {
  321.         this.bustaInByte = bustaInByte;
  322.     }
  323.     public BustaRawContent<?> getBustaAsRawContent() {
  324.         return this.bustaRawContent;
  325.     }
  326.     public void setBustaAsRawContent(BustaRawContent<?> bustaElement) {
  327.         this.bustaRawContent = bustaElement;
  328.     }
  329.     // [wrapper]
  330.     public String getBustaAsString() {
  331.         return this.traccia.getBustaRaw();
  332.     }
  333.     public void setBustaAsString(String bustaAsString) {
  334.         this.traccia.setBustaRaw(bustaAsString);
  335.     }
  336.    
  337.    
  338.     // correlazione [wrapper]
  339.    
  340.     public String getCorrelazioneApplicativa() {
  341.         return this.traccia.getIdentificativoCorrelazioneRichiesta();
  342.     }
  343.     public void setCorrelazioneApplicativa(String value) {
  344.         this.traccia.setIdentificativoCorrelazioneRichiesta(value);
  345.     }
  346.    
  347.     public String getCorrelazioneApplicativaRisposta() {
  348.         return this.traccia.getIdentificativoCorrelazioneRisposta();
  349.     }
  350.     public void setCorrelazioneApplicativaRisposta(String value) {
  351.         this.traccia.setIdentificativoCorrelazioneRisposta(value);
  352.     }
  353.    
  354.    
  355.    
  356.     // location [wrapper]
  357.    
  358.     public String getLocation() {
  359.         return this.traccia.getLocation();
  360.     }
  361.     public void setLocation(String value) {
  362.         this.traccia.setLocation(value);
  363.     }
  364.    
  365.    


  366.     // properties
  367.     public void addPropertyInBusta(String key,String value){
  368.         if(this.traccia.getBusta()==null){
  369.             this.traccia.setBusta(new org.openspcoop2.core.tracciamento.Busta());
  370.         }
  371.         if(this.traccia.getBusta().getProtocollo()==null){
  372.             this.traccia.getBusta().setProtocollo(new Protocollo());
  373.         }
  374.         if(this.traccia.getBusta().getProtocollo().getProprietaList()==null){
  375.             this.traccia.getBusta().getProtocollo().setProprietaList(new ArrayList<Proprieta>());
  376.         }
  377.         boolean exists = false;
  378.         for (int i = 0; i < this.traccia.getBusta().getProtocollo().sizeProprietaList(); i++) {
  379.             Proprieta p = this.traccia.getBusta().getProtocollo().getProprieta(i);
  380.             if(key.equals(p.getNome())){
  381.                 exists = true;
  382.                 break;
  383.             }
  384.         }
  385.         if(!exists){
  386.             Proprieta proprieta = new Proprieta();
  387.             proprieta.setNome(key);
  388.             proprieta.setValore(value);
  389.             this.traccia.getBusta().getProtocollo().addProprieta(proprieta);
  390.         }
  391.     }
  392.     public void addProperty(String key,String value){
  393.         this.properties.put(key,value);
  394.     }  
  395.     public int sizeProperties(){
  396.         return this.properties.size();
  397.     }
  398.     public String getProperty(String key){
  399.         String value = this.properties.get(key);
  400.         if(value==null || "".equals(value)){
  401.             if(Costanti.ID_TRANSAZIONE.getValue().equals(key)){
  402.                 if(this.traccia.getBusta()!=null && this.traccia.getBusta().getProtocollo()!=null &&
  403.                         this.traccia.getBusta().getProtocollo().getProprietaList()!=null){
  404.                     for (int i = 0; i < this.traccia.getBusta().getProtocollo().sizeProprietaList(); i++) {
  405.                         Proprieta p = this.traccia.getBusta().getProtocollo().getProprieta(i);
  406.                         if(Costanti.ID_TRANSAZIONE.getValue().equals(p.getNome())){
  407.                             return p.getValore();
  408.                         }
  409.                     }
  410.                 }
  411.             }
  412.         }
  413.         return value;
  414.     }
  415.     public String removeProperty(String key){
  416.         return this.properties.remove(key);
  417.     }
  418.     public String[] getPropertiesValues() {
  419.         return this.properties.values().toArray(new String[this.properties.size()]);
  420.     }
  421.     public String[] getPropertiesNames() {
  422.         return this.properties.keySet().toArray(new String[this.properties.size()]);
  423.     }
  424.     public void setProperties(Map<String, String> params) {
  425.         this.properties = params;
  426.     }
  427.     public Map<String, String> getProperties() {
  428.         return this.properties;
  429.     }
  430.    
  431.    
  432.     // protocollo
  433.     public String getProtocollo() {
  434.         return this.protocollo;
  435.     }
  436.     public void setProtocollo(String protocollo) {
  437.         this.protocollo = protocollo;
  438.     }
  439.    
  440.    
  441.     // esito [Wrapper]
  442.     public EsitoElaborazioneMessaggioTracciato getEsitoElaborazioneMessaggioTracciato() {
  443.         if(this.traccia.getEsitoElaborazione()!=null){
  444.             EsitoElaborazioneMessaggioTracciato esito = new EsitoElaborazioneMessaggioTracciato();
  445.             esito.setDettaglio(this.traccia.getEsitoElaborazione().getDettaglio());
  446.             if(this.traccia.getEsitoElaborazione().getTipo()!=null){
  447.                 switch (this.traccia.getEsitoElaborazione().getTipo()) {
  448.                 case INVIATO:
  449.                     esito.setEsito(EsitoElaborazioneMessaggioTracciatura.INVIATO);
  450.                     break;
  451.                 case RICEVUTO:
  452.                     esito.setEsito(EsitoElaborazioneMessaggioTracciatura.RICEVUTO);
  453.                     break;
  454.                 case ERRORE:
  455.                     esito.setEsito(EsitoElaborazioneMessaggioTracciatura.ERRORE);
  456.                     break;
  457.                 }
  458.             }
  459.             return esito;
  460.         }
  461.         return null;
  462.     }
  463.     public void setEsitoElaborazioneMessaggioTracciato(
  464.             EsitoElaborazioneMessaggioTracciato esitoElaborazioneMessaggioTracciato) {
  465.         if(esitoElaborazioneMessaggioTracciato==null){
  466.             return;
  467.         }
  468.         if(this.traccia.getEsitoElaborazione()==null){
  469.             this.traccia.setEsitoElaborazione(new TracciaEsitoElaborazione());
  470.         }
  471.         this.traccia.getEsitoElaborazione().setDettaglio(esitoElaborazioneMessaggioTracciato.getDettaglio());
  472.         if(esitoElaborazioneMessaggioTracciato.getEsito()!=null){
  473.             switch (esitoElaborazioneMessaggioTracciato.getEsito()) {
  474.             case INVIATO:
  475.                 this.traccia.getEsitoElaborazione().setTipo(TipoEsitoElaborazione.INVIATO);
  476.                 break;
  477.             case RICEVUTO:
  478.                 this.traccia.getEsitoElaborazione().setTipo(TipoEsitoElaborazione.RICEVUTO);
  479.                 break;
  480.             case ERRORE:
  481.                 this.traccia.getEsitoElaborazione().setTipo(TipoEsitoElaborazione.ERRORE);
  482.                 break;
  483.             }
  484.         }
  485.     }
  486.    
  487.    
  488.     // allegati [wrapped]
  489.     public List<Allegato> getListaAllegati() {
  490.         return this.allegati;
  491.     }
  492.     public int sizeListaAllegati() {
  493.         return this.allegati.size();
  494.     }
  495.     public void addAllegato(Allegato a) {
  496.         this.addAllegato(a, true);
  497.     }
  498.     private void addAllegato(Allegato a, boolean addCore) {
  499.         this.allegati.add(a);
  500.         if(addCore){
  501.             if(this.traccia.getAllegati()==null){
  502.                 this.traccia.setAllegati(new Allegati());
  503.             }
  504.             this.traccia.getAllegati().addAllegato(a.getAllegato());
  505.         }
  506.     }
  507.     public Allegato getAllegato(int index) {
  508.         return this.allegati.get(index);
  509.     }
  510.     public Allegato removeAllegato(int index) {
  511.         this.traccia.getAllegati().removeAllegato(index);
  512.         return this.allegati.remove(index);
  513.     }
  514.     protected void setListaAllegati(List<Allegato> listaAllegati) {
  515.         this.allegati = listaAllegati;
  516.     }
  517.    
  518.    
  519.    
  520.    
  521.     public Traccia newInstance(){
  522.        
  523.         // Non uso il base clone per far si che venga usato il costruttore new String()
  524.        
  525.         Traccia clone = new Traccia();
  526.        
  527.         // id
  528.         clone.setId(this.getId()!=null ? Long.valueOf(this.getId()+"") : null);
  529.        
  530.         // data
  531.         clone.setGdo(this.getGdo()!=null ? new Date(this.getGdo().getTime()) : null);
  532.        
  533.         // dominio
  534.         clone.setIdSoggetto(this.getIdSoggetto()!=null ? this.getIdSoggetto().clone() : null);
  535.         clone.setTipoPdD(this.getTipoPdD());
  536.        
  537.         // tipoTraccia
  538.         clone.setTipoMessaggio(this.getTipoMessaggio());
  539.        
  540.         // busta
  541.         clone.setBusta(this.getBusta()!=null ? this.getBusta().newInstance() : null);
  542.         ByteArrayOutputStream bout = null;
  543.         if(this.getBustaAsByteArray()!=null){
  544.             bout = new ByteArrayOutputStream();
  545.             try{
  546.                 bout.write(this.getBustaAsByteArray());
  547.                 bout.flush();
  548.                 bout.close();
  549.             }catch(Exception e){
  550.                 throw new RuntimeException(e.getMessage(),e);
  551.             }
  552.             clone.setBustaAsByteArray(bout.toByteArray());
  553.         }
  554.         clone.setBustaAsRawContent(this.getBustaAsRawContent()); // non clonato, vedere se si trova un modo efficiente se serve
  555.         clone.setBustaAsString(this.getBustaAsString()!=null ? new String(this.getBustaAsString()) : null);
  556.        
  557.         // correlazione
  558.         clone.setCorrelazioneApplicativa(this.getCorrelazioneApplicativa()!=null ? new String(this.getCorrelazioneApplicativa()) : null);
  559.         clone.setCorrelazioneApplicativaRisposta(this.getCorrelazioneApplicativaRisposta()!=null ? new String(this.getCorrelazioneApplicativaRisposta()) : null);
  560.        
  561.         // location
  562.         clone.setLocation(this.getLocation()!=null ? new String(this.getLocation()) : null);
  563.        
  564.         // esito
  565.         clone.setEsitoElaborazioneMessaggioTracciato(this.getEsitoElaborazioneMessaggioTracciato()!=null ? this.getEsitoElaborazioneMessaggioTracciato().clone() : null);
  566.        
  567.         // properties
  568.         if(this.properties!=null && this.properties.size()>0){
  569.             for (String key : this.properties.keySet()) {
  570.                 String value = this.properties.get(key);
  571.                 if(key!=null && value!=null){
  572.                     clone.addProperty(new String(key), new String(value));
  573.                 }
  574.             }
  575.         }
  576.        
  577.         // protocollo
  578.         clone.setProtocollo(this.protocollo!=null ? new String(this.protocollo) : null);
  579.        
  580.         // allegati
  581.         for(int i=0; i<this.sizeListaAllegati(); i++){
  582.             clone.addAllegato(this.getAllegato(i).clone());
  583.         }
  584.        
  585.         return clone;
  586.     }
  587. }