DynamicExtendedInfoDiagnostico.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.pdd.logger.diagnostica;

  21. import org.openspcoop2.core.commons.CoreException;

  22. /**    
  23.  * DynamicExtendedInfoDiagnostico
  24.  *
  25.  * @author Poli Andrea (poli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  */
  29. public class DynamicExtendedInfoDiagnostico {

  30.     private DynamicExtendedInfoDiagnosticoType type;
  31.     private int diagnosticPosition;
  32.     private String value;
  33.    
  34.     public DynamicExtendedInfoDiagnostico(DynamicExtendedInfoDiagnosticoType type,int diagnosticPosition,String value){
  35.         this.type = type;
  36.         this.diagnosticPosition = diagnosticPosition;
  37.         this.value = value;
  38.     }
  39.    
  40.     public DynamicExtendedInfoDiagnosticoType getType() {
  41.         return this.type;
  42.     }

  43.     public void setType(DynamicExtendedInfoDiagnosticoType type) {
  44.         this.type = type;
  45.     }

  46.     public int getDiagnosticPosition() {
  47.         return this.diagnosticPosition;
  48.     }

  49.     public void setDiagnosticPosition(int diagnosticPosition) {
  50.         this.diagnosticPosition = diagnosticPosition;
  51.     }
  52.    
  53.     public String getValue() {
  54.         return this.value;
  55.     }

  56.     public void setValue(String value) {
  57.         this.value = value;
  58.     }
  59.    
  60.     public String convertToDBValue(){
  61.         StringBuilder bf = new StringBuilder();
  62.         bf.append(this.type.getValue());
  63.         bf.append(CostantiMappingDiagnostici.DIAGNOSTIC_WITH_DYNAMIC_INFO_TYPE_SEPARATOR);
  64.         bf.append(this.diagnosticPosition);
  65.         bf.append(CostantiMappingDiagnostici.DIAGNOSTIC_WITH_DYNAMIC_INFO_TYPE_SEPARATOR);
  66.         bf.append(this.value);
  67.         return bf.toString();
  68.     }
  69.    
  70.     public static DynamicExtendedInfoDiagnostico convertoFromDBColumnValue(String dbValue) throws CoreException{
  71.        
  72.         if(dbValue==null){
  73.             throw new CoreException("Parameter dbValue not defined");
  74.         }
  75.         if(!dbValue.contains(CostantiMappingDiagnostici.DIAGNOSTIC_WITH_DYNAMIC_INFO_TYPE_SEPARATOR)){
  76.             throw new CoreException("Parameter dbValue with wrong format ("+CostantiMappingDiagnostici.DIAGNOSTIC_WITH_DYNAMIC_INFO_TYPE_SEPARATOR+")");
  77.         }
  78.        
  79.         int indexType = dbValue.indexOf(CostantiMappingDiagnostici.DIAGNOSTIC_WITH_DYNAMIC_INFO_TYPE_SEPARATOR);
  80.         if(indexType<=0){
  81.             throw new CoreException("Parameter dbValue with wrong format (indexType:"+indexType+")");
  82.         }
  83.         if(indexType==dbValue.length()){
  84.             throw new CoreException("Parameter dbValue with wrong format (indexType:"+indexType+" equals length)");
  85.         }
  86.         String tipo = dbValue.substring(0, indexType);
  87.         DynamicExtendedInfoDiagnosticoType type = null;
  88.         try{
  89.             type = DynamicExtendedInfoDiagnosticoType.getEnum(tipo);
  90.         }catch(Exception e){
  91.             throw new CoreException("Parameter dbValue with wrong format (type unknwon '"+tipo+"')");
  92.         }
  93.        
  94.         int indexPosition = dbValue.indexOf(CostantiMappingDiagnostici.DIAGNOSTIC_WITH_DYNAMIC_INFO_TYPE_SEPARATOR,(tipo.length()+1));
  95.         String prefixIndexPosition = "Parameter dbValue with wrong format (indexPosition:"+indexPosition;
  96.         if(indexPosition<=0){
  97.             throw new CoreException(prefixIndexPosition+")");
  98.         }
  99.         if(indexPosition==dbValue.length()){
  100.             throw new CoreException(prefixIndexPosition+" equals length)");
  101.         }
  102.         String tmpPosition = dbValue.substring((tipo.length()+1), indexPosition);
  103.         int position = -1;
  104.         try{
  105.             position = Integer.parseInt(tmpPosition);
  106.         }catch(Exception e){
  107.             throw new CoreException(prefixIndexPosition+" format to int ["+tmpPosition+"] error) "+e.getMessage(),e);
  108.         }
  109.        
  110.         String value = dbValue.substring((indexPosition+1), dbValue.length());
  111.         if(value==null || "".equals(value)){
  112.             throw new CoreException("Parameter dbValue with wrong format (value undefined)");
  113.         }
  114.        
  115.         return new DynamicExtendedInfoDiagnostico(type, position, value);
  116.        
  117.     }
  118. }