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.constants.CostantiExporter;
  41. import org.openspcoop2.web.monitor.transazioni.bean.TransazioniSearchForm;

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

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