AccordiCooperazioneAllegatiChange.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.ac;

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

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

  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.apache.struts.upload.FormFile;
  32. import org.openspcoop2.core.registry.AccordoCooperazione;
  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.TipiDocumentoSemiformale;
  37. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  38. import org.openspcoop2.protocol.sdk.IProtocolFactory;
  39. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  40. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  41. import org.openspcoop2.web.ctrlstat.servlet.FileUploadForm;
  42. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  43. import org.openspcoop2.web.ctrlstat.servlet.archivi.ArchiviCore;
  44. import org.openspcoop2.web.ctrlstat.servlet.archivi.ArchiviHelper;
  45. import org.openspcoop2.web.lib.mvc.DataElement;
  46. import org.openspcoop2.web.lib.mvc.ForwardParams;
  47. import org.openspcoop2.web.lib.mvc.GeneralData;
  48. import org.openspcoop2.web.lib.mvc.PageData;
  49. import org.openspcoop2.web.lib.mvc.Parameter;
  50. import org.openspcoop2.web.lib.mvc.ServletUtils;
  51. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  52. /**
  53.  * accordiCoopAllegatiChange
  54.  *
  55.  * @author Andrea Poli (apoli@link.it)
  56.  * @author Stefano Corallo (corallo@link.it)
  57.  * @author Sandra Giangrandi (sandra@link.it)
  58.  * @author $Author$
  59.  * @version $Rev$, $Date$
  60.  *
  61.  */
  62. public final class AccordiCooperazioneAllegatiChange extends Action {

  63.     @SuppressWarnings("incomplete-switch")
  64.     @Override
  65.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

  66.         HttpSession session = request.getSession(true);

  67.         // Inizializzo PageData
  68.         PageData pd = new PageData();

  69.         GeneralHelper generalHelper = new GeneralHelper(session);

  70.         // Inizializzo GeneralData
  71.         GeneralData gd = generalHelper.initGeneralData(request);

  72.         String userLogin = ServletUtils.getUserLoginFromSession(session);
  73.        
  74.         try {
  75.            
  76.             FileUploadForm fileUpload = (FileUploadForm) form;
  77.            
  78.             AccordiCooperazioneHelper acHelper = new AccordiCooperazioneHelper(request, pd, session);
  79.             ArchiviHelper archiviHelper = new ArchiviHelper(request, pd, session);

  80.             String idAllegato = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_ID_ALLEGATO);
  81.             String idAccordo = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_ID_ACCORDO);
  82.             String nomeDocumento = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_NOME_DOCUMENTO);
  83.             long idAllegatoLong = Long.parseLong(idAllegato);
  84.             long idAccordoLong = Long.parseLong(idAccordo);
  85.             String tipoFile = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_TIPO_FILE);
  86.            
  87.             /**String tipoSICA = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_TIPO_SICA);
  88.             if("".equals(tipoSICA))
  89.                 tipoSICA = null;*/
  90.            
  91.             FormFile ff = fileUpload.getTheFile();

  92.             AccordiCooperazioneCore acCore = new AccordiCooperazioneCore();
  93.             ArchiviCore archiviCore = new ArchiviCore(acCore);

  94.             // Preparo il menu
  95.             acHelper.makeMenu();

  96.             // Prendo il nome
  97.             AccordoCooperazione ac = acCore.getAccordoCooperazione(idAccordoLong);
  98.             String titleAS = acHelper.getLabelIdAccordoCooperazione(ac);
  99.             IProtocolFactory<?> pf = ProtocolFactoryManager.getInstance().getProtocolFactoryByOrganizationType(ac.getSoggettoReferente().getTipo());
  100.            
  101.             Documento doc = archiviCore.getDocumento(idAllegatoLong,false);
  102.            
  103.             // Se idhid = null, devo visualizzare la pagina per la
  104.             // modifica dati
  105.             if (acHelper.isEditModeInProgress()) {
  106.                 // setto la barra del titolo
  107.                 List<Parameter> lstParam = new ArrayList<>();
  108.                 lstParam.add(new Parameter(AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE, AccordiCooperazioneCostanti.SERVLET_NAME_ACCORDI_COOPERAZIONE_LIST));
  109.                 lstParam.add(new Parameter(AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE_ALLEGATI_DI+ titleAS,
  110.                         AccordiCooperazioneCostanti.SERVLET_NAME_AC_ALLEGATI_LIST,
  111.                         new Parameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_ID, idAccordo),
  112.                         new Parameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_NOME, ac.getNome())));
  113.                 lstParam.add(new Parameter(nomeDocumento, null));
  114.                
  115.                 ServletUtils.setPageDataTitle(pd, lstParam);
  116.                
  117.                 // preparo i campi
  118.                 List<DataElement> dati = new ArrayList<>();
  119.                 dati.add(ServletUtils.getDataElementForEditModeFinished());

  120.                 dati = acHelper.addAllegatiToDati(TipoOperazione.CHANGE, idAllegato, idAccordo, doc, null,null, dati, ac.getStatoPackage(), true);
  121.                
  122.                 pd.setDati(dati);

  123.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  124.                
  125.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_AC_ALLEGATI,
  126.                         ForwardParams.CHANGE());
  127.             }

  128.             Documento toCheck = new Documento();
  129.             toCheck.setRuolo(RuoliDocumento.valueOf(doc.getRuolo()).toString());
  130.             toCheck.setByteContenuto(ff.getFileData());
  131.             toCheck.setFile(ff.getFileName());
  132.             switch (RuoliDocumento.valueOf(doc.getRuolo())) {
  133.                 case allegato:
  134.                     toCheck.setTipo(ff.getFileName().substring(ff.getFileName().lastIndexOf('.')+1, ff.getFileName().length()));
  135.                     break;

  136.                 case specificaSemiformale:
  137.                     toCheck.setTipo(TipiDocumentoSemiformale.toEnumConstant(tipoFile).getNome());
  138.                     break;
  139.                 default:
  140.                     break;
  141.             }
  142.             toCheck.setIdProprietarioDocumento(ac.getId());
  143.             toCheck.setOraRegistrazione(new Date());
  144.             toCheck.setId(doc.getId());
  145.             // Controlli sui campi immessi
  146.             boolean isOk = archiviHelper.accordiAllegatiCheckData(TipoOperazione.CHANGE,ff,toCheck,ProprietariDocumento.accordoCooperazione, pf);
  147.             if (!isOk) {
  148.                  
  149.                 // setto la barra del titolo
  150.                 List<Parameter> lstParam = new ArrayList<>();
  151.                 lstParam.add(new Parameter(AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE, AccordiCooperazioneCostanti.SERVLET_NAME_ACCORDI_COOPERAZIONE_LIST));
  152.                 lstParam.add(new Parameter(AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE_ALLEGATI_DI+ titleAS,
  153.                         AccordiCooperazioneCostanti.SERVLET_NAME_AC_ALLEGATI_LIST,
  154.                         new Parameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_ID, idAccordo),
  155.                         new Parameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_NOME, ac.getNome())));
  156.                 lstParam.add(new Parameter(nomeDocumento, null));
  157.                
  158.                 ServletUtils.setPageDataTitle(pd, lstParam);

  159.                 // preparo i campi
  160.                 List<DataElement> dati = new ArrayList<>();
  161.              
  162.                 dati.add(ServletUtils.getDataElementForEditModeFinished());
  163.                
  164.                 dati = acHelper.addAllegatiToDati(TipoOperazione.CHANGE, idAllegato, idAccordo, doc, null,null,
  165.                         dati, null, false);
  166.                
  167.                 pd.setDati(dati);

  168.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  169.                
  170.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_AC_ALLEGATI,
  171.                         ForwardParams.CHANGE());
  172.             }

  173.            

  174.             switch (RuoliDocumento.valueOf(doc.getRuolo())) {
  175.                 case allegato:
  176.                     //rimuovo il vecchio doc dalla lista
  177.                     for (int i = 0; i < ac.sizeAllegatoList(); i++) {
  178.                         Documento documento = ac.getAllegato(i);                        
  179.                         if(documento.getId().equals(doc.getId())){
  180.                             ac.removeAllegato(i);
  181.                             break;
  182.                         }
  183.                     }
  184.                     //aggiungo il nuovo
  185.                     ac.addAllegato(toCheck);
  186.                    
  187.                     break;

  188.                 case specificaSemiformale:
  189.                    
  190.                     for (int i = 0; i < ac.sizeSpecificaSemiformaleList(); i++) {
  191.                         Documento documento = ac.getSpecificaSemiformale(i);                        
  192.                         if(documento.getId().equals(doc.getId())){
  193.                             ac.removeSpecificaSemiformale(i);
  194.                             break;
  195.                         }
  196.                     }
  197.                     //aggiungo il nuovo
  198.                     ac.addSpecificaSemiformale(toCheck);
  199.                     break;
  200.                    
  201.                 default:
  202.                     break;
  203.             }
  204.            

  205.             // effettuo le operazioni
  206.             acCore.performUpdateOperation(userLogin, acHelper.smista(), ac);

  207.             // Preparo la lista
  208.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);
  209.            
  210.             List<Documento> lista = acCore.accordiCoopAllegatiList(ac.getId().intValue(), ricerca);

  211.             acHelper.prepareAccordiCoopAllegatiList(ac, ricerca, lista);

  212.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  213.            
  214.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_AC_ALLEGATI,
  215.                     ForwardParams.CHANGE());
  216.         } catch (Exception e) {
  217.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  218.                     AccordiCooperazioneCostanti.OBJECT_NAME_AC_ALLEGATI,
  219.                     ForwardParams.CHANGE());
  220.         }  
  221.     }
  222. }