OperazioniHelper.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.web.ctrlstat.servlet.operazioni;

  21. import java.text.SimpleDateFormat;
  22. import java.util.ArrayList;
  23. import java.util.Iterator;
  24. import java.util.List;

  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpSession;

  27. import org.openspcoop2.utils.date.DateUtils;
  28. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  29. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  30. import org.openspcoop2.web.ctrlstat.servlet.ConsoleHelper;
  31. import org.openspcoop2.web.ctrlstat.servlet.monitor.MonitorCostanti;
  32. import org.openspcoop2.web.lib.mvc.Costanti;
  33. import org.openspcoop2.web.lib.mvc.DataElement;
  34. import org.openspcoop2.web.lib.mvc.DataElementType;
  35. import org.openspcoop2.web.lib.mvc.PageData;
  36. import org.openspcoop2.web.lib.mvc.Parameter;
  37. import org.openspcoop2.web.lib.mvc.ServletUtils;
  38. import org.openspcoop2.web.lib.queue.costanti.OperationStatus;
  39. import org.openspcoop2.web.lib.queue.dao.Operation;
  40. import org.openspcoop2.web.lib.users.dao.PermessiUtente;
  41. import org.openspcoop2.web.lib.users.dao.User;


  42. /****
  43.  *
  44.  * OperazioniHelper
  45.  *
  46.  * @author Pintori Giuliano (pintori@link.it)
  47.  * @author $Author$
  48.  * @version $Rev$, $Date$
  49.  */
  50. public class OperazioniHelper extends ConsoleHelper{

  51.     private SimpleDateFormat formatter = DateUtils.getSimpleDateFormatSecond();

  52.     public OperazioniHelper(HttpServletRequest request, PageData pd,
  53.             HttpSession session) throws Exception {
  54.         super(request, pd,  session);
  55.     }
  56.     public OperazioniHelper(ControlStationCore core, HttpServletRequest request, PageData pd,
  57.             HttpSession session) throws Exception {
  58.         super(core, request, pd,  session);
  59.     }

  60.     public void prepareOperazioniList(ConsoleSearch ricerca, List<Operation> lista)throws Exception {
  61.         try {

  62.             ArrayList<String> errors = new ArrayList<>();
  63.             OperazioniFormBean formBean = this.getBeanForm(errors);

  64.             Parameter pUtente = new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_UTENTE, formBean.getUtente());
  65.             Parameter pOperazione = new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE, formBean.getTipo());
  66.             Parameter pDetail = new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_METHOD, OperazioniCostanti.DEFAULT_VALUE_FORM_BEAN_METHOD_DETAILS);

  67.             ServletUtils.addListElementIntoSession(this.request, this.session, OperazioniCostanti.OBJECT_NAME_OPERAZIONI, pOperazione, pUtente);

  68.             int idLista =  this.operazioniCore.getIdLista(formBean);
  69.             int limit = ricerca.getPageSize(idLista);
  70.             int offset = ricerca.getIndexIniziale(idLista);
  71.             String search = ServletUtils.getSearchFromSession(ricerca, idLista);

  72.             this.pd.setIndex(offset);
  73.             this.pd.setPageSize(limit);
  74.             this.pd.setNumEntries(ricerca.getNumEntries(idLista));

  75.             // setto la barra del titolo
  76.             List<Parameter> lstParam = new ArrayList<>();

  77.             String elencoLabel = OperazioniCostanti.getTipoOperazioneLabelFromValue(formBean.getTipo());

  78.             lstParam.add(new Parameter(OperazioniCostanti.LABEL_OPERAZIONI, OperazioniCostanti.SERVLET_NAME_OPERAZIONI, pUtente, pOperazione));
  79.             if(search.equals("")){
  80.                 this.pd.setSearchDescription("");
  81.                 lstParam.add(new Parameter(elencoLabel,null));
  82.             }
  83.             else{
  84.                 lstParam.add(new Parameter(elencoLabel, OperazioniCostanti.SERVLET_NAME_OPERAZIONI, pUtente, pOperazione));
  85.             }

  86.             // setto la barra del titolo
  87.             ServletUtils.setPageDataTitle(this.pd, lstParam.toArray(new Parameter[lstParam.size()]));

  88.             // controllo eventuali risultati ricerca
  89.             if (!search.equals("")) {
  90.                 ServletUtils.enabledPageDataSearch(this.pd, OperazioniCostanti.LABEL_OPERAZIONI_CONFIGURAZIONI, search);
  91.             }

  92.             // Genero l'elenco delle labels
  93.             List<String> listaLabel = getListaLabel(formBean);  


  94.             String[] labels = listaLabel.toArray(new String[listaLabel.size()]);
  95.             this.pd.setLabels(labels);

  96.             // preparo i dati
  97.             List<List<DataElement>> dati = new ArrayList<>();

  98.             // Risultati Ricerca

  99.             if (lista != null) {
  100.                 Iterator<Operation> it = lista.iterator();
  101.                 while (it.hasNext()) {
  102.                     Operation op = it.next();

  103.                     List<DataElement> e = new ArrayList<>();

  104.                     Parameter pId = new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_ID, op.getId()+"");

  105.                     // Colonna Id
  106.                     DataElement de = new DataElement();
  107.                     de.setUrl(OperazioniCostanti.SERVLET_NAME_OPERAZIONI, pId, pOperazione ,pUtente,pDetail);
  108.                     de.setValue(op.getId()+"");
  109.                     de.setIdToRemove(op.getId().toString());
  110.                     e.add(de);

  111.                     // Colonna Operazione

  112.                     de = new DataElement();
  113.                     de.setValue(op.getOperation());
  114.                     e.add(de);

  115.                     // Colonna Host

  116.                     de = new DataElement();
  117.                     de.setValue(op.getHostname());
  118.                     e.add(de);

  119.                     // Colonna Utente
  120.                     User user = ServletUtils.getUserFromSession(this.request, this.session);
  121.                     if(user.getPermessi().isUtenti()){
  122.                         de = new DataElement();
  123.                         de.setValue(op.getSuperUser());
  124.                         e.add(de);
  125.                     }
  126.                    
  127.                     // Colonna Data Richiesta

  128.                     de = new DataElement();
  129.                     de.setValue(this.formatter.format(op.getTimeReq().getTime()));
  130.                     e.add(de);

  131.                     // Colonna Data Esecuzione

  132.                     if(!formBean.getTipo().equals(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE_IN_CODA)){
  133.                         de = new DataElement();
  134.                         de.setValue(this.formatter.format(op.getTimeExecute().getTime()));
  135.                         e.add(de);  
  136.                     }

  137.                     // Colonna Eliminata
  138.                     if(formBean.getTipo().equals(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE_ESEGUITE)){
  139.                         de = new DataElement();
  140.                         if (op.isDeleted())
  141.                             de.setValue(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_ELIMINAZIONE_OPERATORE);
  142.                         else
  143.                             de.setValue("");
  144.                         e.add(de);
  145.                     }

  146.                     dati.add(e);
  147.                 }
  148.             }

  149.             this.pd.setDati(dati);

  150.             this.pd.setAddButton(false);

  151.             if(formBean.getTipo().equals(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE_ESEGUITE)){
  152.                 this.pd.setRemoveButton(false);
  153.                 this.pd.setSelect(false);
  154.             }
  155.         } catch (Exception e) {
  156.             this.log.error("Exception: " + e.getMessage(), e);
  157.             throw new Exception(e);
  158.         }


  159.     }

  160.     private List<String> getListaLabel(OperazioniFormBean formBean) {
  161.         List<String> listaLabel = new ArrayList<>();

  162.         User user = ServletUtils.getUserFromSession(this.request, this.session);
  163.        
  164.         String operazione = formBean.getTipo();
  165.         // Label comuni
  166.         listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_ID);
  167.         listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_OPERAZIONE);
  168.         listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_HOST);
  169.         if(user.getPermessi().isUtenti()){
  170.             listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_UTENTE);
  171.         }
  172.         listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_DATA_RICHIESTA);

  173.         // tutte tranne quelle in coda hanno la colonna data esecuzione
  174.         if(!operazione.equals(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE_IN_CODA))
  175.             listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_DATA_ESECUZIONE);

  176.         // quelle eseguite hanno anche la colonne eliminate
  177.         if(operazione.equals(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE_ESEGUITE))
  178.             listaLabel.add(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_ELIMINATA);

  179.         return listaLabel;
  180.     }


  181.     public void showForm(String azione, String soglia, OperazioniFormBean formBean ) throws Exception {
  182.         try {
  183.             User user = ServletUtils.getUserFromSession(this.request, this.session);
  184.             PermessiUtente permessi = user.getPermessi();
  185.             boolean hasPermessiUtenti = permessi.isUtenti();

  186.             // setto la barra del titolo
  187.             ServletUtils.setPageDataTitle_ServletFirst(this.pd, OperazioniCostanti.LABEL_OPERAZIONI, OperazioniCostanti.SERVLET_NAME_OPERAZIONI);

  188.             // preparo i campi
  189.             List<DataElement> dati = new ArrayList<>();
  190.             dati.add(ServletUtils.getDataElementForEditModeFinished());


  191.             // Campi Ricerca

  192.             // Tipo Operazione
  193.             String[] tipoOperazioneValori = OperazioniCostanti.getParametroOperazioniOperazioneList();
  194.             String[] tipoOperazioneLabel = OperazioniCostanti.getLabelParametroOperazioniOperazioneList();

  195.             DataElement de = new DataElement();
  196.             de.setLabel(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_OPERAZIONE);
  197.             de.setValues(tipoOperazioneValori);
  198.             de.setLabels(tipoOperazioneLabel);
  199.             de.setSelected(formBean.getTipo());
  200.             de.setType(DataElementType.SELECT);
  201.             de.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE);
  202.             if(!hasPermessiUtenti)
  203.                 de.setSize(this.getSize());
  204.             dati.add(de);



  205.             // Utente

  206.             de = new DataElement();

  207.             if(hasPermessiUtenti){
  208.                 // Titolo Filter
  209.                 de.setLabel(MonitorCostanti.LABEL_MONITOR_FILTRO_RICERCA);
  210.                 de.setType(DataElementType.TITLE);
  211.                 dati.add(de);

  212.                 de = new DataElement();
  213.                 de.setType(DataElementType.SELECT);
  214.                 List<User> listaUser =  formBean.getListaUser();
  215.                 List<String> utentiLabels = new ArrayList<>();
  216.                 List<String> utentiValues = new ArrayList<>();

  217.                 if(listaUser != null && !listaUser.isEmpty()){
  218.                     for (User ut : listaUser) {
  219.                         utentiLabels.add(ut.getLogin());
  220.                         utentiValues.add(ut.getLogin());
  221.                     }
  222.                 }

  223.                 utentiLabels.add(0, OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_UTENTE_ALL);
  224.                 utentiValues.add(0, OperazioniCostanti.PARAMETRO_OPERAZIONI_UTENTE_ALL);

  225.                 de.setLabels(utentiLabels);
  226.                 de.setValues(utentiValues);
  227.                 de.setSelected(formBean.getUtente());
  228.             }else {
  229.                 de.setValue(formBean.getUtente());
  230.                 de.setType(DataElementType.HIDDEN);
  231.             }
  232.             de.setLabel(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_UTENTE);
  233.             de.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_UTENTE);
  234.             dati.add(de);

  235.             this.pd.setDati(dati);
  236.         } catch (Exception e) {
  237.             throw new Exception(e);
  238.         }
  239.     }

  240.     /**
  241.      * Recupera i dati dalla request e riempe il form
  242.      *
  243.      * @param errors Errori
  244.      * @return OperazioniFormBean
  245.      * @throws Exception
  246.      */
  247.     public OperazioniFormBean getBeanForm(List<String> errors) throws Exception {
  248.         try {
  249.             OperazioniFormBean form = null;

  250.             String operazione = null;
  251.             // controllo se richiesta corretta
  252.             boolean trovato = false;
  253.             operazione = this.getParameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE);
  254.             if ((operazione == null) || operazione.equals("")) {
  255.                 operazione = OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE_IN_CODA;
  256.             }


  257.             String [] tipiOperazione = OperazioniCostanti.getParametroOperazioniOperazioneList();

  258.             for (int i = 0; (i < tipiOperazione.length) && (!trovato); i++) {
  259.                 if (operazione.equals(tipiOperazione[i])){
  260.                     trovato = true;
  261.                     continue;
  262.                 }
  263.             }

  264.             if (trovato == false) {
  265.                 errors.add(OperazioniCostanti.ERRORE_TIPO_OPERAZIONE_SCONOSCIUTO);
  266.                 throw new Exception("Tipo Operazione selezionato SCONOSCIUTO.");
  267.             }

  268.             form = new OperazioniFormBean();

  269.             form.setTipo(operazione);

  270.             String idOperazione = this.getParameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_ID);
  271.             if ((idOperazione != null) && !idOperazione.equals("")) {
  272.                 form.setIdOperazione(idOperazione);
  273.             }

  274.             // utente

  275.             String utente = this.getParameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_UTENTE);

  276.             List<User> userList = new ArrayList<>();
  277.             String utenteTmp = null;
  278.             HttpSession session = this.request.getSession(true);
  279.             User user = ServletUtils.getUserFromSession(this.request, session);
  280.             PermessiUtente permessi = user.getPermessi();

  281.             if(permessi.isUtenti()) {
  282.                 utenteTmp = OperazioniCostanti.PARAMETRO_OPERAZIONI_UTENTE_ALL;
  283.                 userList = this.utentiCore.userList(new ConsoleSearch(true));
  284.             } else {
  285.                 utenteTmp = user.getLogin();
  286.                 userList = new ArrayList<>();
  287.             }

  288.             if(utente == null){
  289.                 form.setUtente(utenteTmp);
  290.             } else
  291.                 form.setUtente(utente);


  292.             form.setListaUser(userList);

  293.             // Pagina da visualizzare
  294.             String metodo = this.getParameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_METHOD);

  295.             if(metodo == null)
  296.                 metodo = OperazioniCostanti.DEFAULT_VALUE_FORM_BEAN_METHOD_FORM;

  297.             form.setMethod(metodo);

  298.             return form;
  299.         } catch (Exception e) {
  300.             throw new Exception(e);
  301.         }
  302.     }
  303.    
  304.     public List<Parameter> getFormBeanAsParameters(OperazioniFormBean formBean){
  305.         List<Parameter> listaParametri = new ArrayList<>();
  306.        
  307.         if(formBean.getTipo() != null){
  308.             listaParametri.add(new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_OPERAZIONE, formBean.getTipo()));
  309.         }
  310.        
  311.         if(formBean.getMethod() != null){
  312.             listaParametri.add(new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_METHOD, formBean.getMethod()));
  313.         }
  314.        
  315.         if(formBean.getUtente() != null){
  316.             listaParametri.add(new Parameter(OperazioniCostanti.PARAMETRO_OPERAZIONI_UTENTE, formBean.getUtente()));
  317.         }
  318.        
  319.         return listaParametri;
  320.     }


  321.     public void showDettagliMessaggio(Operation op, OperazioniFormBean formBean)
  322.             throws Exception {
  323.         try {

  324.             List<Parameter> lstUrlParam = this.getFormBeanAsParameters(formBean);

  325.             // setto la barra del titolo
  326.             List<Parameter> lstParam = new ArrayList<>();

  327.             lstParam.add(new Parameter(OperazioniCostanti.LABEL_OPERAZIONI, OperazioniCostanti.SERVLET_NAME_OPERAZIONI,lstUrlParam));

  328.             String elencoLabel = OperazioniCostanti.getTipoOperazioneLabelFromValue(formBean.getTipo());
  329.             lstParam.add(new Parameter(elencoLabel, OperazioniCostanti.SERVLET_NAME_OPERAZIONI_LIST, lstUrlParam));
  330.             lstParam.add(new Parameter(
  331.                     op.getOperation() + " (" + op.getId() + ") - " + op.getHostname(), null));

  332.             ServletUtils.setPageDataTitle(this.pd, lstParam);

  333.             List<DataElement> dati = new ArrayList<>();
  334.             DataElement de = new DataElement();

  335.             de.setType(DataElementType.TITLE);
  336.             de.setLabel(OperazioniCostanti.LABEL_OPERAZIONI_DETTAGLI);
  337.             dati.add(de);

  338.             DataElement timereq = new DataElement();
  339.             timereq.setLabel(OperazioniCostanti.LABEL_OPERAZIONI_RICHIESTA_IL);
  340.             timereq.setValue("" + op.getTimeReq());
  341.             dati.add(timereq);
  342.             if (OperationStatus.NOT_SET.equals(op.getStatus())) {
  343.                 String tmpDet = op.getDetails();
  344.                 if ((tmpDet != null) && !tmpDet.equals("")) {
  345.                     // Questo caso si verifica quando l'host o il
  346.                     // superutente non sono abilitati ad effettuare
  347.                     // operazioni
  348.                     DataElement errore = new DataElement();
  349.                     errore.setLabel(OperazioniCostanti.LABEL_OPERAZIONI_PARAMETRO_ERRORE);
  350.                     errore.setValue(formatDetail(op.getDetails()));
  351.                     errore.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_ERRORE);
  352.                     dati.add(errore);
  353.                 }
  354.             } else {
  355.                 DataElement timexecute = new DataElement();
  356.                 if (op.isDeleted()) {
  357.                     timexecute.setLabel(OperazioniCostanti.LABEL_OPERAZIONI_ELIMINATA_IL);
  358.                 } else {
  359.                     timexecute.setLabel(OperazioniCostanti.LABEL_OPERAZIONI_ESEGUITA_IL);
  360.                 }
  361.                 timexecute.setValue("" + op.getTimeExecute());
  362.                 timexecute.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_TEMPO_ESECUZIONE);
  363.                 dati.add(timexecute);
  364.             }
  365.             if (OperationStatus.ERROR.equals(op.getStatus()) ||
  366.                     OperationStatus.INVALID.equals(op.getStatus()) ||
  367.                     OperationStatus.WAIT.equals(op.getStatus()) ) {
  368.                 DataElement errore = new DataElement();
  369.                 errore.setLabel(OperazioniCostanti.LABEL_OPERAZIONI_PARAMETRO_ERRORE);
  370.                 errore.setValue(formatDetail(op.getDetails()));
  371.                 errore.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_ERRORE);
  372.                 dati.add(errore);
  373.             }
  374.             if (OperationStatus.WAIT.equals(op.getStatus())) {
  375.                 DataElement timeWait = new DataElement();
  376.                 timeWait.setLabel(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_WAITING_TIME);
  377.                 String tw = "";
  378.                 String val = op.getStatus().toString();
  379.                 if (val.equals(OperazioniCostanti.DEFAULT_VALUE_PARAMETRO_OPERAZIONI_WAIT_TIME_WAIT)) {
  380.                     tw = "" + op.getWaitTime();
  381.                 }
  382.                 timeWait.setValue(tw);
  383.                 timeWait.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_WAIT_TIME);
  384.                 dati.add(timeWait);
  385.             }

  386.             DataElement partitle = new DataElement();
  387.             partitle.setType(DataElementType.TITLE);
  388.             partitle.setLabel(OperazioniCostanti.LABEL_PARAMETRI);
  389.             dati.add(partitle);

  390.             org.openspcoop2.web.lib.queue.dao.Parameter[] parList = op.getParameters();
  391.             for (int i = 0; i < parList.length; i++) {
  392.                 org.openspcoop2.web.lib.queue.dao.Parameter singlePar = parList[i];

  393.                 DataElement par = new DataElement();
  394.                 String name = singlePar.getName();
  395.                 par.setLabel(name);
  396.                 if (name.equals(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_PASSWORD) ||
  397.                         name.equals(OperazioniCostanti.LABEL_PARAMETRO_OPERAZIONI_NUOVA_PASSWORD)) {
  398.                     par.setValue("********");
  399.                 } else {
  400.                     par.setValue(singlePar.getValue());
  401.                 }
  402.                 par.setType(DataElementType.TEXT);
  403.                 par.setName(OperazioniCostanti.PARAMETRO_OPERAZIONI_PAR);
  404.                 dati.add(par);
  405.             }

  406.             this.pd.setDati(dati);
  407.             this.pd.setMode(Costanti.DATA_ELEMENT_EDIT_MODE_DISABLE_NAME);
  408.         } catch (Exception e) {
  409.             throw new Exception(e);
  410.         }
  411.     }
  412.    
  413.     private String formatDetail(String detail){
  414.         if(detail==null){
  415.             return null;
  416.         }
  417.         else{
  418.             return detail.replaceAll("\n", org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE);
  419.         }
  420.     }
  421. }