SearchFormUtilities.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.core.monitor.rs.server.api.impl.utils;

  21. import org.joda.time.DateTime;
  22. import org.openspcoop2.core.monitor.rs.server.config.ServerProperties;
  23. import org.openspcoop2.core.monitor.rs.server.config.SoggettiConfig;
  24. import org.openspcoop2.core.monitor.rs.server.model.FiltroRicercaRuoloTransazioneEnum;
  25. import org.openspcoop2.core.monitor.rs.server.model.FormatoReportEnum;
  26. import org.openspcoop2.core.transazioni.constants.PddRuolo;
  27. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  28. import org.openspcoop2.utils.service.beans.ProfiloEnum;
  29. import org.openspcoop2.utils.service.beans.TransazioneRuoloEnum;
  30. import org.openspcoop2.utils.service.context.IContext;
  31. import org.openspcoop2.utils.service.fault.jaxrs.FaultCode;
  32. import org.openspcoop2.web.lib.users.dao.InterfaceType;
  33. import org.openspcoop2.web.lib.users.dao.PermessiUtente;
  34. import org.openspcoop2.web.lib.users.dao.User;
  35. import org.openspcoop2.web.monitor.core.bean.BaseSearchForm;
  36. import org.openspcoop2.web.monitor.core.constants.ModalitaRicercaTransazioni;
  37. import org.openspcoop2.web.monitor.core.constants.TipologiaRicerca;
  38. import org.openspcoop2.web.monitor.eventi.bean.EventiSearchForm;
  39. import org.openspcoop2.web.monitor.statistiche.bean.ConfigurazioniGeneraliSearchForm;
  40. import org.openspcoop2.web.monitor.statistiche.bean.StatistichePdndTracingSearchForm;
  41. import org.openspcoop2.web.monitor.statistiche.constants.CostantiExporter;
  42. import org.openspcoop2.web.monitor.transazioni.bean.TransazioniSearchForm;

  43. /**
  44.  * BaseSearchForm
  45.  *
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  *
  49.  */
  50. public class SearchFormUtilities {

  51.     private ServerProperties serverProperties;
  52.     private ProtocolFactoryManager protocolFactoryManager;
  53.    
  54.     public SearchFormUtilities() throws Exception {
  55.         this.serverProperties = ServerProperties.getInstance();
  56.         this.protocolFactoryManager = ProtocolFactoryManager.getInstance();
  57.     }
  58.    
  59.     public User getUtente(IContext context) {
  60.         User user = new User();
  61.         user.setInterfaceType(InterfaceType.STANDARD);
  62.         user.setLogin(context.getAuthentication().getName());
  63.         PermessiUtente permessi = new PermessiUtente();
  64.         permessi.setDiagnostica(true);
  65.         permessi.setReportistica(true);
  66.         user.setPermessi(permessi);
  67.         user.setPermitAllServizi(true);
  68.         user.setPermitAllSoggetti(true);
  69.         //user.setProtocolliSupportati(protocolFactoryManager.getProtocolNamesAsList());
  70.         return user;
  71.     }
  72.    
  73.     private void initBaseInfo(BaseSearchForm searchForm, IContext context, ProfiloEnum profilo, String soggetto, FiltroRicercaRuoloTransazioneEnum ruolo) throws Exception {
  74.         this.initBaseInfo(searchForm, context, profilo, soggetto);
  75.         if(ruolo!=null) {
  76.             switch (ruolo) {
  77.             case FRUIZIONE:
  78.                 searchForm.setTipologiaRicerca(TipologiaRicerca.uscita);
  79.                 break;
  80.             case EROGAZIONE:
  81.                 searchForm.setTipologiaRicerca(TipologiaRicerca.ingresso);  
  82.                 break;
  83.             case QUALSIASI:
  84.                 searchForm.setTipologiaRicerca(TipologiaRicerca.all);  
  85.                 break;
  86.             }
  87.         }
  88.     }
  89.    
  90.     private void initBaseInfo(BaseSearchForm searchForm, IContext context, ProfiloEnum profilo, String soggetto) throws Exception {
  91.         searchForm.setUser(this.getUtente(context));
  92.         String protocollo = Converter.toProtocollo(profilo);
  93.         searchForm.setProtocollo(protocollo);
  94.         String tipoSoggettoLocale = this.protocolFactoryManager.getDefaultOrganizationTypes().get(protocollo);
  95.         String nomeSoggettoLocale = soggetto;
  96.         if(nomeSoggettoLocale==null) {
  97.             if(this.serverProperties.useSoggettoDefault()) {
  98.                 nomeSoggettoLocale = this.serverProperties.getSoggettoDefaultIfEnabled(protocollo);
  99.             }
  100.         }
  101.         if(nomeSoggettoLocale!=null) {
  102.             if(!SoggettiConfig.existsIdentificativoPorta(tipoSoggettoLocale, nomeSoggettoLocale)) {
  103.                 throw FaultCode.RICHIESTA_NON_VALIDA.toException("Il soggetto '"+nomeSoggettoLocale+"' indicato non esiste");
  104.             }
  105.             searchForm.setTipoNomeSoggettoLocale(tipoSoggettoLocale+"/"+nomeSoggettoLocale);
  106.             searchForm.setSoggettoPddMonitor(tipoSoggettoLocale+"/"+nomeSoggettoLocale);
  107.         }
  108.         else {
  109.             searchForm.setCheckSoggettoPddMonitor(false);
  110.         }
  111.         searchForm.saveProtocollo();
  112.     }
  113.    
  114.     public HttpRequestWrapper getHttpRequestWrapper(IContext context, ProfiloEnum profilo, String soggetto, FiltroRicercaRuoloTransazioneEnum ruolo,
  115.             FormatoReportEnum formato, boolean formatoStat,
  116.             TipoReport tipo) throws Exception {
  117.         HttpRequestWrapper request = new HttpRequestWrapper(context.getServletRequest());
  118.         request.overrideParameter(CostantiExporter.TIPO_DISTRIBUZIONE, tipo.getValue());
  119.         String protocollo = Converter.toProtocollo(profilo);
  120.         request.overrideParameter(CostantiExporter.PROTOCOLLO,protocollo);
  121.         String tipoSoggettoLocale = this.protocolFactoryManager.getDefaultOrganizationTypes().get(protocollo);
  122.         String nomeSoggettoLocale = soggetto;
  123.         if(nomeSoggettoLocale==null) {
  124.             if(this.serverProperties.useSoggettoDefault()) {
  125.                 nomeSoggettoLocale = this.serverProperties.getSoggettoDefaultIfEnabled(protocollo);
  126.             }
  127.         }
  128.         if(nomeSoggettoLocale!=null) {
  129.             if(!SoggettiConfig.existsIdentificativoPorta(tipoSoggettoLocale, nomeSoggettoLocale)) {
  130.                 throw FaultCode.RICHIESTA_NON_VALIDA.toException("Il soggetto '"+nomeSoggettoLocale+"' indicato non esiste");
  131.             }
  132.             request.overrideParameter(CostantiExporter.SOGGETTO_LOCALE,tipoSoggettoLocale+"/"+nomeSoggettoLocale);
  133.         }
  134.         if(ruolo!=null) {
  135.             switch (ruolo) {
  136.             case FRUIZIONE:
  137.                 request.overrideParameter(CostantiExporter.TIPOLOGIA,CostantiExporter.TIPOLOGIA_FRUIZIONE);
  138.                 break;
  139.             case EROGAZIONE:
  140.                 request.overrideParameter(CostantiExporter.TIPOLOGIA,CostantiExporter.TIPOLOGIA_EROGAZIONE);
  141.                 break;
  142.             case QUALSIASI:
  143.                 request.overrideParameter(CostantiExporter.TIPOLOGIA,CostantiExporter.TIPOLOGIA_EROGAZIONE_FRUIZIONE);
  144.                 break;
  145.             }
  146.         }
  147.         String nomeParametroTipoFormato = null;
  148.         if(formatoStat) {
  149.             nomeParametroTipoFormato = CostantiExporter.TIPO_FORMATO;
  150.         }
  151.         else {
  152.             nomeParametroTipoFormato = CostantiExporter.TIPO_FORMATO_CONFIGURAZIONE;
  153.         }
  154.         switch (formato) {
  155.         case CSV:
  156.             request.overrideParameter(nomeParametroTipoFormato, CostantiExporter.TIPO_FORMATO_CSV);
  157.             break;
  158.         case PDF:
  159.             request.overrideParameter(nomeParametroTipoFormato, CostantiExporter.TIPO_FORMATO_PDF);
  160.             break;
  161.         case XLS:
  162.             request.overrideParameter(nomeParametroTipoFormato, CostantiExporter.TIPO_FORMATO_XLS);
  163.             break;
  164.         case XML:
  165.             request.overrideParameter(nomeParametroTipoFormato, CostantiExporter.TIPO_FORMATO_XML);
  166.             break;
  167.         case JSON:
  168.             request.overrideParameter(nomeParametroTipoFormato, CostantiExporter.TIPO_FORMATO_JSON);
  169.             break;
  170.         default:
  171.             break;
  172.         }
  173.         return request;
  174.     }
  175.     public HttpRequestWrapper getHttpRequestWrapper(IContext context, ProfiloEnum profilo, String soggetto, TransazioneRuoloEnum ruolo,
  176.             FormatoReportEnum formato, boolean formatoStat,
  177.             TipoReport tipo) throws Exception {
  178.         FiltroRicercaRuoloTransazioneEnum ruoloNull = null;
  179.         HttpRequestWrapper wrapper = getHttpRequestWrapper(context, profilo, soggetto, ruoloNull, formato, formatoStat, tipo);
  180.         if(ruolo!=null) {
  181.             switch (ruolo) {
  182.             case FRUIZIONE:
  183.                 wrapper.overrideParameter(CostantiExporter.TIPOLOGIA,CostantiExporter.TIPOLOGIA_FRUIZIONE);
  184.                 break;
  185.             case EROGAZIONE:
  186.                 wrapper.overrideParameter(CostantiExporter.TIPOLOGIA,CostantiExporter.TIPOLOGIA_EROGAZIONE);
  187.                 break;
  188.             }
  189.         }
  190.         return wrapper;
  191.     }
  192.    
  193.     public TransazioniSearchForm getAndamentoTemporaleSearchForm(IContext context, ProfiloEnum profilo, String soggetto, FiltroRicercaRuoloTransazioneEnum ruolo,
  194.             DateTime dataInizio, DateTime dataFine) throws Exception {
  195.         TransazioniSearchForm searchForm = new TransazioniSearchForm();
  196.         initBaseInfo(searchForm, context, profilo, soggetto, ruolo);
  197.         searchForm.setModalitaRicercaStorico(ModalitaRicercaTransazioni.ANDAMENTO_TEMPORALE.getValue());
  198.         if (dataInizio != null && dataFine != null) {
  199.             searchForm.setDataInizio(dataInizio.toDate());
  200.             searchForm.setDataFine(dataFine.toDate());
  201.         }
  202.         return searchForm;
  203.     }
  204.    
  205.     public TransazioniSearchForm getIdMessaggioSearchForm(IContext context, ProfiloEnum profilo, String soggetto) throws Exception {
  206.         TransazioniSearchForm searchForm = new TransazioniSearchForm();
  207.         initBaseInfo(searchForm, context, profilo, soggetto, null);
  208.         searchForm.setModalitaRicercaStorico(ModalitaRicercaTransazioni.ID_MESSAGGIO.getValue());
  209.        
  210.         return searchForm;
  211.     }
  212.    
  213.     public TransazioniSearchForm getPurposeIdSearchForm(IContext context, String soggetto) throws Exception {
  214.         TransazioniSearchForm searchForm = new TransazioniSearchForm();
  215.         initBaseInfo(searchForm, context, ProfiloEnum.MODIPA, soggetto, null);
  216.         searchForm.setModalitaRicercaStorico(ModalitaRicercaTransazioni.PURPOSE_ID.getValue());
  217.        
  218.         return searchForm;
  219.     }
  220.    
  221.     public TransazioniSearchForm getIdApplicativoBaseSearchForm(IContext context, ProfiloEnum profilo, String soggetto) throws Exception {
  222.         TransazioniSearchForm searchForm = new TransazioniSearchForm();
  223.         initBaseInfo(searchForm, context, profilo, soggetto, null);
  224.         searchForm.setModalitaRicercaStorico(ModalitaRicercaTransazioni.ID_APPLICATIVO_BASE.getValue());
  225.        
  226.         return searchForm;
  227.     }
  228.    
  229.    
  230.     public EventiSearchForm getEventiSearchForm(IContext context, DateTime dataInizio, DateTime dataFine) throws Exception {
  231.         EventiSearchForm searchForm = new EventiSearchForm();
  232.         searchForm.setDataInizio(dataInizio.toDate());
  233.         searchForm.setDataFine(dataFine.toDate());
  234.         return searchForm;
  235.     }
  236.    
  237.     public TransazioniSearchForm getIdApplicativoSearchForm(IContext context, ProfiloEnum profilo, String soggetto, FiltroRicercaRuoloTransazioneEnum ruolo,
  238.             DateTime dataInizio, DateTime dataFine) throws Exception {
  239.         TransazioniSearchForm searchForm = new TransazioniSearchForm();
  240.         initBaseInfo(searchForm, context, profilo, soggetto, ruolo);
  241.         searchForm.setModalitaRicercaStorico(ModalitaRicercaTransazioni.ID_APPLICATIVO_AVANZATA.getValue());
  242.         if (dataInizio != null && dataFine != null) {
  243.             searchForm.setDataInizio(dataInizio.toDate());
  244.             searchForm.setDataFine(dataFine.toDate());
  245.         }
  246.         return searchForm;
  247.     }
  248.    
  249.     public ConfigurazioniGeneraliSearchForm getConfigurazioniGeneraliSearchForm(IContext context, ProfiloEnum profilo, String soggetto, TransazioneRuoloEnum ruolo) throws Exception {
  250.         ConfigurazioniGeneraliSearchForm searchForm = new ConfigurazioniGeneraliSearchForm();
  251.         initBaseInfo(searchForm, context, profilo, soggetto);
  252.         if(ruolo!=null) {
  253.             switch (ruolo) {
  254.             case FRUIZIONE:
  255.                 searchForm.setTipologiaTransazioni(PddRuolo.DELEGATA);
  256.                 break;
  257.             case EROGAZIONE:
  258.                 searchForm.setTipologiaTransazioni(PddRuolo.APPLICATIVA);
  259.                 break;
  260.             }
  261.         }
  262.         return searchForm;
  263.     }
  264.    
  265.     public StatistichePdndTracingSearchForm getStatistichePdndTracingSearchForm(IContext context, String soggetto) throws Exception {
  266.         StatistichePdndTracingSearchForm searchForm = new StatistichePdndTracingSearchForm();
  267.         initBaseInfo(searchForm, context, ProfiloEnum.MODI, soggetto);
  268.         return searchForm;
  269.     }
  270. }