StatisticheMensili.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.StatisticaMensile;
  24. import org.openspcoop2.core.statistiche.constants.TipoIntervalloStatistico;
  25. import org.openspcoop2.core.statistiche.dao.IDBStatisticaMensileService;
  26. import org.openspcoop2.core.statistiche.dao.IStatisticaMensileService;
  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.slf4j.Logger;
  34. import org.openspcoop2.generic_project.exception.NotImplementedException;
  35. import org.openspcoop2.generic_project.exception.ServiceException;

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

  44.     public static StatisticheMensili getInstanceForUtils(){
  45.         return new StatisticheMensili();
  46.     }
  47.     StatisticheMensili(){
  48.             super();
  49.     }
  50.     public StatisticheMensili(
  51.             Logger logger, boolean logQuery, boolean useUnionForLatency,  
  52.             boolean generazioneStatisticheCustom, boolean analisiTransazioniCustom,
  53.             StatisticsForceIndexConfig forceIndexConfig,
  54.             org.openspcoop2.core.statistiche.dao.IServiceManager statisticheSM,
  55.             org.openspcoop2.core.transazioni.dao.IServiceManager transazioniSM,
  56.             org.openspcoop2.monitor.engine.config.statistiche.dao.IServiceManager pluginsStatisticheSM,
  57.             org.openspcoop2.core.plugins.dao.IServiceManager pluginsBaseSM,
  58.             org.openspcoop2.core.commons.search.dao.IServiceManager utilsSM,
  59.             org.openspcoop2.monitor.engine.config.transazioni.dao.IServiceManager pluginsTransazioniSM) {
  60.         super( logger, logQuery, useUnionForLatency,
  61.                 generazioneStatisticheCustom,analisiTransazioniCustom,
  62.                 forceIndexConfig,
  63.                 statisticheSM,transazioniSM,
  64.                 pluginsStatisticheSM,pluginsBaseSM,utilsSM,pluginsTransazioniSM);
  65.        
  66.         try {
  67.            
  68.             if(statisticheSM==null){
  69.                 throw new ServiceException("ServiceManager ["+org.openspcoop2.core.statistiche.dao.IServiceManager.class.getName()+"] non inizializzato");
  70.             }
  71.            
  72.             this.model = StatisticaMensile.model().STATISTICA_BASE;
  73.             this.statisticaServiceDAO = this.statisticheSM.getStatisticaMensileService( );
  74.             this.statisticaServiceSearchDAO = this.statisticheSM.getStatisticaMensileServiceSearch();
  75.        
  76.         } catch (ServiceException e) {
  77.             this.logger.error(e.getMessage(), e);
  78.         } catch (NotImplementedException e) {
  79.             this.logger.error(e.getMessage(), e);
  80.         }
  81.     }
  82.    
  83.     private Calendar truncDate(Date date){
  84.         Calendar cTmp = Calendar.getInstance();
  85.         cTmp.setTime(date);
  86.         cTmp.set(Calendar.DAY_OF_MONTH, 1);
  87.         cTmp.set(Calendar.HOUR_OF_DAY, 0);
  88.         cTmp.set(Calendar.MINUTE, 0);
  89.         cTmp.set(Calendar.SECOND, 0);
  90.         cTmp.set(Calendar.MILLISECOND, 0);
  91.         return cTmp;
  92.     }
  93.    
  94.     @Override
  95.     public Date truncDate(Date date,boolean print){
  96.         Calendar cTmp = truncDate(date);
  97.         if(print){
  98.             printCalendar(cTmp);
  99.         }
  100.         return cTmp.getTime();
  101.     }
  102.    
  103.     @Override
  104.     public String getIntervalloStatistica(Date date,boolean print){
  105.        
  106.         //System.out.println("INTERVALLO MENSILE PER ["+date.toString()+"]");
  107.        
  108.         Calendar cTmp = truncDate(date);
  109.         StringBuilder bf = new StringBuilder();
  110.         switch (cTmp.get(Calendar.MONTH)) {
  111.         case Calendar.JANUARY:
  112.             bf.append("Gennaio");  
  113.             break;
  114.         case Calendar.FEBRUARY:
  115.             bf.append("Febbraio");  
  116.             break;
  117.         case Calendar.MARCH:
  118.             bf.append("Marzo");
  119.             break;
  120.         case Calendar.APRIL:
  121.             bf.append("Aprile");    
  122.             break;
  123.         case Calendar.MAY:
  124.             bf.append("Maggio");    
  125.             break;
  126.         case Calendar.JUNE:
  127.             bf.append("Giugno");    
  128.             break;
  129.         case Calendar.JULY:
  130.             bf.append("Luglio");    
  131.             break;
  132.         case Calendar.AUGUST:
  133.             bf.append("Agosto");    
  134.             break;
  135.         case Calendar.SEPTEMBER:
  136.             bf.append("Settembre");
  137.             break;
  138.         case Calendar.OCTOBER:
  139.             bf.append("Ottobre");  
  140.             break;
  141.         case Calendar.NOVEMBER:
  142.             bf.append("Novembre");  
  143.             break;
  144.         case Calendar.DECEMBER:
  145.             bf.append("Dicembre");  
  146.             break;
  147.         default:
  148.             bf.append("Non definito?");
  149.             break;
  150.         }
  151.        
  152.         //System.out.println("INTERVALLO MENSILE PER ["+date.toString()+"]: "+bf.toString());
  153.        
  154.         return bf.toString();
  155.     }
  156.    
  157.     @Override
  158.     public Date incrementDate(Date date,boolean print){
  159.         return operation(date, print, +1);
  160.     }
  161.    
  162.     @Override
  163.     public Date decrementDate(Date date,boolean print){
  164.         return operation(date, print, -1);
  165.     }
  166.    
  167.     private Date operation(Date date,boolean print, int value){
  168.         Calendar cTmp = Calendar.getInstance();
  169.         cTmp.setTime(date);
  170.         if(print){
  171.             super.logger.debug(">>> incrementDate >>>>");
  172.             super.logger.debug("Before: ");
  173.             printCalendar(cTmp);
  174.         }
  175.         /*else{
  176.             super.log.debug("***** ("+value+") data PRIMA OPERAZIONE:");
  177.             printCalendar(cTmp);
  178.         }*/
  179.        
  180.         // prelevo giorno impostato nella data
  181.         int giorno = cTmp.get(Calendar.DAY_OF_MONTH);
  182.         //super.log.debug("***** ("+value+") prelevo giorno impostato nella data: "+giorno);
  183.        
  184.         // prelevo ultimo giorno del mese impostato nell'attuale data
  185.         int lastDayActualMonth = cTmp.getActualMaximum(Calendar.DAY_OF_MONTH);
  186.         //super.log.debug("***** ("+value+") prelevo ultimo giorno del mese impostato nell'attuale data: "+lastDayActualMonth);
  187.        
  188.         // incremento
  189.         //super.log.debug("***** ("+value+") effettuo operazione");
  190.         cTmp.add(Calendar.MONTH, value);
  191.        
  192.         // prelevo giorno impostato nella data modificata
  193.         //int giornoDataModificata = cTmp.get(Calendar.DAY_OF_MONTH);
  194.         //super.log.debug("***** ("+value+") prelevo giorno impostato nella data modificata: "+giornoDataModificata);
  195.        
  196.         // Se il giorno della data originale era uguale all'ultimo giorno del mese della data originale,
  197.         // ripristino questa condizione anche nella data modificata
  198.         if(giorno == lastDayActualMonth){
  199.            
  200.             //super.log.debug("***** ("+value+") ripristino ultimo giorno del mese");
  201.            
  202.             // prelevo ultimo giorno del mese impostato nella data modificata
  203.             int lastDayMonth = cTmp.getActualMaximum(Calendar.DAY_OF_MONTH);
  204.             //super.log.debug("***** ("+value+") prelevo ultimo giorno del mese impostato nella data modificata: "+lastDayMonth);
  205.            
  206.             // ripristino condizione
  207.             cTmp.set(Calendar.DAY_OF_MONTH, lastDayMonth);
  208.            
  209.             //int giornoDataRipristinata = cTmp.get(Calendar.DAY_OF_MONTH);
  210.             //super.log.debug("***** ("+value+") prelevo giorno impostato nella data ripristinata: "+giornoDataRipristinata);
  211.            
  212.         }
  213.        
  214.         if(print){
  215.             super.logger.debug("After: ");
  216.             printCalendar(cTmp);
  217.             super.logger.debug(">>> incrementDate end >>>>");
  218.         }
  219.         /*else{
  220.             super.log.debug("***** ("+value+") data FINE OPERAZIONE:");
  221.             printCalendar(cTmp);
  222.         }*/
  223.         return cTmp.getTime();
  224.     }
  225.    
  226.     @Override
  227.     public TipoIntervalloStatistico getTipoStatistiche(){
  228.         return TipoIntervalloStatistico.STATISTICHE_MENSILI;
  229.     }
  230.    
  231.     @Override
  232.     public void callStatisticPluginMethod(IStatisticProcessing statProcessing,
  233.             StatisticBean stat) throws StatisticException {
  234.         IStatistic statContext = new CustomStatisticsSdkGenerator(stat, StatisticType.MENSILE, this);
  235.         statProcessing.createMonthlyStatisticData(statContext);
  236.     }
  237.    
  238.     @Override
  239.     public String getStatisticPluginMethodName() throws StatisticException {
  240.         return "createMonthlyStatisticData";
  241.     }
  242.    

  243.     @Override
  244.     protected Long insertStatistica(Statistica statistica) throws StatisticException {
  245.        
  246.         try{
  247.        
  248.             StatisticaMensile stat = new StatisticaMensile();
  249.             stat.setStatisticaBase(statistica);
  250.             ((IStatisticaMensileService)this.statisticaServiceDAO).create(stat);
  251.             return stat.getId();
  252.            
  253.         }catch(Exception e){
  254.             throw new StatisticException(e.getMessage(),e);
  255.         }

  256.     }
  257.    
  258.     @Override
  259.     protected void updateStatistica(long idStatistica, StatisticaContenuti ... statisticaContenuti) throws StatisticException {
  260.        
  261.         try{
  262.        
  263.             StatisticaMensile stat = ((IDBStatisticaMensileService)this.statisticaServiceDAO).get(idStatistica);
  264.             for (int i = 0; i < statisticaContenuti.length; i++) {
  265.                 statisticaContenuti[i].setData(stat.getStatisticaBase().getData());
  266.                 stat.addStatisticaMensileContenuti(statisticaContenuti[i]);
  267.             }
  268.             ((IStatisticaMensileService)this.statisticaServiceDAO).update(stat);
  269.            
  270.         }catch(Exception e){
  271.             throw new StatisticException(e.getMessage(),e);
  272.         }

  273.     }
  274.    
  275. }