UtentiSoggettiAdd.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.utenti;

  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  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.commons.Filtri;
  32. import org.openspcoop2.core.commons.Liste;
  33. import org.openspcoop2.core.id.IDSoggetto;
  34. import org.openspcoop2.core.registry.Soggetto;
  35. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  36. import org.openspcoop2.web.ctrlstat.core.ConsoleSearch;
  37. import org.openspcoop2.web.ctrlstat.costanti.CostantiControlStation;
  38. import org.openspcoop2.web.ctrlstat.servlet.GeneralHelper;
  39. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCore;
  40. import org.openspcoop2.web.ctrlstat.servlet.soggetti.SoggettiCostanti;
  41. import org.openspcoop2.web.lib.mvc.DataElement;
  42. import org.openspcoop2.web.lib.mvc.ForwardParams;
  43. import org.openspcoop2.web.lib.mvc.GeneralData;
  44. import org.openspcoop2.web.lib.mvc.MessageType;
  45. import org.openspcoop2.web.lib.mvc.PageData;
  46. import org.openspcoop2.web.lib.mvc.Parameter;
  47. import org.openspcoop2.web.lib.mvc.ServletUtils;
  48. import org.openspcoop2.web.lib.mvc.TipoOperazione;
  49. import org.openspcoop2.web.lib.users.dao.User;

  50. /**    
  51.  * UtentiSoggettiAdd
  52.  *
  53.  * @author Pintori Giuliano (pintori@link.it)
  54.  * @author $Author$
  55.  * @version $Rev$, $Date$
  56.  */
  57. public class UtentiSoggettiAdd extends Action {

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

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

  61.         // Inizializzo PageData
  62.         PageData pd = new PageData();

  63.         GeneralHelper generalHelper = new GeneralHelper(session);

  64.         // Inizializzo GeneralData
  65.         GeneralData gd = generalHelper.initGeneralData(request);

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

  67.         try {
  68.             UtentiHelper utentiHelper = new UtentiHelper(request, pd, session);
  69.             String nomesu = utentiHelper.getParameter(UtentiCostanti.PARAMETRO_UTENTI_USERNAME);
  70.             String soggetto = utentiHelper.getParameter(UtentiCostanti.PARAMETRO_UTENTI_SOGGETTO);
  71.             String protocollo = utentiHelper.getParameter(UtentiCostanti.PARAMETRO_UTENTI_PROTOCOLLO);

  72.             // Preparo il menu
  73.             utentiHelper.makeMenu();

  74.             UtentiCore utentiCore = new UtentiCore();
  75.             SoggettiCore soggettiCore = new SoggettiCore(utentiCore);
  76.             User user = utentiCore.getUser(nomesu);

  77.             ConsoleSearch searchSoggetti = new ConsoleSearch(true);
  78.             List<String> protocolli = user.getProtocolliSupportati();
  79.            
  80.             if(protocollo == null &&
  81.                 protocolli!=null && !protocolli.isEmpty()) {
  82.                 protocollo = utentiCore.getProtocolloDefault(request, null, protocolli);
  83.             }
  84.            
  85.             searchSoggetti.addFilter(Liste.SOGGETTI, Filtri.FILTRO_PROTOCOLLO, protocollo);
  86.             searchSoggetti.addFilter(Liste.SOGGETTI, Filtri.FILTRO_DOMINIO, SoggettiCostanti.SOGGETTO_DOMINIO_OPERATIVO_VALUE);
  87.             List<Soggetto> listaSoggettiRegistro = soggettiCore.soggettiRegistroList(null, searchSoggetti);

  88.             List<Soggetto> listaSoggettiNonUtilizzati = new ArrayList<>();

  89.             for (Soggetto sog : listaSoggettiRegistro) {
  90.                 boolean found = false;
  91.                 for (IDSoggetto idSog : user.getSoggetti()) {
  92.                     if(sog.getTipo().equals(idSog.getTipo()) && sog.getNome().equals(idSog.getNome()) && sog.getIdentificativoPorta().equals(idSog.getCodicePorta())) {
  93.                         found = true;
  94.                         break;
  95.                     }
  96.                 }  
  97.                
  98.                 if(!found) {
  99.                     listaSoggettiNonUtilizzati.add(sog);
  100.                 }
  101.             }

  102.             String [] soggettiLabels = new String [listaSoggettiNonUtilizzati.size() + 1];
  103.             String [] soggettiValues = new String [listaSoggettiNonUtilizzati.size() + 1];

  104.             soggettiLabels[0] = CostantiControlStation.LABEL_LIST_VALORE_NON_PRESENTE;
  105.             soggettiValues[0] = CostantiControlStation.DEFAULT_VALUE_NON_SELEZIONATO;

  106.             for (int i = 0; i < listaSoggettiNonUtilizzati.size(); i++) {
  107.                 Soggetto soggetto2 = listaSoggettiNonUtilizzati.get(i);
  108.                 String protocolloTmp = soggettiCore.getProtocolloAssociatoTipoSoggetto(soggetto2.getTipo());
  109.                 soggettiLabels[(i+1)] = utentiHelper.getLabelNomeSoggetto(protocolloTmp, soggetto2.getTipo(), soggetto2.getNome());
  110.                 soggettiValues[(i+1)] = soggetto2.getId() + "";
  111.             }

  112.             List<Parameter> lstParam = new ArrayList<>();
  113.             lstParam.add(new Parameter(UtentiCostanti.LABEL_UTENTI ,UtentiCostanti.SERVLET_NAME_UTENTI_LIST));
  114.             lstParam.add(new Parameter(nomesu, UtentiCostanti.SERVLET_NAME_UTENTI_CHANGE, new Parameter(UtentiCostanti.PARAMETRO_UTENTI_USERNAME, nomesu)));
  115.             lstParam.add(new Parameter(UtentiCostanti.LABEL_UTENTI_SOGGETTI, UtentiCostanti.SERVLET_NAME_UTENTI_SOGGETTI_LIST, new Parameter(UtentiCostanti.PARAMETRO_UTENTI_USERNAME, nomesu)));
  116.             lstParam.add(ServletUtils.getParameterAggiungi());

  117.             if(utentiHelper.isEditModeInProgress()){
  118.                 ServletUtils.setPageDataTitle(pd,lstParam);
  119.                
  120.                 // preparo i campi
  121.                 List<DataElement> dati = new ArrayList<>();

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

  123.                 if(soggetto==null){
  124.                     soggetto = "";
  125.                 }
  126.                
  127.                 if(listaSoggettiNonUtilizzati.isEmpty()){
  128.                     if(protocolli.size() == 1) {
  129.                         pd.setMessage(UtentiCostanti.LABEL_UTENTI_SOGGETTI_DISPONIBILI_ESAURITI, MessageType.INFO);
  130.                         pd.disableEditMode();
  131.                         pd.setDati(dati);
  132.                         ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);
  133.                         return ServletUtils.getStrutsForwardEditModeInProgress(mapping, UtentiCostanti.OBJECT_NAME_UTENTI_SOGGETTI,ForwardParams.ADD());
  134.                     } else {
  135.                         String msf = MessageFormat.format(UtentiCostanti.LABEL_UTENTI_SOGGETTI_DISPONIBILI_ESAURITI_PER_LA_MODALITA_XX, utentiHelper.getLabelProtocollo(protocollo));
  136.                         pd.setMessage(msf, MessageType.INFO);
  137.                         pd.disableOnlyButton();
  138.                     }
  139.                 }
  140.                
  141.                 utentiHelper.addUtentiSoggettiToDati(dati, TipoOperazione.ADD,nomesu,soggetto,soggettiValues,soggettiLabels, protocolli,protocollo);

  142.                 pd.setDati(dati);

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

  144.                 return ServletUtils.getStrutsForwardEditModeInProgress(mapping, UtentiCostanti.OBJECT_NAME_UTENTI_SOGGETTI,ForwardParams.ADD());
  145.             }

  146.             // Controlli sui campi immessi
  147.             boolean isOk = utentiHelper.utentiSoggettiCheckData(TipoOperazione.ADD,nomesu,soggetto);
  148.             if (!isOk) {
  149.                 ServletUtils.setPageDataTitle(pd,lstParam);

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

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

  153.                 utentiHelper.addUtentiSoggettiToDati(dati, TipoOperazione.ADD,nomesu,soggetto,soggettiValues,soggettiLabels,protocolli,protocollo);

  154.                 pd.setDati(dati);

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

  156.                 return ServletUtils.getStrutsForwardEditModeCheckError(mapping, UtentiCostanti.OBJECT_NAME_UTENTI_SOGGETTI,ForwardParams.ADD());
  157.             }

  158.             // aggiungo il nuovo soggetto alla lista
  159.             Long newId = Long.parseLong(soggetto);
  160.             IDSoggetto idSoggetto = soggettiCore.getIdSoggettoRegistro(newId);
  161.             user.getSoggetti().add(idSoggetto);

  162.             // salvataggio sul db
  163.             utentiCore.performUpdateOperation(userLogin, utentiHelper.smista(), user);

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

  166.             int idLista = Liste.UTENTI_SOGGETTI;

  167.             ricerca = utentiHelper.checkSearchParameters(idLista, ricerca);

  168.             List<IDSoggetto> lista = utentiCore.utentiSoggettiList(nomesu, ricerca);

  169.             utentiHelper.prepareUtentiSoggettiList(ricerca, lista, user);

  170.             ServletUtils.setSearchObjectIntoSession(request, session, ricerca);
  171.             ServletUtils.setGeneralAndPageDataIntoSession(request, session, gd, pd);

  172.             return ServletUtils.getStrutsForwardEditModeFinished(mapping, UtentiCostanti.OBJECT_NAME_UTENTI_SOGGETTI, ForwardParams.ADD());

  173.         } catch (Exception e) {
  174.             return ServletUtils.getStrutsForwardError(ControlStationCore.getLog(), e, pd, request, session, gd, mapping,
  175.                     UtentiCostanti.OBJECT_NAME_UTENTI_SOGGETTI, ForwardParams.ADD());
  176.         }
  177.     }

  178. }