SelectList.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 java.util.List;

  23. import org.apache.commons.lang.StringUtils;
  24. import org.openspcoop2.monitor.engine.dynamic.IDynamicLoader;
  25. import org.openspcoop2.monitor.sdk.exceptions.ParameterException;
  26. import org.openspcoop2.monitor.sdk.parameters.Parameter;
  27. import org.openspcoop2.web.lib.mvc.DataElement;
  28. import org.openspcoop2.web.lib.mvc.DataElementType;

  29. /**
  30.  * SelectList
  31.  *
  32.  * @author Pintori Giuliano (pintori@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  *
  36.  */
  37. public class SelectList extends BaseComponent<String> {

  38.     public SelectList(Parameter<String> parameter,IDynamicLoader loader) {
  39.         super(parameter,loader);
  40.     }

  41.     @Override
  42.     public DataElement toDataElement() throws ParameterException {
  43.         DataElement de = new DataElement();
  44.         de.setName(this.getId());
  45.         de.setLabel(this.getRendering().getLabel());
  46.         de.setPostBack_viaPOST(this.getRefreshParamIds().size() > 0);
  47.         de.setRequired(this.getRendering().isRequired());
  48.         de.setNote(this.getRendering().getSuggestion());

  49.         de.setSelected(this.getValue());
  50.         de.setType(DataElementType.SELECT);

  51.         List<String> valuesList = new ArrayList<>();
  52.         List<String> labelsList = new ArrayList<>();
  53.         List<String> values = this.getRendering().getValues();
  54.         List<String> labels = this.getRendering().getLabels();
  55.         if(values!=null && values.size()>0) {
  56.             for (int i = 0; i < values.size(); i++) {
  57.                 String itemValue = values.get(i);
  58.                 String itemLabel = itemValue;
  59.                 if(labels!=null && labels.size()==values.size()) {
  60.                     itemLabel = labels.get(i);
  61.                 }
  62.                 valuesList.add(itemValue);
  63.                 labelsList.add(itemLabel);
  64.             }
  65.         }
  66. //      else if(this.provider!=null){
  67. //          List<String> tmp = this.provider.getValues(this.name);
  68. //          if(tmp!=null && tmp.size()>0) {
  69. //              valuesList.addAll(tmp);
  70. //          }
  71. //          tmp = this.provider.getLabels(this.name);
  72. //          if(tmp!=null && tmp.size()>0) {
  73. //              labelsList.addAll(tmp);
  74. //          }
  75. //      }
  76.         de.setValues(valuesList);
  77.         de.setLabels(labelsList);

  78.         return de;
  79.     }
  80.    
  81.     @Override
  82.     public void setValueFromRequest(String parameterValue) throws ParameterException {
  83.         if(parameterValue == null) {
  84.             if(StringUtils.isNotEmpty(this.getRendering().getDefaultValue())) {
  85.                 this.setValue(this.getRendering().getDefaultValue());
  86.             }
  87.             else {
  88.                 this.setValue(null);
  89.             }
  90.         }else {
  91.             this.setValue(parameterValue);
  92.         }
  93.     }
  94. }