AccordiCooperazioneAdd.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.Calendar;
  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.openspcoop2.core.id.IDAccordo;
  32. import org.openspcoop2.core.registry.AccordoCooperazione;
  33. import org.openspcoop2.core.registry.IdSoggetto;
  34. import org.openspcoop2.core.registry.Soggetto;
  35. import org.openspcoop2.core.registry.constants.StatiAccordo;
  36. import org.openspcoop2.core.registry.driver.ValidazioneStatoPackageException;
  37. import org.openspcoop2.protocol.engine.ProtocolFactoryManager;
  38. import org.openspcoop2.protocol.sdk.ProtocolException;
  39. import org.openspcoop2.protocol.sdk.constants.ConsoleOperationType;
  40. import org.openspcoop2.protocol.sdk.properties.ProtocolPropertiesUtils;
  41. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  42. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  43. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  44. import org.openspcoop2.web.ctrlstat.servlet.apc.AccordiServizioParteComuneCostanti;
  45. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  46. import org.openspcoop2.web.lib.mvc.Costanti;
  47. import org.openspcoop2.web.lib.mvc.DataElement;
  48. import org.openspcoop2.web.lib.mvc.ForwardParams;
  49. import org.openspcoop2.web.lib.mvc.GeneralData;
  50. import org.openspcoop2.web.lib.mvc.PageData;
  51. import org.openspcoop2.web.lib.mvc.ServletUtils;
  52. import org.openspcoop2.web.lib.mvc.TipoOperazione;

  53. /**
  54.  * accordiCooperazioneAdd
  55.  *
  56.  * @author Andrea Poli (apoli@link.it)
  57.  * @author Stefano Corallo (corallo@link.it)
  58.  * @author Sandra Giangrandi (sandra@link.it)
  59.  * @author $Author$
  60.  * @version $Rev$, $Date$
  61.  *
  62.  */
  63. public final class AccordiCooperazioneAdd extends Action {
  64.    
  65.     @Override
  66.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

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

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

  70.         GeneralHelper generalHelper = new GeneralHelper(session);

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

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

  74.         AccordiCooperazioneAddStrutsBean strutsBean = new AccordiCooperazioneAddStrutsBean();
  75.        
  76.         // Parametri Protocol Properties relativi al tipo di operazione e al tipo di visualizzazione
  77.         strutsBean.consoleOperationType = ConsoleOperationType.ADD;
  78.        
  79.         // Parametri relativi al tipo operazione
  80.         TipoOperazione tipoOp = TipoOperazione.ADD;

  81.         try {
  82.             AccordiCooperazioneHelper acHelper = new AccordiCooperazioneHelper(request, pd, session);
  83.            
  84.             strutsBean.editMode = acHelper.getParameter(Costanti.DATA_ELEMENT_EDIT_MODE_NAME);
  85.             strutsBean.nome = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_NOME);
  86.             strutsBean.descr = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_DESCRIZIONE);
  87.             strutsBean.referente = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_REFERENTE);
  88.             strutsBean.versione = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_VERSIONE);
  89.             strutsBean.tipoProtocollo = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_PROTOCOLLO);
  90.             // patch per version spinner fino a che non si trova un modo piu' elegante
  91.             /*if(ch.core.isBackwardCompatibilityAccordo11()){
  92.                 if("0".equals(strutsBean.versione))
  93.                     strutsBean.versione = "";
  94.             }*/
  95.             String privatoS = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_PRIVATO);
  96.             strutsBean.privato = ServletUtils.isCheckBoxEnabled(privatoS); // privatoS != null && Costanti.CHECK_BOX_ENABLED.equals(privatoS) ? true : false;
  97.             strutsBean.statoPackage = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_STATO);
  98.             String tipoSICA = acHelper.getParameter(AccordiCooperazioneCostanti.PARAMETRO_ACCORDI_COOPERAZIONE_TIPO_SICA);
  99.             if("".equals(tipoSICA))
  100.                 tipoSICA = null;

  101.             AccordiCooperazioneCore acCore = new AccordiCooperazioneCore();
  102.             SoggettiCore soggettiCore = new SoggettiCore(acCore);

  103.             // Preparo il menu
  104.             acHelper.makeMenu();

  105.             // Tipi protocollo supportati
  106.             List<String> listaTipiProtocollo = acCore.getProtocolliByFilter(request, session, true, false);
  107.            
  108.             // primo accesso inizializzo con il protocollo di default
  109.             if(strutsBean.tipoProtocollo == null){
  110.                 strutsBean.tipoProtocollo = acCore.getProtocolloDefault(request, session, listaTipiProtocollo);
  111.             }
  112.            
  113.             if(listaTipiProtocollo.isEmpty()) {
  114.                 pd.setMessage("Non risultano registrati soggetti", Costanti.MESSAGE_TYPE_INFO);
  115.                 pd.disableEditMode();

  116.                 List<DataElement> dati = new ArrayList<>();

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

  118.                 pd.setDati(dati);

  119.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  120.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_ACCORDI_COOPERAZIONE,
  121.                         ForwardParams.ADD());
  122.             }

  123.             //Carico la lista dei tipi di soggetti gestiti dal protocollo
  124.             List<String> tipiSoggettiGestitiProtocollo = soggettiCore.getTipiSoggettiGestitiProtocollo(strutsBean.tipoProtocollo);

  125.             // Prendo la lista di provider e la metto in un array
  126.             String[] providersList = null;
  127.             String[] providersListLabel = null;

  128.             // Provider
  129.             List<Soggetto> lista = null;
  130.             if(acCore.isVisioneOggettiGlobale(userLogin)){
  131.                 lista = soggettiCore.soggettiRegistroList(null, new ConsoleSearch(true));
  132.             }else{
  133.                 lista = soggettiCore.soggettiRegistroList(userLogin, new ConsoleSearch(true));
  134.             }

  135.             List<String> soggettiListTmp = new ArrayList<>();
  136.             List<String> soggettiListLabelTmp = new ArrayList<>();
  137.             soggettiListTmp.add("-");
  138.             soggettiListLabelTmp.add("-");

  139.             if (!lista.isEmpty()) {
  140.                 for (Soggetto soggetto : lista) {
  141.                     if(tipiSoggettiGestitiProtocollo.contains(soggetto.getTipo())){
  142.                         soggettiListTmp.add(soggetto.getId().toString());
  143.                         soggettiListLabelTmp.add(acHelper.getLabelNomeSoggetto(strutsBean.tipoProtocollo, soggetto.getTipo() , soggetto.getNome()));
  144.                     }
  145.                 }
  146.             }
  147.             providersList = soggettiListTmp.toArray(new String[1]);
  148.             providersListLabel = soggettiListLabelTmp.toArray(new String[1]);

  149.             strutsBean.protocolFactory = ProtocolFactoryManager.getInstance().getProtocolFactoryByName(strutsBean.tipoProtocollo);
  150.             strutsBean.consoleDynamicConfiguration =  strutsBean.protocolFactory.createDynamicConfigurationConsole();
  151.             strutsBean.registryReader = soggettiCore.getRegistryReader(strutsBean.protocolFactory);
  152.             strutsBean.configRegistryReader = soggettiCore.getConfigIntegrationReader(strutsBean.protocolFactory);
  153.            
  154.             // ID Accordo Null per default
  155.             IDAccordo idAc = null;
  156.             strutsBean.consoleConfiguration = strutsBean.consoleDynamicConfiguration.getDynamicConfigAccordoCooperazione(strutsBean.consoleOperationType, acHelper,
  157.                     strutsBean.registryReader, strutsBean.configRegistryReader, idAc );
  158.             strutsBean.protocolProperties = acHelper.estraiProtocolPropertiesDaRequest(strutsBean.consoleConfiguration, strutsBean.consoleOperationType);

  159.             String postBackElementName = acHelper.getParameter(Costanti.POSTBACK_ELEMENT_NAME);

  160.             // Controllo se ho modificato il protocollo, resetto il referente
  161.             if(postBackElementName != null ){
  162.                 if(postBackElementName.equalsIgnoreCase(AccordiServizioParteComuneCostanti.PARAMETRO_APC_PROTOCOLLO)){
  163.                     strutsBean.referente = null;
  164.                     acHelper.deleteProtocolPropertiesBinaryParameters();
  165.                 }
  166.             }


  167.             // Se nome = null, devo visualizzare la pagina per l'inserimento
  168.             // dati
  169.             if (ServletUtils.isEditModeInProgress(strutsBean.editMode)) {
  170.                 // setto la barra del titolo
  171.                
  172.                 // setto la barra del titolo
  173.                 ServletUtils.setPageDataTitle_ServletAdd(pd, AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE,
  174.                         AccordiCooperazioneCostanti.SERVLET_NAME_ACCORDI_COOPERAZIONE_LIST);

  175.                 // preparo i campi
  176.                 List<DataElement> dati = new ArrayList<>();

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

  178.                 if(acHelper.isShowGestioneWorkflowStatoDocumenti()){
  179.                     if(strutsBean.statoPackage==null)
  180.                         strutsBean.statoPackage=StatiAccordo.bozza.toString();
  181.                 }else{
  182.                     strutsBean.statoPackage=StatiAccordo.finale.toString();
  183.                 }

  184.                 strutsBean.versione="1";

  185.                 if(strutsBean.nome == null)
  186.                     strutsBean.nome = "";

  187.                 if(strutsBean.descr == null)
  188.                     strutsBean.descr = "";

  189.                 if(strutsBean.referente == null)
  190.                     strutsBean.referente = "";

  191.                 strutsBean.consoleDynamicConfiguration.updateDynamicConfigAccordoCooperazione(strutsBean.consoleConfiguration,
  192.                         strutsBean.consoleOperationType, acHelper, strutsBean.protocolProperties,
  193.                         strutsBean.registryReader, strutsBean.configRegistryReader, idAc);

  194.                 dati = acHelper.addAccordiCooperazioneToDati(dati, strutsBean.nome, strutsBean.descr, "0", tipoOp, strutsBean.referente,
  195.                         strutsBean.versione, providersList, providersListLabel, false,strutsBean.statoPackage,strutsBean.statoPackage, strutsBean.tipoProtocollo, listaTipiProtocollo,false);

  196.                 // aggiunta campi custom
  197.                 dati = acHelper.addProtocolPropertiesToDatiRegistry(dati, strutsBean.consoleConfiguration,strutsBean.consoleOperationType, strutsBean.protocolProperties);

  198.                 pd.setDati(dati);

  199.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  200.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_ACCORDI_COOPERAZIONE,
  201.                         ForwardParams.ADD());
  202.             }

  203.             // Controlli sui campi immessi
  204.             boolean isOk = acHelper.accordiCooperazioneCheckData(tipoOp, strutsBean.nome, strutsBean.descr, "0",
  205.                     strutsBean.referente, strutsBean.versione, strutsBean.privato, null);

  206.             // updateDynamic
  207.             if(isOk) {
  208.                 strutsBean.consoleDynamicConfiguration.updateDynamicConfigAccordoCooperazione(strutsBean.consoleConfiguration,
  209.                         strutsBean.consoleOperationType, acHelper, strutsBean.protocolProperties,
  210.                         strutsBean.registryReader, strutsBean.configRegistryReader, idAc);
  211.             }
  212.            
  213.             // Validazione base dei parametri custom
  214.             if(isOk){
  215.                 try{
  216.                     acHelper.validaProtocolProperties(strutsBean.consoleConfiguration, strutsBean.consoleOperationType, strutsBean.protocolProperties);
  217.                 }catch(ProtocolException e){
  218.                     ControlStationCore.getLog().error(e.getMessage(),e);
  219.                     pd.setMessage(e.getMessage());
  220.                     isOk = false;
  221.                 }
  222.             }

  223.             // Valido i parametri custom se ho gia' passato tutta la validazione prevista
  224.             if(isOk){
  225.                 try{
  226.                     idAc = acHelper.getIDAccordoFromValues(strutsBean.nome, strutsBean.referente, strutsBean.versione);
  227.                     //validazione campi dinamici
  228.                     strutsBean.consoleDynamicConfiguration.validateDynamicConfigCooperazione(strutsBean.consoleConfiguration, strutsBean.consoleOperationType, acHelper, strutsBean.protocolProperties,
  229.                             strutsBean.registryReader, strutsBean.configRegistryReader, idAc);
  230.                 }catch(ProtocolException e){
  231.                     ControlStationCore.getLog().error(e.getMessage(),e);
  232.                     pd.setMessage(e.getMessage());
  233.                     isOk = false;
  234.                 }
  235.             }

  236.             if (!isOk) {
  237.                 // setto la barra del titolo
  238.                 ServletUtils.setPageDataTitle_ServletAdd(pd, AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE,
  239.                         AccordiCooperazioneCostanti.SERVLET_NAME_ACCORDI_COOPERAZIONE_LIST);

  240.                 // preparo i campi
  241.                 List<DataElement> dati = new ArrayList<>();

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

  243.                 strutsBean.consoleDynamicConfiguration.updateDynamicConfigAccordoCooperazione(strutsBean.consoleConfiguration,
  244.                         strutsBean.consoleOperationType, acHelper, strutsBean.protocolProperties,
  245.                         strutsBean.registryReader, strutsBean.configRegistryReader, idAc);

  246.                 dati = acHelper.addAccordiCooperazioneToDati(dati, strutsBean.nome, strutsBean.descr, "0", tipoOp,
  247.                         strutsBean.referente, strutsBean.versione, providersList, providersListLabel, strutsBean.privato,strutsBean.statoPackage,strutsBean.statoPackage, strutsBean.tipoProtocollo, listaTipiProtocollo,false);

  248.                 // aggiunta campi custom
  249.                 dati = acHelper.addProtocolPropertiesToDatiRegistry(dati, strutsBean.consoleConfiguration,strutsBean.consoleOperationType, strutsBean.protocolProperties);

  250.                 pd.setDati(dati);

  251.                 ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  252.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_ACCORDI_COOPERAZIONE,
  253.                         ForwardParams.ADD());
  254.             }

  255.             // Inserisco l'accordo nel db
  256.             AccordoCooperazione ac = new AccordoCooperazione();
  257.             ac.setNome(strutsBean.nome);
  258.             ac.setDescrizione(strutsBean.descr);
  259.             ac.setOraRegistrazione(Calendar.getInstance().getTime());
  260.             if(strutsBean.referente!=null && !"".equals(strutsBean.referente) && !"-".equals(strutsBean.referente)){
  261.                 int idRef = 0;
  262.                 try {
  263.                     idRef = Integer.parseInt(strutsBean.referente);
  264.                 } catch (Exception e) {
  265.                     // ignore
  266.                 }
  267.                 if (idRef != 0) {
  268.                     int idReferente = Integer.parseInt(strutsBean.referente);
  269.                     Soggetto s = soggettiCore.getSoggettoRegistro(idReferente);        
  270.                     IdSoggetto acsr = new IdSoggetto();
  271.                     acsr.setTipo(s.getTipo());
  272.                     acsr.setNome(s.getNome());
  273.                     ac.setSoggettoReferente(acsr);
  274.                 }
  275.             }else{
  276.                 ac.setSoggettoReferente(null);
  277.             }
  278.             if(strutsBean.versione!=null){
  279.                 ac.setVersione(Integer.parseInt(strutsBean.versione));
  280.             }
  281.             ac.setPrivato(strutsBean.privato ? Boolean.TRUE : Boolean.FALSE);
  282.             ac.setSuperUser(userLogin);

  283.             // stato    
  284.             ac.setStatoPackage(strutsBean.statoPackage);

  285.             // Check stato
  286.             if(acHelper.isShowGestioneWorkflowStatoDocumenti()){

  287.                 try{
  288.                     acCore.validaStatoAccordoCooperazione(ac);
  289.                 }catch(ValidazioneStatoPackageException validazioneException){

  290.                     // Setto messaggio di errore
  291.                     pd.setMessage(validazioneException.toString());

  292.                     // setto la barra del titolo
  293.                     ServletUtils.setPageDataTitle_ServletAdd(pd, AccordiCooperazioneCostanti.LABEL_ACCORDI_COOPERAZIONE,
  294.                             AccordiCooperazioneCostanti.SERVLET_NAME_ACCORDI_COOPERAZIONE_LIST);

  295.                     // preparo i campi
  296.                     List<DataElement> dati = new ArrayList<>();

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

  298.                     strutsBean.consoleDynamicConfiguration.updateDynamicConfigAccordoCooperazione(strutsBean.consoleConfiguration,
  299.                             strutsBean.consoleOperationType, acHelper, strutsBean.protocolProperties,
  300.                             strutsBean.registryReader, strutsBean.configRegistryReader, idAc);

  301.                     dati = acHelper.addAccordiCooperazioneToDati(dati, strutsBean.nome, strutsBean.descr, "0", tipoOp,
  302.                             strutsBean.referente, strutsBean.versione, providersList, providersListLabel, strutsBean.privato,strutsBean.statoPackage,strutsBean.statoPackage, strutsBean.tipoProtocollo, listaTipiProtocollo,false);

  303.                     // aggiunta campi custom
  304.                     dati = acHelper.addProtocolPropertiesToDatiRegistry(dati, strutsBean.consoleConfiguration,strutsBean.consoleOperationType, strutsBean.protocolProperties);

  305.                     pd.setDati(dati);

  306.                     ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  307.                     return ServletUtils.getStrutsForwardEditModeCheckError(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_ACCORDI_COOPERAZIONE,
  308.                             ForwardParams.ADD());
  309.                 }
  310.             }

  311.             //imposto properties custom
  312.             ac.setProtocolPropertyList(ProtocolPropertiesUtils.toProtocolPropertiesRegistry(strutsBean.protocolProperties, strutsBean.consoleOperationType,null));

  313.             // effettuo le operazioni
  314.             acCore.performCreateOperation(userLogin, acHelper.smista(), ac);
  315.            
  316.             // cancello i file temporanei
  317.             acHelper.deleteBinaryProtocolPropertiesTmpFiles(strutsBean.protocolProperties);

  318.             // Preparo la lista
  319.             ConsoleSearch ricerca = (ConsoleSearch) ServletUtils.getSearchObjectFromSession(request, session, ConsoleSearch.class);

  320.             List<AccordoCooperazione> listaAC = null;
  321.             if(acCore.isVisioneOggettiGlobale(userLogin)){
  322.                 listaAC = acCore.accordiCooperazioneList(null, ricerca);
  323.             }else{
  324.                 listaAC = acCore.accordiCooperazioneList(userLogin, ricerca);
  325.             }

  326.             acHelper.prepareAccordiCooperazioneList(listaAC, ricerca);

  327.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  328.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, AccordiCooperazioneCostanti.OBJECT_NAME_ACCORDI_COOPERAZIONE,
  329.                     ForwardParams.ADD());
  330.         } catch (Exception e) {
  331.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  332.                     AccordiCooperazioneCostanti.OBJECT_NAME_ACCORDI_COOPERAZIONE,
  333.                     ForwardParams.ADD());
  334.         }  
  335.     }
  336. }