EsitoUtils.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.condition;

  21. import java.util.List;

  22. import org.openspcoop2.core.constants.CostantiLabel;
  23. import org.openspcoop2.generic_project.beans.IField;
  24. import org.openspcoop2.generic_project.exception.ExpressionException;
  25. import org.openspcoop2.generic_project.exception.ExpressionNotImplementedException;
  26. import org.openspcoop2.generic_project.exception.NotImplementedException;
  27. import org.openspcoop2.generic_project.exception.ServiceException;
  28. import org.openspcoop2.generic_project.expression.IExpression;
  29. import org.openspcoop2.monitor.sdk.transaction.FaseTracciamento;
  30. import org.openspcoop2.protocol.sdk.ProtocolException;
  31. import org.openspcoop2.protocol.sdk.constants.CostantiProtocollo;
  32. import org.openspcoop2.protocol.sdk.constants.EsitoTransazioneName;
  33. import org.openspcoop2.protocol.utils.EsitiProperties;
  34. import org.slf4j.Logger;

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

  43.     public static final String ALL_LABEL  = "[Qualsiasi]";
  44.     public static final String ALL_ERROR_LABEL  = "Fallite";
  45.     public static final String ALL_OK_LABEL  = "Completate con successo";
  46.     public static final String ALL_FAULT_APPLICATIVO_LABEL  = "Fault Applicativo";
  47.     public static final String ALL_PERSONALIZZATO_LABEL  = "Personalizzato";
  48.     public static final String ALL_ERROR_FAULT_APPLICATIVO_LABEL  = "Fallite - Fault Applicativo";
  49.     public static final String ALL_ERROR_CONSEGNA_LABEL  = "Errori di Consegna";
  50.     public static final String ALL_ERROR_RICHIESTE_SCARTATE_LABEL  = "Richieste Scartate";
  51.    
  52.     public static final String ALL_VALUE_AS_STRING = "-";
  53.    
  54.     public static final Integer ALL_VALUE = -1;
  55.     public static final Integer ALL_ERROR_VALUE = -2;
  56.     public static final Integer ALL_OK_VALUE = -3;
  57.     public static final Integer ALL_FAULT_APPLICATIVO_VALUE = -4;
  58.     public static final Integer ALL_PERSONALIZZATO_VALUE = -5;
  59.     public static final Integer ALL_ERROR_FAULT_APPLICATIVO_VALUE = -6;
  60.     public static final Integer ALL_ERROR_CONSEGNA_VALUE = -7;
  61.     public static final Integer ALL_ERROR_RICHIESTE_SCARTATE_VALUE = -8;
  62.    
  63.     public static final boolean DEFAULT_VALUE_ESCLUDI_RICHIESTE_SCARTATE = true;
  64.    
  65.     public static final String LABEL_ESITO_CONSEGNA_MULTIPLA_SENZA_STATI = "Consegna Multipla";
  66.    
  67.     private Logger logger;
  68.     private EsitiProperties esitiProperties;
  69.     public EsitoUtils(Logger logger,String protocollo) throws ProtocolException{
  70.         this.logger = logger;
  71.         this.esitiProperties = EsitiProperties.getInstanceFromProtocolName(this.logger,protocollo);
  72.     }
  73.    
  74.     public void setExpression(IExpression expr,Integer esitoGruppo, Integer esitoDettaglio, Integer[] esitoDettaglioPersonalizzato, String contesto , boolean escludiRichiesteScartate,
  75.             IField fieldEsito, IField fieldContesto, IExpression newExpression) throws ProtocolException,ExpressionException, ExpressionNotImplementedException, ServiceException, NotImplementedException{
  76.            
  77.         this.setExpression(expr, esitoGruppo, esitoDettaglio, esitoDettaglioPersonalizzato, escludiRichiesteScartate, fieldEsito, newExpression);
  78.        
  79.         this.setExpressionContesto(expr, fieldContesto, contesto);

  80.     }
  81.    
  82.     public void setExpression(IExpression expr,Integer esitoGruppo, Integer esitoDettaglio, Integer[] esitoDettaglioPersonalizzato, boolean escludiRichiesteScartate,
  83.             IField fieldEsito, IExpression newExpression) throws ProtocolException,ExpressionException, ExpressionNotImplementedException, ServiceException, NotImplementedException{
  84.            
  85.         boolean senzaFiltro =
  86.                 (esitoGruppo!=null && EsitoUtils.ALL_VALUE.intValue() == esitoGruppo.intValue())
  87.                 &&
  88.                 (esitoDettaglio!=null && EsitoUtils.ALL_VALUE.intValue() == esitoDettaglio.intValue());
  89.         boolean soloOk =
  90.                 (esitoGruppo!=null && EsitoUtils.ALL_OK_VALUE.intValue() == esitoGruppo.intValue())
  91.                 &&
  92.                 (esitoDettaglio!=null && EsitoUtils.ALL_VALUE.intValue() == esitoDettaglio.intValue());
  93.         boolean faultApplicativo =
  94.                 (esitoGruppo!=null && EsitoUtils.ALL_FAULT_APPLICATIVO_VALUE.intValue() == esitoGruppo.intValue());
  95.         boolean soloErrori =
  96.                 (esitoGruppo!=null && EsitoUtils.ALL_ERROR_VALUE.intValue() == esitoGruppo.intValue())
  97.                 &&
  98.                 (esitoDettaglio!=null && EsitoUtils.ALL_VALUE.intValue() == esitoDettaglio.intValue());
  99.         boolean personalizzato =
  100.                 (esitoGruppo!=null && EsitoUtils.ALL_PERSONALIZZATO_VALUE.intValue() == esitoGruppo.intValue());
  101.         boolean soloErroriPiuFaultApplicativi =
  102.                 (esitoGruppo!=null && EsitoUtils.ALL_ERROR_FAULT_APPLICATIVO_VALUE.intValue() == esitoGruppo.intValue())
  103.                 &&
  104.                 (esitoDettaglio!=null && EsitoUtils.ALL_VALUE.intValue() == esitoDettaglio.intValue());
  105.         boolean soloErroriConsegna =
  106.                 (esitoGruppo!=null && EsitoUtils.ALL_ERROR_CONSEGNA_VALUE.intValue() == esitoGruppo.intValue())
  107.                 &&
  108.                 (esitoDettaglio!=null && EsitoUtils.ALL_VALUE.intValue() == esitoDettaglio.intValue());
  109.         boolean soloRichiesteScartate =
  110.                 (esitoGruppo!=null && EsitoUtils.ALL_ERROR_RICHIESTE_SCARTATE_VALUE.intValue() == esitoGruppo.intValue())
  111.                 &&
  112.                 (esitoDettaglio!=null && EsitoUtils.ALL_VALUE.intValue() == esitoDettaglio.intValue());
  113.        
  114.         if(senzaFiltro && escludiRichiesteScartate) {
  115.             senzaFiltro = false;
  116.         }
  117.        
  118.         if(!senzaFiltro){
  119.            
  120.             if(personalizzato){
  121.                 if(esitoDettaglioPersonalizzato==null || esitoDettaglioPersonalizzato.length<=0){
  122.                     throw new ServiceException("Esito Personalizzato richiede la selezione di almeno un dettaglio");
  123.                 }
  124.                 expr.and().in(fieldEsito, (Object[]) esitoDettaglioPersonalizzato);
  125.             }
  126.             else if(soloOk){
  127.                 List<Integer> esitiOk = this.esitiProperties.getEsitiCodeOk_senzaFaultApplicativo();
  128.                 expr.and().in(fieldEsito, esitiOk);
  129.             }
  130.             else if(faultApplicativo){
  131.                 int codeFaultApplicativo = this.esitiProperties.convertNameToCode(EsitoTransazioneName.ERRORE_APPLICATIVO.name());
  132.                 expr.and().equals(fieldEsito, codeFaultApplicativo);
  133.             }
  134.             else if(soloErrori || soloErroriPiuFaultApplicativi){
  135.                 // Troppi valori dentro gli IN
  136. //              List<Integer> esitiKo = esitiProperties.getEsitiCodeKo();
  137. //              expr.and().in(fieldEsito, esitiKo);
  138.                
  139.                 IExpression exprOk = newExpression;
  140.                 List<Integer> esitiOk = null;
  141.                 if(soloErrori) {
  142.                     esitiOk = this.esitiProperties.getEsitiCodeOk(); // li prendo tutti anche il fault, poichè faccio il not
  143.                 }
  144.                 else {
  145.                     esitiOk = this.esitiProperties.getEsitiCodeOk_senzaFaultApplicativo();
  146.                 }
  147.                 exprOk.and().in(fieldEsito, esitiOk);
  148.                 expr.and().not(exprOk);
  149.                
  150.                 if(escludiRichiesteScartate) {
  151.                     List<Integer> esitiRichiesteMalformate = this.esitiProperties.getEsitiCodeRichiestaScartate();
  152.                     IExpression exprRichiesteMalformate = newExpression;
  153.                     exprRichiesteMalformate.and().in(fieldEsito, esitiRichiesteMalformate);
  154.                     expr.and().not(exprRichiesteMalformate);
  155.                 }
  156.             }
  157.             else if(soloErroriConsegna) {
  158.                 List<Integer> esitiErroriConsegna = this.esitiProperties.getEsitiCodeErroriConsegna();
  159.                 expr.and().in(fieldEsito, esitiErroriConsegna);
  160.             }
  161.             else if(soloRichiesteScartate) {
  162.                 List<Integer> esitiRichiesteMalformate = this.esitiProperties.getEsitiCodeRichiestaScartate();
  163.                 expr.and().in(fieldEsito, esitiRichiesteMalformate);
  164.             }
  165.             else{
  166.                 if(esitoDettaglio!=null && (esitoDettaglio.intValue() == ALL_FAULT_APPLICATIVO_VALUE.intValue())){
  167.                     // si tratta del fault, devo trasformarlo nel codice ufficiale
  168.                     // Questo caso avviene quando si seleziona qualsiasi esito, e poi come dettaglio il fault
  169.                     int codeFaultApplicativo = this.esitiProperties.convertNameToCode(EsitoTransazioneName.ERRORE_APPLICATIVO.name());
  170.                     expr.and().equals(fieldEsito, codeFaultApplicativo);
  171.                 }
  172.                 else if(esitoDettaglio!=null && esitoDettaglio>=0){
  173.                     expr.and().equals(fieldEsito, esitoDettaglio);
  174.                 }
  175.                 else if(escludiRichiesteScartate) {
  176.                     List<Integer> esitiRichiesteMalformate = this.esitiProperties.getEsitiCodeRichiestaScartate();
  177.                     IExpression exprRichiesteMalformate = newExpression;
  178.                     exprRichiesteMalformate.and().in(fieldEsito, esitiRichiesteMalformate);
  179.                     expr.and().not(exprRichiesteMalformate);
  180.                 }
  181.             }
  182.            
  183.         }
  184.        
  185.     }
  186.    
  187.     public void setExpressionContesto(IExpression expr,IField fieldContesto,String contesto) throws ExpressionNotImplementedException, ExpressionException{
  188.         if(!EsitoUtils.ALL_VALUE_AS_STRING.equals(contesto)){
  189.             expr.and().equals(fieldContesto, contesto);
  190.         }
  191.     }
  192.    
  193.     public Integer getEsitoValueFromLabel(String label){
  194.         if(ALL_LABEL.equals(label)){
  195.             return ALL_VALUE;
  196.         }
  197.         else if(ALL_ERROR_LABEL.equals(label)){
  198.             return ALL_ERROR_VALUE;
  199.         }
  200.         else if(ALL_OK_LABEL.equals(label)){
  201.             return ALL_OK_VALUE;
  202.         }
  203.         else if(ALL_FAULT_APPLICATIVO_LABEL.equals(label)){
  204.             return ALL_FAULT_APPLICATIVO_VALUE;
  205.         }
  206.         else if(ALL_PERSONALIZZATO_LABEL.equals(label)){
  207.             return ALL_PERSONALIZZATO_VALUE;
  208.         }
  209.         else if(ALL_ERROR_FAULT_APPLICATIVO_LABEL.equals(label)){
  210.             return ALL_ERROR_FAULT_APPLICATIVO_VALUE;
  211.         }
  212.         else if(ALL_ERROR_CONSEGNA_LABEL.equals(label)){
  213.             return ALL_ERROR_CONSEGNA_VALUE;
  214.         }
  215.         else if(ALL_ERROR_RICHIESTE_SCARTATE_LABEL.equals(label)){
  216.             return ALL_ERROR_RICHIESTE_SCARTATE_VALUE;
  217.         }
  218.         else if(LABEL_ESITO_CONSEGNA_MULTIPLA_SENZA_STATI.equals(label)) {
  219.             try{
  220.                 return this.esitiProperties.convertoToCode(EsitoTransazioneName.CONSEGNA_MULTIPLA);
  221.             }catch(Exception e){
  222.                 this.logger.error("Conversione non riuscita: "+e.getMessage(),e);
  223.                 return null;
  224.             }
  225.         }
  226.        
  227.         try{
  228.             return this.esitiProperties.convertLabelToCode(label);
  229.         }catch(Exception e){
  230.             this.logger.error("Conversione non riuscita: "+e.getMessage(),e);
  231.             return null;
  232.         }
  233.     }
  234.    
  235.     public String getEsitoLabelFromValue(Object value, boolean consegnaMultiplaSenzaVariStati){
  236.         if(value!=null && value instanceof Integer){
  237.            
  238.             if(ALL_VALUE.intValue() == ((Integer)value).intValue()){
  239.                 return ALL_LABEL;
  240.             }
  241.             else if(ALL_ERROR_VALUE.intValue() == ((Integer)value).intValue()){
  242.                 return ALL_ERROR_LABEL;
  243.             }
  244.             else if(ALL_OK_VALUE.intValue() == ((Integer)value).intValue()){
  245.                 return ALL_OK_LABEL;
  246.             }
  247.             else if(ALL_FAULT_APPLICATIVO_VALUE.intValue() == ((Integer)value).intValue()){
  248.                 return ALL_FAULT_APPLICATIVO_LABEL;
  249.             }
  250.             else if(ALL_PERSONALIZZATO_VALUE.intValue() == ((Integer)value).intValue()){
  251.                 return ALL_PERSONALIZZATO_LABEL;
  252.             }
  253.             else if(ALL_ERROR_FAULT_APPLICATIVO_VALUE.intValue() == ((Integer)value).intValue()){
  254.                 return ALL_ERROR_FAULT_APPLICATIVO_LABEL;
  255.             }
  256.             else if(ALL_ERROR_CONSEGNA_VALUE.intValue() == ((Integer)value).intValue()){
  257.                 return ALL_ERROR_CONSEGNA_LABEL;
  258.             }
  259.             else if(ALL_ERROR_RICHIESTE_SCARTATE_VALUE.intValue() == ((Integer)value).intValue()){
  260.                 return ALL_ERROR_RICHIESTE_SCARTATE_LABEL;
  261.             }
  262.            
  263.            
  264.             try{
  265.                 int valueInt = (Integer)value;
  266.                 if(consegnaMultiplaSenzaVariStati) {
  267.                     int consegnaMultipla = this.esitiProperties.convertoToCode(EsitoTransazioneName.CONSEGNA_MULTIPLA);
  268.                     if(consegnaMultipla == valueInt) {
  269.                         return LABEL_ESITO_CONSEGNA_MULTIPLA_SENZA_STATI;
  270.                     }
  271.                 }
  272.                 return this.esitiProperties.getEsitoLabel(valueInt);
  273.             }catch(Exception e){
  274.                 this.logger.error("Conversione non riuscita: "+e.getMessage(),e);
  275.                 return "Conversione non riuscita";
  276.             }
  277.         }
  278.         else{
  279.             this.logger.error("Conversione non riuscita: tipo ["+(value!=null ? value.getClass().getName() : "value is null")+"] non supportato");
  280.             return "Conversione non riuscita";
  281.         }
  282.     }
  283.    
  284.     public String getEsitoContestoValueFromLabel(String label){
  285.        
  286.         if(ALL_LABEL.equals(label)){
  287.             return ALL_VALUE_AS_STRING;
  288.         }
  289.        
  290.         try{
  291.             return this.esitiProperties.convertLabelToContextTypeCode(label);
  292.         }catch(Exception e){
  293.             this.logger.error("Conversione non riuscita: "+e.getMessage(),e);
  294.             return null;
  295.         }
  296.     }
  297.    
  298.     public String getEsitoContestoLabelFromValue(Object value){
  299.         if(value instanceof String){
  300.            
  301.             if(ALL_VALUE_AS_STRING.equals(value)){
  302.                 return ALL_LABEL;
  303.             }
  304.            
  305.             try{
  306.                 boolean moreContext = this.esitiProperties.getEsitiTransactionContextCode().size()>1;
  307.                
  308.                 String code = (String)value;
  309.                
  310.                 String label = null;
  311.                 if(isFaseRequestIn(code)) {
  312.                     label = (moreContext ? this.esitiProperties.getEsitoTransactionContextLabel(getRawEsitoContext(code))+" - " : "" ) + CostantiLabel.LABEL_CONFIGURAZIONE_AVANZATA_REQ_IN;
  313.                 }
  314.                 else if(isFaseRequestOut(code)) {
  315.                     label = (moreContext ? this.esitiProperties.getEsitoTransactionContextLabel(getRawEsitoContext(code))+" - " : "" ) + CostantiLabel.LABEL_CONFIGURAZIONE_AVANZATA_REQ_OUT;
  316.                 }
  317.                 else if(isFaseResponseOut(code)) {
  318.                     label = (moreContext ? this.esitiProperties.getEsitoTransactionContextLabel(getRawEsitoContext(code))+" - " : "" ) + CostantiLabel.LABEL_CONFIGURAZIONE_AVANZATA_RES_OUT;
  319.                 }
  320.                 else {
  321.                     label = this.esitiProperties.getEsitoTransactionContextLabel(code);
  322.                 }
  323.                
  324.                 return label;
  325.             }catch(Exception e){
  326.                 this.logger.error("Conversione non riuscita: "+e.getMessage(),e);
  327.                 return "Conversione non riuscita";
  328.             }
  329.         }
  330.         else{
  331.             this.logger.error("Conversione non riuscita: tipo ["+value.getClass().getName()+"] non supportato");
  332.             return "Conversione non riuscita";
  333.         }
  334.     }
  335.    
  336.    
  337.    
  338.    
  339.     private static final String SUFFIX_IN_REQUEST = "_irq";
  340.     private static final String SUFFIX_OUT_REQUEST = "_orq";
  341.     private static final String SUFFIX_OUT_RESPONSE = "_ors";
  342.     public static String buildEsitoContext(String esitoContext, FaseTracciamento fase) {
  343.         // non deve superare i 20 caratteri
  344.         if(esitoContext==null) {
  345.             esitoContext= CostantiProtocollo.ESITO_TRANSACTION_CONTEXT_STANDARD;
  346.         }
  347.         switch (fase) {
  348.         case IN_REQUEST:
  349.             return esitoContext + SUFFIX_IN_REQUEST;
  350.         case OUT_REQUEST:
  351.             return esitoContext + SUFFIX_OUT_REQUEST;
  352.         case OUT_RESPONSE:
  353.             return esitoContext + SUFFIX_OUT_RESPONSE;
  354.         default:
  355.             return esitoContext;
  356.         }
  357.     }
  358.     public static boolean isFaseIntermedia(String esito) {
  359.         return isFaseRequestIn(esito) || isFaseRequestOut(esito) || isFaseResponseOut(esito);
  360.     }
  361.     public static String getRawEsitoContext(String esito) {
  362.         if(isFaseIntermedia(esito) && esito.length()>SUFFIX_IN_REQUEST.length()) { // tutti e 3 stessa lunghezza
  363.             return esito.substring(0,esito.length()-SUFFIX_IN_REQUEST.length());
  364.         }
  365.         return esito;
  366.     }
  367.     public static boolean isFaseRequestIn(String esito) {
  368.         return esito!=null && esito.endsWith(SUFFIX_IN_REQUEST);
  369.     }
  370.     public static boolean isFaseRequestOut(String esito) {
  371.         return esito!=null && esito.endsWith(SUFFIX_OUT_REQUEST);
  372.     }
  373.     public static boolean isFaseResponseOut(String esito) {
  374.         return esito!=null && esito.endsWith(SUFFIX_OUT_RESPONSE);
  375.     }
  376. }