DriverRegistroServiziDB_documentiDriver.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.core.registry.driver.db;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.util.ArrayList;
  25. import java.util.List;

  26. import org.openspcoop2.core.commons.DBUtils;
  27. import org.openspcoop2.core.commons.ISearch;
  28. import org.openspcoop2.core.commons.Liste;
  29. import org.openspcoop2.core.constants.CostantiDB;
  30. import org.openspcoop2.core.id.IDAccordo;
  31. import org.openspcoop2.core.id.IDServizio;
  32. import org.openspcoop2.core.registry.Documento;
  33. import org.openspcoop2.core.registry.constants.ProprietariDocumento;
  34. import org.openspcoop2.core.registry.constants.RuoliDocumento;
  35. import org.openspcoop2.core.registry.constants.TipiDocumentoLivelloServizio;
  36. import org.openspcoop2.core.registry.constants.TipiDocumentoSemiformale;
  37. import org.openspcoop2.core.registry.constants.TipiDocumentoSicurezza;
  38. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  39. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  40. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  41. import org.openspcoop2.utils.sql.ISQLQueryObject;
  42. import org.openspcoop2.utils.sql.SQLObjectFactory;

  43. /**
  44.  * DriverRegistroServiziDB_scopeDriver
  45.  *
  46.  *
  47.  * @author Sandra Giangrandi (sandra@link.it)
  48.  * @author Stefano Corallo (corallo@link.it)
  49.  * @author $Author$
  50.  * @version $Rev$, $Date$
  51.  */
  52. public class DriverRegistroServiziDB_documentiDriver {

  53.     private DriverRegistroServiziDB driver = null;
  54.    
  55.     protected DriverRegistroServiziDB_documentiDriver(DriverRegistroServiziDB driver) {
  56.         this.driver = driver;
  57.     }
  58.    
  59.     protected boolean existsDocumento(String nome, String tipo, String ruolo, long idProprietario, ProprietariDocumento proprietarioDocumento) throws DriverRegistroServiziException {

  60.         boolean exist = false;
  61.         Connection connection;
  62.         PreparedStatement stm = null;
  63.         ResultSet rs = null;
  64.         if (this.driver.atomica) {
  65.             try {
  66.                 connection = this.driver.getConnectionFromDatasource("existsDocumento");
  67.             } catch (Exception e) {
  68.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::existsDocumento] Exception accedendo al datasource :" + e.getMessage(),e);

  69.             }

  70.         } else
  71.             connection = this.driver.globalConnection;

  72.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  73.         try {
  74.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  75.             sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  76.             sqlQueryObject.addSelectField("*");
  77.             sqlQueryObject.addWhereCondition("id_proprietario = ?");
  78.             sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  79.             sqlQueryObject.addWhereCondition("nome = ?");
  80.             if(tipo!=null) {
  81.                 sqlQueryObject.addWhereCondition("tipo = ?");
  82.             }
  83.             if(ruolo!=null) {
  84.                 sqlQueryObject.addWhereCondition("ruolo = ?");
  85.             }
  86.             sqlQueryObject.setANDLogicOperator(true);
  87.             String sqlQuery = sqlQueryObject.createSQLQuery();
  88.             stm = connection.prepareStatement(sqlQuery);
  89.             int index = 1;
  90.             stm.setLong(index++, idProprietario);
  91.             stm.setString(index++, proprietarioDocumento.toString());
  92.             stm.setString(index++, nome);
  93.             if(tipo!=null) {
  94.                 stm.setString(index++, tipo);
  95.             }
  96.             if(ruolo!=null) {
  97.                 stm.setString(index++, ruolo);
  98.             }
  99.             rs = stm.executeQuery();
  100.             if (rs.next())
  101.                 exist = true;
  102.             rs.close();
  103.             stm.close();
  104.         } catch (Exception e) {
  105.             exist = false;
  106.             throw new DriverRegistroServiziException(e.getMessage(),e);
  107.         } finally {

  108.             //Chiudo statement and resultset
  109.             JDBCUtilities.closeResources(rs, stm);

  110.             this.driver.closeConnection(connection);
  111.         }

  112.         return exist;
  113.     }

  114.     protected Documento getDocumento(String nome, String tipo, String ruolo, long idProprietario,boolean readBytes,ProprietariDocumento tipoProprietario) throws DriverRegistroServiziException, DriverRegistroServiziNotFound {
  115.         String nomeMetodo = "getDocumento";

  116.         Connection con = null;

  117.         if (this.driver.atomica) {
  118.             try {
  119.                 con = this.driver.getConnectionFromDatasource("getDocumento");

  120.             } catch (Exception e) {
  121.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(), e);

  122.             }

  123.         } else {
  124.             con = this.driver.globalConnection;
  125.         }

  126.         try {
  127.             String tipoProprietarioAsString = null;
  128.             if(tipoProprietario!=null){
  129.                 tipoProprietarioAsString = tipoProprietario.toString();
  130.             }
  131.             long idDoc = DBUtils.getIdDocumento(nome, tipo, ruolo, idProprietario,con,this.driver.tipoDB,tipoProprietarioAsString);
  132.             if(idDoc <= 0 ) {
  133.                 throw new DriverRegistroServiziNotFound("Documento richiesto non esistente (nome:"+nome+", tipo:"+tipo+", ruolo:"+ruolo+", idProprietario:"+idProprietario+", tipoProprietario:"+tipoProprietarioAsString+")");
  134.             }
  135.             return DriverRegistroServiziDB_documentiLIB.getDocumento(idDoc, readBytes, con, this.driver.tipoDB);

  136.         }
  137.         catch (DriverRegistroServiziNotFound se) {
  138.             throw se;
  139.         }
  140.         catch (Exception se) {
  141.             throw new DriverRegistroServiziException("[DriverRegistroServiziException::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  142.         } finally {
  143.             this.driver.closeConnection(con);
  144.         }
  145.     }

  146.     protected Documento getDocumento(long idDocumento,boolean readBytes) throws DriverRegistroServiziException {
  147.         String nomeMetodo = "getDocumento";

  148.         Connection con = null;

  149.         if (this.driver.atomica) {
  150.             try {
  151.                 con = this.driver.getConnectionFromDatasource("getDocumento");

  152.             } catch (Exception e) {
  153.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(), e);

  154.             }

  155.         } else {
  156.             con = this.driver.globalConnection;
  157.         }

  158.         try {

  159.             return DriverRegistroServiziDB_documentiLIB.getDocumento(idDocumento, readBytes, con, this.driver.tipoDB);

  160.         } catch (Exception se) {
  161.             throw new DriverRegistroServiziException("[DriverRegistroServiziException::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  162.         } finally {
  163.             this.driver.closeConnection(con);
  164.         }
  165.     }

  166.     protected List<Documento> serviziAllegatiList(long idServizio, ISearch ricerca) throws DriverRegistroServiziException {
  167.         String nomeMetodo = "serviziAllegatiList";
  168.         int idLista = Liste.SERVIZI_ALLEGATI;
  169.         int offset;
  170.         int limit;
  171.         String search;
  172.         String queryString;

  173.         limit = ricerca.getPageSize(idLista);
  174.         offset = ricerca.getIndexIniziale(idLista);
  175.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  176.         ricerca.getSearchString(idLista);

  177.         Connection con = null;
  178.         PreparedStatement stmt = null;
  179.         ResultSet risultato = null;

  180.         ArrayList<Documento> lista = new ArrayList<Documento>();

  181.         if (this.driver.atomica) {
  182.             try {
  183.                 con = this.driver.getConnectionFromDatasource("serviziAllegatiList");

  184.             } catch (Exception e) {
  185.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  186.             }

  187.         } else
  188.             con = this.driver.globalConnection;

  189.         try {

  190.             if (!search.equals("")) {
  191.                 //query con search
  192.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  193.                 sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  194.                 sqlQueryObject.addSelectCountField("*", "cont");
  195.                 sqlQueryObject.addWhereCondition("id_proprietario = ?");
  196.                 sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  197.                 sqlQueryObject.addWhereCondition(false,
  198.                         //sqlQueryObject.getWhereLikeCondition("ruolo",search,true,true),
  199.                         sqlQueryObject.getWhereLikeCondition("nome",search,true,true));
  200.                 sqlQueryObject.setANDLogicOperator(true);
  201.                 queryString = sqlQueryObject.createSQLQuery();
  202.             } else {
  203.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  204.                 sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  205.                 sqlQueryObject.addSelectCountField("*", "cont");
  206.                 sqlQueryObject.addWhereCondition("id_proprietario = ?");
  207.                 sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  208.                 sqlQueryObject.setANDLogicOperator(true);
  209.                 queryString = sqlQueryObject.createSQLQuery();
  210.             }

  211.             stmt = con.prepareStatement(queryString);
  212.             stmt.setLong(1,idServizio);
  213.             stmt.setString(2,ProprietariDocumento.servizio.toString());
  214.             risultato = stmt.executeQuery();
  215.             if (risultato.next())
  216.                 ricerca.setNumEntries(idLista,risultato.getInt("cont"));
  217.             risultato.close();
  218.             stmt.close();

  219.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  220.             sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  221.             sqlQueryObject.addSelectField("id");
  222.             sqlQueryObject.addSelectField("nome");
  223.             sqlQueryObject.addSelectField("ruolo");
  224.             sqlQueryObject.addSelectField("id_proprietario");
  225.             sqlQueryObject.addSelectField("tipo_proprietario");
  226.             //where
  227.             sqlQueryObject.addWhereCondition("id_proprietario = ?");
  228.             sqlQueryObject.addWhereCondition("tipo_proprietario = ?");

  229.             // ricavo le entries
  230.             if (limit == 0) // con limit
  231.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;

  232.             if (!search.equals("")) { // con search
  233.                 sqlQueryObject.addWhereCondition(false,
  234.                         //sqlQueryObject.getWhereLikeCondition("ruolo",search,true,true),
  235.                         sqlQueryObject.getWhereLikeCondition("nome",search,true,true));
  236.             }

  237.             sqlQueryObject.setANDLogicOperator(true);
  238.             sqlQueryObject.addOrderBy("nome");
  239.             sqlQueryObject.setSortType(true);
  240.             sqlQueryObject.setLimit(limit);
  241.             sqlQueryObject.setOffset(offset);
  242.             queryString = sqlQueryObject.createSQLQuery();

  243.             stmt = con.prepareStatement(queryString);
  244.             stmt.setLong(1, idServizio);
  245.             stmt.setString(2,ProprietariDocumento.servizio.toString());
  246.             risultato = stmt.executeQuery();

  247.             while(risultato.next()){
  248.                 Documento doc = DriverRegistroServiziDB_documentiLIB.getDocumento(risultato.getLong("id"),false, con, this.driver.tipoDB);
  249.                 lista.add(doc);
  250.             }

  251.             return lista;

  252.         } catch (Exception se) {

  253.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  254.         } finally {
  255.             //Chiudo statement and resultset
  256.             JDBCUtilities.closeResources(risultato, stmt);
  257.             this.driver.closeConnection(con);
  258.         }
  259.     }

  260.     protected Documento getAllegato(IDServizio idASPS, String nome)throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  261.         return getEngineDocumento(idASPS, RuoliDocumento.allegato.toString(), null, nome);
  262.     }
  263.     protected Documento getSpecificaSemiformale(IDServizio idASPS, TipiDocumentoSemiformale tipo, String nome)throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  264.         return getEngineDocumento(idASPS, RuoliDocumento.specificaSemiformale.toString(), tipo.toString(), nome);
  265.     }
  266.     protected Documento getSpecificaSicurezza(IDServizio idASPS, TipiDocumentoSicurezza tipo, String nome)throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  267.         return getEngineDocumento(idASPS, RuoliDocumento.specificaSicurezza.toString(), tipo.toString(), nome);
  268.     }
  269.     protected Documento getSpecificaLivelloServizio(IDServizio idASPS, TipiDocumentoLivelloServizio tipo, String nome)throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  270.         return getEngineDocumento(idASPS, RuoliDocumento.specificaLivelloServizio.toString(), tipo.toString(), nome);
  271.     }
  272.     private Documento getEngineDocumento(IDServizio idASPS, String ruolo, String tipo, String nome) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  273.         String nomeMetodo = "getDocumento";
  274.         String queryString;
  275.        
  276.         Connection con = null;
  277.         PreparedStatement stmt = null;
  278.         ResultSet risultato = null;

  279.         if (this.driver.atomica) {
  280.             try {
  281.                 con = this.driver.getConnectionFromDatasource("serviziAllegatiList");

  282.             } catch (Exception e) {
  283.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  284.             }

  285.         } else
  286.             con = this.driver.globalConnection;

  287.         try {

  288.             long idServizio = DBUtils.getIdAccordoServizioParteSpecifica(idASPS, con, this.driver.tipoDB);
  289.             if(idServizio<=0) {
  290.                 throw new DriverRegistroServiziNotFound("ApiImpl '"+idASPS+"' undefined");
  291.             }  

  292.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  293.             sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  294.             sqlQueryObject.addSelectField("id");
  295.             sqlQueryObject.addWhereCondition("id_proprietario = ?");
  296.             sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  297.             sqlQueryObject.addWhereCondition("ruolo = ?");
  298.             if(tipo!=null) {
  299.                 sqlQueryObject.addWhereCondition("tipo = ?");
  300.             }
  301.             sqlQueryObject.addWhereCondition("nome = ?");
  302.            
  303.             sqlQueryObject.setANDLogicOperator(true);
  304.             queryString = sqlQueryObject.createSQLQuery();

  305.             stmt = con.prepareStatement(queryString);
  306.             int index = 1;
  307.             stmt.setLong(index++, idServizio);
  308.             stmt.setString(index++, ProprietariDocumento.servizio.toString());
  309.             stmt.setString(index++, ruolo);
  310.             if(tipo!=null) {
  311.                 stmt.setString(index++, tipo);
  312.             }
  313.             stmt.setString(index++, nome);
  314.             risultato = stmt.executeQuery();

  315.             if(risultato.next()){
  316.                 Documento doc = DriverRegistroServiziDB_documentiLIB.getDocumento(risultato.getLong("id"),true, con, this.driver.tipoDB);
  317.                 return doc;
  318.             }

  319.             String tipoS = (tipo!=null) ? ("tipo:"+tipo+" ") : "";
  320.             throw new DriverRegistroServiziNotFound("Documento (ruolo:"+ruolo+" "+tipoS+"nome:"+nome+") non trovato");

  321.         } catch(DriverRegistroServiziNotFound notFound) {
  322.             throw notFound;
  323.         }
  324.         catch (Exception se) {
  325.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  326.         } finally {
  327.             //Chiudo statement and resultset
  328.             JDBCUtilities.closeResources(risultato, stmt);
  329.             this.driver.closeConnection(con);
  330.         }
  331.     }

  332.    
  333.     protected Documento getAllegato(IDAccordo idAccordo, String nome)throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  334.         return getEngineDocumento(idAccordo, RuoliDocumento.allegato.toString(), null, nome);
  335.     }
  336.     protected Documento getSpecificaSemiformale(IDAccordo idAccordo, TipiDocumentoSemiformale tipo, String nome)throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  337.         return getEngineDocumento(idAccordo, RuoliDocumento.specificaSemiformale.toString(), tipo.toString(), nome);
  338.     }
  339.     private Documento getEngineDocumento(IDAccordo idAccordo, String ruolo, String tipo, String nome) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  340.         String nomeMetodo = "getDocumento";
  341.         String queryString;
  342.        
  343.         Connection con = null;
  344.         PreparedStatement stmt = null;
  345.         ResultSet risultato = null;

  346.         if (this.driver.atomica) {
  347.             try {
  348.                 con = this.driver.getConnectionFromDatasource("serviziAllegatiList");

  349.             } catch (Exception e) {
  350.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception accedendo al datasource :" + e.getMessage(),e);

  351.             }

  352.         } else
  353.             con = this.driver.globalConnection;

  354.         try {

  355.             long idAccordoLong = DBUtils.getIdAccordoServizioParteComune(idAccordo, con, this.driver.tipoDB);
  356.             if(idAccordoLong<=0) {
  357.                 throw new DriverRegistroServiziNotFound("Api '"+idAccordo+"' undefined");
  358.             }  

  359.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  360.             sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  361.             sqlQueryObject.addSelectField("id");
  362.             sqlQueryObject.addWhereCondition("id_proprietario = ?");
  363.             sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  364.             sqlQueryObject.addWhereCondition("ruolo = ?");
  365.             if(tipo!=null) {
  366.                 sqlQueryObject.addWhereCondition("tipo = ?");
  367.             }
  368.             sqlQueryObject.addWhereCondition("nome = ?");
  369.            
  370.             sqlQueryObject.setANDLogicOperator(true);
  371.             queryString = sqlQueryObject.createSQLQuery();

  372.             stmt = con.prepareStatement(queryString);
  373.             int index = 1;
  374.             stmt.setLong(index++, idAccordoLong);
  375.             stmt.setString(index++, ProprietariDocumento.accordoServizio.toString());
  376.             stmt.setString(index++, ruolo);
  377.             if(tipo!=null) {
  378.                 stmt.setString(index++, tipo);
  379.             }
  380.             stmt.setString(index++, nome);
  381.             risultato = stmt.executeQuery();

  382.             if(risultato.next()){
  383.                 Documento doc = DriverRegistroServiziDB_documentiLIB.getDocumento(risultato.getLong("id"),true, con, this.driver.tipoDB);
  384.                 return doc;
  385.             }

  386.             String tipoS = (tipo!=null) ? ("tipo:"+tipo+" ") : "";
  387.             throw new DriverRegistroServiziNotFound("Documento (ruolo:"+ruolo+" "+tipoS+"nome:"+nome+") non trovato");

  388.         } catch(DriverRegistroServiziNotFound notFound) {
  389.             throw notFound;
  390.         }
  391.         catch (Exception se) {
  392.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  393.         } finally {
  394.             //Chiudo statement and resultset
  395.             JDBCUtilities.closeResources(risultato, stmt);
  396.             this.driver.closeConnection(con);
  397.         }
  398.     }
  399. }