BaseComponent.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.dynamic.components;

  21. import java.util.ArrayList;

  22. import javax.faces.event.ActionEvent;

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.monitor.engine.dynamic.IDynamicLoader;
  25. import org.openspcoop2.monitor.sdk.condition.Context;
  26. import org.openspcoop2.monitor.sdk.constants.Constants;
  27. import org.openspcoop2.monitor.sdk.exceptions.ParameterException;
  28. import org.openspcoop2.monitor.sdk.exceptions.SearchException;
  29. import org.openspcoop2.monitor.sdk.parameters.Parameter;
  30. import org.openspcoop2.utils.LoggerWrapperFactory;
  31. import org.openspcoop2.web.lib.mvc.DataElement;
  32. import org.slf4j.Logger;

  33. /**
  34.  * BaseComponent
  35.  *
  36.  * @author Pintori Giuliano (pintori@link.it)
  37.  * @author $Author$
  38.  * @version $Rev$, $Date$
  39.  *
  40.  */
  41. public abstract class BaseComponent<T> extends Parameter<T>{

  42.     protected static final String CONTANER_SUFFIX = "-ctr";
  43.    
  44.     private static Logger log = LoggerWrapperFactory.getLogger(BaseComponent.class);
  45.    
  46.     private Context context;
  47.     private IDynamicLoader loader;
  48.    
  49.     private Parameter<T> sdkParameter;
  50.    
  51.     public BaseComponent(Parameter<T> parameter, IDynamicLoader loader) {
  52.         super(parameter);
  53.         this.sdkParameter = parameter; // L'SDK Parameter viene utilizzato per implementare i metodi astratti setValueAsString e getValueAsString
  54.         this.loader = loader;
  55.     }  

  56.     public Context getContext() {
  57.         return this.context;
  58.     }

  59.     public void setContext(Context context) {
  60.         this.context = context;
  61.     }

  62.     @Override
  63.     public void setValueAsString(String value) throws ParameterException{
  64.         this.sdkParameter.setValueAsString(value);
  65.         this.setValue(this.sdkParameter.getValue());
  66.     }
  67.    
  68.     @Override
  69.     public String getValueAsString() throws ParameterException{
  70.         return this.sdkParameter.getValueAsString();
  71.     }
  72.    
  73.     private boolean initialized = false;
  74.    
  75.     @Override
  76.     public T getValue(){
  77.         if(super.getValue()==null){
  78.             if(this.initialized){
  79.                 return null;
  80.             }
  81.             else{
  82.                 this.initialized = true;
  83.                 T defaultValue = this.getRendering().getDefaultValue();
  84.                 this.setValue(defaultValue);
  85.                 return defaultValue;
  86.             }
  87.         }
  88.         else{
  89.             return super.getValue();
  90.         }
  91.     }
  92.    
  93.     @Override
  94.     public void setValue(T val){
  95.         if( (val instanceof String) &&
  96.             (("--".equals(val)) || "".equals(val))
  97.           ){        
  98.             val = null;
  99.         }
  100.         super.setValue(val);
  101.         this.sdkParameter.setValue(val);
  102.     }
  103.    
  104.     /**
  105.      * Indica se il componente deve essere renderizzato o meno.
  106.      *
  107.      * Se e' presente un {@link IDynamicLoader} allora viene utilizzato per decidere se renderizzare il componente
  108.      * altrimenti viene sempre renderizzato.
  109.      * @return true se il componente deve essere renderizzato false altrimenti.
  110.      */
  111.     public boolean getRendered() {
  112.         if(this.getLoader()==null)
  113.             return true;
  114.         try{
  115.             //r potrebbe essere null qualora il loader non trovasse il componente tramite l'id fornito
  116.             this.getLoader().updateRendering(this, this.context);
  117.             return this.getRendering().isHidden()==false;
  118.         }catch (Exception e) {
  119.             BaseComponent.log.error("Impossibile recuperare le informazioni di rendering dal Loader: "+e.getMessage());
  120.             return true;
  121.         }
  122.     }
  123.    
  124.     public IDynamicLoader getLoader() {
  125.         return this.loader;
  126.     }

  127.    
  128.     public void setLoader(IDynamicLoader loader) {
  129.         this.loader = loader;
  130.     }
  131.    
  132.     public String getContainerId() {
  133.         String cid =  this.buildContainerId(this.getId());
  134.         return cid;
  135.     }
  136.    
  137.     public String getContainersIdToRefresh(){
  138.         if(this.getRefreshParamIds()!=null){
  139.             ArrayList<String> res = new ArrayList<>();
  140.             for (String pid : this.getRefreshParamIds()) {
  141.                  res.add(this.buildContainerId(pid));
  142.             }
  143.             return StringUtils.join(res, ",");      
  144.         }else{
  145.             return this.buildContainerId(this.getId());
  146.         }
  147.     }
  148.    
  149.     public void valueSelectedListener(ActionEvent ae){
  150.         this.getLoader().valueSelectedListener(this, this.getContext());
  151.     }
  152.    
  153.     public void valueSelectedListener(){
  154.         this.getLoader().valueSelectedListener(this, this.getContext());
  155.     }
  156.    
  157.     public void updateRendering() throws SearchException{
  158.         this.getLoader().updateRendering(this, this.getContext());
  159.     }
  160.    
  161.     private String buildContainerId(String id) {
  162.         if(id!=null){
  163.             String tmp = id.trim();
  164.             if(tmp.endsWith(Constants.CONTANER_SUFFIX_NO_PLUGIN)){
  165.                 return tmp.substring(0, (tmp.length()-Constants.CONTANER_SUFFIX_NO_PLUGIN.length()));
  166.             }
  167.             else{
  168.                 return tmp + CONTANER_SUFFIX;
  169.             }
  170.         }
  171.         return null;
  172.     }
  173.    
  174.     public abstract DataElement toDataElement() throws ParameterException ;
  175.    
  176.     public abstract void setValueFromRequest(String parameterValue) throws ParameterException;
  177. }