GruppiHelper.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.gruppi;

  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.List;

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

  28. import org.apache.commons.lang.StringUtils;
  29. import org.openspcoop2.core.commons.ErrorsHandlerCostant;
  30. import org.openspcoop2.core.commons.Filtri;
  31. import org.openspcoop2.core.commons.ISearch;
  32. import org.openspcoop2.core.commons.Liste;
  33. import org.openspcoop2.core.commons.SearchUtils;
  34. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  35. import org.openspcoop2.core.id.IDGruppo;
  36. import org.openspcoop2.core.registry.Gruppo;
  37. import org.openspcoop2.protocol.engine.utils.DBOggettiInUsoUtils;
  38. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  39. import org.openspcoop2.web.ctrlstat.costanti.InUsoType;
  40. import org.openspcoop2.web.ctrlstat.servlet.ConsoleHelper;
  41. import org.openspcoop2.web.ctrlstat.servlet.archivi.ExporterUtils;
  42. import org.openspcoop2.web.lib.mvc.AreaBottoni;
  43. import org.openspcoop2.web.lib.mvc.Costanti;
  44. import org.openspcoop2.web.lib.mvc.DataElement;
  45. import org.openspcoop2.web.lib.mvc.DataElementType;
  46. import org.openspcoop2.web.lib.mvc.PageData;
  47. import org.openspcoop2.web.lib.mvc.Parameter;
  48. import org.openspcoop2.web.lib.mvc.ServletUtils;
  49. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  50. /**
  51.  * GruppiHelper
  52.  *
  53.  * @author Poli Andrea (apoli@link.it)
  54.  * @author Giuliano Pintori (pintori@link.it)
  55.  * @author $Author$
  56.  * @version $Rev$, $Date$
  57.  */
  58. public class GruppiHelper extends ConsoleHelper{

  59.     public GruppiHelper(HttpServletRequest request, PageData pd,
  60.             HttpSession session) throws Exception {
  61.         super(request, pd,  session);
  62.     }
  63.     public GruppiHelper(ControlStationCore core, HttpServletRequest request, PageData pd,
  64.             HttpSession session) throws Exception {
  65.         super(core, request, pd,  session);
  66.     }

  67.     public List<DataElement> addGruppoToDati(TipoOperazione tipoOP, Long gruppoId, String nome, String descrizione, String serviceBinding, List<DataElement> dati) throws DriverConfigurazioneException {
  68.        
  69.         Gruppo gruppo = null;
  70.         if(TipoOperazione.CHANGE.equals(tipoOP) && nome!=null && StringUtils.isNotEmpty(nome)){
  71.             gruppo = this.gruppiCore.getGruppo(nome);
  72.         }
  73.        
  74.         if(TipoOperazione.CHANGE.equals(tipoOP)){
  75.            
  76.             // In Uso Button
  77.             this.addComandoInUsoButton(nome,
  78.                     nome,
  79.                     InUsoType.GRUPPO);
  80.                    
  81.             // Proprieta Button
  82.             if(gruppo!=null && this.existsProprietaOggetto(gruppo.getProprietaOggetto(), gruppo.getDescrizione())) {
  83.                 this.addComandoProprietaOggettoButton(nome,
  84.                         nome, InUsoType.GRUPPO);
  85.             }
  86.         }
  87.        
  88.         DataElement de = new DataElement();
  89.         de.setLabel(GruppiCostanti.LABEL_GRUPPO);
  90.         de.setType(DataElementType.TITLE);
  91.         dati.add(de);
  92.        
  93.         if(gruppoId!=null){
  94.             de = new DataElement();
  95.             de.setLabel(GruppiCostanti.PARAMETRO_GRUPPO_ID);
  96.             de.setValue(gruppoId.longValue()+"");
  97.             de.setType(DataElementType.HIDDEN);
  98.             de.setName(GruppiCostanti.PARAMETRO_GRUPPO_ID);
  99.             de.setSize( getSize());
  100.             dati.add(de);
  101.         }
  102.        
  103.         de = new DataElement();
  104.         de.setLabel(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_NOME);
  105.         de.setValue(nome);
  106.         de.setType(DataElementType.TEXT_EDIT);
  107.         de.setName(GruppiCostanti.PARAMETRO_GRUPPO_NOME);
  108.         de.setSize( getSize());
  109.         de.setRequired(true);
  110.         dati.add(de);

  111.         de = new DataElement();
  112.         de.setLabel(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_DESCRIZIONE);
  113.         de.setValue(descrizione);
  114.         de.setType(DataElementType.TEXT_AREA);
  115.         de.setRows(2);
  116.         de.setName(GruppiCostanti.PARAMETRO_GRUPPO_DESCRIZIONE);
  117.         de.setSize( getSize());
  118.         dati.add(de);

  119.         de = new DataElement();
  120.         de.setLabel(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING);
  121.         de.setType(DataElementType.SELECT);
  122.         de.setName(GruppiCostanti.PARAMETRO_GRUPPO_SERVICE_BINDING);
  123.         de.setLabels(GruppiCostanti.getLabelsSelectParametroGruppoServiceBinding());
  124.         de.setValues(GruppiCostanti.getValuesSelectParametroGruppoServiceBinding());
  125.         de.setSelected(serviceBinding);
  126.         dati.add(de);
  127.    
  128.         return dati;
  129.     }
  130.    
  131.    
  132.     // Controlla i dati del registro
  133.     public boolean gruppoCheckData(TipoOperazione tipoOp, Gruppo gruppo) throws Exception {

  134.         try{

  135.             String nome = this.getParameter(GruppiCostanti.PARAMETRO_GRUPPO_NOME);
  136.             String descrizione = this.getParameter(GruppiCostanti.PARAMETRO_GRUPPO_DESCRIZIONE);
  137.             String serviceBinding = this.getParameter(GruppiCostanti.PARAMETRO_GRUPPO_SERVICE_BINDING);
  138.            
  139.             // Campi obbligatori
  140.             if (nome.equals("")) {
  141.                 String tmpElenco = "";
  142.                 if (nome.equals("")) {
  143.                     tmpElenco = GruppiCostanti.LABEL_PARAMETRO_GRUPPO_NOME;
  144.                 }
  145.                 this.pd.setMessage("Dati incompleti. &Egrave; necessario indicare: " + tmpElenco);
  146.                 return false;
  147.             }

  148.             // Controllo che non ci siano spazi nei campi di testo
  149.             if ((nome.indexOf(" ") != -1) ) {
  150.                 this.pd.setMessage("Non inserire spazi nel campo '"+GruppiCostanti.LABEL_PARAMETRO_GRUPPO_NOME+"'");
  151.                 return false;
  152.             }
  153.             if(!this.checkNCName(nome, GruppiCostanti.LABEL_PARAMETRO_GRUPPO_NOME)){
  154.                 return false;
  155.             }
  156.             if(!this.checkLength255(nome, GruppiCostanti.LABEL_PARAMETRO_GRUPPO_NOME)) {
  157.                 return false;
  158.             }
  159.            
  160.             if(descrizione!=null && !"".equals(descrizione) &&
  161.                 !this.checkLength4000(descrizione, GruppiCostanti.LABEL_PARAMETRO_GRUPPO_DESCRIZIONE)) {
  162.                 return false;
  163.             }

  164.             // Se tipoOp = add, controllo che il registro non sia gia' stato
  165.             // registrata
  166.             if (tipoOp.equals(TipoOperazione.ADD)) {
  167.                
  168.                 if(this.gruppiCore.existsGruppo(nome)){
  169.                     this.pd.setMessage("Un gruppo con nome '" + nome + "' risulta gi&agrave; stato registrato");
  170.                     return false;
  171.                 }
  172.                
  173.             }
  174.             else{
  175.                
  176.                 // se ho modificato il service binding controllo che il gruppo non sia usato in un accordo con service binding non piu' compatibile.
  177.                 String oldServiceBinding = GruppiCostanti.DEFAULT_VALUE_PARAMETRO_GRUPPO_SERVICE_BINDING_QUALSIASI;
  178.                 if(gruppo.getServiceBinding() != null)
  179.                     oldServiceBinding = gruppo.getServiceBinding().name();
  180.                
  181.                 // casi da controllare:
  182.                 // service binding era vuoto e viene impostato a SOAP o REST
  183.                 // service binding cambia da SOAP a REST o viceversa
  184.                 if(!serviceBinding.equals(GruppiCostanti.DEFAULT_VALUE_PARAMETRO_GRUPPO_SERVICE_BINDING_QUALSIASI) && !serviceBinding.equals(oldServiceBinding)){
  185.                         //
  186.                     HashMap<ErrorsHandlerCostant, List<String>> whereIsInUso = new HashMap<>();
  187.                     boolean normalizeObjectIds = !this.isModalitaCompleta();
  188.                     boolean gruppoInUso = this.gruppiCore.isGruppoInUso(gruppo.getNome(),whereIsInUso,normalizeObjectIds);
  189.                     String newLine = org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE;
  190.                     StringBuilder inUsoMessage = new StringBuilder();
  191.                    
  192.                     if (gruppoInUso) {
  193.                         inUsoMessage.append(DBOggettiInUsoUtils.toString(new IDGruppo(gruppo.getNome()), whereIsInUso, true, newLine, " non modificabile perch&egrave; :"));
  194.                         inUsoMessage.append(newLine);
  195.                        
  196.                         this.pd.setMessage(inUsoMessage.toString());
  197.                         return false;
  198.                     }
  199.                 }
  200.                
  201.                 if(!gruppo.getNome().equals(nome) &&
  202.                     // e' stato modificato ilnome
  203.                    
  204.                     // e' stato implementato l'update
  205. /**                 java.util.HashMap<org.openspcoop2.core.commons.ErrorsHandlerCostant, List<String>> whereIsInUso = new java.util.HashMap<org.openspcoop2.core.commons.ErrorsHandlerCostant, List<String>>();
  206. //                  boolean gruppoInUso = this.confCore.isGruppoInUso(gruppo.getNome(),whereIsInUso);
  207. //                  if (gruppoInUso) {
  208. //                      String msg = "";
  209. //                      msg += org.openspcoop2.core.commons.DBOggettiInUsoUtils.toString(new org.openspcoop2.core.id.IDGruppo(gruppo.getNome()), whereIsInUso, true, org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE,
  210. //                              " non modificabile in '"+nome+"' perch&egrave; risulta utilizzato:");
  211. //                      msg += org.openspcoop2.core.constants.Costanti.WEB_NEW_LINE;
  212. //                      this.pd.setMessage(msg);
  213. //                      return false;
  214. //                  }
  215. //                  */
  216.                     (this.gruppiCore.existsGruppo(nome))
  217.                     ){
  218.                     this.pd.setMessage("Un gruppo con nome '" + nome + "' risulta gi&agrave; stato registrato");
  219.                     return false;
  220.                    
  221.                 }
  222.                
  223.             }
  224.                
  225.             return true;

  226.         } catch (Exception e) {
  227.             this.logError("Exception: " + e.getMessage(), e);
  228.             throw new Exception(e.getMessage(),e);
  229.         }
  230.     }
  231.    
  232.    
  233.     // Prepara la lista di gruppi
  234.     public void prepareGruppiList(ISearch ricerca, List<Gruppo> lista)
  235.             throws Exception {
  236.         try {
  237.             ServletUtils.addListElementIntoSession(this.request, this.session, GruppiCostanti.OBJECT_NAME_GRUPPI);
  238.            
  239.             this.pd.setCustomListViewName(GruppiCostanti.GRUPPI_NOME_VISTA_CUSTOM_LISTA);

  240.             int idLista = Liste.GRUPPI;
  241.             int limit = ricerca.getPageSize(idLista);
  242.             int offset = ricerca.getIndexIniziale(idLista);
  243.             String search = ServletUtils.getSearchFromSession(ricerca, idLista);

  244.             String filterGruppoServiceBinding = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_SERVICE_BINDING);
  245.             this.addFilterServiceBinding(filterGruppoServiceBinding, false, false);
  246.            
  247.             this.pd.setIndex(offset);
  248.             this.pd.setPageSize(limit);
  249.             this.pd.setNumEntries(ricerca.getNumEntries(idLista));

  250.             // setto la barra del titolo
  251.             if (search.equals("")) {
  252.                 this.pd.setSearchDescription("");
  253.                 ServletUtils.setPageDataTitle(this.pd,
  254.                         new Parameter(GruppiCostanti.LABEL_GRUPPI, GruppiCostanti.SERVLET_NAME_GRUPPI_LIST));
  255.             }
  256.             else{
  257.                 ServletUtils.setPageDataTitle(this.pd,
  258.                         new Parameter(GruppiCostanti.LABEL_GRUPPI, GruppiCostanti.SERVLET_NAME_GRUPPI_LIST),
  259.                         new Parameter(Costanti.PAGE_DATA_TITLE_LABEL_RISULTATI_RICERCA, null));
  260.             }

  261.             // controllo eventuali risultati ricerca
  262.             this.pd.setSearchLabel(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_NOME);
  263.             if (!search.equals("")) {
  264.                 ServletUtils.enabledPageDataSearch(this.pd, GruppiCostanti.LABEL_GRUPPI, search);
  265.             }

  266.             // setto le label delle colonne
  267.             String[] labels = {
  268.                     GruppiCostanti.LABEL_GRUPPI
  269.             };
  270.            
  271.             this.pd.setLabels(labels);

  272.             // preparo i dati
  273.             List<List<DataElement>> dati = new ArrayList<>();

  274.             if (lista != null) {
  275.                 Iterator<Gruppo> it = lista.iterator();
  276.                 while (it.hasNext()) {
  277.                     List<DataElement> e = creaEntryGruppoCustom(it);
  278.                    
  279.                     dati.add(e);
  280.                 }
  281.             }

  282.             this.pd.setDati(dati);
  283.             this.pd.setAddButton(true);
  284.            
  285.             // preparo bottoni
  286.             if(lista!=null && !lista.isEmpty() &&
  287.                 this.core.isShowPulsantiImportExport()) {

  288.                 ExporterUtils exporterUtils = new ExporterUtils(this.archiviCore);
  289.                 if(exporterUtils.existsAtLeastOneExportMode(org.openspcoop2.protocol.sdk.constants.ArchiveType.GRUPPO, this.request, this.session)){

  290.                     List<AreaBottoni> bottoni = new ArrayList<>();

  291.                     AreaBottoni ab = new AreaBottoni();
  292.                     List<DataElement> otherbott = new ArrayList<>();
  293.                     DataElement de = new DataElement();
  294.                     de.setValue(GruppiCostanti.LABEL_GRUPPI_ESPORTA_SELEZIONATI);
  295.                     de.setOnClick(GruppiCostanti.LABEL_GRUPPI_ESPORTA_SELEZIONATI_ONCLICK);
  296.                     de.setDisabilitaAjaxStatus();
  297.                     otherbott.add(de);
  298.                     ab.setBottoni(otherbott);
  299.                     bottoni.add(ab);

  300.                     this.pd.setAreaBottoni(bottoni);

  301.                 }

  302.             }
  303.            
  304.         } catch (Exception e) {
  305.             this.logError("Exception: " + e.getMessage(), e);
  306.             throw new Exception(e.getMessage(),e);
  307.         }
  308.     }
  309.    
  310.     public List<DataElement> creaEntryGruppo(Iterator<Gruppo> it) {
  311.         Gruppo gruppo = it.next();

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

  313.         DataElement de = new DataElement();
  314.         Parameter pId = new Parameter(GruppiCostanti.PARAMETRO_GRUPPO_ID, gruppo.getId()+"");
  315.         de.setUrl(GruppiCostanti.SERVLET_NAME_GRUPPI_CHANGE , pId);
  316.         de.setToolTip(gruppo.getDescrizione());
  317.         de.setValue(gruppo.getNome());
  318.         de.setIdToRemove(gruppo.getNome());
  319.         de.setToolTip(gruppo.getDescrizione());
  320.         e.add(de);

  321.         de = new DataElement();
  322.         if(gruppo.getServiceBinding() == null) {
  323.             de.setValue(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING_QUALSIASI);
  324.         } else {
  325.             switch (gruppo.getServiceBinding()) {
  326.             case REST:
  327.                 de.setValue(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING_REST);
  328.                 break;
  329.             case SOAP:
  330.                 de.setValue(GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING_SOAP);
  331.                 break;
  332.             default:
  333.                 de.setValue("?");
  334.                 break;
  335.             }
  336.         }
  337.         e.add(de);

  338.         this.addInUsoButtonVisualizzazioneClassica(e, gruppo.getNome(), gruppo.getNome(), InUsoType.GRUPPO);
  339.         return e;
  340.     }
  341.    
  342.     private List<DataElement> creaEntryGruppoCustom(Iterator<Gruppo> it) {
  343.         Gruppo gruppo = it.next();

  344.         List<DataElement> e = new ArrayList<>();
  345.        
  346.         // Titolo (nome)
  347.         DataElement de = new DataElement();
  348.         Parameter pId = new Parameter(GruppiCostanti.PARAMETRO_GRUPPO_ID, gruppo.getId()+"");
  349.         de.setUrl(GruppiCostanti.SERVLET_NAME_GRUPPI_CHANGE , pId);
  350.         de.setValue(gruppo.getNome());
  351.         de.setIdToRemove(gruppo.getNome());
  352.         de.setToolTip(gruppo.getDescrizione());
  353.         de.setType(DataElementType.TITLE);
  354.         e.add(de);
  355.        
  356.        
  357.         de = new DataElement();
  358.         if(gruppo.getServiceBinding() == null) {
  359.             de.setValue(MessageFormat.format(GruppiCostanti.MESSAGE_METADATI_GRUPPO_TIPO, GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING_QUALSIASI));
  360.         } else {
  361.             switch (gruppo.getServiceBinding()) {
  362.             case REST:
  363.                 de.setValue(MessageFormat.format(GruppiCostanti.MESSAGE_METADATI_GRUPPO_TIPO, GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING_REST));
  364.                 break;
  365.             case SOAP:
  366.                 de.setValue(MessageFormat.format(GruppiCostanti.MESSAGE_METADATI_GRUPPO_TIPO, GruppiCostanti.LABEL_PARAMETRO_GRUPPO_SERVICE_BINDING_SOAP));
  367.                 break;
  368.             default:
  369.                 de.setValue("?");
  370.                 break;
  371.             }
  372.         }
  373.        
  374.         de.setType(DataElementType.SUBTITLE);
  375.         e.add(de);

  376.         this.addInUsoButton(e, gruppo.getNome(), gruppo.getNome(), InUsoType.GRUPPO);
  377.        
  378.         // Proprieta Button
  379.         /**if(this.existsProprietaOggetto(gruppo.getProprietaOggetto(), gruppo.getDescrizione())) {
  380.          * ** la lista non riporta le proprietà. Ma esistono e poi sarà la servlet a gestirlo
  381.          */
  382.         this.addProprietaOggettoButton(e, gruppo.getNome(), gruppo.getNome(), InUsoType.GRUPPO);
  383.        
  384.         return e;
  385.     }
  386. }