RadioButton.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.  * RadioButton
  31.  *
  32.  * @author Pintori Giuliano (pintori@link.it)
  33.  * @author $Author$
  34.  * @version $Rev$, $Date$
  35.  *
  36.  */
  37. public class RadioButton extends BaseComponent<String> {

  38.     public RadioButton(Parameter<String> parameter,IDynamicLoader loader) {
  39.         super(parameter,loader);
  40.     }
  41.        
  42.     @Override
  43.     public DataElement toDataElement() throws ParameterException {
  44.         DataElement de = new DataElement();
  45.         de.setName(this.getId());
  46.         de.setLabel(this.getRendering().getLabel());
  47.         de.setPostBack_viaPOST(this.getRefreshParamIds().size() > 0);
  48.         de.setRequired(this.getRendering().isRequired());
  49.         de.setNote(this.getRendering().getSuggestion());

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

  52.         List<String> valuesList = new ArrayList<>();
  53.         List<String> labelsList = new ArrayList<>();
  54.         List<String> values = this.getRendering().getValues();
  55.         List<String> labels = this.getRendering().getLabels();
  56.         if(values!=null && values.size()>0) {
  57.             for (int i = 0; i < values.size(); i++) {
  58.                 String itemValue = values.get(i);
  59.                 String itemLabel = itemValue;
  60.                 if(labels!=null && labels.size()==values.size()) {
  61.                     itemLabel = labels.get(i);
  62.                 }
  63.                 valuesList.add(itemValue);
  64.                 labelsList.add(itemLabel);
  65.             }
  66.         }
  67.         de.setValues(valuesList);
  68.         de.setLabels(labelsList);

  69.         return de;
  70.     }
  71.    
  72.     @Override
  73.     public void setValueFromRequest(String parameterValue) throws ParameterException {
  74.         if(parameterValue == null) {
  75.             if(StringUtils.isNotEmpty(this.getRendering().getDefaultValue())) {
  76.                 this.setValue(this.getRendering().getDefaultValue());
  77.             }
  78.             else {
  79.                 this.setValue(null);
  80.             }
  81.         }else {
  82.             this.setValue(parameterValue);
  83.         }
  84.     }
  85. }