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

  44.     public static StatisticheSettimanali getInstanceForUtils(){
  45.         return new StatisticheSettimanali();
  46.     }
  47.     StatisticheSettimanali(){
  48.             super();
  49.     }
  50.     public StatisticheSettimanali(
  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 = StatisticaSettimanale.model().STATISTICA_BASE;
  73.             this.statisticaServiceDAO = this.statisticheSM.getStatisticaSettimanaleService();
  74.             this.statisticaServiceSearchDAO = this.statisticheSM.getStatisticaSettimanaleServiceSearch();
  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.         while(Calendar.MONDAY!=cTmp.get(Calendar.DAY_OF_WEEK)){
  87.             cTmp.add(Calendar.DAY_OF_WEEK, -1);
  88.         }
  89.         cTmp.set(Calendar.HOUR_OF_DAY, 0);
  90.         cTmp.set(Calendar.MINUTE, 0);
  91.         cTmp.set(Calendar.SECOND, 0);
  92.         cTmp.set(Calendar.MILLISECOND, 0);
  93.         return cTmp;
  94.     }
  95.    
  96.     @Override
  97.     public Date truncDate(Date date,boolean print){
  98.         Calendar cTmp = truncDate(date);
  99.         if(print){
  100.             printCalendar(cTmp);
  101.         }
  102.         return cTmp.getTime();
  103.     }
  104.    
  105.     @Override
  106.     public String getIntervalloStatistica(Date date,boolean print){
  107.        
  108.         //System.out.println("INTERVALLO SETTIMANALE PER ["+date.toString()+"]");
  109.        
  110.         Calendar cTmp = truncDate(date);
  111.         StringBuilder bf = new StringBuilder();
  112.         bf.append("Settimana N.");
  113.         bf.append(cTmp.get(Calendar.WEEK_OF_YEAR));
  114.        
  115.         bf.append(" (");
  116.        
  117.         switch (cTmp.get(Calendar.DAY_OF_WEEK)) {
  118.         case Calendar.MONDAY:
  119.             bf.append("Lunedì");
  120.             break;
  121.         case Calendar.TUESDAY:
  122.             bf.append("Martedì");
  123.             break;
  124.         case Calendar.WEDNESDAY:
  125.             bf.append("Mercoledì");
  126.             break;
  127.         case Calendar.THURSDAY:
  128.             bf.append("Giovedì");
  129.             break;
  130.         case Calendar.FRIDAY:
  131.             bf.append("Venerdì");
  132.             break;
  133.         case Calendar.SATURDAY:
  134.             bf.append("Sabato");
  135.             break;
  136.         case Calendar.SUNDAY:
  137.             bf.append("Domenica");
  138.             break;  
  139.         }
  140.         bf.append(",");
  141.         bf.append(cTmp.get(Calendar.DAY_OF_MONTH));
  142.         bf.append("/");
  143.         bf.append((cTmp.get(Calendar.MONTH)+1));
  144.         bf.append("/");
  145.         bf.append(cTmp.get(Calendar.YEAR));
  146.        
  147.         bf.append(" - ");
  148.         cTmp.add(Calendar.DAY_OF_YEAR, 7);
  149.         cTmp.add(Calendar.MILLISECOND, -1);
  150.         switch (cTmp.get(Calendar.DAY_OF_WEEK)) {
  151.         case Calendar.MONDAY:
  152.             bf.append("Lunedì");
  153.             break;
  154.         case Calendar.TUESDAY:
  155.             bf.append("Martedì");
  156.             break;
  157.         case Calendar.WEDNESDAY:
  158.             bf.append("Mercoledì");
  159.             break;
  160.         case Calendar.THURSDAY:
  161.             bf.append("Giovedì");
  162.             break;
  163.         case Calendar.FRIDAY:
  164.             bf.append("Venerdì");
  165.             break;
  166.         case Calendar.SATURDAY:
  167.             bf.append("Sabato");
  168.             break;
  169.         case Calendar.SUNDAY:
  170.             bf.append("Domenica");
  171.             break;
  172.         }
  173.         bf.append(",");
  174.         bf.append(cTmp.get(Calendar.DAY_OF_MONTH));
  175.         bf.append("/");
  176.         bf.append((cTmp.get(Calendar.MONTH)+1));
  177.         bf.append("/");
  178.         bf.append(cTmp.get(Calendar.YEAR));
  179.        
  180.         bf.append(")");
  181.        
  182.         //System.out.println("INTERVALLO SETTIMANALE PER ["+date.toString()+"]: "+bf.toString());
  183.        
  184.         return bf.toString();
  185.     }
  186.    
  187.     @Override
  188.     public Date incrementDate(Date date,boolean print){
  189.         //return operation(date, print, +1);
  190.         return operation(date, print, +7);
  191.     }
  192.    
  193.     @Override
  194.     public Date decrementDate(Date date,boolean print){
  195.         //return operation(date, print, -1);
  196.         return operation(date, print, -7);
  197.     }
  198.    
  199.     private Date operation(Date date,boolean print, int value){
  200.    
  201.         Calendar cTmp = Calendar.getInstance();
  202.         cTmp.setTime(date);
  203.         if(print){
  204.             super.logger.debug(">>> incrementDate >>>>");
  205.             super.logger.debug("Before: ");
  206.             printCalendar(cTmp);
  207.         }
  208.         //cTmp.add(Calendar.WEEK_OF_YEAR, 1);
  209.         cTmp.add(Calendar.DAY_OF_YEAR, value);
  210.         if(print){
  211.             super.logger.debug("After: ");
  212.             printCalendar(cTmp);
  213.             super.logger.debug(">>> incrementDate end >>>>");
  214.         }
  215.         return cTmp.getTime();
  216.     }
  217.    
  218.     @Override
  219.     public TipoIntervalloStatistico getTipoStatistiche(){
  220.         return TipoIntervalloStatistico.STATISTICHE_SETTIMANALI;
  221.     }
  222.  

  223.     @Override
  224.     public void callStatisticPluginMethod(IStatisticProcessing statProcessing,
  225.             StatisticBean stat) throws StatisticException {
  226.         IStatistic statContext = new CustomStatisticsSdkGenerator(stat, StatisticType.SETTIMANALE, this);
  227.         statProcessing.createWeeklyStatisticData(statContext);
  228.     }

  229.     @Override
  230.     public String getStatisticPluginMethodName() throws StatisticException {
  231.         return "createWeeklyStatisticData";
  232.     }

  233.    
  234.     @Override
  235.     protected Long insertStatistica(Statistica statistica) throws StatisticException {
  236.        
  237.         try{
  238.        
  239.             StatisticaSettimanale stat = new StatisticaSettimanale();
  240.             stat.setStatisticaBase(statistica);
  241.             ((IStatisticaSettimanaleService)this.statisticaServiceDAO).create(stat);
  242.             return stat.getId();
  243.            
  244.         }catch(Exception e){
  245.             throw new StatisticException(e.getMessage(),e);
  246.         }

  247.     }
  248.    
  249.     @Override
  250.     protected void updateStatistica(long idStatistica, StatisticaContenuti ... statisticaContenuti) throws StatisticException {
  251.        
  252.         try{
  253.        
  254.             StatisticaSettimanale stat = ((IDBStatisticaSettimanaleService)this.statisticaServiceDAO).get(idStatistica);
  255.             for (int i = 0; i < statisticaContenuti.length; i++) {
  256.                 statisticaContenuti[i].setData(stat.getStatisticaBase().getData());
  257.                 stat.addStatisticaSettimanaleContenuti(statisticaContenuti[i]);
  258.             }
  259.             ((IStatisticaSettimanaleService)this.statisticaServiceDAO).update(stat);
  260.            
  261.         }catch(Exception e){
  262.             throw new StatisticException(e.getMessage(),e);
  263.         }

  264.     }
  265.    
  266. }