CustomStatisticsSdkGenerator.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.commons.dao.DAOFactory;
  22. import org.openspcoop2.monitor.sdk.condition.IFilter;
  23. import org.openspcoop2.monitor.sdk.constants.StatisticType;
  24. import org.openspcoop2.monitor.sdk.exceptions.StatisticException;
  25. import org.openspcoop2.monitor.sdk.statistic.IStatistic;
  26. import org.openspcoop2.monitor.sdk.statistic.StatisticResourceFilter;
  27. import org.openspcoop2.monitor.sdk.statistic.StatisticFilter;

  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;

  32. import org.slf4j.Logger;
  33. import org.openspcoop2.core.constants.TipoPdD;
  34. import org.openspcoop2.utils.TipiDatabase;


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

  43.     private StatisticBean statistic;
  44.    
  45.     private StatisticType statisticType;
  46.    
  47.     private AbstractStatistiche abstractStatisticheCore;
  48.    
  49.     protected CustomStatisticsSdkGenerator(StatisticBean stat, StatisticType statisticType, AbstractStatistiche abstractStatisticheCore) {
  50.         this.statistic = stat;
  51.         this.statisticType = statisticType;
  52.         this.abstractStatisticheCore = abstractStatisticheCore;
  53.     }

  54.     @Override
  55.     public TipoPdD getTipoPorta(){
  56.         return this.statistic.getTipoPorta();
  57.     }
  58.    
  59.     @Override
  60.     public String getIdentificativoPorta(){
  61.         return this.statistic.getIdPorta();
  62.     }

  63.     @Override
  64.     public String getTipoSoggettoMittente() {
  65.         if(this.statistic.getMittente()!=null)
  66.             return this.statistic.getMittente().getTipo();
  67.         else
  68.             return null;
  69.     }
  70.     @Override
  71.     public String getSoggettoMittente() {
  72.         if(this.statistic.getMittente()!=null)
  73.             return this.statistic.getMittente().getNome();
  74.         else
  75.             return null;
  76.     }
  77.    
  78.     @Override
  79.     public String getTipoSoggettoDestinatario() {
  80.         if(this.statistic.getDestinatario()!=null)
  81.             return this.statistic.getDestinatario().getTipo();
  82.         else
  83.             return null;
  84.     }
  85.     @Override
  86.     public String getSoggettoDestinatario() {
  87.         if(this.statistic.getDestinatario()!=null)
  88.             return this.statistic.getDestinatario().getNome();
  89.         else
  90.             return null;
  91.     }
  92.    
  93.     @Override
  94.     public String getTipoServizio() {
  95.         return this.statistic.getTipoServizio();
  96.     }
  97.     @Override
  98.     public String getServizio() {
  99.         return this.statistic.getServizio();
  100.     }
  101.     @Override
  102.     public Integer getVersioneServizio() {
  103.         return this.statistic.getVersioneServizio();
  104.     }
  105.    
  106.     @Override
  107.     public String getAzione() {
  108.         return this.statistic.getAzione();
  109.     }
  110.    
  111.     @Override
  112.     public String getServizioApplicativo() {
  113.         return this.statistic.getServizioApplicativo();
  114.     }
  115.    
  116.     @Override
  117.     public String getIdentificativoAutenticato() {
  118.         return this.statistic.getTrasportoMittente();
  119.     }
  120.    
  121.     @Override
  122.     public String getTokenInfoIssuer(){
  123.         return this.statistic.getTokenIssuer();
  124.     }
  125.     @Override
  126.     public String getTokenInfoClientId(){
  127.         return this.statistic.getTokenClientId();
  128.     }  
  129.     @Override
  130.     public String getTokenInfoSubject(){
  131.         return this.statistic.getTokenSubject();
  132.     }  
  133.     @Override
  134.     public String getTokenInfoUsername(){
  135.         return this.statistic.getTokenUsername();
  136.     }  
  137.     @Override
  138.     public String getTokenInfoEmail(){
  139.         return this.statistic.getTokenMail();
  140.     }
  141.    
  142.     @Override
  143.     public String getClientAddress(){
  144.         return this.statistic.getClientAddress();
  145.     }
  146.    
  147.     @Override
  148.     public String getGruppo(){
  149.         return this.statistic.getGruppo();
  150.     }
  151.    
  152.     @Override
  153.     public String getApi(){
  154.         return this.statistic.getApi();
  155.     }
  156.    
  157.     @Override
  158.     public String getClusterId() {
  159.         return this.statistic.getClusterId();
  160.     }
  161.    
  162.     @Override
  163.     public TipiDatabase getDatabaseType(){
  164.         return this.abstractStatisticheCore.getDatabaseType();
  165.     }
  166.    
  167.     @Override
  168.     public StatisticType getStatisticType(){
  169.         return this.statisticType;
  170.     }
  171.    
  172.     @Override
  173.     public Logger getLogger(){
  174.         return this.abstractStatisticheCore.logger;
  175.     }
  176.    
  177.     @Override
  178.     public DAOFactory getDAOFactory() throws StatisticException{
  179.         try{
  180.             return DAOFactory.getInstance(this.abstractStatisticheCore.logger);
  181.         }catch(Exception e){
  182.             throw new StatisticException(e.getMessage(),e);
  183.         }
  184.     }
  185.    
  186.     public StatisticBean getStatistic() {
  187.         return this.statistic;
  188.     }

  189.     public AbstractStatistiche getAbstractStatisticheCore() {
  190.         return this.abstractStatisticheCore;
  191.     }
  192.    
  193.    
  194.     private int numeroVolteCreazioneStatisticaSemplice = 0;
  195.    
  196.     @Override
  197.     public void createStatistics(String idRisorsa) throws StatisticException {
  198.         this.createStatistics(idRisorsa, new StatisticResourceFilter[0]);
  199.     }
  200.    
  201.     @Override
  202.     public void createStatistics(String idRisorsa, StatisticResourceFilter... idRisorseFiltri)
  203.             throws StatisticException {

  204.         if(idRisorsa==null){
  205.             throw new StatisticException("IdResource undefined");
  206.         }
  207.         if(this.numeroVolteCreazioneStatisticaSemplice>0){
  208.             throw new StatisticException("It is not allowed to generate more of a statistic without providing the identifier of the statistic (use method createStatistics(idStatistic,String idResource, ...))");
  209.         }
  210.        
  211.         RisorsaSemplice risorsa = new RisorsaSemplice();
  212.         risorsa.setIdRisorsa(idRisorsa);
  213.         if(idRisorseFiltri!=null && idRisorseFiltri.length>0){
  214.             for (int i = 0; i < idRisorseFiltri.length; i++) {
  215.                 risorsa.getFiltri().add(idRisorseFiltri[i]);
  216.             }
  217.         }
  218.        
  219.         this.abstractStatisticheCore.generaStatisticaPersonalizzata(this.statistic, risorsa);
  220.        
  221.         this.numeroVolteCreazioneStatisticaSemplice++;
  222.    
  223.     }
  224.    
  225.    
  226.     private List<String> idStatistiche = new ArrayList<>();
  227.     private List<String> idRisorse = new ArrayList<>();
  228.     private Map<String, String> mapIdRisorsaToIdStatistica = new HashMap<>();
  229.    
  230.     @Override
  231.     public void createStatistics(String idStatistica,String idRisorsa) throws StatisticException{
  232.         this.createStatistics(idStatistica, idRisorsa, new StatisticResourceFilter[0]);
  233.     }
  234.        
  235.     @Override
  236.     public void createStatistics(String idStatistica,String idRisorsa, StatisticResourceFilter ... idRisorseFiltri) throws StatisticException{
  237.        
  238.         if(idStatistica==null || "".equals(idStatistica)){
  239.             throw new StatisticException("IdStatistic undefined");
  240.         }
  241.         if(this.idStatistiche.contains(idStatistica)){
  242.             throw new StatisticException("IdStatistic ["+idStatistica+"] already used");
  243.         }
  244.         if(idRisorsa==null || "".equals(idRisorsa)){
  245.             throw new StatisticException("IdResource undefined");
  246.         }
  247.         if(this.idRisorse.contains(idRisorsa)){
  248.             throw new StatisticException("IdResource ["+idRisorsa+"] already used for statistic with id: "+this.mapIdRisorsaToIdStatistica.get(idRisorsa));
  249.         }

  250.        
  251.         RisorsaSemplice risorsa = new RisorsaSemplice();
  252.         risorsa.setIdStatistica(idStatistica);
  253.         risorsa.setIdRisorsa(idRisorsa);
  254.         if(idRisorseFiltri!=null && idRisorseFiltri.length>0){
  255.             for (int i = 0; i < idRisorseFiltri.length; i++) {
  256.                 risorsa.getFiltri().add(idRisorseFiltri[i]);
  257.             }
  258.         }
  259.        
  260.         this.abstractStatisticheCore.generaStatisticaPersonalizzata(this.statistic, risorsa);
  261.        
  262.         this.idStatistiche.add(idStatistica);
  263.         this.idRisorse.add(idRisorsa);
  264.         this.mapIdRisorsaToIdStatistica.put(idRisorsa, idStatistica);
  265.     }
  266.    
  267.    
  268.    
  269.    
  270.     //private List<String> valoreRisorseAggregate = new ArrayList<>();
  271.    
  272.     @Override
  273.     public void createStatistics(IFilter filtro, String valoreRisorsaAggregata)
  274.             throws StatisticException {
  275.         this.createStatistics(filtro, valoreRisorsaAggregata, new StatisticFilter[0]);
  276.     }

  277.     @Override
  278.     public void createStatistics(IFilter filtro, String valoreRisorsaAggregata,
  279.             StatisticFilter... filtriRicerca) throws StatisticException {
  280.        
  281.         if(filtro==null){
  282.             throw new StatisticException("Filter undefined");
  283.         }
  284.         if(valoreRisorsaAggregata==null || "".equals(valoreRisorsaAggregata)){
  285.             throw new StatisticException("AggregateResourceValue undefined");
  286.         }
  287.         // Posso implementare più statistiche con lo stesso valore di risorsa aggregata se cambio il filtro
  288.         // TODO: Lavorare sul controllare che tutta la coppia valoreRisorsaAggregata - filtro non sia già usata
  289. //      if(this.valoreRisorseAggregate.contains(valoreRisorsaAggregata)){
  290. //          throw new StatisticException("AggregateResourceValue ["+valoreRisorsaAggregata+"] already used");
  291. //      }
  292.        
  293.         RisorsaAggregata risorsa = new RisorsaAggregata();
  294.         risorsa.setFiltro(filtro);
  295.         try{
  296.             risorsa.setValoreRisorsaAggregata(valoreRisorsaAggregata);
  297.         }catch(Exception e){
  298.             throw new StatisticException(e.getMessage(),e);
  299.         }
  300.         if(filtriRicerca!=null && filtriRicerca.length>0){
  301.             for (int i = 0; i < filtriRicerca.length; i++) {
  302.                 risorsa.getFiltri().add(filtriRicerca[i]);
  303.             }
  304.         }
  305.        
  306.         this.abstractStatisticheCore.generaStatisticaPersonalizzata(this.statistic, risorsa);
  307.        
  308.         //this.valoreRisorseAggregate.add(valoreRisorsaAggregata);
  309.     }
  310.    
  311.    
  312.    
  313.    
  314.     public void createStatisticsByStato(){
  315.         this.abstractStatisticheCore.generaStatisticaPersonalizzataByStato(this.statistic);
  316.     }
  317. }