RuoliCore.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.ruoli;

  21. import java.sql.Connection;
  22. import java.util.EnumMap;
  23. import java.util.List;
  24. import java.util.Map;

  25. import org.openspcoop2.core.commons.ErrorsHandlerCostant;
  26. import org.openspcoop2.core.commons.ISearch;
  27. import org.openspcoop2.core.config.driver.DriverConfigurazioneException;
  28. import org.openspcoop2.core.id.IDRuolo;
  29. import org.openspcoop2.core.registry.Ruolo;
  30. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  31. import org.openspcoop2.protocol.engine.utils.DBOggettiInUsoUtils;
  32. import org.openspcoop2.web.ctrlstat.core.ControlStationCore;
  33. import org.openspcoop2.web.ctrlstat.driver.DriverControlStationDB;

  34. /**
  35.  * RuoliCore
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author $Author$
  39.  * @version $Rev$, $Date$
  40.  */
  41. public class RuoliCore extends ControlStationCore {

  42.     public RuoliCore() throws Exception {
  43.         super();
  44.     }
  45.     public RuoliCore(ControlStationCore core) throws Exception {
  46.         super(core);
  47.     }
  48.    
  49.    
  50.    
  51.     public List<Ruolo> ruoliList(String superuser,ISearch ricerca) throws DriverConfigurazioneException {
  52.         Connection con = null;
  53.         String nomeMetodo = "ruoliList";
  54.         DriverControlStationDB driver = null;

  55.         try {
  56.             // prendo una connessione
  57.             con = ControlStationCore.dbM.getConnection();
  58.             // istanzio il driver
  59.             driver = new DriverControlStationDB(con, null, this.tipoDB);
  60.             return driver.getDriverRegistroServiziDB().ruoliList(superuser, ricerca);
  61.         } catch (Exception e) {
  62.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  63.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo,  e),e);
  64.         } finally {
  65.             ControlStationCore.dbM.releaseConnection(con);
  66.         }
  67.     }
  68.    
  69.     public Ruolo getRuolo(long id) throws DriverConfigurazioneException {
  70.         Connection con = null;
  71.         String nomeMetodo = "getRuolo";
  72.         DriverControlStationDB driver = null;

  73.         try {
  74.             // prendo una connessione
  75.             con = ControlStationCore.dbM.getConnection();
  76.             // istanzio il driver
  77.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  78.             return driver.getDriverRegistroServiziDB().getRuolo(id);

  79.         } catch (Exception e) {
  80.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  81.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo,  e),e);
  82.         } finally {
  83.             ControlStationCore.dbM.releaseConnection(con);
  84.         }

  85.     }
  86.    
  87.     public Ruolo getRuolo(String nome) throws DriverConfigurazioneException {
  88.         Connection con = null;
  89.         String nomeMetodo = "getRuolo";
  90.         DriverControlStationDB driver = null;

  91.         try {
  92.             // prendo una connessione
  93.             con = ControlStationCore.dbM.getConnection();
  94.             // istanzio il driver
  95.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  96.             IDRuolo idRuolo = new IDRuolo(nome);
  97.             return driver.getDriverRegistroServiziDB().getRuolo(idRuolo);

  98.         } catch (DriverRegistroServiziNotFound e) {
  99.             // Lasciare DEBUG, usato anche in servizio API RS
  100.             ControlStationCore.logDebug("[ControlStationCore::" + nomeMetodo + "] NotFound :" + e.getMessage(), e);
  101.             throw new DriverConfigurazioneException("[ControlStationCore::" + nomeMetodo + "] NotFound :" + e.getMessage(),e);
  102.         } catch (Exception e) {
  103.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  104.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo,  e),e);
  105.         } finally {
  106.             ControlStationCore.dbM.releaseConnection(con);
  107.         }

  108.     }
  109.    
  110.     public boolean existsRuolo(String nome) throws DriverConfigurazioneException {
  111.         Connection con = null;
  112.         String nomeMetodo = "existsRuolo";
  113.         DriverControlStationDB driver = null;

  114.         try {
  115.             // prendo una connessione
  116.             con = ControlStationCore.dbM.getConnection();
  117.             // istanzio il driver
  118.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  119.             IDRuolo idRuolo = new IDRuolo(nome);
  120.             return driver.getDriverRegistroServiziDB().existsRuolo(idRuolo);

  121.         } catch (Exception e) {
  122.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  123.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo,  e),e);
  124.         } finally {
  125.             ControlStationCore.dbM.releaseConnection(con);
  126.         }

  127.     }
  128.    
  129.     public boolean isRuoloInUso(String ruolo, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverConfigurazioneException {
  130.         Connection con = null;
  131.         String nomeMetodo = "isRuoloInUso";
  132.         DriverControlStationDB driver = null;

  133.         try {
  134.             // prendo una connessione
  135.             con = ControlStationCore.dbM.getConnection();
  136.             // istanzio il driver
  137.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  138.             return driver.isRuoloInUso(new IDRuolo(ruolo), whereIsInUso, normalizeObjectIds);

  139.         } catch (Exception e) {
  140.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  141.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo,  e),e);
  142.         } finally {
  143.             ControlStationCore.dbM.releaseConnection(con);
  144.         }

  145.     }
  146.    
  147.     public boolean isRuoloConfigInUso(String ruolo, Map<ErrorsHandlerCostant, List<String>> whereIsInUso, boolean normalizeObjectIds) throws DriverConfigurazioneException {
  148.         Connection con = null;
  149.         String nomeMetodo = "isRuoloConfigInUso";
  150.         DriverControlStationDB driver = null;

  151.         try {
  152.             // prendo una connessione
  153.             con = ControlStationCore.dbM.getConnection();
  154.             // istanzio il driver
  155.             driver = new DriverControlStationDB(con, null, this.tipoDB);

  156.             return driver.isRuoloConfigInUso(new IDRuolo(ruolo), whereIsInUso, normalizeObjectIds);

  157.         } catch (Exception e) {
  158.             ControlStationCore.logError(getPrefixError(nomeMetodo,  e), e);
  159.             throw new DriverConfigurazioneException(getPrefixError(nomeMetodo,  e),e);
  160.         } finally {
  161.             ControlStationCore.dbM.releaseConnection(con);
  162.         }

  163.     }
  164.    
  165.     public String getDettagliRuoloInUso(IDRuolo ruolo) throws DriverConfigurazioneException {
  166.         EnumMap<ErrorsHandlerCostant, List<String>> whereIsInUso = new EnumMap<>(ErrorsHandlerCostant.class);
  167.         boolean normalizeObjectIds = true;
  168.         boolean saInUso  = this.isRuoloInUso(ruolo.getNome(), whereIsInUso, normalizeObjectIds );
  169.        
  170.         StringBuilder inUsoMessage = new StringBuilder();
  171.         if(saInUso) {
  172.             String s = DBOggettiInUsoUtils.toString(ruolo, whereIsInUso, false, "\n");
  173.             if(s!=null && s.startsWith("\n") && s.length()>1) {
  174.                 s = s.substring(1);
  175.             }
  176.             inUsoMessage.append(s);
  177.             inUsoMessage.append("\n");
  178.         } else {
  179.             inUsoMessage.append(RuoliCostanti.LABEL_IN_USO_BODY_HEADER_NESSUN_RISULTATO);
  180.         }
  181.        
  182.         return inUsoMessage.toString();
  183.     }
  184. }