AbstractConsoleItem.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.protocol.sdk.properties;

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

  23. import org.openspcoop2.protocol.sdk.ProtocolException;
  24. import org.openspcoop2.protocol.sdk.constants.ConsoleItemType;
  25. import org.openspcoop2.utils.SortedMap;
  26. import org.openspcoop2.utils.UtilsRuntimeException;

  27. /**
  28.  * AbstractConsoleItem
  29.  *
  30.  * @author Poli Andrea (apoli@link.it)
  31.  * @author $Author$
  32.  * @version $Rev$, $Date$
  33.  */
  34. public abstract class AbstractConsoleItem<T> extends BaseConsoleItem {

  35.     private T defaultValue;
  36.     private T defaultValueForCloseableSection; // il default value viene modificato, questo rimane impostato al valore iniziale
  37.     private boolean reloadOnChange;
  38.     private boolean reloadOnHttpPost;
  39.     private boolean required;
  40.     private String regexpr;
  41.     private SortedMap<T> mapLabelValues;
  42.     /**private TreeMap<String,T> mapLabelValues;*/
  43.     private String note;
  44.     private ConsoleItemInfo info;
  45.     private String labelRight;

  46.     protected AbstractConsoleItem(String id, String label, ConsoleItemType type) throws ProtocolException{
  47.         super(id, label, type);
  48.         /**this.mapLabelValues = new TreeMap<String,T>();*/
  49.         this.mapLabelValues = new SortedMap<>();
  50.     }

  51.     public T getDefaultValue() {
  52.         return this.defaultValue;
  53.     }
  54.     public void setDefaultValue(T defaultValue) {
  55.         this.defaultValue = defaultValue;
  56.     }

  57.     public boolean isReloadOnChange() {
  58.         return this.reloadOnChange;
  59.     }
  60.     public boolean isReloadOnHttpPost() {
  61.         return this.reloadOnHttpPost;
  62.     }
  63.     public void setReloadOnChange(boolean reloadOnChange) {
  64.         this.setReloadOnChange(reloadOnChange, false);
  65.     }
  66.     public void setReloadOnChange(boolean reloadOnChange, boolean reloadOnHttpPost) {
  67.         this.reloadOnChange = reloadOnChange;
  68.         this.reloadOnHttpPost = reloadOnHttpPost;
  69.     }

  70.     public boolean isRequired() {
  71.         return this.required;
  72.     }
  73.     public void setRequired(boolean required) {
  74.         this.required = required;
  75.     }

  76.     public String getRegexpr() {
  77.         return this.regexpr;
  78.     }
  79.     public void setRegexpr(String regexpr) {
  80.         this.regexpr = regexpr;
  81.     }
  82.    
  83.     public SortedMap<T> getMapLabelValues() {
  84.         return this.mapLabelValues;
  85.     }
  86.     public List<String> getLabels(){
  87.         List<String> labels = null;
  88.         if(this.mapLabelValues!=null && this.mapLabelValues.size()>0){  
  89.             labels = new ArrayList<>();
  90.             labels.addAll(this.mapLabelValues.keys());
  91.             return labels;
  92.         }
  93.         return labels;
  94.     }
  95.     public List<T> getValues(){
  96.         List<T> values = null;
  97.         if(this.mapLabelValues!=null && this.mapLabelValues.size()>0){  
  98.             values = new ArrayList<>();
  99.             values.addAll(this.mapLabelValues.values());
  100.             return values;
  101.         }
  102.         return values;
  103.     }
  104.     public void clearMapLabelValues(){
  105.         this.mapLabelValues.clear();
  106.     }
  107.     public void addLabelValue(String key, T value) {
  108.         try {
  109.             if(this.mapLabelValues.containsKey(key)) {
  110.                 this.mapLabelValues.remove(key);
  111.             }
  112.             this.mapLabelValues.put(key, value);
  113.         }catch(Exception e) {
  114.             throw new UtilsRuntimeException(e.getMessage(),e);
  115.         }
  116.     }
  117.    
  118.     public void removeLabelValue(String label){
  119.         this.mapLabelValues.remove(label);
  120.     }
  121.    
  122.     public String getNote() {
  123.         return this.note;
  124.     }
  125.     public void setNote(String note) {
  126.         this.note = note;
  127.     }
  128.    
  129.     public ConsoleItemInfo getInfo() {
  130.         return this.info;
  131.     }
  132.     public void setInfo(ConsoleItemInfo info) {
  133.         this.info = info;
  134.     }

  135.     public String getLabelRight() {
  136.         return this.labelRight;
  137.     }
  138.     public void setLabelRight(String labelRight) {
  139.         this.labelRight = labelRight;
  140.     }
  141.    
  142.     public void setUseDefaultValueForCloseableSection(boolean useDefaultValueForCloseableSection) throws ProtocolException {
  143.         if(this.defaultValue==null) {
  144.             throw new ProtocolException("Default value undefined (useDefaultValue:"+useDefaultValueForCloseableSection+")");
  145.         }
  146.         this.defaultValueForCloseableSection = cloneValue(this.defaultValue);
  147.     }
  148.    
  149.     protected abstract T cloneValue(T value) throws ProtocolException;
  150.    
  151.     public T getDefaultValueForCloseableSection() {
  152.         return this.defaultValueForCloseableSection;
  153.     }
  154. }