InfoDiagnostico.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 java.text.SimpleDateFormat;
  22. import java.util.Calendar;
  23. import java.util.Date;

  24. import org.openspcoop2.core.commons.CoreException;
  25. import org.openspcoop2.protocol.sdk.diagnostica.MsgDiagnostico;
  26. import org.openspcoop2.utils.date.DateUtils;

  27. /**    
  28.  * InfoDiagnostico
  29.  *
  30.  * @author Poli Andrea (poli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public class InfoDiagnostico {

  35.     /*
  36.      * Ogni diagnostico e' rappresentato da 6 caratteri che indicano il codice +
  37.      * 9 caratteri che rappresentano l'ora nel formato HHMMSSsss +
  38.      * 1 caratteri che rappresenta il fatto se deve essere incrementata e di quanti giorni la data giornaliera
  39.      * rispetto alla data del primo diagnostici
  40.      */
  41.        
  42.     private Date gdoFirstDiagnostic;
  43.     private Date gdo;
  44.     private String code;
  45.    
  46.     public Date getGdo() {
  47.         return this.gdo;
  48.     }
  49.     public String getCode() {
  50.         return this.code;
  51.     }
  52.    
  53.     public InfoDiagnostico(Date gdoFirstDiagnostic, MsgDiagnostico msgDiag){
  54.         this.gdoFirstDiagnostic = gdoFirstDiagnostic;
  55.         this.gdo = msgDiag.getGdo();
  56.         this.code = msgDiag.getCodice();
  57.     }
  58.     private InfoDiagnostico(Date gdoFirstDiagnostic){
  59.         this.gdoFirstDiagnostic = gdoFirstDiagnostic;
  60.     }
  61.    
  62.     public String convertToDBValue(){
  63.         SimpleDateFormat dateformat = DateUtils.getDefaultTimeFormatter("HHmmssSSS");
  64.         StringBuilder bf = new StringBuilder();
  65.         bf.append(this.code);
  66.         bf.append(dateformat.format(this.gdo));
  67.         bf.append(diffDay(this.gdo, this.gdoFirstDiagnostic));
  68.         return bf.toString();
  69.     }
  70.    
  71.     public static InfoDiagnostico convertoFromDBColumnValue(Date gdoFirstDiagnostic, String dbValue) throws CoreException{
  72.         if(gdoFirstDiagnostic==null){
  73.             throw new CoreException("Parameter gdoFirstDiagnostic not defined");
  74.         }
  75.         if(dbValue==null){
  76.             throw new CoreException("Parameter dbValue not defined");
  77.         }
  78.         if(dbValue.length()!=16){
  79.             throw new CoreException("Formato diagnostico["+dbValue+"] possiede una lunghezza ["+dbValue.length()+"] differente da quella attesa di 16 caratteri (CODICEHHmmssSSST)");
  80.         }
  81.        
  82.         InfoDiagnostico info = new InfoDiagnostico(gdoFirstDiagnostic);
  83.        
  84.         // ** codice diagnostico **
  85.         info.code = dbValue.substring(0, 6);
  86.        
  87.         // ** data **
  88.         // prelevo la data dal primo diagnostico
  89.         SimpleDateFormat dateformat = DateUtils.getDefaultDateFormatter("yyyyMMdd");
  90.         String data = dateformat.format(gdoFirstDiagnostic);
  91.         // aggiungo il tempo presente nell'informazione letta dal database
  92.         String dataConTime = data + dbValue.substring(6, (6+"HHmmssSSS".length()));
  93.         dateformat =  DateUtils.getDefaultDateTimeFormatter("yyyyMMddHHmmssSSS");
  94.         Date gdo = null;
  95.         try {
  96.             gdo = dateformat.parse(dataConTime);
  97.         }catch(Exception e) {
  98.             throw new CoreException(e.getMessage(),e);
  99.         }
  100.         // aggiungo la differenza temporale dei giorni rispetto alla data del primo diagnostico
  101.         long dayDiff = diffDay(gdoFirstDiagnostic, gdo);
  102.         Calendar calendar = Calendar.getInstance();
  103.         calendar.setTime(gdo);
  104.         calendar.add(Calendar.DAY_OF_YEAR,(int) dayDiff);
  105.         info.gdo = calendar.getTime();
  106.        
  107.         return info;
  108.     }

  109.     public static long diffDay(Date gdoFirstDiagnosticParam, Date gdo){
  110.         return (gdo.getTime() - gdoFirstDiagnosticParam.getTime()) / (24 * 3600 * 1000);
  111.     }
  112. }