StatisticheOrarie.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.statistic;

  21. import org.openspcoop2.core.statistiche.Statistica;
  22. import org.openspcoop2.core.statistiche.StatisticaContenuti;
  23. import org.openspcoop2.core.statistiche.StatisticaOraria;
  24. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  25. import org.openspcoop2.core.statistiche.dao.IDBStatisticaOrariaService;
  26. import org.openspcoop2.core.statistiche.dao.IStatisticaOrariaService;
  27. import org.openspcoop2.monitor.sdk.constants.StatisticType;
  28. import org.openspcoop2.monitor.sdk.exceptions.StatisticException;
  29. import org.openspcoop2.monitor.sdk.plugins.IStatisticProcessing;
  30. import org.openspcoop2.monitor.sdk.statistic.IStatistic;

  31. import java.util.Calendar;
  32. import java.util.Date;

  33. import org.openspcoop2.generic_project.exception.NotImplementedException;
  34. import org.openspcoop2.generic_project.exception.ServiceException;

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

  43.     public static StatisticheOrarie getInstanceForUtils(){
  44.         return new StatisticheOrarie();
  45.     }
  46.     StatisticheOrarie(){
  47.             super();
  48.     }
  49.    
  50.     @Override
  51.     public void init(
  52.             StatisticsConfig config,
  53.             org.openspcoop2.core.statistiche.dao.IServiceManager statisticheSM,
  54.             org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM,
  55.             org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager pluginsStatisticheSM,
  56.             org.openspcoop2.core.plugins.dao.IServiceManager pluginsBaseSM,
  57.             org.openspcoop2.core.commons.search.dao.IServiceManager utilsSM,
  58.             org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager pluginsTransazioniSM) {
  59.         super.init( config,
  60.                 statisticheSM,transazioniSM,
  61.                 pluginsStatisticheSM,pluginsBaseSM,utilsSM,pluginsTransazioniSM);
  62.        
  63.         try {
  64.            
  65.             if(statisticheSM==null){
  66.                 throw new ServiceException("ServiceManager ["+org.openspcoop2.core.statistiche.dao.IServiceManager.class.getName()+"] non inizializzato");
  67.             }
  68.            
  69.             this.model = StatisticaOraria.model().STATISTICA_BASE;
  70.             this.statisticaServiceDAO = this.statisticheSM.getStatisticaOrariaService();
  71.             this.statisticaServiceSearchDAO = this.statisticheSM.getStatisticaOrariaServiceSearch();
  72.        
  73.         } catch (ServiceException e) {
  74.             this.logger.error(e.getMessage(), e);
  75.         } catch (NotImplementedException e) {
  76.             this.logger.error(e.getMessage(), e);
  77.         }
  78.        
  79.        
  80.     }
  81.    
  82.     private Calendar truncDate(Date date){
  83.         Calendar cTmp = Calendar.getInstance();
  84.         cTmp.setTime(date);
  85.         cTmp.set(Calendar.MINUTE, 0);
  86.         cTmp.set(Calendar.SECOND, 0);
  87.         cTmp.set(Calendar.MILLISECOND, 0);
  88.         return cTmp;
  89.     }
  90.    
  91.     @Override
  92.     public Date truncDate(Date date,boolean print){
  93.         Calendar cTmp = truncDate(date);
  94.         if(print){
  95.             printCalendar(cTmp);
  96.         }
  97.         return cTmp.getTime();
  98.     }
  99.    
  100.     @Override
  101.     public String getIntervalloStatistica(Date date,boolean print){
  102.        
  103.         //System.out.println("INTERVALLO ORARIOE PER ["+date.toString()+"]");
  104.        
  105.         Calendar cTmp = truncDate(date);
  106.         StringBuilder bf = new StringBuilder();
  107.         bf.append(cTmp.get(Calendar.DAY_OF_MONTH));
  108.         bf.append("/");
  109.         bf.append((cTmp.get(Calendar.MONTH)+1));
  110.         bf.append("/");
  111.         bf.append(cTmp.get(Calendar.YEAR));
  112.         bf.append(" ");
  113.         bf.append(cTmp.get(Calendar.HOUR_OF_DAY));
  114.         bf.append(":00.000-");
  115.         bf.append(cTmp.get(Calendar.HOUR_OF_DAY));
  116.         bf.append(":59.999-");
  117.        
  118.         //System.out.println("INTERVALLO ORARIOE PER ["+date.toString()+"]: "+bf.toString());
  119.        
  120.         return bf.toString();
  121.     }
  122.    
  123.     @Override
  124.     public Date incrementDate(Date date,boolean print){
  125.         return operation(date, print, +1);
  126.     }
  127.    
  128.     @Override
  129.     public Date decrementDate(Date date,boolean print){
  130.         return operation(date, print, -1);
  131.     }
  132.    
  133.     private Date operation(Date date,boolean print, int value){
  134.    
  135.         Calendar cTmp = Calendar.getInstance();
  136.         cTmp.setTime(date);
  137.         if(print){
  138.             super.logger.debug(">>> incrementDate >>>>");
  139.             super.logger.debug("Before: ");
  140.             printCalendar(cTmp);
  141.         }
  142.         cTmp.add(Calendar.HOUR_OF_DAY, value);
  143.         if(print){
  144.             super.logger.debug("After: ");
  145.             printCalendar(cTmp);
  146.             super.logger.debug(">>> incrementDate end >>>>");
  147.         }
  148.         return cTmp.getTime();
  149.     }
  150.    
  151.     @Override
  152.     public TipoIntervalloStatistico getTipoStatistiche(){
  153.         return TipoIntervalloStatistico.STATISTICHE_ORARIE;
  154.     }
  155.  

  156.     @Override
  157.     public void callStatisticPluginMethod(IStatisticProcessing statProcessing,
  158.             StatisticBean stat) throws StatisticException {
  159.         IStatistic statContext = new CustomStatisticsSdkGenerator(stat, StatisticType.ORARIA , this);
  160.         statProcessing.createHourlyStatisticData(statContext);
  161.     }

  162.     @Override
  163.     public String getStatisticPluginMethodName() throws StatisticException {
  164.         return "createHourlyStatisticData";
  165.     }
  166.    
  167.    
  168.     @Override
  169.     protected Long insertStatistica(Statistica statistica) throws StatisticException {
  170.        
  171.         try{
  172.        
  173.             StatisticaOraria stat = new StatisticaOraria();
  174.             stat.setStatisticaBase(statistica);
  175.             ((IStatisticaOrariaService)this.statisticaServiceDAO).create(stat);
  176.             return stat.getId();
  177.            
  178.         }catch(Exception e){
  179.             throw new StatisticException(e.getMessage(),e);
  180.         }

  181.     }
  182.    
  183.     @Override
  184.     protected void updateStatistica(long idStatistica, StatisticaContenuti ... statisticaContenuti) throws StatisticException {
  185.        
  186.         try{
  187.        
  188.             StatisticaOraria stat = ((IDBStatisticaOrariaService)this.statisticaServiceDAO).get(idStatistica);
  189.             for (int i = 0; i < statisticaContenuti.length; i++) {
  190.                 statisticaContenuti[i].setData(stat.getStatisticaBase().getData());
  191.                 stat.addStatisticaOrariaContenuti(statisticaContenuti[i]);
  192.             }
  193.             ((IStatisticaOrariaService)this.statisticaServiceDAO).update(stat);
  194.            
  195.         }catch(Exception e){
  196.             throw new StatisticException(e.getMessage(),e);
  197.         }

  198.     }
  199.    
  200.     @Override
  201.     protected boolean isGestioneUltimoIntervallo(StatisticsConfig config) {
  202.         return config.isStatisticheOrarieGestioneUltimoIntervallo();
  203.     }
  204.    
  205.     @Override
  206.     public boolean isEnabled(StatisticsConfig config) {
  207.         return config.isStatisticheOrarie();
  208.     }
  209. }