ItemBean.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.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;

  24. import org.apache.commons.lang.StringUtils;
  25. import org.apache.commons.lang3.math.NumberUtils;
  26. import org.openspcoop2.core.mvc.properties.Conditions;
  27. import org.openspcoop2.core.mvc.properties.Item;
  28. import org.openspcoop2.core.mvc.properties.ItemValue;
  29. import org.openspcoop2.core.mvc.properties.ItemValues;
  30. import org.openspcoop2.core.mvc.properties.Property;
  31. import org.openspcoop2.core.mvc.properties.constants.ItemType;
  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.ProviderInfo;
  36. import org.openspcoop2.utils.regexp.RegularExpressionEngine;
  37. import org.openspcoop2.web.lib.mvc.Costanti;
  38. import org.openspcoop2.web.lib.mvc.DataElement;
  39. import org.openspcoop2.web.lib.mvc.DataElementInfo;
  40. import org.openspcoop2.web.lib.mvc.DataElementType;
  41. import org.openspcoop2.web.lib.mvc.ServletUtils;
  42. import org.openspcoop2.web.lib.mvc.byok.LockUtilities;
  43. import org.openspcoop2.web.lib.mvc.properties.exception.UserInputValidationException;

  44. /**
  45.  * Bean di tipo Item arricchito delle informazioni grafiche.
  46.  *
  47.  * @author Pintori Giuliano (pintori@link.it)
  48.  * @author $Author$
  49.  * @version $Rev$, $Date$
  50.  *
  51.  */
  52. public class ItemBean extends BaseItemBean<Item>{

  53.     public ItemBean(Item item, String name, IProvider provider) {
  54.         super(item, name, provider);
  55.     }

  56.     @Override
  57.     public void init(String value, ExternalResources externalResources) throws ProviderException {
  58.         /**Property saveProperty = this.getSaveProperty();*/

  59.         // caso value == null e non devo forzare con il valore letto dal db cerco un default
  60.         if(value == null) {
  61.             /**if(value == null && !saveProperty.isForce()) {*/
  62.             switch(this.getItem().getType()) {
  63.             case CHECKBOX:
  64.                 this.value = this.getItem().getDefaultSelected() ? Costanti.CHECK_BOX_ENABLED : Costanti.CHECK_BOX_DISABLED;
  65.                 break;
  66.             case HIDDEN:
  67.             case LOCK_HIDDEN:
  68.                 this.value = this.getItem().getValue();
  69.                 break;
  70.             case NUMBER:
  71.             case SELECT:
  72.             case TEXT:
  73.             case TEXTAREA:
  74.             case LOCK:
  75.             default:
  76.                 if(StringUtils.isNotEmpty(this.getItem().getDefault())) {
  77.                     this.value = this.getItem().getDefault();
  78.                 }
  79.                 else if(this.provider!=null) {
  80.                     this.value = this.provider.getDefault(this.name, externalResources);
  81.                 }
  82.                 else {
  83.                     this.value = null;
  84.                 }
  85.                 /**System.out.println("init default ["+this.name+"] value '"+this.value+"'");*/
  86.                 break;
  87.             }
  88.         } else {
  89.             switch(this.getItem().getType()) {
  90.             case CHECKBOX:
  91.                 this.value = this.getCheckBoxValue(value);
  92.                 break;
  93.             case HIDDEN:
  94.             case NUMBER:
  95.             case SELECT:
  96.             case TEXT:
  97.             case TEXTAREA:
  98.             case LOCK:
  99.             case LOCK_HIDDEN:
  100.             default:
  101.                 this.value = value;
  102.                 /**System.out.println("init ["+this.name+"] value '"+this.value+"'");*/
  103.                 break;
  104.             }
  105.         }
  106.     }

  107.     @Override
  108.     public DataElement toDataElement(ConfigBean config, Map<String, String> mapNameValue, ExternalResources externalResources,
  109.             LockUtilities lockUtilities) throws ProviderException {

  110.         if(this.provider!=null){
  111.             this.value = this.provider.dynamicUpdate(config.getListaItemSDK(), mapNameValue, this.getItem(), this.value, externalResources);
  112.         }
  113.        
  114.         mapNameValue.put(this.name, this.value);
  115.        
  116.         DataElement de = new DataElement();
  117.         de.setName(this.getName());
  118.         de.setLabel(this.getItem().getLabel());
  119.         /**de.setPostBack(this.getItem().getReloadOnChange());*/
  120.         de.setPostBack_viaPOST(true); // per la cifratura
  121.         de.setRequired(this.getItem().isRequired());
  122.        
  123.         if(this.getItem().getNote()!=null && StringUtils.isNotEmpty(this.getItem().getNote())) {
  124.             de.setNote(this.getItem().getNote());
  125.         }
  126.         else if(this.provider!=null){
  127.             de.setNote(this.provider.getNote(this.name, this.value));
  128.         }

  129.         if(this.provider!=null){
  130.             addProviderInfo(de);
  131.         }
  132.        
  133.         switch(this.getItem().getType()) {
  134.         case CHECKBOX:
  135.             de.setSelected(this.value);
  136.             de.setType(DataElementType.CHECKBOX);
  137.             break;
  138.         case HIDDEN:
  139.             de.setValue(this.value);
  140.             de.setType(DataElementType.HIDDEN);
  141.             break;
  142.         case NUMBER:
  143.             de.setValue(this.value);
  144.             de.setType(DataElementType.NUMBER);
  145.             de.setMinValue(this.getItem().getMin());
  146.             de.setMaxValue(this.getItem().getMax());
  147.             break;
  148.         case SELECT:
  149.             processSelectElement(de, externalResources);
  150.             break;
  151.         case TEXT:
  152.             de.setValue(this.value);
  153.             de.setType(DataElementType.TEXT_EDIT);
  154.             break;
  155.         case TEXTAREA:
  156.             de.setValue(this.value);
  157.             de.setType(DataElementType.TEXT_AREA);
  158.             if(this.getItem().getMax()!=null && this.getItem().getMax().intValue()>0) {
  159.                 de.setRows(this.getItem().getMax().intValue());
  160.             }
  161.             else {
  162.                 de.setRows(3);
  163.             }
  164.             break;
  165.         case LOCK_HIDDEN:
  166.             /**System.out.println("DATAELEMENT LOCK_HIDDEN ["+this.name+"] value '"+this.value+"'");*/
  167.             try {
  168.                 lockUtilities.lockHidden(de, this.value);
  169.             }catch(Exception e) {
  170.                 throw new ProviderException(e.getMessage(),e);
  171.             }
  172.             break;
  173.         case LOCK:
  174.             /**System.out.println("DATAELEMENT LOCK ["+this.name+"] value '"+this.value+"'");*/
  175.             try {
  176.                 lockUtilities.lock(de, this.value);
  177.             }catch(Exception e) {
  178.                 throw new ProviderException(e.getMessage(),e);
  179.             }
  180.             break;
  181.         default:
  182.             break;
  183.         }
  184.        
  185.         return de;
  186.     }
  187.     private void addProviderInfo(DataElement de) throws ProviderException {
  188.         ProviderInfo pInfo = this.provider.getProviderInfo(this.name);
  189.         if(pInfo!=null) {
  190.             DataElementInfo dInfo = new DataElementInfo(pInfo.getHeaderFinestraModale()!=null ? pInfo.getHeaderFinestraModale() : this.getItem().getLabel());
  191.             dInfo.setBody(pInfo.getBody());
  192.             dInfo.setHeaderBody(pInfo.getHeaderBody());
  193.             dInfo.setListBody(pInfo.getListBody());
  194.             de.setInfo(dInfo);
  195.         }
  196.     }
  197.     private void processSelectElement(DataElement de, ExternalResources externalResources) throws ProviderException {
  198.         de.setSelected(this.value);
  199.         de.setType(DataElementType.SELECT);

  200.         List<String> valuesList = new ArrayList<>();
  201.         List<String> labelsList = new ArrayList<>();
  202.         ItemValues values = this.getItem().getValues();
  203.         if(values!=null && values.sizeValueList()>0) {
  204.             for (ItemValue itemValue : values.getValueList()) {
  205.                 valuesList.add(itemValue.getValue());
  206.                 labelsList.add(itemValue.getLabel() != null ? itemValue.getLabel() : itemValue.getValue());
  207.             }
  208.         }
  209.         else if(this.provider!=null){
  210.             List<String> tmp = this.provider.getValues(this.name, externalResources);
  211.             if(tmp!=null && !tmp.isEmpty()) {
  212.                 valuesList.addAll(tmp);
  213.             }
  214.             tmp = this.provider.getLabels(this.name, externalResources);
  215.             if(tmp!=null && !tmp.isEmpty()) {
  216.                 labelsList.addAll(tmp);
  217.             }
  218.         }
  219.         de.setValues(valuesList);
  220.         de.setLabels(labelsList);
  221.     }

  222.     @Override
  223.     public void setValueFromRequest(String parameterValue, ExternalResources externalResources, LockUtilities lockUtilities) throws ProviderException {
  224.         if(parameterValue == null && !this.isOldVisible()) {
  225.             setDefaultValueFromRequest(externalResources);
  226.         }
  227.         else {
  228.             switch(this.getItem().getType()) {
  229.             case HIDDEN:
  230.             case LOCK_HIDDEN:
  231.                 this.value = (parameterValue == null && this.getSaveProperty().isForce()) ? this.getItem().getValue() : parameterValue;
  232.                 break;
  233.             case CHECKBOX:
  234.             case NUMBER:
  235.             case SELECT:
  236.             case TEXT:
  237.             case TEXTAREA:
  238.             case LOCK:
  239.             default:
  240.                 /**System.out.println("setValueFromRequest ["+this.name+"] value '"+parameterValue+"'");*/
  241.                 this.value = parameterValue;
  242.                 break;
  243.             }
  244.         }
  245.        
  246.         if(ItemType.LOCK.equals(this.getItem().getType()) ||
  247.                 ItemType.LOCK_HIDDEN.equals(this.getItem().getType())) {
  248.             try {
  249.                 this.value = lockUtilities.getDriverBYOKUtilities().wrap(this.value);
  250.             }catch(Exception e) {
  251.                 throw new ProviderException(e.getMessage(),e);
  252.             }
  253.         }
  254.        
  255.         /**System.out.println("ITEM: ["+this.getName()+"] REQVALUE ["+parameterValue+"] NEW VALUE["+this.getValue()+"]");*/
  256.     }
  257.     private void setDefaultValueFromRequest(ExternalResources externalResources) throws ProviderException {
  258.         switch(this.getItem().getType()) {
  259.         case CHECKBOX:
  260.             this.value = this.getItem().getDefaultSelected() ? Costanti.CHECK_BOX_ENABLED : Costanti.CHECK_BOX_DISABLED;
  261.             break;
  262.         case HIDDEN:
  263.         case LOCK_HIDDEN:
  264.             this.value = this.getItem().getValue();
  265.             break;
  266.         case NUMBER:
  267.         case SELECT:
  268.         case TEXT:
  269.         case TEXTAREA:
  270.         case LOCK:
  271.         default:
  272.             if(StringUtils.isNotEmpty(this.getItem().getDefault())) {
  273.                 this.value = this.getItem().getDefault();
  274.             }
  275.             else if(this.provider!=null) {
  276.                 this.value = this.provider.getDefault(this.name, externalResources);
  277.             }
  278.             else {
  279.                 this.value = null;
  280.             }
  281.             /**System.out.println("setDefaultValueFromRequest ["+this.name+"] value '"+this.value+"'");*/
  282.             break;
  283.         }
  284.     }

  285.     @Override
  286.     public Property getSaveProperty() {
  287.         return this.getItem().getProperty();
  288.     }

  289.     @Override
  290.     public String getPropertyValue() {
  291.         switch (this.getItem().getType()) {
  292.         case CHECKBOX:
  293.             return getCheckboxPropertyValue();
  294.         case HIDDEN:
  295.         case LOCK_HIDDEN:
  296.             return this.getSaveProperty().isForce() ? this.getItem().getValue() : this.value;
  297.         case NUMBER:
  298.         case SELECT:
  299.         case TEXT:
  300.         case TEXTAREA:
  301.         case LOCK:
  302.         default:
  303.             /**if(ItemType.LOCK.equals(this.getItem().getType())) {
  304.                 System.out.println("getPropertyValue ["+this.name+"] value '"+this.value+"'");
  305.             }*/
  306.             return this.value;
  307.         }
  308.     }
  309.     private String getCheckboxPropertyValue() {
  310.         String valueToCheck = null;
  311.         if(ServletUtils.isCheckBoxEnabled(this.value)) {
  312.             valueToCheck = this.getSaveProperty().getSelectedValue() != null ? this.getSaveProperty().getSelectedValue() : null;
  313.         } else {
  314.             valueToCheck = this.getSaveProperty().getUnselectedValue() != null ? this.getSaveProperty().getUnselectedValue() :null;
  315.         }
  316.        
  317.         if(valueToCheck == null) {
  318.             valueToCheck = ServletUtils.isCheckBoxEnabled(this.value) ? "true" : "false";
  319.         }
  320.        
  321.         return valueToCheck;
  322.     }

  323.     public String getCheckBoxValue(String value) {
  324.         String valueToCheck = null;

  325.         if(this.getItem().getProperty().getSelectedValue() != null &&
  326.             value.equals(this.getSaveProperty().getSelectedValue())) {
  327.             valueToCheck = Costanti.CHECK_BOX_ENABLED;
  328.         }
  329.         if(valueToCheck == null &&
  330.             this.getItem().getProperty().getUnselectedValue() != null &&
  331.             value.equals(this.getSaveProperty().getUnselectedValue())) {
  332.             valueToCheck = Costanti.CHECK_BOX_DISABLED;
  333.         }

  334.         if(valueToCheck == null){
  335.             valueToCheck = ServletUtils.isCheckBoxEnabled(value) ? Costanti.CHECK_BOX_ENABLED : Costanti.CHECK_BOX_DISABLED;
  336.         }

  337. /**     if(valueToCheck == null){
  338. //          valueToCheck = this.getItem().getDefaultSelected() ? Costanti.CHECK_BOX_ENABLED : Costanti.CHECK_BOX_DISABLED;
  339. //      }*/

  340.         return valueToCheck;
  341.     }

  342.     @Override
  343.     public Conditions getConditions() {
  344.         return this.item.getConditions();
  345.     }

  346.     @Override
  347.     public ItemType getItemType() {
  348.         return this.item.getType();
  349.     }

  350.     @Override
  351.     public String getLabel() {
  352.         return this.item.getLabel();
  353.     }
  354.    
  355.     @Override
  356.     public void validate(ExternalResources externalResources) throws UserInputValidationException {
  357.         String itemValue = this.getPropertyValue(); // valore della property
  358.         Property saveProperty = this.getSaveProperty();

  359.         /**System.out.println("VALIDATE -> Item: Name ["+this.getName()+"] Value ["+itemValue+"]...");*/  

  360.         // un elemento e' salvabile se non e' visible o e' da forzare
  361.         boolean save =
  362.                 (saveProperty != null)
  363.                 &&
  364.                 (saveProperty.isForce()
  365.                         ||
  366.                         (
  367.                             this.isVisible()
  368.                             // in teoria gli hidden visibili dovrebbe essere salvabili
  369.                             /**&&
  370.                             !org.openspcoop2.core.mvc.properties.constants.ItemType.HIDDEN.equals(this.getItemType())*/
  371.                         )
  372.                 );
  373.        

  374.         /**System.out.println("VALIDATE -> Item: Name ["+this.getName()+"] Value ["+itemValue+"] Validazione Abilitata ["+save+"]");*/  

  375.         // validazione solo per gli elementi da salvare
  376.         if(save) {

  377.             String prefixIlCampo = "Il Campo "+this.getLabel();
  378.            
  379.             // 1. Validazione campi obbligatori
  380.             if(this.getItem().isRequired() && StringUtils.isEmpty(itemValue)) {
  381.                 throw new UserInputValidationException(prefixIlCampo+" &egrave; obbligatorio");
  382.             }

  383.             // 2. validazione generica basata sul tipo
  384.             switch(this.getItem().getType()) {
  385.             case NUMBER:
  386.                 if(StringUtils.isNotEmpty(itemValue)) {
  387.                     boolean numeric = NumberUtils.isParsable(itemValue);
  388.                     if(!numeric) {
  389.                         throw new UserInputValidationException(prefixIlCampo+" non contiene un valore di tipo numerico");
  390.                     }
  391.                     int number = -1;
  392.                     try {
  393.                         number = Integer.valueOf(itemValue);
  394.                     }catch(Exception e) {
  395.                         throw new UserInputValidationException(prefixIlCampo+" non contiene un valore di tipo numerico");
  396.                     }
  397.                     if(this.getItem().getMin()!=null &&
  398.                         number<this.getItem().getMin().intValue()) {
  399.                         throw new UserInputValidationException(prefixIlCampo+" deve contenere un valore >= "+this.getItem().getMin().intValue());
  400.                     }
  401.                     if(this.getItem().getMax()!=null &&
  402.                         number>this.getItem().getMax().intValue()) {
  403.                         throw new UserInputValidationException(prefixIlCampo+" deve contenere un valore <= "+this.getItem().getMax().intValue());
  404.                     }
  405.                 }
  406.                 break;
  407.             case SELECT:
  408.                 if(StringUtils.isNotEmpty(itemValue)) {
  409.                     ItemValues values = this.getItem().getValues();
  410.                     boolean found = false;
  411.                     if(values!=null && values.sizeValueList()>0) {
  412.                         for (ItemValue selectItemValue : values.getValueList()) {
  413.                             if(selectItemValue.getValue().equals(itemValue)) {
  414.                                 found = true;
  415.                                 break;
  416.                             }
  417.                         }
  418.                     }
  419.                     else {
  420.                         try {
  421.                             List<String> tmp = this.provider.getValues(this.name, externalResources);
  422.                             if(tmp.contains(itemValue)) {
  423.                                 found = true;
  424.                             }
  425.                         }catch(Exception e) {
  426.                             throw new UserInputValidationException("Errore durante la validazione del Campo "+this.getLabel()+": "+e.getMessage(),e);
  427.                         }
  428.                     }

  429.                     if(!found)
  430.                         throw new UserInputValidationException(prefixIlCampo+" contiene un valore non previsto");
  431.                 }
  432.                 break;
  433.             case TEXT:
  434.             case TEXTAREA:
  435.                 if(itemValue!=null && itemValue.length()>4000) {
  436.                     throw new UserInputValidationException(prefixIlCampo+" non deve contenere più di 4000 caratteri");
  437.                 }
  438.                 if(itemValue!=null && (itemValue.startsWith(" ") || itemValue.startsWith("\t"))) {
  439.                     throw new UserInputValidationException("Il valore inserito nel Campo "+this.getLabel()+" non può iniziare con uno spazio");
  440.                 }
  441.                 if(itemValue!=null && (itemValue.endsWith(" ") || itemValue.endsWith("\t"))) {
  442.                     throw new UserInputValidationException("Il valore inserito nel Campo "+this.getLabel()+" non può terminare con uno spazio");
  443.                 }
  444.                 break;
  445.             case LOCK:
  446.             case LOCK_HIDDEN:
  447.             case CHECKBOX:
  448.             case HIDDEN:
  449.                 break;
  450.             }

  451.             // 3. validazione basata sul pattern
  452.             if(this.getItem().getValidation() != null && StringUtils.isNotEmpty(itemValue)) {
  453.                 try {
  454.                     boolean match = RegularExpressionEngine.isMatch(itemValue, this.getItem().getValidation());

  455.                     if(!match)
  456.                         throw new UserInputValidationException(prefixIlCampo+" non rispetta il pattern di validazione previsto ("+this.getItem().getValidation()+")");

  457.                 }catch(UserInputValidationException e) {
  458.                     throw e;
  459.                 }catch(Exception e) {
  460.                     throw new UserInputValidationException("Impossibile validare il campo "+this.getLabel()+" secondo il pattern previsto nella configurazione ("+this.getItem().getValidation()+")",e);
  461.                 }
  462.             }
  463.         }
  464.     }
  465. }