RemoteStoresHelper.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.remote_stores;

  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.apache.commons.lang.StringUtils;
  28. import org.openspcoop2.core.commons.Filtri;
  29. import org.openspcoop2.core.commons.Liste;
  30. import org.openspcoop2.core.commons.SearchUtils;
  31. import org.openspcoop2.pdd.core.keystore.RemoteStore;
  32. import org.openspcoop2.pdd.core.keystore.RemoteStoreKeyEntry;
  33. import org.openspcoop2.utils.date.DateUtils;
  34. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  35. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  36. import org.openspcoop2.web.ctrlstat.servlet.ConsoleHelper;
  37. import org.openspcoop2.web.ctrlstat.servlet.archivi.ArchiviCostanti;
  38. import org.openspcoop2.web.lib.mvc.DataElement;
  39. import org.openspcoop2.web.lib.mvc.DataElementType;
  40. import org.openspcoop2.web.lib.mvc.PageData;
  41. import org.openspcoop2.web.lib.mvc.Parameter;
  42. import org.openspcoop2.web.lib.mvc.ServletUtils;

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

  52.     public RemoteStoresHelper(HttpServletRequest request, PageData pd, HttpSession session) {
  53.         super(request, pd,  session);
  54.     }
  55.     public RemoteStoresHelper(ControlStationCore core, HttpServletRequest request, PageData pd, HttpSession session) {
  56.         super(core, request, pd,  session);
  57.     }


  58.     public void prepareRemoteStoreKeysList(ConsoleSearch ricerca, List<RemoteStoreKeyEntry> lista, long remoteStoreId) {
  59.         try {
  60.             Parameter pIdRemoteStore = new Parameter(RemoteStoresCostanti.PARAMETRO_REMOTE_STORE_ID, remoteStoreId + "");
  61.            
  62.             ServletUtils.addListElementIntoSession(this.request, this.session, RemoteStoresCostanti.OBJECT_NAME_REMOTE_STORES_KEYS, pIdRemoteStore);

  63.             int idLista = Liste.REMOTE_STORE_KEY;
  64.             int limit = ricerca.getPageSize(idLista);
  65.             int offset = ricerca.getIndexIniziale(idLista);

  66.             String filterRemoteStoreId = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_ID);

  67.             // primo accesso
  68.             if(StringUtils.isEmpty(filterRemoteStoreId)) {
  69.                 filterRemoteStoreId = remoteStoreId + "";
  70.             }
  71.            
  72.             this.addFilterRemoteStoreId(filterRemoteStoreId, true);

  73.             String filterRemoteStoreKid = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_KEY_KID);
  74.             this.addFilterRemoteStoreKid(filterRemoteStoreKid);

  75.             String filterRemoteStoreClientId = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_KEY_CLIENT_ID);
  76.             this.addFilterRemoteStoreClientId(filterRemoteStoreClientId);

  77.             String filterRemoteStoreOrganizzazione = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_REMOTE_STORE_KEY_ORGANIZZAZIONE);
  78.             this.addFilterRemoteStoreOrganizzazione(filterRemoteStoreOrganizzazione);

  79.             this.pd.setIndex(offset);
  80.             this.pd.setPageSize(limit);
  81.             this.pd.setNumEntries(ricerca.getNumEntries(idLista));
  82.             this.pd.nascondiTextFilterAutomatico();

  83.             // setto la barra del titolo
  84.             ServletUtils.setPageDataTitle(this.pd,
  85.                     new Parameter(RemoteStoresCostanti.LABEL_CACHE_PDND, RemoteStoresCostanti.SERVLET_NAME_REMOTE_STORES_KEYS_LIST));

  86.             // Label colonne
  87.             String[] labels = {
  88.                     RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_DATA_REGISTRAZIONE,
  89.                     RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CONTENT_KEY,
  90.                     RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CLIENT_ID,
  91.                     RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_ORGANIZATION_DETAILS
  92.             };
  93.             this.pd.setLabels(labels);

  94.             // preparo i dati
  95.             List<List<DataElement>> dati = new ArrayList<>();

  96.             if (lista != null) {
  97.                 Iterator<RemoteStoreKeyEntry> it = lista.iterator();
  98.                 while (it.hasNext()) {
  99.                     List<DataElement> e = this.creaEntry(it, pIdRemoteStore);
  100.                     dati.add(e);
  101.                 }
  102.             }

  103.             this.pd.setDati(dati);
  104.             // rimuovo il tasto add
  105.             this.pd.setAddButton(false);
  106.         } catch (Exception e) {
  107.             this.log.error("Exception: " + e.getMessage(), e);
  108.         }
  109.     }


  110.     private List<DataElement> creaEntry(Iterator<RemoteStoreKeyEntry> it, Parameter pIdRemoteStore) {
  111.         RemoteStoreKeyEntry entry = it.next();
  112.         List<DataElement> e = new ArrayList<>();

  113.         // Data creazione
  114.         SimpleDateFormat formatter = DateUtils.getDefaultDateTimeFormatter("yyyy-MM-dd HH:mm:ss.SSS");
  115.         DataElement de = new DataElement();
  116.         Parameter pId = new Parameter(RemoteStoresCostanti.PARAMETRO_REMOTE_STORE_KEY_ID, entry.getId()+"");
  117.         de.setUrl(RemoteStoresCostanti.SERVLET_NAME_REMOTE_STORES_KEYS_CHANGE , pId, pIdRemoteStore);
  118.         de.setValue(formatter.format(entry.getDataRegistrazione()));
  119.         de.setIdToRemove(entry.getId() + "");
  120.         de.setSize(this.core.getElenchiMenuIdentificativiLunghezzaMassima());
  121.         e.add(de);
  122.        
  123.         // chiave pubblica

  124.         de = new DataElement();
  125.         if(entry.getContentKey() != null && isKidNotStartsWithClientId(entry.getKid())) {
  126.             /** de.setValue(RemoteStoresCostanti.LABEL_DOWNLOAD.toLowerCase());
  127.                 de.setToolTip(entry.getKid());*/
  128.             de.setValue(entry.getKid());
  129.             de.setToolTip(RemoteStoresCostanti.LABEL_DOWNLOAD.toLowerCase());
  130.            
  131.             de.setUrl(ArchiviCostanti.SERVLET_NAME_DOCUMENTI_EXPORT,
  132.                     new Parameter(ArchiviCostanti.PARAMETRO_ARCHIVI_ALLEGATO_ID_ACCORDO, entry.getId()+""),
  133.                     new Parameter(ArchiviCostanti.PARAMETRO_ARCHIVI_ALLEGATO_TIPO_ACCORDO_TIPO_DOCUMENTO, ArchiviCostanti.PARAMETRO_VALORE_ARCHIVI_ALLEGATO_TIPO_DOCUMENTO_CHIAVE_PUBBLICA ),
  134.                     new Parameter(ArchiviCostanti.PARAMETRO_ARCHIVI_ALLEGATO_TIPO_ACCORDO, ArchiviCostanti.PARAMETRO_VALORE_ARCHIVI_ALLEGATO_TIPO_REMOTE_STORE_ENTRY));
  135.             de.setDisabilitaAjaxStatus();
  136.            
  137.         }else {
  138.             de.setValue(RemoteStoresCostanti.VALUE_PARAMETRO_NON_PRESENTE);
  139.         }
  140.         e.add(de);
  141.        
  142.         // client id
  143.         de = new DataElement();
  144.         if(entry.getClientId() != null) {
  145.             de.setValue(entry.getClientId());
  146.             de.setSize(this.core.getElenchiMenuIdentificativiLunghezzaMassima());
  147.         }else {
  148.             de.setValue(RemoteStoresCostanti.VALUE_PARAMETRO_NON_PRESENTE);
  149.         }
  150.         e.add(de);
  151.        
  152.         // dettagli organizzazione
  153.         de = new DataElement();
  154.         if(entry.getOrganizationName() != null) {
  155.             StringBuilder sb = new StringBuilder();
  156.             sb.append(entry.getOrganizationName()).append(" (").append(entry.getOrganizationExternalOrigin()).append(" ").append(entry.getOrganizationExternalId()).append(")");
  157.             de.setValue(sb.toString());
  158.             de.setToolTip(entry.getOrganizationCategory());
  159.             de.setSize(this.core.getElenchiMenuIdentificativiLunghezzaMassima());
  160.         }else {
  161.             de.setValue(RemoteStoresCostanti.VALUE_PARAMETRO_NON_PRESENTE);
  162.         }
  163.         e.add(de);
  164.        
  165.        
  166.         return e;
  167.     }
  168.    
  169.     private boolean isKidNotStartsWithClientId(String kid) {
  170.         return kid != null && !kid.startsWith(RemoteStoresCostanti.VALUE_PARAMETRO_REMOTE_STORE_KEY_KID_STARTS_WITH_CLIENT_ID);
  171.     }
  172.    
  173.    
  174.    
  175.     private void addFilterRemoteStoreOrganizzazione(String filterRemoteStoreOrganizzazione) throws Exception {
  176.         String label = RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_ORGANIZATION_DETAILS;
  177.         this.pd.addTextFilter(Filtri.FILTRO_REMOTE_STORE_KEY_ORGANIZZAZIONE, label, filterRemoteStoreOrganizzazione, this.getSize());
  178.     }
  179.    
  180.     private void addFilterRemoteStoreClientId(String filterRemoteStoreClientId) throws Exception {
  181.         String label = RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CLIENT_ID;
  182.         this.pd.addTextFilter(Filtri.FILTRO_REMOTE_STORE_KEY_CLIENT_ID, label, filterRemoteStoreClientId, this.getSize());
  183.     }
  184.    
  185.     private void addFilterRemoteStoreKid(String filterRemoteStoreKid) throws Exception {
  186.         String label = RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_KID;
  187.         this.pd.addTextFilter(Filtri.FILTRO_REMOTE_STORE_KEY_KID, label, filterRemoteStoreKid, this.getSize());
  188.     }
  189.     private void addFilterRemoteStoreId(String filterRemoteStoreId, boolean postback) throws Exception {
  190.         // se e' presente solo un remote store allora inserisco un campo hidden altriment una select list
  191.         List<RemoteStore> remoteStoresList = this.remoteStoresCore.remoteStoresList();
  192.        
  193.         if(remoteStoresList.size() > 1) {
  194.             String label = RemoteStoresCostanti.LABEL_REMOTE_STORE;
  195.            
  196.             List<String> labels = new ArrayList<>();
  197.             List<String> values = new ArrayList<>();
  198.            
  199.             for (RemoteStore remoteStore : remoteStoresList) {
  200.                 labels.add(remoteStore.getNome());
  201.                 values.add(remoteStore.getId() + "");
  202.             }
  203.            
  204.             this.pd.addFilter(Filtri.FILTRO_REMOTE_STORE_ID, label, filterRemoteStoreId, values.toArray(new String[values.size()]), labels.toArray(new String[labels.size()]), postback, this.getSize());
  205.            
  206.         } else {
  207.             this.pd.addHiddenFilter(Filtri.FILTRO_REMOTE_STORE_ID, filterRemoteStoreId, this.getSize());
  208.         }
  209.     }
  210.    
  211.     public List<DataElement> addRemoteStoreKeyToDati(long remoteStoreId, long id, RemoteStoreKeyEntry entry, List<DataElement> dati) {
  212.         // le informazioni verranno visualizzate in modalita' noedit
  213.         this.pd.disableEditMode();
  214.        
  215.         // data registrazione
  216.         SimpleDateFormat formatter = DateUtils.getDefaultDateTimeFormatter("yyyy-MM-dd HH:mm:ss.SSS");
  217.         DataElement de = new DataElement();
  218.         de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_DATA_REGISTRAZIONE);
  219.         de.setValue(formatter.format(entry.getDataRegistrazione()));
  220.         de.setType(DataElementType.TEXT_EDIT);
  221.         dati.add(de);
  222.        
  223.         // identificativi
  224.         de = new DataElement();
  225.         de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_ID);
  226.         de.setValue(remoteStoreId +"");
  227.         de.setType(DataElementType.HIDDEN);
  228.         dati.add(de);
  229.        
  230.         de = new DataElement();
  231.         de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_ID);
  232.         de.setValue(id +"");
  233.         de.setType(DataElementType.HIDDEN);
  234.         dati.add(de);
  235.        
  236.         // sottosezione chiave pubblica (visualizza solo se il kid non inizia per ClientId--
  237.         if(this.isKidNotStartsWithClientId(entry.getKid())) {
  238.             if(entry.getDataAggiornamento() != null || entry.getContentKey() != null) {
  239.                 de = new DataElement();
  240.                 de.setType(DataElementType.SUBTITLE);
  241.                 de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CONTENT_KEY);
  242.                 dati.add(de);
  243.             }
  244.            
  245.            
  246.             if(entry.getDataAggiornamento() != null) {
  247.                 de = new DataElement();
  248.                 de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_DATA_AGGIORAMENTO);
  249.                 de.setValue(formatter.format(entry.getDataAggiornamento()));
  250.                 de.setType(DataElementType.TEXT_EDIT);
  251.                 dati.add(de);
  252.             }
  253.            
  254.             de = new DataElement();
  255.             de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_KID);
  256.             de.setValue(entry.getKid());
  257.             de.setType(DataElementType.TEXT_EDIT);
  258.             dati.add(de);
  259.            
  260.             if(entry.getContentKey() != null) {
  261.                 de = new DataElement();
  262.                 de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CONTENT_KEY);
  263.                 de.setValue(new String(entry.getContentKey()));
  264.                 de.setType(DataElementType.TEXT_AREA_NO_EDIT);
  265.                 dati.add(de);
  266.                
  267.                 de = new DataElement();
  268.                 de.setLabel("");
  269.                 de.setValue(RemoteStoresCostanti.LABEL_DOWNLOAD.toLowerCase());
  270.                 de.setUrl(ArchiviCostanti.SERVLET_NAME_DOCUMENTI_EXPORT,
  271.                         new Parameter(ArchiviCostanti.PARAMETRO_ARCHIVI_ALLEGATO_ID_ACCORDO, entry.getId()+""),
  272.                         new Parameter(ArchiviCostanti.PARAMETRO_ARCHIVI_ALLEGATO_TIPO_ACCORDO_TIPO_DOCUMENTO, ArchiviCostanti.PARAMETRO_VALORE_ARCHIVI_ALLEGATO_TIPO_DOCUMENTO_CHIAVE_PUBBLICA ),
  273.                         new Parameter(ArchiviCostanti.PARAMETRO_ARCHIVI_ALLEGATO_TIPO_ACCORDO, ArchiviCostanti.PARAMETRO_VALORE_ARCHIVI_ALLEGATO_TIPO_REMOTE_STORE_ENTRY));
  274.                 de.setDisabilitaAjaxStatus();
  275.                 de.setType(DataElementType.LINK);
  276.                 dati.add(de);
  277.             }
  278.         }
  279.        
  280.         // sottosezione informazioni client (visualizza se presente client id)
  281.         if(entry.getClientId() != null) {
  282.             de = new DataElement();
  283.             de.setType(DataElementType.SUBTITLE);
  284.             de.setLabel(RemoteStoresCostanti.LABEL_INFORMAZIONI_CLIENT);
  285.             dati.add(de);
  286.            
  287.             if(entry.getClientDataAggiornamento() != null) {
  288.                 de = new DataElement();
  289.                 de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CLIENT_DATA_AGGIORNAMENTO);
  290.                 de.setValue(formatter.format(entry.getClientDataAggiornamento()));
  291.                 de.setType(DataElementType.TEXT_EDIT);
  292.                 dati.add(de);
  293.             }
  294.            
  295.             de = new DataElement();
  296.             de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CLIENT_ID);
  297.             de.setValue(entry.getClientId());
  298.             de.setType(DataElementType.TEXT_EDIT);
  299.             dati.add(de);
  300.            
  301.             de = new DataElement();
  302.             de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_CLIENT_DETAILS);
  303.             de.setValue(entry.getClientDetails());
  304.             de.setType(DataElementType.TEXT_AREA_NO_EDIT);
  305.             dati.add(de);
  306.            
  307.             de = new DataElement();
  308.             de.setLabel(RemoteStoresCostanti.LABEL_PARAMETRO_REMOTE_STORE_KEY_ORGANIZATION_DETAILS);
  309.             de.setValue(entry.getOrganizationDetails());
  310.             de.setType(DataElementType.TEXT_AREA_NO_EDIT);
  311.             dati.add(de);
  312.         }
  313.        
  314.         return dati;
  315.     }
  316. }