ProtocolPropertiesUtilities.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.ctrlstat.servlet.protocol_properties;

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

  24. import org.apache.commons.lang.StringEscapeUtils;
  25. import org.openspcoop2.core.constants.ProprietariProtocolProperty;
  26. import org.openspcoop2.core.registry.ProtocolProperty;
  27. import org.openspcoop2.protocol.sdk.ProtocolException;
  28. import org.openspcoop2.protocol.sdk.constants.ConsoleInterfaceType;
  29. import org.openspcoop2.protocol.sdk.constants.ConsoleItemType;
  30. import org.openspcoop2.protocol.sdk.constants.ConsoleItemValueType;
  31. import org.openspcoop2.protocol.sdk.constants.ConsoleOperationType;
  32. import org.openspcoop2.protocol.sdk.properties.AbstractConsoleItem;
  33. import org.openspcoop2.protocol.sdk.properties.BaseConsoleItem;
  34. import org.openspcoop2.protocol.sdk.properties.BinaryConsoleItem;
  35. import org.openspcoop2.protocol.sdk.properties.BooleanConsoleItem;
  36. import org.openspcoop2.protocol.sdk.properties.NumberConsoleItem;
  37. import org.openspcoop2.protocol.sdk.properties.ProtocolPropertiesUtils;
  38. import org.openspcoop2.protocol.sdk.properties.StringConsoleItem;
  39. import org.openspcoop2.protocol.sdk.properties.SubtitleConsoleItem;
  40. import org.openspcoop2.protocol.sdk.properties.TitleConsoleItem;
  41. import org.openspcoop2.utils.SortedMap;
  42. import org.openspcoop2.web.ctrlstat.servlet.ConsoleHelper;
  43. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneUtilities;
  44. import org.openspcoop2.web.lib.mvc.BinaryParameter;
  45. import org.openspcoop2.web.lib.mvc.DataElement;
  46. import org.openspcoop2.web.lib.mvc.DataElement.STATO_APERTURA_SEZIONI;
  47. import org.openspcoop2.web.lib.mvc.DataElementInfo;
  48. import org.openspcoop2.web.lib.mvc.DataElementType;
  49. import org.openspcoop2.web.lib.mvc.Parameter;


  50. /**
  51.  * ProtocolPropertiesUtilities
  52.  *
  53.  * @author Pintori Giuliano (pintori@link.it)
  54.  * @author $Author$
  55.  * @version $Rev$, $Date$
  56.  */
  57. public class ProtocolPropertiesUtilities {
  58.    
  59.     private ProtocolPropertiesUtilities() {}


  60.     public static ConsoleInterfaceType getTipoInterfaccia(ConsoleHelper consoleHelper){
  61.         if(consoleHelper.isModalitaStandard()) {
  62.             return ConsoleInterfaceType.STANDARD;
  63.         }
  64.         if(consoleHelper.isModalitaCompleta()) {
  65.             return ConsoleInterfaceType.COMPLETA;
  66.         }
  67.         return ConsoleInterfaceType.AVANZATA;
  68.     }

  69.     private static String getPrefixErrorItemConClasse(AbstractConsoleItem<?> abItem) {
  70.         return "Item con classe ["+abItem.getClass()+"] ";
  71.     }
  72.     private static String getPrefixErrorItemConClasse(BaseConsoleItem item) {
  73.         return "Item con classe ["+item.getClass()+"] ";
  74.     }
  75.    
  76.    
  77.     public static List<DataElement> itemToDataElement(List<DataElement> dati ,
  78.             ConsoleHelper consoleHelper,
  79.             BaseConsoleItem item, Object defaultItemValue,
  80.             ConsoleOperationType consoleOperationType,
  81.             Properties binaryChangeProperties, ProtocolProperty protocolProperty, int size) throws ProtocolException {
  82.         return itemToDataElementEngine(dati, consoleHelper,
  83.                 item, defaultItemValue,
  84.                 consoleOperationType,
  85.                 binaryChangeProperties,
  86.                 protocolProperty!=null ? protocolProperty.getId() : null,
  87.                 protocolProperty!=null ? protocolProperty.getFile() : null,
  88.                 size,
  89.                 protocolProperty!=null && (protocolProperty.getByteFile()!=null));
  90.     }
  91.     public static List<DataElement> itemToDataElement(List<DataElement> dati ,
  92.             ConsoleHelper consoleHelper,
  93.             BaseConsoleItem item, Object defaultItemValue,
  94.             ConsoleOperationType consoleOperationType,
  95.             Properties binaryChangeProperties, org.openspcoop2.core.config.ProtocolProperty protocolProperty, int size) throws ProtocolException {
  96.         return itemToDataElementEngine(dati, consoleHelper,
  97.                 item, defaultItemValue,
  98.                 consoleOperationType,
  99.                 binaryChangeProperties,
  100.                 protocolProperty!=null ? protocolProperty.getId() : null,
  101.                 protocolProperty!=null ? protocolProperty.getFile() : null,
  102.                 size,
  103.                 protocolProperty!=null && (protocolProperty.getByteFile()!=null));
  104.     }
  105.     private static List<DataElement> itemToDataElementEngine(List<DataElement> dati ,
  106.             ConsoleHelper consoleHelper,
  107.             BaseConsoleItem item, Object defaultItemValue,
  108.             ConsoleOperationType consoleOperationType,
  109.             Properties binaryChangeProperties,
  110.             Long protocolPropertyId, String protocolPropertyFile, int size,
  111.             boolean contentSaved) throws ProtocolException {
  112.         if(item == null)
  113.             return dati;

  114.         // tipi con valore
  115.         if(item instanceof AbstractConsoleItem<?>){
  116.             AbstractConsoleItem<?> abItem = (AbstractConsoleItem<?>) item;
  117.             switch (abItem.getType()) {
  118.             case CHECKBOX:
  119.                 getCheckbox(dati,abItem, defaultItemValue, size);
  120.                 break;
  121.             case LOCK:
  122.                 getText(dati, consoleHelper,abItem, size, DataElementType.LOCK);
  123.                 break;
  124.             case LOCK_HIDDEN:
  125.                 getHidden(dati, consoleHelper, abItem, size);
  126.                 break;
  127.             case CRYPT:
  128.                 getText(dati, consoleHelper,abItem, size, DataElementType.CRYPT);
  129.                 break;
  130.             case FILE:
  131.                 getFile(dati, consoleHelper, abItem, size, consoleOperationType,binaryChangeProperties, protocolPropertyId, protocolPropertyFile, contentSaved);
  132.                 break;
  133.             case HIDDEN:
  134.                 getHidden(dati, consoleHelper, abItem, size);
  135.                 break;
  136.             case SELECT:
  137.                 getSelect(dati,abItem, defaultItemValue, size);
  138.                 break;
  139.             case MULTI_SELECT:
  140.                 getMultiSelect(dati,abItem, defaultItemValue, size);
  141.                 break;
  142.             case TEXT:
  143.                 getText(dati, consoleHelper,abItem, size, DataElementType.TEXT);
  144.                 break;
  145.             case TEXT_AREA:
  146.                 getText(dati, consoleHelper,abItem, size, DataElementType.TEXT_AREA);
  147.                 break;
  148.             case TEXT_AREA_NO_EDIT:
  149.                 getText(dati, consoleHelper,abItem, size, DataElementType.TEXT_AREA_NO_EDIT);
  150.                 break;
  151.             case TEXT_EDIT:
  152.                 getText(dati, consoleHelper,abItem, size, DataElementType.TEXT_EDIT);
  153.                 break;
  154.             case TAGS:
  155.                 getText(dati, consoleHelper,abItem, size, DataElementType.TEXT_EDIT);
  156.                 dati.get(dati.size()-1).enableTags();
  157.                 break;
  158.             case NUMBER:
  159.                 getText(dati, consoleHelper,abItem, size, DataElementType.NUMBER);
  160.                 break;
  161.             default:
  162.                 throw new ProtocolException(getPrefixErrorItemConClasse(abItem)+"identificato come tipo AbstractConsoleItem ma con Type: ["+abItem.getType()+"] di tipo titolo o note");
  163.             }
  164.         } else {
  165.             // titoli e note
  166.             switch (item.getType()) {
  167.             case NOTE:
  168.                 getTitle(dati,item, size,DataElementType.NOTE);
  169.                 break;
  170.             case SUBTITLE:
  171.                 getTitle(dati,item, size,DataElementType.SUBTITLE);
  172.                 break;
  173.             case TITLE:
  174.                 getTitle(dati,item, size,DataElementType.TITLE);
  175.                 break;
  176.             case HIDDEN:
  177.                 getHidden(dati, consoleHelper, item, size);
  178.                 break;
  179.             default:
  180.                 throw new ProtocolException(getPrefixErrorItemConClasse(item)+"non identificato come tipo AbstractConsoleItem ma con Type: ["+item.getType()+"] non di tipo titolo o note");
  181.             }
  182.         }

  183.         return dati;
  184.     }
  185.    
  186.     public static List<DataElement> itemToDataElementAsHidden(List<DataElement> dati ,ConsoleHelper consoleHelper, BaseConsoleItem item, Object defaultItemValue,
  187.             ConsoleOperationType consoleOperationType,
  188.             Properties binaryChangeProperties, ProtocolProperty protocolProperty, int size) throws ProtocolException {
  189.        
  190.         if(defaultItemValue!=null || consoleOperationType!=null || binaryChangeProperties!=null || protocolProperty!=null) {
  191.             // nop
  192.         }
  193.        
  194.         if(item == null)
  195.             return dati;

  196.         // tipi con valore
  197.         if(item instanceof AbstractConsoleItem<?>){
  198.             AbstractConsoleItem<?> abItem = (AbstractConsoleItem<?>) item;
  199.             switch (abItem.getType()) {
  200.             case CHECKBOX:
  201.             case LOCK:
  202.             case LOCK_HIDDEN:
  203.             case CRYPT:
  204.             case FILE:
  205.             case HIDDEN:
  206.             case SELECT:
  207.             case MULTI_SELECT:
  208.             case TEXT:
  209.             case TEXT_AREA:
  210.             case TEXT_AREA_NO_EDIT:
  211.             case TEXT_EDIT:
  212.             case TAGS:
  213.             case NUMBER:
  214.                 getHidden(dati, consoleHelper, abItem, size);
  215.                 break;
  216.             default:
  217.                 throw new ProtocolException(getPrefixErrorItemConClasse(abItem)+"identificato come tipo AbstractConsoleItem ma con Type: ["+abItem.getType()+"] di tipo titolo o note");
  218.             }
  219.         } else {
  220.             // titoli e note non vengono aggiunti lascio il controllo dei tipi per sicurezza
  221.             switch (item.getType()) {
  222.             case NOTE:
  223.             case SUBTITLE:
  224.             case TITLE:
  225.             case HIDDEN:
  226.                 break;
  227.             default:
  228.                 throw new ProtocolException(getPrefixErrorItemConClasse(item)+"non identificato come tipo AbstractConsoleItem ma con Type: ["+item.getType()+"] non di tipo titolo o note");
  229.             }
  230.         }

  231.         return dati;
  232.     }

  233.     public static List<DataElement> getTitle(List<DataElement> dati ,BaseConsoleItem item, int size, DataElementType type) {
  234.         DataElement de = new DataElement();
  235.         de.setName(item.getId());
  236.         de.setType(type);
  237.         de.setLabel(item.getLabel());
  238.         de.setSize(size);
  239.         de.setValue("");

  240.         if(ConsoleItemType.TITLE.equals(item.getType()) && item instanceof TitleConsoleItem) {
  241.             TitleConsoleItem titleItem = (TitleConsoleItem) item;
  242.             if(titleItem.isCloseable()) {
  243.                 de.setStatoAperturaSezioni(STATO_APERTURA_SEZIONI.CHIUSO);
  244.             }
  245.         }
  246.         else if(ConsoleItemType.SUBTITLE.equals(item.getType()) && item instanceof SubtitleConsoleItem) {
  247.             SubtitleConsoleItem subItem = (SubtitleConsoleItem) item;
  248.             if(subItem.isCloseable()) {
  249.                 de.setStatoAperturaSezioni(STATO_APERTURA_SEZIONI.CHIUSO);
  250.             }
  251.         }
  252.        
  253.        
  254.         dati.add(de);

  255.         return dati;
  256.     }

  257.     private static String getPrefixMessageErrorConsoleItemType(ConsoleItemValueType consoleItemValueType) {
  258.         return "Item con consoleItemType ["+consoleItemValueType+"] ";
  259.     }
  260.    
  261.     public static List<DataElement> getText(List<DataElement> dati ,ConsoleHelper consoleHelper, AbstractConsoleItem<?> item, int size, DataElementType type) throws ProtocolException {
  262.         DataElement de = new DataElement();
  263.         de.setName(item.getId());
  264.         if(DataElementType.LOCK.equals(type)) {
  265.             // imposto il tipo insieme al valore
  266.         }
  267.         else {
  268.             de.setType(type);
  269.         }
  270.         de.setRequired(item.isRequired());
  271.         de.setLabel(item.getLabel());
  272.         de.setLabelRight(item.getLabelRight());
  273.         if(item.isReloadOnChange()) {
  274.             if(item.isReloadOnHttpPost()) {
  275.                 de.setPostBack_viaPOST(true);
  276.             }
  277.             else {
  278.                 de.setPostBack(true);
  279.             }
  280.         }
  281.         de.setNote(item.getNote());
  282.         de.setSize(size);
  283.         addDataElementInfo(item, de);
  284.        
  285.         ConsoleItemValueType consoleItemValueType = ProtocolPropertiesUtils.getConsoleItemValueType(item);

  286.         setTextValue(consoleItemValueType, consoleHelper, item, de, type);
  287.        
  288.         if(item.getDefaultValueForCloseableSection() instanceof String) {
  289.             String s = (String) item.getDefaultValueForCloseableSection();
  290.             de.setValoreDefault(s);
  291.         }      

  292.         dati.add(de);

  293.         return dati;
  294.     }
  295.     private static void setTextValue(ConsoleItemValueType consoleItemValueType,ConsoleHelper consoleHelper,AbstractConsoleItem<?> item,DataElement de, DataElementType type) throws ProtocolException {
  296.         String value = null;
  297.         switch(consoleItemValueType){
  298.         case BOOLEAN:
  299.             BooleanConsoleItem booleanItem = (BooleanConsoleItem) item;
  300.             value = booleanItem.getDefaultValue() != null ? booleanItem.getDefaultValue() + "" : "";
  301.             break;
  302.         case NUMBER:
  303.             NumberConsoleItem numberItem = (NumberConsoleItem) item;
  304.             value = numberItem.getDefaultValue() != null ? numberItem.getDefaultValue() + "" : "";
  305.             if(DataElementType.NUMBER.equals(type)) {
  306.                 de.setMinValue((int)numberItem.getMin());
  307.                 de.setMaxValue((int)numberItem.getMax());
  308.             }
  309.             break;
  310.         case STRING:
  311.             StringConsoleItem stringItem = (StringConsoleItem) item;
  312.             value = stringItem.getDefaultValue() != null ? stringItem.getDefaultValue() : "";
  313.             if(stringItem.getRows()!=null) {
  314.                 de.setRows(stringItem.getRows());
  315.             }
  316.             break;
  317.         case BINARY: // NOP
  318.             value = "Prova Binary";
  319.             break;
  320.         default:
  321.             throw new ProtocolException(getPrefixMessageErrorConsoleItemType(consoleItemValueType)+"non puo' essere visualizzato come un "+type);
  322.         }
  323.        
  324.         if(DataElementType.LOCK.equals(type)) {
  325.             // imposto il tipo insieme al valore
  326.             try {
  327.                 consoleHelper.getCore().getLockUtilities().lock(de, value);
  328.             }catch(Exception e) {
  329.                 throw new ProtocolException(e.getMessage(),e);
  330.             }
  331.         }
  332.         else {
  333.             de.setValue(value);
  334.         }

  335.     }

  336.     public static List<DataElement> getFile(List<DataElement> dati ,
  337.             ConsoleHelper consoleHelper,
  338.             AbstractConsoleItem<?> item, int size, ConsoleOperationType consoleOperationType,
  339.             Properties binaryChangeProperties,
  340.             Long protocolPropertyId, String protocolPropertyFile,
  341.             boolean contentSaved) {
  342.        
  343.         if(consoleHelper!=null) {
  344.             // nop
  345.         }
  346.        
  347.         DataElement de = new DataElement();
  348.         DataElement de2 =null ;
  349.         List<DataElement> df = null;
  350.         de.setName(item.getId());

  351.         /**String nameRechange = ProtocolPropertiesCostanti.PARAMETER_FILENAME_RECHANGE_PREFIX + item.getId();
  352.         String nameRechangeParamTmp = consoleHelper.getParameter(nameRechange);
  353.         boolean isNameRechange = "true".equals(nameRechangeParamTmp);
  354.        
  355.         org.openspcoop2.protocol.sdk.properties.BinaryConsoleItem bci = null;
  356.         String fileName = null;
  357.         if(item!=null && item instanceof org.openspcoop2.protocol.sdk.properties.BinaryConsoleItem) {
  358.             bci = (org.openspcoop2.protocol.sdk.properties.BinaryConsoleItem) item;
  359.             fileName = bci.getFileName();
  360.         }
  361.         if(!consoleOperationType.equals(ConsoleOperationType.ADD) && fileName==null) {
  362.             // aggiungo riferimento per dopo
  363.             DataElement deChange = new DataElement();
  364.             deChange.setName(nameRechange);
  365.             deChange.setValue("true");
  366.             deChange.setType(DataElementType.HIDDEN);
  367.             dati.add(deChange);
  368.         }*/
  369.        
  370.         /**boolean addDE = true;*/
  371.        
  372.         if(consoleOperationType.equals(ConsoleOperationType.ADD) || !contentSaved){
  373.             BinaryConsoleItem binaryItem = (BinaryConsoleItem) item;

  374.             BinaryParameter bp = new BinaryParameter();
  375.             bp.setName(binaryItem.getId());
  376.             bp.setFilename(binaryItem.getFileName());
  377.             bp.setId(binaryItem.getFileId());

  378.             de = bp.getFileDataElement(item.getLabel(), "", size);
  379.             de.setRequired(item.isRequired());
  380.             /**         de.setPostBack(item.isReloadOnChange()); */
  381.             df = bp.getFileNameDataElement();
  382.             de2 = bp.getFileIdDataElement();
  383.         } else {
  384.            
  385.             /**if(fileName==null || isNameRechange) {
  386.                 addDE = false;
  387.             }*/
  388.            
  389.             de.setType(DataElementType.LINK);
  390.             String idItem = protocolPropertyId != null ? protocolPropertyId + "" : "";
  391.             String idProprietario = binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_ID_PROPRIETARIO);
  392.             String urlChange= binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_URL_ORIGINALE_CHANGE);
  393.             String tipoProprietario = binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_TIPO_PROPRIETARIO);
  394.             String nomeProprietario = binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_NOME_PROPRIETARIO);
  395.             String parentProprietario = binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_NOME_PARENT_PROPRIETARIO);
  396.             if(parentProprietario == null)
  397.                 parentProprietario = "";
  398.             String protocollo = binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_PROTOCOLLO);
  399.             String tipoAccordo = binaryChangeProperties.getProperty(ProtocolPropertiesCostanti.PARAMETRO_PP_TIPO_ACCORDO);
  400.             if(tipoAccordo == null)
  401.                 tipoAccordo = "";

  402.             de.setUrl(ProtocolPropertiesCostanti.SERVLET_NAME_BINARY_PROPERTY_CHANGE,
  403.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_ID,idItem),
  404.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_NOME,item.getId()),
  405.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_ID_PROPRIETARIO,idProprietario),
  406.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_TIPO_PROPRIETARIO,tipoProprietario),
  407.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_NOME_PROPRIETARIO,nomeProprietario),
  408.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_NOME_PARENT_PROPRIETARIO,parentProprietario),
  409.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_PROTOCOLLO,protocollo),
  410.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_TIPO_ACCORDO, tipoAccordo),
  411.                     new Parameter(ProtocolPropertiesCostanti.PARAMETRO_PP_URL_ORIGINALE_CHANGE,urlChange));
  412.             // CHANGE Label del link
  413.             de.setValue(item.getLabel());

  414.             if(protocolPropertyFile != null){
  415.                 de2 = new DataElement();
  416.                 de2.setName(ProtocolPropertiesCostanti.PARAMETER_FILENAME_PREFIX + item.getId());
  417.                 de2.setValue(protocolPropertyFile);
  418.                 de2.setType(DataElementType.HIDDEN);

  419.             }
  420.         }

  421.         /**if(addDE) {*/
  422.         dati.add(de);
  423.         /**}*/
  424.        
  425.         if(df != null)
  426.             dati.addAll(df);

  427.         if(de2 != null)
  428.             dati.add(de2);

  429.         return dati;
  430.     }

  431.     private static void addDataElementInfo(AbstractConsoleItem<?> item, DataElement de) {
  432.         if(item.getInfo()!=null) {
  433.             DataElementInfo dInfo = new DataElementInfo(item.getInfo().getHeaderFinestraModale());
  434.             dInfo.setHeaderBody(StringEscapeUtils.escapeHtml(item.getInfo().getHeaderBody()));
  435.             if(item.getInfo().getListBody()!=null && !item.getInfo().getListBody().isEmpty()) {
  436.                 List<String> escapedValues = new ArrayList<>();
  437.                 for (String v : item.getInfo().getListBody()) {
  438.                     escapedValues.add(StringEscapeUtils.escapeHtml(v));
  439.                 }
  440.                 dInfo.setListBody(escapedValues);
  441.             }
  442.             de.setInfo(dInfo);
  443.         }
  444.     }
  445.    
  446.     private static Boolean getSelectedValue(BooleanConsoleItem booleanItem, Object defaultItemValue) {
  447.         Boolean selectedBooleanValue = null;
  448.         if(booleanItem.getDefaultValue() != null) {
  449.             selectedBooleanValue = booleanItem.getDefaultValue();
  450.         }
  451.         else if(defaultItemValue instanceof Boolean) {
  452.             selectedBooleanValue = (Boolean) defaultItemValue;
  453.         }
  454.         else {
  455.             selectedBooleanValue = false;
  456.         }
  457.         return selectedBooleanValue;
  458.     }
  459.     private static String getSelectedValue(NumberConsoleItem numberItem, Object defaultItemValue) {
  460.         String selectedNumberValue = null;
  461.         if(numberItem.getDefaultValue()!=null) {
  462.             selectedNumberValue = numberItem.getDefaultValue() + "";
  463.         }
  464.         else if(defaultItemValue instanceof Long) {
  465.             selectedNumberValue = (defaultItemValue) + "";
  466.         }
  467.         else if(defaultItemValue instanceof Integer) {
  468.             selectedNumberValue = (defaultItemValue) + "";
  469.         }
  470.         else {
  471.             selectedNumberValue = null;
  472.         }
  473.         return selectedNumberValue;
  474.     }
  475.     private static String getSelectedValue(StringConsoleItem stringItem, Object defaultItemValue) {
  476.         String selectedStringValue = null;
  477.         if(stringItem.getDefaultValue()!=null) {
  478.             selectedStringValue = stringItem.getDefaultValue();
  479.         }
  480.         else if(defaultItemValue instanceof String) {
  481.             selectedStringValue = ((String) defaultItemValue);
  482.         }
  483.         else {
  484.             selectedStringValue = null;
  485.         }
  486.         return selectedStringValue;
  487.     }
  488.    
  489.     public static List<DataElement> getCheckbox(List<DataElement> dati ,AbstractConsoleItem<?> item, Object defaultItemValue, int size) throws ProtocolException{
  490.         DataElement de = new DataElement();
  491.         de.setName(item.getId());
  492.         de.setType(DataElementType.CHECKBOX);
  493.         de.setRequired(item.isRequired());
  494.         de.setLabel(item.getLabel());
  495.         de.setLabelRight(item.getLabelRight());
  496.         if(item.isReloadOnChange()) {
  497.             if(item.isReloadOnHttpPost()) {
  498.                 de.setPostBack_viaPOST(true);
  499.             }
  500.             else {
  501.                 de.setPostBack(true);
  502.             }
  503.         }
  504.         de.setNote(item.getNote());
  505.         de.setSize(size);
  506.         addDataElementInfo(item, de);

  507.         ConsoleItemValueType consoleItemValueType = ProtocolPropertiesUtils.getConsoleItemValueType(item);

  508.         switch(consoleItemValueType){
  509.         case BOOLEAN:
  510.             BooleanConsoleItem booleanItem = (BooleanConsoleItem) item;
  511.             Boolean selectedBooleanValue = getSelectedValue(booleanItem, defaultItemValue);
  512.             de.setSelected(selectedBooleanValue);
  513.             break;
  514.         case NUMBER:
  515.             NumberConsoleItem numberItem = (NumberConsoleItem) item;
  516.             String selectedNumberValue = getSelectedValue(numberItem, defaultItemValue);
  517.             de.setSelected(selectedNumberValue);
  518.             break;
  519.         case STRING:
  520.             StringConsoleItem stringItem = (StringConsoleItem) item;
  521.             String selectedStringValue = getSelectedValue(stringItem, defaultItemValue);
  522.             de.setSelected(selectedStringValue);
  523.             break;
  524.         case BINARY: // non supportato
  525.         default:
  526.             throw new ProtocolException(getPrefixMessageErrorConsoleItemType(consoleItemValueType)+"non puo' essere visualizzato come una CheckBox");
  527.         }
  528.        
  529.         if(item.getDefaultValueForCloseableSection() instanceof Boolean) {
  530.             boolean b = (Boolean) item.getDefaultValueForCloseableSection();
  531.             de.setValoreDefaultCheckbox(b);
  532.         }

  533.         dati.add(de);

  534.         return dati;
  535.     }

  536.     public static List<DataElement> getHidden(List<DataElement> dati, ConsoleHelper consoleHelper, AbstractConsoleItem<?> item, int size) throws ProtocolException {
  537.         DataElement de = new DataElement();
  538.         de.setName(item.getId());
  539.         de.setType(DataElementType.HIDDEN);
  540.         de.setRequired(item.isRequired());
  541.         de.setLabel(item.getLabel());
  542.         de.setLabelRight(item.getLabelRight());
  543.         if(item.isReloadOnChange()) {
  544.             if(item.isReloadOnHttpPost()) {
  545.                 de.setPostBack_viaPOST(true);
  546.             }
  547.             else {
  548.                 de.setPostBack(true);
  549.             }
  550.         }
  551.         de.setNote(item.getNote());
  552.         de.setSize(size);
  553.         /**addDataElementInfo(item, de);*/

  554.         ConsoleItemValueType consoleItemValueType = ProtocolPropertiesUtils.getConsoleItemValueType(item);

  555.         setHiddenValue(consoleItemValueType, consoleHelper, item, de);

  556.         dati.add(de);

  557.         return dati;
  558.     }
  559.     private static void setHiddenValue(ConsoleItemValueType consoleItemValueType,ConsoleHelper consoleHelper,AbstractConsoleItem<?> item,DataElement de) throws ProtocolException {
  560.        
  561.         String value = null;
  562.         switch(consoleItemValueType){
  563.         case BOOLEAN:
  564.             BooleanConsoleItem booleanItem = (BooleanConsoleItem) item;
  565.             value = booleanItem.getDefaultValue() != null ? booleanItem.getDefaultValue() + "" : "";
  566.             break;
  567.         case NUMBER:
  568.             NumberConsoleItem numberItem = (NumberConsoleItem) item;
  569.             value = numberItem.getDefaultValue() != null ? numberItem.getDefaultValue() + "" : "";
  570.             break;
  571.         case STRING:
  572.             StringConsoleItem stringItem = (StringConsoleItem) item;
  573.             value = stringItem.getDefaultValue() != null ? stringItem.getDefaultValue() + "" : "";
  574.             break;
  575.         case BINARY: // NOP
  576.             value = "";
  577.             break;
  578.         default:
  579.             throw new ProtocolException(getPrefixMessageErrorConsoleItemType(consoleItemValueType)+"non puo' essere visualizzato come una Hidden");
  580.         }
  581.        
  582.         if(item.isLockedType()) {
  583.             // imposto il tipo insieme al valore
  584.             try {
  585.                 consoleHelper.getCore().getLockUtilities().lockHidden(de, value);
  586.             }catch(Exception e) {
  587.                 throw new ProtocolException(e.getMessage(),e);
  588.             }
  589.         }
  590.         else {
  591.             de.setValue(value);
  592.         }

  593.     }
  594.    
  595.     public static List<DataElement> getHidden(List<DataElement> dati, ConsoleHelper consoleHelper ,BaseConsoleItem item, int size) {
  596.         DataElement de = new DataElement();
  597.         de.setName(item.getId());
  598.         de.setType(DataElementType.HIDDEN);
  599.         de.setLabel(item.getLabel());
  600.         de.setSize(size);
  601.         dati.add(de);

  602.         return dati;
  603.     }

  604.     public static List<DataElement> getSelect(List<DataElement> dati ,AbstractConsoleItem<?> item, Object defaultItemValue, int size) throws ProtocolException {
  605.         DataElement de = new DataElement();
  606.         de.setName(item.getId());
  607.         de.setType(DataElementType.SELECT);
  608.         de.setRequired(item.isRequired());
  609.         de.setLabel(item.getLabel());
  610.         de.setLabelRight(item.getLabelRight());
  611.         if(item.isReloadOnChange()) {
  612.             if(item.isReloadOnHttpPost()) {
  613.                 de.setPostBack_viaPOST(true);
  614.             }
  615.             else {
  616.                 de.setPostBack(true);
  617.             }
  618.         }
  619.         de.setNote(item.getNote());
  620.         de.setSize(size);
  621.         addDataElementInfo(item, de);

  622.         ConsoleItemValueType consoleItemValueType = ProtocolPropertiesUtils.getConsoleItemValueType(item);

  623.         List<String> values = new ArrayList<>();
  624.         List<String> labels = new ArrayList<>();

  625.         switch(consoleItemValueType){
  626.         case BOOLEAN:
  627.             BooleanConsoleItem booleanItem = (BooleanConsoleItem) item;
  628.             Boolean selectedBooleanValue = getSelectedValue(booleanItem, defaultItemValue);
  629.             de.setSelected(selectedBooleanValue);

  630.             SortedMap<Boolean> booleanMapLabelValues = booleanItem.getMapLabelValues();
  631.             for (String key : booleanMapLabelValues.keys()) {
  632.                 labels.add(key);
  633.                 values.add(booleanMapLabelValues.get(key)+ "");
  634.             }
  635.             break;
  636.         case NUMBER:
  637.             NumberConsoleItem numberItem = (NumberConsoleItem) item;
  638.             String selectedNumberValue = getSelectedValue(numberItem, defaultItemValue);
  639.             de.setSelected(selectedNumberValue);

  640.             SortedMap<Long> numberMapLabelValues = numberItem.getMapLabelValues();
  641.             for (String key : numberMapLabelValues.keys()) {
  642.                 labels.add(key);
  643.                 values.add(numberMapLabelValues.get(key)+ "");
  644.             }
  645.             break;
  646.         case STRING:
  647.             StringConsoleItem stringItem = (StringConsoleItem) item;
  648.             String selectedStringValue = getSelectedValue(stringItem, defaultItemValue);
  649.             de.setSelected(selectedStringValue);

  650.             SortedMap<String> stringMapLabelValues = stringItem.getMapLabelValues();
  651.             for (String key : stringMapLabelValues.keys()) {
  652.                 labels.add(key);
  653.                 values.add(stringMapLabelValues.get(key)+ "");
  654.             }
  655.             break;
  656.         case BINARY: // non supportato
  657.         default:
  658.             throw new ProtocolException(getPrefixMessageErrorConsoleItemType(consoleItemValueType)+"non puo' essere visualizzato come una Select List");
  659.         }

  660.         de.setValues(values);
  661.         de.setLabels(labels);

  662.         if(item.getDefaultValueForCloseableSection() instanceof String) {
  663.             String s = (String) item.getDefaultValueForCloseableSection();
  664.             de.setValoreDefaultSelect(s);
  665.         }
  666.        
  667.         dati.add(de);

  668.         return dati;
  669.     }
  670.    
  671.     public static List<DataElement> getMultiSelect(List<DataElement> dati ,AbstractConsoleItem<?> item, Object defaultItemValue, int size) throws ProtocolException {
  672.         DataElement de = new DataElement();
  673.         de.setName(item.getId());
  674.         de.setType(DataElementType.MULTI_SELECT);
  675.         de.setRequired(item.isRequired());
  676.         de.setLabel(item.getLabel());
  677.         de.setLabelRight(item.getLabelRight());
  678.         if(item.isReloadOnChange()) {
  679.             if(item.isReloadOnHttpPost()) {
  680.                 de.setPostBack_viaPOST(true);
  681.             }
  682.             else {
  683.                 de.setPostBack(true);
  684.             }
  685.         }
  686.         de.setNote(item.getNote());
  687.         de.setSize(size);
  688.         addDataElementInfo(item, de);

  689.         ConsoleItemValueType consoleItemValueType = ProtocolPropertiesUtils.getConsoleItemValueType(item);

  690.         setValuesLabelsMultiSelect(consoleItemValueType, item, defaultItemValue, de);
  691.        
  692.         if(item.getDefaultValueForCloseableSection() instanceof String) {
  693.             String s = (String) item.getDefaultValueForCloseableSection();
  694.             de.setValoreDefaultMultiSelect(new String [] {s});
  695.         }
  696.        
  697.         dati.add(de);

  698.         return dati;
  699.     }
  700.     private static void setValuesLabelsMultiSelect(ConsoleItemValueType consoleItemValueType, AbstractConsoleItem<?> item, Object defaultItemValue, DataElement de) throws ProtocolException {
  701.         List<String> values = new ArrayList<>();
  702.         List<String> labels = new ArrayList<>();

  703.         switch(consoleItemValueType){
  704.         case STRING:
  705.             StringConsoleItem stringItem = (StringConsoleItem) item;
  706.             String selectedStringValue = getSelectedValue(stringItem, defaultItemValue);
  707.             if(selectedStringValue!=null) {
  708.                 if(selectedStringValue.contains(",")) {
  709.                     de.setSelezionati(selectedStringValue.split(","));
  710.                 }
  711.                 else {
  712.                     de.setSelezionati(new String[] {selectedStringValue});
  713.                 }
  714.             }
  715.             if(stringItem.getRows()!=null) {
  716.                 de.setRows(stringItem.getRows());
  717.             }
  718.            
  719.             SortedMap<String> stringMapLabelValues = stringItem.getMapLabelValues();
  720.             for (String key : stringMapLabelValues.keys()) {
  721.                 labels.add(key);
  722.                 values.add(stringMapLabelValues.get(key)+ "");
  723.             }
  724.             break;
  725.        
  726.         case BINARY:
  727.         case BOOLEAN:
  728.         case NUMBER:
  729.         default:
  730.             throw new ProtocolException(getPrefixMessageErrorConsoleItemType(consoleItemValueType)+"non puo' essere visualizzato come una Multi-Select List");
  731.         }

  732.         de.setValues(values);
  733.         de.setLabels(labels);
  734.     }

  735.     public static String getLabelTipoProprietario(ProprietariProtocolProperty tipoProprietario, String tipoAccordo) {
  736.         if(tipoProprietario != null){
  737.             switch (tipoProprietario) {
  738.             case ACCORDO_COOPERAZIONE:
  739.                 return ProtocolPropertiesCostanti.LABEL_AC;
  740.             case ACCORDO_SERVIZIO_PARTE_COMUNE:
  741.                 return AccordiServizioParteComuneUtilities.getTerminologiaAccordoServizio(tipoAccordo);
  742.             case ACCORDO_SERVIZIO_PARTE_SPECIFICA:
  743.                 return ProtocolPropertiesCostanti.LABEL_APS;
  744.             case AZIONE_ACCORDO:
  745.                 return ProtocolPropertiesCostanti.LABEL_AZIONE_ACCORDO;
  746.             case FRUITORE:
  747.                 return ProtocolPropertiesCostanti.LABEL_FRUITORE;
  748.             case OPERATION:
  749.                 return ProtocolPropertiesCostanti.LABEL_OPERATION;
  750.             case PORT_TYPE:
  751.                 return ProtocolPropertiesCostanti.LABEL_PORT_TYPE;
  752.             case RESOURCE:
  753.                 return ProtocolPropertiesCostanti.LABEL_RESOURCE;
  754.             case SOGGETTO:
  755.                 return ProtocolPropertiesCostanti.LABEL_SOGGETTO;
  756.             case SERVIZIO_APPLICATIVO:
  757.                 return ProtocolPropertiesCostanti.LABEL_SERVIZIO_APPLICATIVO;
  758.             }
  759.         }
  760.         return null;
  761.     }

  762.     public static String getValueParametroTipoProprietarioAccordoServizio(String tipo){
  763.         if(ProtocolPropertiesCostanti.PARAMETRO_VALORE_PP_TIPO_ACCORDO_PARTE_COMUNE.equals(tipo)){
  764.             return ProtocolPropertiesCostanti.PARAMETRO_PP_TIPO_PROPRIETARIO_VALUE_ACCORDO_SERVIZIO_PARTE_COMUNE;
  765.         }
  766.         else{
  767.             return ProtocolPropertiesCostanti.PARAMETRO_PP_TIPO_PROPRIETARIO_VALUE_ACCORDO_SERVIZIO_COMPOSTO;
  768.         }
  769.     }
  770. }