BinaryParameter.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;

  21. import java.io.Serializable;
  22. import java.text.MessageFormat;
  23. import java.util.ArrayList;
  24. import java.util.List;

  25. import org.apache.commons.lang.StringUtils;

  26. /**
  27.  * BinaryParameter
  28.  *
  29.  * @author Giuliano Pintori (pintori@link.it)
  30.  * @author $Author$
  31.  * @version $Rev$, $Date$
  32.  */
  33. public class BinaryParameter implements Serializable {

  34.     /**
  35.      *
  36.      */
  37.     private static final long serialVersionUID = 1L;

  38.     private String name;
  39.     private byte[] value;
  40.     private String filename;
  41.     private String id;
  42.    
  43.     public String getName() {
  44.         return this.name;
  45.     }
  46.     public void setName(String name) {
  47.         this.name = name;
  48.     }
  49.     public byte[] getValue() {
  50.         return this.value;
  51.     }
  52.     public void setValue(byte[] value) {
  53.         this.value = value;
  54.     }
  55.     public String getFilename() {
  56.         return this.filename;
  57.     }
  58.     public void setFilename(String filename) {
  59.         this.filename = filename;
  60.     }
  61.     public String getId() {
  62.         return this.id;
  63.     }
  64.     public void setId(String id) {
  65.         this.id = id;
  66.     }

  67.     public List<DataElement> getFileNameDataElement(){
  68.         List<DataElement> dati = new ArrayList<DataElement>();
  69.        
  70.         DataElement de1 = new DataElement();
  71.         DataElement de2 = null;
  72.         de1.setName(Costanti.PARAMETER_FILENAME_PREFIX + this.name);
  73.         de1.setValue(this.filename);
  74.         de1.setType(DataElementType.HIDDEN);
  75.        
  76.         if(StringUtils.isNotBlank(this.filename)){
  77.             de2 = new DataElement();
  78.             de2.setName("_" + Costanti.PARAMETER_FILENAME_PREFIX + this.name);
  79.             de2.setValue("<I>" + this.filename + "</I>");
  80.             de2.setLabel("");
  81.             de2.setType(DataElementType.TEXT);
  82.            
  83.             DataElementImage newImage = new DataElementImage();
  84.            
  85.             newImage.setImage(Costanti.ICON_ELIMINA_FILE);
  86.             newImage.setTarget(TargetType.SELF);
  87.             newImage.setUrl("#");
  88.             newImage.setToolTip(MessageFormat.format(Costanti.TOOLTIP_ELIMINA_FILE, this.filename));
  89.            
  90.             StringBuilder onClickFunction = new StringBuilder();
  91.             onClickFunction.append(Costanti.POSTBACK_FUNCTION_WITH_PARAMETER_START);
  92.             onClickFunction.append(this.name  + Costanti.PARAMETER_FILENAME_REMOVE_PLACEHOLDER + 0);
  93.             onClickFunction.append(Costanti.POSTBACK_FUNCTION_WITH_PARAMETER_END);
  94.             newImage.setOnClick(onClickFunction.toString());
  95.            
  96.             de2.setImage(newImage);
  97.         }  
  98.        
  99.         dati.add(de1);
  100.        
  101.         if(de2 != null)
  102.             dati.add(de2);
  103.        
  104.         return dati;
  105.     }
  106.    
  107.     public DataElement getFileIdDataElement(){
  108.         DataElement de = new DataElement();
  109.        
  110.         de.setType(DataElementType.HIDDEN);
  111.         de.setName(Costanti.PARAMETER_FILEID_PREFIX + this.name);
  112.         de.setValue(this.id);
  113.        
  114.         return de;
  115.     }
  116.    
  117.     public DataElement getFileDataElement(String label, String value, int size){
  118.         DataElement de = new DataElement();
  119.        
  120.         de = new DataElement();
  121.         de.setLabel(label);
  122.         de.setValue(value);
  123.         de.setType(DataElementType.FILE);
  124.         de.setName(this.name);
  125.         de.setSize(size);
  126.         de.setPostBack(true);
  127.        
  128.         return de;
  129.     }
  130.    
  131.     public static List<DataElement> getFileNameDataElement(List<BinaryParameter> listaParametri) {
  132.         List<DataElement> dati = new ArrayList<DataElement>();
  133.        
  134.         BinaryParameter bp0 = listaParametri.get(0);
  135.        
  136.         List<String> fileNames = new ArrayList<>();
  137.        
  138.         for (BinaryParameter bp : listaParametri) {
  139.             if(StringUtils.isNotBlank(bp.getFilename()))
  140.                 fileNames.add(bp.getFilename());
  141.         }
  142.        
  143.         DataElement de1 = new DataElement();
  144.         DataElement de2 = null;
  145.         de1.setName(Costanti.PARAMETER_FILENAME_PREFIX + bp0.getName());
  146.         de1.setValue(StringUtils.join(fileNames, ","));
  147.         de1.setType(DataElementType.HIDDEN);
  148.        
  149.         dati.add(de1);
  150.        
  151.         int i=0;
  152.         for (String fileName : fileNames) {
  153.             if(StringUtils.isNotBlank(fileName)){
  154.                 de2 = new DataElement();
  155.                 de2.setName("_" + Costanti.PARAMETER_FILENAME_PREFIX + bp0.getName());
  156.                 de2.setValue("<I>" + fileName + "</I>");
  157.                 de2.setLabel("");
  158.                 de2.setType(DataElementType.TEXT);
  159.                
  160.                 DataElementImage newImage = new DataElementImage();
  161.                
  162.                 newImage.setImage(Costanti.ICON_ELIMINA_FILE);
  163.                 newImage.setTarget(TargetType.SELF);
  164.                 newImage.setUrl("#");
  165.                 newImage.setToolTip(MessageFormat.format(Costanti.TOOLTIP_ELIMINA_FILE, fileName));
  166.                
  167.                 StringBuilder onClickFunction = new StringBuilder();
  168.                 onClickFunction.append(Costanti.POSTBACK_FUNCTION_WITH_PARAMETER_START);
  169.                 onClickFunction.append(bp0.getName() + Costanti.PARAMETER_FILENAME_REMOVE_PLACEHOLDER + i);
  170.                 onClickFunction.append(Costanti.POSTBACK_FUNCTION_WITH_PARAMETER_END);
  171.                 newImage.setOnClick(onClickFunction.toString());
  172.                
  173.                 de2.setImage(newImage);
  174.                
  175.                 dati.add(de2);
  176.                 i++;
  177.             }
  178.         }
  179.        
  180.         return dati;
  181.     }

  182.     public static DataElement getFileIdDataElement(List<BinaryParameter> listaParametri){
  183.         DataElement de = new DataElement();
  184.        
  185.         BinaryParameter bp0 = listaParametri.get(0);
  186.        
  187.         List<String> ids = new ArrayList<>();
  188.        
  189.         for (BinaryParameter bp : listaParametri) {
  190.             if(StringUtils.isNotBlank(bp.getId()))
  191.                 ids.add(bp.getId());
  192.         }
  193.        
  194.         de.setType(DataElementType.HIDDEN);
  195.         de.setName(Costanti.PARAMETER_FILEID_PREFIX + bp0.getName());
  196.         de.setValue(StringUtils.join(ids, ","));
  197.        
  198.         return de;
  199.     }
  200. }