ScopeCore.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.scope;

  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.IDScope;
  29. import org.openspcoop2.core.registry.Scope;
  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.  * ScopeCore
  36.  *
  37.  * @author Poli Andrea (apoli@link.it)
  38.  * @author Giuliano Pintori (pintori@link.it)
  39.  * @author $Author$
  40.  * @version $Rev$, $Date$
  41.  */
  42. public class ScopeCore extends ControlStationCore {

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

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

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

  79.             return driver.getDriverRegistroServiziDB().getScope(id);

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

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

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

  97.             IDScope idScope = new IDScope(nome);
  98.             return driver.getDriverRegistroServiziDB().getScope(idScope);

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

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

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

  120.             IDScope idScope = new IDScope(nome);
  121.             return driver.getDriverRegistroServiziDB().existsScope(idScope);

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

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

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

  139.             return driver.isScopeInUso(new IDScope(scope), whereIsInUso, normalizeObjectIds);

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

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

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

  157.             return driver.isScopeConfigInUso(new IDScope(scope), whereIsInUso, normalizeObjectIds);

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

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