ConfigBean.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.lib.mvc.properties.beans;

  21. import java.lang.reflect.InvocationTargetException;
  22. import java.util.ArrayList;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import java.util.Properties;

  27. import org.openspcoop2.core.mvc.properties.utils.Costanti;
  28. import org.openspcoop2.utils.resources.ClassLoaderUtilities;
  29. import org.apache.commons.lang.StringUtils;
  30. import org.openspcoop2.core.mvc.properties.Config;
  31. import org.openspcoop2.core.mvc.properties.Property;
  32. import org.openspcoop2.core.mvc.properties.provider.ExternalResources;
  33. import org.openspcoop2.core.mvc.properties.provider.IProvider;
  34. import org.openspcoop2.core.mvc.properties.provider.ProviderException;
  35. import org.openspcoop2.core.mvc.properties.provider.ProviderValidationException;
  36. import org.openspcoop2.web.lib.mvc.byok.LockUtilities;
  37. import org.openspcoop2.web.lib.mvc.properties.exception.ConditionException;
  38. import org.openspcoop2.web.lib.mvc.properties.exception.UserInputValidationException;
  39. import org.openspcoop2.web.lib.mvc.properties.exception.ValidationException;
  40. import org.openspcoop2.web.lib.mvc.properties.utils.ConditionsEngine;

  41. /***
  42.  *
  43.  * ConfigBean memorizza le informazioni relative ai bean della configurazione
  44.  *
  45.  * @author Pintori Giuliano (pintori@link.it)
  46.  * @author $Author$
  47.  * @version $Rev$, $Date$
  48.  *
  49.  */
  50. public class ConfigBean {

  51.     private String id = null;
  52.     private List<String> listaNomiProperties= null;
  53.     private List<String> listaKeysItem= null;
  54.     private Map<String, BaseItemBean<?>> mapItem = null;
  55.     private Map<String, List<BaseItemBean<?>>> mapPropertyItem = null;
  56.     private IProvider provider;

  57.     public ConfigBean(IProvider provider) {
  58.         this.listaNomiProperties = new ArrayList<>();
  59.         this.listaKeysItem = new ArrayList<>();
  60.         this.mapItem = new HashMap<>();
  61.         this.mapPropertyItem = new HashMap<>();
  62.         this.provider = provider;
  63.     }

  64.     public void clear() {
  65.         this.listaNomiProperties.clear();
  66.         this.listaKeysItem.clear();
  67.         this.mapItem.clear();
  68.         this.mapPropertyItem.clear();
  69.     }

  70.     public void addItem(BaseItemBean<?> item) throws ValidationException{
  71.         if(this.mapItem.containsKey(item.getName()))
  72.             throw new ValidationException("Item ["+item.getName()+"] viola il vincolo di univocita' degli elementi: rinominare uno dei due item.");

  73.         this.listaKeysItem.add(item.getName());
  74.         this.mapItem.put(item.getName(), item);

  75.         // solo per gli item, aggrego per property destionazione
  76.         if(item.getSaveProperty() != null) {
  77.             List<BaseItemBean<?>> lstItems = null;
  78.             if(this.mapPropertyItem.containsKey(item.getSaveProperty().getName())) {
  79.                 lstItems = this.mapPropertyItem.remove(item.getSaveProperty().getName());
  80.             } else {
  81.                 lstItems = new ArrayList<>();
  82.             }
  83.             lstItems.add(item);
  84.             this.mapPropertyItem.put(item.getSaveProperty().getName(), lstItems);
  85.         }
  86.     }

  87.     public IProvider getProvider() {
  88.         return this.provider;
  89.     }
  90.    
  91.     public BaseItemBean<?> getItem(String name){
  92.         return this.mapItem.get(name);
  93.     }

  94.     public List<String> getListakeys(){
  95.         return this.listaKeysItem;
  96.     }

  97.     public List<BaseItemBean<?>> getListaItem(){
  98.         List<BaseItemBean<?>> lista = new ArrayList<>();
  99.         for (String key : this.listaKeysItem) {
  100.             lista.add(this.mapItem.get(key));
  101.         }

  102.         return lista;
  103.     }

  104.     public List<?> getListaItemSDK(){
  105.         List<BaseItemBean<?>> list = getListaItem();
  106.         if(list!=null && !list.isEmpty()) {
  107.             List<Object> l = new ArrayList<>();
  108.             for (BaseItemBean<?> base : list) {
  109.                 l.add(base.getItem());
  110.             }
  111.             return l;
  112.         }
  113.         return null;
  114.     }
  115.    
  116.     public Map<String, Properties> getPropertiesMap (){
  117.         Map<String, Properties> map = new HashMap<>();

  118.         List<BaseItemBean<?>> listaItem = this.getListaItem();

  119.         for (BaseItemBean<?> item : listaItem) { // Scorro la lista degli elementi
  120.             Property saveProperty = item.getSaveProperty();
  121.             String itemValue = item.getPropertyValue(); // valore della property

  122.             // un elemento e' salvabile se non e' visible o e' da forzare
  123.             boolean save =
  124.                     (saveProperty != null)
  125.                     &&
  126.                     (saveProperty.isForce()
  127.                             ||
  128.                             (
  129.                                 StringUtils.isNotEmpty(itemValue)
  130.                                     &&
  131.                                     (item.isVisible()
  132.                                             // in teoria gli hidden visibili dovrebbe essere salvabili
  133. //                                          &&
  134. //                                          !org.openspcoop2.core.mvc.properties.constants.ItemType.HIDDEN.equals(item.getItemType())
  135.                                     )
  136.                             )
  137.                     );
  138.            
  139. //          System.out.println("SAVE -> Item: Name ["+item.getName()+"] Value ["+itemValue+"] Force: ["+(saveProperty != null ? saveProperty.isForce() : false)+"] VisibleAND!hidden: ["+(item.isVisible() && !ItemType.HIDDEN.equals(item.getItemType()))+"] SAVE: ["+save+"]");  
  140.             if(save) { // per ogni elemento salvabile

  141.                 String propertyName = saveProperty.getName(); // nome della property di destinazione
  142.                 String propertiesName = saveProperty.getProperties() != null ? saveProperty.getProperties() : Costanti.NOME_MAPPA_PROPERTIES_DEFAULT; // nome delle properties di destinazione (vuoto quelle di default)

  143. //              System.out.println("SAVE -> Item: propertyName ["+propertyName+"] propertiesName ["+propertiesName+"]");                
  144.                 Properties p = null; // controllo esistenza properties selezionate
  145.                 if(map.containsKey(propertiesName)) {
  146.                     p = map.remove(propertiesName);
  147.                 } else {
  148.                     p = new Properties();
  149.                 }
  150.                 map.put(propertiesName, p);

  151.                 if(!saveProperty.isAppend()) { // se la property non e' di tipo append allora setto il valore
  152.                     p.setProperty(propertyName, itemValue);
  153.                 } else {
  154.                     String appendPropertyKey = Costanti.PRE_KEY_PROPERTIES_DEFAULT + propertyName;
  155.                     String appendKeyPropertyValue = null;

  156.                     // genero la chiave per decodificare le properties concatenate
  157.                     if(p.containsKey(appendPropertyKey)) {
  158.                         appendKeyPropertyValue = p.getProperty(appendPropertyKey);
  159.                         p.remove(appendPropertyKey);
  160.                         appendKeyPropertyValue += Costanti.KEY_PROPERTIES_DEFAULT_SEPARATOR;
  161.                         appendKeyPropertyValue += item.getName();
  162.                     } else {
  163.                         appendKeyPropertyValue = item.getName();
  164.                     }
  165.                     p.setProperty(appendPropertyKey, appendKeyPropertyValue);

  166.                     String apValue = null;
  167.                     if(p.containsKey(propertyName)) { // controllo se la property di tipo append e' gia presente aggiungo separatore e nuovo valore a quello gia' presente
  168.                         apValue = p.getProperty(propertyName);
  169.                         p.remove(propertyName);
  170.                         apValue += saveProperty.getAppendSeparator();
  171.                         apValue += itemValue;
  172.                     } else {
  173.                         apValue = itemValue;
  174.                     }
  175.                     p.setProperty(propertyName, apValue);
  176.                 }
  177.             }
  178.         }

  179.         return map;
  180.     }

  181.     public void setValueFromRequest(String name, String parameterValue, ExternalResources externalResources, LockUtilities lockUtilities) throws ProviderException {
  182.         this.getItem(name).setValueFromRequest(parameterValue, externalResources, lockUtilities);
  183. //      System.out.println("Item ["+name+"] Valore dalla request ["+parameterValue+"], Nuovo Valore ["+this.getItem(name).getValue()+"]");
  184.     }

  185.     public void updateConfigurazione(Config config) throws ConditionException {
  186.         List<BaseItemBean<?>> listaItem = this.getListaItem();

  187. //      System.out.println("Update Configurazione...");
  188.        
  189.         for (BaseItemBean<?> item : listaItem) {
  190.             boolean resolve = ConditionsEngine.resolve(item.getConditions(), this);
  191. //          System.out.println("Item ["+item.getName()+"] Valore ["+ item.getValue()+"] Visibile ["+resolve+"]");
  192.             item.setVisible(resolve);
  193.         }

  194. //      System.out.println("Update Configurazione completato, controllo sezioni da nascondere...");
  195.         // sistemo la visualizzazione delle sezioni e subsection che hanno tutti gli elementi hidden

  196.         ConditionsEngine.controllaSezioniDaNascondere(config, this);
  197.        
  198. //      for (BaseItemBean<?> item : listaItem) {
  199. //          System.out.println("Item ["+item.getName()+"] Valore ["+ item.getValue()+"] Visibile ["+item.isVisible()+"]");
  200. //      }
  201. //      
  202. //      System.out.println("Update Sezioni da nascondere completato .");
  203.     }

  204.     public void validazioneInputUtente(String nome, String descrizione, Config config, ExternalResources externalResources) throws UserInputValidationException, ClassNotFoundException, InstantiationException, IllegalAccessException,
  205.         IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ProviderException, ProviderValidationException {
  206.         List<BaseItemBean<?>> listaItem = this.getListaItem();

  207.         for (BaseItemBean<?> item : listaItem) {
  208.             item.validate(externalResources);
  209.         }
  210.        
  211.         IProvider provider = null;
  212.         if(StringUtils.isNotEmpty(config.getProvider())) {
  213.             try {
  214.                 provider = (IProvider) ClassLoaderUtilities.newInstance(config.getProvider());
  215.             }catch(Exception e) {
  216.                 throw new ProviderException("Errore durante l'istanziazione del provider ["+config.getProvider()+"]: "+e.getMessage(),e);
  217.             }
  218.             provider.validateId(nome);
  219.             provider.validateDescription(descrizione);
  220.             provider.validate(this.getPropertiesMap());
  221.         }
  222.     }

  223.     public List<String> getListaNomiProperties() {
  224.         return this.listaNomiProperties;
  225.     }

  226.     public Map<String, List<BaseItemBean<?>>> getMapPropertyItem() {
  227.         return this.mapPropertyItem;
  228.     }

  229.     public String getId() {
  230.         return this.id;
  231.     }

  232.     public void setId(String id) {
  233.         this.id = id;
  234.     }
  235. }