AbstractServletListUtilities.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.plugins.servlet;

  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;

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

  26. import org.slf4j.Logger;
  27. import org.apache.struts.action.Action;
  28. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  29. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  30. import org.openspcoop2.web.ctrlstat.core.UrlParameters;
  31. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  32. import org.openspcoop2.web.ctrlstat.plugins.IExtendedBean;
  33. import org.openspcoop2.web.ctrlstat.plugins.IExtendedListServlet;
  34. import org.openspcoop2.web.ctrlstat.servlet.ConsoleHelper;
  35. import org.openspcoop2.web.lib.mvc.DataElement;
  36. import org.openspcoop2.web.lib.mvc.PageData;
  37. import org.openspcoop2.web.lib.mvc.Parameter;
  38. import org.openspcoop2.web.lib.mvc.ServletUtils;
  39. import org.openspcoop2.web.lib.mvc.TipoOperazione;


  40. /**
  41.  * AbstractServletListUtilities
  42.  *
  43.  * @author Poli Andrea (apoli@link.it)
  44.  * @author $Author$
  45.  * @version $Rev$, $Date$
  46.  */
  47. public abstract class AbstractServletListUtilities extends Action {

  48.     protected abstract UrlParameters getUrlExtendedFather(ConsoleHelper consoleHelper) throws Exception;
  49.    
  50.     protected abstract ConsoleHelper getConsoleHelper(HttpServletRequest request, PageData pd, HttpSession session) throws Exception;
  51.    
  52.     protected abstract ControlStationCore getConsoleCore() throws Exception;
  53.    
  54.     protected abstract IExtendedListServlet getExtendedServlet(ConsoleHelper consoleHelper) throws Exception;
  55.    
  56.     protected abstract Object getObject(ConsoleHelper consoleHelper) throws Exception;
  57.    
  58.     protected abstract List<Parameter> getTitle(Object object, ConsoleHelper consoleHelper) throws Exception;
  59.    
  60.     protected abstract int getIdList() throws Exception;
  61.    
  62.     protected abstract Parameter[] getParameterList(ConsoleHelper consoleHelper) throws Exception;
  63.    
  64.     protected abstract String getObjectName() throws Exception;
  65.    
  66.     protected abstract UrlParameters getUrlExtendedChange(ConsoleHelper consoleHelper) throws Exception;
  67.    
  68.     protected void prepareList(TipoOperazione tipoOperazione, ConsoleHelper consoleHelper, ConsoleSearch ricerca, Object object, IExtendedListServlet extendedServlet, List<IExtendedBean> lista,
  69.             Logger log, UrlParameters extendedFather)
  70.             throws Exception {
  71.         try {

  72.             HttpSession session = consoleHelper.getSession();
  73.             HttpServletRequest request = consoleHelper.getRequest();
  74.             PageData pd = consoleHelper.getPd();
  75.             ControlStationCore consoleCore = consoleHelper.getCore();
  76.            
  77.             List<Parameter> newLista = new ArrayList<>();
  78.            
  79.             Parameter[] pSession = this.getParameterList(consoleHelper);
  80.             if(pSession!=null && pSession.length>0){
  81.                 for (int i = 0; i < pSession.length; i++) {
  82.                     newLista.add(pSession[i]);
  83.                 }
  84.             }
  85.            
  86.             List<Parameter> p = extendedServlet.getParameterForListElementIntoSession(tipoOperazione,consoleHelper);
  87.             if(p!=null && p.size()>0){
  88.                 for (Parameter parameter : p) {
  89.                     newLista.add(parameter);
  90.                 }
  91.             }
  92.            
  93.             String tmp = consoleHelper.getRequest().getParameter(CostantiControlStation.PARAMETRO_EXTENDED_FORM_ID);
  94.             if(tmp!=null && !"".equals(tmp)){
  95.                 newLista.add(new Parameter(CostantiControlStation.PARAMETRO_EXTENDED_FORM_ID, tmp));
  96.             }
  97.            
  98.             Parameter[] pTmp = null;
  99.             if(newLista.size()>0){
  100.                 pTmp = newLista.toArray(new Parameter[1]);
  101.             }
  102.            
  103.             ServletUtils.addListElementIntoSession(request, session, this.getObjectName(),
  104.                     pTmp);

  105.             int idLista = this.getIdList();
  106.             int limit = ricerca.getPageSize(idLista);
  107.             int offset = ricerca.getIndexIniziale(idLista);
  108.             String search = ServletUtils.getSearchFromSession(ricerca, idLista);

  109.             pd.setIndex(offset);
  110.             pd.setPageSize(limit);
  111.             pd.setNumEntries(ricerca.getNumEntries(idLista));

  112.             // setto la barra del titolo 1
  113.             List<Parameter> lstParam = this.getTitle(object,consoleHelper);
  114.             List<Parameter> listP = new ArrayList<>();
  115.             if(extendedFather.sizeParameter()>0){
  116.                 listP.addAll(extendedFather.getParameter());
  117.             }
  118.             String uniqueId = consoleHelper.getRequest().getParameter(CostantiControlStation.PARAMETRO_EXTENDED_FORM_ID);
  119.             if(uniqueId!=null && !"".equals(uniqueId)){
  120.                 listP.add(new Parameter(CostantiControlStation.PARAMETRO_EXTENDED_FORM_ID, uniqueId));
  121.             }
  122.             List<Parameter> listPTitleDynamic = extendedServlet.getListTitleUrlParameters(tipoOperazione, consoleHelper);
  123.             if(listPTitleDynamic!=null && listPTitleDynamic.size()>0){
  124.                 listP.addAll(listPTitleDynamic);
  125.             }
  126.             Parameter [] pLinkTitle = null;
  127.             if(listP.size()>0){
  128.                 pLinkTitle = listP.toArray(new Parameter[1]);
  129.             }
  130.             String title1 = extendedServlet.getListTitle(tipoOperazione,consoleHelper);
  131.            
  132.             String urlTitolo1 = extendedServlet.getListTitleUrl(tipoOperazione, consoleHelper);
  133.             if(urlTitolo1==null || "".equals(urlTitolo1)){
  134.                 urlTitolo1 = extendedFather.getUrl();
  135.             }
  136.            
  137.             // Titolo 2
  138.             String title2 = extendedServlet.getListItemTitle(tipoOperazione,consoleHelper);
  139.            
  140.             if(search.equals("")){
  141.                 pd.setSearchDescription("");
  142.                 lstParam.add(new Parameter(title1, urlTitolo1,pLinkTitle));
  143.                 if(title2!=null){
  144.                     lstParam.add(new Parameter(title2, null));
  145.                 }
  146.             }
  147.             else{
  148.                 lstParam.add(new Parameter(title1, urlTitolo1,pLinkTitle));
  149.                 if(title2!=null){
  150.                     lstParam.add(new Parameter(title2, null));
  151.                 }
  152.                 lstParam.add(new Parameter(AbstractServletCostanti.RISULTATI_RICERCA, null));
  153.             }

  154.             ServletUtils.setPageDataTitle(pd, lstParam.toArray(new Parameter[lstParam.size()]));

  155.             // controllo eventuali risultati ricerca
  156.             if (!search.equals("")) {
  157.                 ServletUtils.enabledPageDataSearch(pd, "Elementi", search);
  158.             }

  159.             // setto le label delle colonne
  160.             String[] labels = extendedServlet.getColumnLabels(tipoOperazione, consoleHelper, consoleCore, object);
  161.             pd.setLabels(labels);

  162.             // preparo i dati
  163.             List<List<DataElement>> dati = new ArrayList<>();

  164.             pd.setAddButton(true); // prima della chiamata ad addDatiToList, in modo da permettere di disattivarlo
  165.            
  166.             if (lista != null) {
  167.                 Iterator<IExtendedBean> it = lista.iterator();
  168.                 while (it.hasNext()) {
  169.                     IExtendedBean extendendBean = it.next();

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

  171.                     extendedServlet.addDatiToList(e, tipoOperazione, consoleHelper, consoleCore, object, extendendBean,
  172.                             this.getUrlExtendedChange(consoleHelper));
  173.                                        
  174.                     dati.add(e);
  175.                 }
  176.             }

  177.             pd.setDati(dati);
  178.            
  179.         } catch (Exception e) {
  180.             log.error("Exception: " + e.getMessage(), e);
  181.             throw new Exception(e);
  182.         }
  183.     }
  184.    
  185.     protected void setFormTitle(Object object,ConsoleHelper consoleHelper,
  186.             IExtendedListServlet extendedServlet,IExtendedBean extendedBean,PageData pd,TipoOperazione tipoOperazione,UrlParameters extendedList) throws Exception{
  187.         List<Parameter> lstParam = this.getTitle(object,consoleHelper);
  188.        
  189.         List<Parameter> listP = new ArrayList<>();
  190.         if(extendedList.sizeParameter()>0){
  191.             listP.addAll(extendedList.getParameter());
  192.         }
  193.         String uniqueId = consoleHelper.getRequest().getParameter(CostantiControlStation.PARAMETRO_EXTENDED_FORM_ID);
  194.         if(uniqueId!=null && !"".equals(uniqueId)){
  195.             listP.add(new Parameter(CostantiControlStation.PARAMETRO_EXTENDED_FORM_ID, uniqueId));
  196.         }
  197.         List<Parameter> listPTitleDynamic = extendedServlet.getFormTitleUrlParameters(tipoOperazione, consoleHelper);
  198.         if(listPTitleDynamic!=null && listPTitleDynamic.size()>0){
  199.             listP.addAll(listPTitleDynamic);
  200.         }
  201.        
  202.         Parameter [] p = null;
  203.         if(listP.size()>0){
  204.             p = listP.toArray(new Parameter[1]);
  205.         }
  206.        
  207.         String urlTitolo1 = extendedServlet.getFormTitleUrl(tipoOperazione, consoleHelper);
  208.         if(urlTitolo1==null || "".equals(urlTitolo1)){
  209.             urlTitolo1 = extendedList.getUrl();
  210.         }
  211.        
  212.         lstParam.add(new Parameter(extendedServlet.getFormTitle(tipoOperazione, consoleHelper),urlTitolo1,p));
  213.        
  214.         lstParam.add(new Parameter(extendedServlet.getFormItemTitle(tipoOperazione, consoleHelper,extendedBean), null));

  215.         ServletUtils.setPageDataTitle(pd, lstParam);
  216.     }
  217. }