AccordiServizioParteComuneAllegatiAdd.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.apc;

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

  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import javax.servlet.http.HttpSession;

  26. import org.apache.commons.lang.StringUtils;
  27. import org.apache.struts.action.Action;
  28. import org.apache.struts.action.ActionForm;
  29. import org.apache.struts.action.ActionForward;
  30. import org.apache.struts.action.ActionMapping;
  31. import org.openspcoop2.core.registry.AccordoServizioParteComune;
  32. import org.openspcoop2.core.registry.AccordoServizioParteComuneServizioComposto;
  33. import org.openspcoop2.core.registry.Documento;
  34. import org.openspcoop2.core.registry.constants.ProprietariDocumento;
  35. import org.openspcoop2.core.registry.constants.RuoliDocumento;
  36. import org.openspcoop2.core.registry.constants.TipiDocumentoCoordinamento;
  37. import org.openspcoop2.core.registry.constants.TipiDocumentoSemiformale;
  38. import org.openspcoop2.core.registry.driver.IDAccordoFactory;
  39. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  40. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  41. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  42. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  43. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  44. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  45. import org.openspcoop2.web.ctrlstat.servlet.apc.api.ApiCostanti;
  46. import org.openspcoop2.web.ctrlstat.servlet.archivi.ArchiviHelper;
  47. import org.openspcoop2.web.lib.mvc.BinaryParameter;
  48. import org.openspcoop2.web.lib.mvc.Costanti;
  49. import org.openspcoop2.web.lib.mvc.ForwardParams;
  50. import org.openspcoop2.web.lib.mvc.GeneralData;
  51. import org.openspcoop2.web.lib.mvc.PageData;
  52. import org.openspcoop2.web.lib.mvc.Parameter;
  53. import org.openspcoop2.web.lib.mvc.ServletUtils;
  54. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  55. /**
  56.  * accordiAllegatiAdd
  57.  *
  58.  * @author Andrea Poli (apoli@link.it)
  59.  * @author Stefano Corallo (corallo@link.it)
  60.  * @author Sandra Giangrandi (sandra@link.it)
  61.  * @author $Author$
  62.  * @version $Rev$, $Date$
  63.  *
  64.  */
  65. public final class AccordiServizioParteComuneAllegatiAdd extends Action {

  66.     @Override
  67.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  68.         HttpSession session = request.getSession(true);
  69.        
  70.         // Inizializzo PageData
  71.         PageData pd = new PageData();

  72.         GeneralHelper generalHelper = new GeneralHelper(session);

  73.         // Inizializzo GeneralData
  74.         GeneralData gd = generalHelper.initGeneralData(request);

  75.         String userLogin = ServletUtils.getUserLoginFromSession(session);

  76.         try {
  77.             AccordiServizioParteComuneHelper apcHelper = new AccordiServizioParteComuneHelper(request, pd, session);
  78.             ArchiviHelper archiviHelper = new ArchiviHelper(request, pd, session);
  79.            
  80.             Boolean isShowAccordiCooperazione = ServletUtils.getObjectFromSession(request, session, Boolean.class, CostantiControlStation.SESSION_PARAMETRO_VISUALIZZA_ACCORDI_COOPERAZIONE);

  81.             String idAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID);
  82.             long idAccordoLong = Long.parseLong(idAccordo);
  83.             String ruolo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ALLEGATI_RUOLO);
  84.             String tipoFile = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ALLEGATI_TIPO_FILE);
  85.            
  86.             String tipoAccordo = apcHelper.getParameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_TIPO_ACCORDO);
  87.             if("".equals(tipoAccordo))
  88.                 tipoAccordo = null;
  89.            
  90.             List<BinaryParameter> binaryParameterDocumenti = apcHelper.getBinaryParameters(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ALLEGATI_DOCUMENTO);
  91.            
  92.             AccordiServizioParteComuneCore apcCore = new AccordiServizioParteComuneCore();

  93.             // Preparo il menu
  94.             apcHelper.makeMenu();

  95.             // Prendo il nome
  96.             AccordoServizioParteComune as = apcCore.getAccordoServizioFull(idAccordoLong);
  97.             String labelASTitle = apcHelper.getLabelIdAccordo(as);
  98.            
  99.             String[] ruoli = null;
  100.             if(isShowAccordiCooperazione!=null && isShowAccordiCooperazione.booleanValue()){
  101.                 ruoli = as.getServizioComposto() ==null ? new String[2] : new String[3];
  102.             }else{
  103.                 ruoli = new String[2];
  104.             }
  105.            
  106.            
  107.             ruoli[0]=RuoliDocumento.allegato.toString();
  108.             ruoli[1]=RuoliDocumento.specificaSemiformale.toString();
  109.            
  110.             if(isShowAccordiCooperazione!=null && isShowAccordiCooperazione.booleanValue() &&
  111.                 as.getServizioComposto()!=null) {
  112.                 ruoli[2]=RuoliDocumento.specificaCoordinamento.toString();
  113.             }
  114.            
  115.             String[] tipiAmmessi = null;
  116.             String[] tipiAmmessiLabel = null;
  117.            
  118.             if(ruolo!=null && !"".equals(ruolo)){
  119.                 switch (RuoliDocumento.valueOf(ruolo)) {
  120.                     case allegato:
  121.                         //non ci sono vincoli
  122.                         break;
  123.                     case specificaCoordinamento:
  124.                         tipiAmmessi = TipiDocumentoCoordinamento.toEnumNameArray();
  125.                         tipiAmmessiLabel=TipiDocumentoCoordinamento.toStringArray();
  126.                         break;
  127.                     case specificaSemiformale:
  128.                         tipiAmmessi = TipiDocumentoSemiformale.toEnumNameArray();
  129.                         tipiAmmessiLabel=TipiDocumentoSemiformale.toStringArray();
  130.                         break;
  131.                     default:
  132.                         break;
  133.                 }
  134.             }
  135.            
  136.             boolean find = false;
  137.             if(tipoFile!=null && StringUtils.isNotEmpty(tipoFile) && tipiAmmessi!=null && tipiAmmessi.length>0) {
  138.                 for (String t : tipiAmmessi) {
  139.                     if(tipoFile.equals(t)) {
  140.                         find = true;
  141.                         break;
  142.                     }
  143.                 }
  144.             }
  145.             if(!find) {
  146.                 tipoFile = null;
  147.             }
  148.            
  149.             IDAccordoFactory idAccordoFactory = IDAccordoFactory.getInstance();
  150.             String uri = idAccordoFactory.getUriFromAccordo(as);
  151.             Parameter pTipoAccordo = AccordiServizioParteComuneUtilities.getParametroAccordoServizio(tipoAccordo);
  152.             Parameter pIdAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_ID, idAccordo);
  153.             Parameter pNomeAccordo = new Parameter(AccordiServizioParteComuneCostanti.PARAMETRO_APC_NOME, uri);
  154.             IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByOrganizationType(as.getSoggettoReferente().getTipo());
  155.            
  156.             Boolean isModalitaVistaApiCustom = ServletUtils.getBooleanAttributeFromSession(ApiCostanti.SESSION_ATTRIBUTE_VISTA_APC_API, session, request, false).getValue();
  157.             List<Parameter> listaParams = apcHelper.getTitoloApc(TipoOperazione.ADD, as, tipoAccordo, labelASTitle, null, false);
  158.            
  159.             String labelAllegati = (isModalitaVistaApiCustom!=null && isModalitaVistaApiCustom.booleanValue()) ? AccordiServizioParteComuneCostanti.LABEL_ALLEGATI : AccordiServizioParteComuneCostanti.LABEL_ALLEGATI + " di " + labelASTitle;
  160.              
  161.             listaParams.add(new Parameter(labelAllegati, AccordiServizioParteComuneCostanti.SERVLET_NAME_APC_ALLEGATI_LIST, pIdAccordo, pNomeAccordo, pTipoAccordo));
  162.             listaParams.add(new Parameter(Costanti.PAGE_DATA_TITLE_LABEL_AGGIUNGI, null));
  163.            
  164.             // Se idhid = null, devo visualizzare la pagina per l'inserimento
  165.             // dati
  166.             if(apcHelper.isEditModeInProgress()){
  167.                
  168.                 // setto la barra del titolo
  169.                 ServletUtils.setPageDataTitle(pd, listaParams);

  170.                 // preparo i campi
  171.                 List<Object> dati = new ArrayList<>();

  172.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  173.                 apcHelper.addAccordiAllegatiToDati(dati,TipoOperazione.ADD,idAccordo,
  174.                         ruolo,ruoli,tipiAmmessi,tipiAmmessiLabel,tipoAccordo,tipoFile,
  175.                         null,null,as,null,null, binaryParameterDocumenti);
  176.                
  177.                 pd.setDati(dati);

  178.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  179.                
  180.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_ALLEGATI, ForwardParams.ADD());
  181.             }

  182.             // Controlli sui campi immessi
  183.             boolean isOk = archiviHelper.accordiAllegatiCheckData(TipoOperazione.ADD,binaryParameterDocumenti,as.getId(),ProprietariDocumento.accordoServizio, pf);
  184.             if (!isOk) {
  185.                
  186.                 // setto la barra del titolo
  187.                 ServletUtils.setPageDataTitle(pd, listaParams);

  188.                 // preparo i campi
  189.                 List<Object> dati = new ArrayList<>();

  190.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  191.                 apcHelper.addAccordiAllegatiToDati(dati,TipoOperazione.ADD,idAccordo,
  192.                         ruolo,ruoli,tipiAmmessi,tipiAmmessiLabel,tipoAccordo,tipoFile,
  193.                         null,null,as,null,null, binaryParameterDocumenti);
  194.                
  195.                 pd.setDati(dati);

  196.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  197.                
  198.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_ALLEGATI, ForwardParams.ADD());
  199.             }

  200.             //inserimento documenti in accordo
  201.             for (BinaryParameter binaryParameter : binaryParameterDocumenti) {
  202.                 Documento documento = new Documento();
  203.                 documento.setRuolo(RuoliDocumento.valueOf(ruolo).toString());
  204.                 documento.setByteContenuto(binaryParameter.getValue());
  205.                 documento.setFile(binaryParameter.getFilename());
  206.                 documento.setIdProprietarioDocumento(as.getId());

  207.                 switch (RuoliDocumento.valueOf(ruolo)) {
  208.                     case allegato:
  209.                         documento.setTipo(binaryParameter.getFilename().substring(binaryParameter.getFilename().lastIndexOf('.')+1, binaryParameter.getFilename().length()));
  210.                         as.addAllegato(documento);
  211.                         break;
  212.                     case specificaSemiformale:
  213.                         documento.setTipo(TipiDocumentoSemiformale.valueOf(tipoFile).getNome());
  214.                         as.addSpecificaSemiformale(documento);
  215.                         break;
  216.                     case specificaCoordinamento:
  217.                         documento.setTipo(TipiDocumentoCoordinamento.valueOf(tipoFile).getNome());
  218.                         AccordoServizioParteComuneServizioComposto assc = as.getServizioComposto();
  219.                         if(assc==null)
  220.                             assc=new AccordoServizioParteComuneServizioComposto();
  221.                         assc.addSpecificaCoordinamento(documento);
  222.                         as.setServizioComposto(assc);
  223.                         break;
  224.                     default:
  225.                         break;
  226.                 }
  227.             }
  228.            
  229.            
  230.             // effettuo le operazioni
  231.             apcCore.performUpdateOperation(userLogin, apcHelper.smista(), as);

  232.             // Preparo la lista
  233.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  234.            
  235.             List<Documento> lista = apcCore.accordiAllegatiList(idAccordoLong, ricerca);

  236.             apcHelper.prepareAccordiAllegatiList(as, ricerca, lista, tipoAccordo);

  237.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  238.            
  239.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_ALLEGATI, ForwardParams.ADD());
  240.            
  241.         } catch (Exception e) {
  242.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  243.                     AccordiServizioParteComuneCostanti.OBJECT_NAME_APC_ALLEGATI, ForwardParams.ADD());
  244.         }
  245.     }
  246. }