StatisticheGiornaliere.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.StatisticaGiornaliera;
  24. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  25. import org.openspcoop2.core.statistiche.dao.IDBStatisticaGiornalieraService;
  26. import org.openspcoop2.core.statistiche.dao.IStatisticaGiornalieraService;
  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.  * StatisticheGiornaliere
  37.  *
  38.  * @author Poli Andrea (apoli@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class StatisticheGiornaliere extends AbstractStatistiche {

  43.     public static StatisticheGiornaliere getInstanceForUtils(){
  44.         return new StatisticheGiornaliere();
  45.     }
  46.     StatisticheGiornaliere(){
  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 = StatisticaGiornaliera.model().STATISTICA_BASE;
  70.             this.statisticaServiceDAO = this.statisticheSM.getStatisticaGiornalieraService();
  71.             this.statisticaServiceSearchDAO = this.statisticheSM.getStatisticaGiornalieraServiceSearch();
  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.     private Calendar truncDate(Date date){
  81.         Calendar cTmp = Calendar.getInstance();
  82.         cTmp.setTime(date);
  83.         cTmp.set(Calendar.HOUR_OF_DAY, 0);
  84.         cTmp.set(Calendar.MINUTE, 0);
  85.         cTmp.set(Calendar.SECOND, 0);
  86.         cTmp.set(Calendar.MILLISECOND, 0);
  87.         return cTmp;
  88.     }
  89.    
  90.     @Override
  91.     public Date truncDate(Date date,boolean print){
  92.         Calendar cTmp = truncDate(date);
  93.         if(print){
  94.             printCalendar(cTmp);
  95.         }
  96.         return cTmp.getTime();
  97.     }
  98.    
  99.     @Override
  100.     public String getIntervalloStatistica(Date date,boolean print){
  101.        
  102.         //System.out.println("INTERVALLO GIORNALIERO PER ["+date.toString()+"]");
  103.        
  104.         Calendar cTmp = truncDate(date);
  105.         StringBuilder bf = new StringBuilder();
  106.         bf.append(cTmp.get(Calendar.DAY_OF_MONTH));
  107.         bf.append("/");
  108.         bf.append((cTmp.get(Calendar.MONTH)+1));
  109.         bf.append("/");
  110.         bf.append(cTmp.get(Calendar.YEAR));
  111.        
  112.         //System.out.println("INTERVALLO GIORNALIERO PER ["+date.toString()+"]: "+bf.toString());
  113.        
  114.         return bf.toString();
  115.     }
  116.    
  117.     @Override
  118.     public Date incrementDate(Date date,boolean print){
  119.         return operation(date, print, +1);
  120.     }
  121.    
  122.     @Override
  123.     public Date decrementDate(Date date,boolean print){
  124.         return operation(date, print, -1);
  125.     }
  126.    
  127.     private Date operation(Date date,boolean print, int value){
  128.         Calendar cTmp = Calendar.getInstance();
  129.         cTmp.setTime(date);
  130.         if(print){
  131.             super.logger.debug(">>> incrementDate >>>>");
  132.             super.logger.debug("Before: ");
  133.             printCalendar(cTmp);
  134.         }
  135.         cTmp.add(Calendar.DAY_OF_YEAR, value);
  136.         if(print){
  137.             super.logger.debug("After: ");
  138.             printCalendar(cTmp);
  139.             super.logger.debug(">>> incrementDate end >>>>");
  140.         }
  141.         return cTmp.getTime();
  142.     }
  143.    
  144.     @Override
  145.     public TipoIntervalloStatistico getTipoStatistiche(){
  146.         return TipoIntervalloStatistico.STATISTICHE_GIORNALIERE;
  147.     }
  148.  
  149.     @Override
  150.     public void callStatisticPluginMethod(IStatisticProcessing statProcessing,
  151.             StatisticBean stat) throws StatisticException {
  152.         IStatistic statContext = new CustomStatisticsSdkGenerator(stat, StatisticType.GIORNALIERA, this);
  153.         statProcessing.createDailyStatisticData(statContext);
  154.     }


  155.     @Override
  156.     public String getStatisticPluginMethodName() throws StatisticException {
  157.         return "createDailyStatisticData";
  158.     }

  159.     @Override
  160.     protected Long insertStatistica(Statistica statistica) throws StatisticException {
  161.        
  162.         try{
  163.        
  164.             StatisticaGiornaliera stat = new StatisticaGiornaliera();
  165.             stat.setStatisticaBase(statistica);
  166.             ((IStatisticaGiornalieraService)this.statisticaServiceDAO).create(stat);
  167.             return stat.getId();
  168.            
  169.         }catch(Exception e){
  170.             throw new StatisticException(e.getMessage(),e);
  171.         }

  172.     }
  173.    
  174.     @Override
  175.     protected void updateStatistica(long idStatistica, StatisticaContenuti ... statisticaContenuti) throws StatisticException {
  176.        
  177.         try{
  178.        
  179.             StatisticaGiornaliera stat = ((IDBStatisticaGiornalieraService)this.statisticaServiceDAO).get(idStatistica);
  180.             for (int i = 0; i < statisticaContenuti.length; i++) {
  181.                 statisticaContenuti[i].setData(stat.getStatisticaBase().getData());
  182.                 stat.addStatisticaGiornalieraContenuti(statisticaContenuti[i]);
  183.             }
  184.             ((IStatisticaGiornalieraService)this.statisticaServiceDAO).update(stat);
  185.            
  186.         }catch(Exception e){
  187.             throw new StatisticException(e.getMessage(),e);
  188.         }

  189.     }
  190.    
  191.     @Override
  192.     protected boolean isGestioneUltimoIntervallo(StatisticsConfig config) {
  193.         return config.isStatisticheGiornaliereGestioneUltimoIntervallo();
  194.     }
  195.    
  196.     @Override
  197.     public boolean isEnabled(StatisticsConfig config) {
  198.         return config.isStatisticheGiornaliere();
  199.     }
  200. }