DriverRegistroServiziDB_accordiCooperazioneDriver.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.Date;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.sql.Timestamp;
  27. import java.util.ArrayList;
  28. import java.util.List;

  29. import org.openspcoop2.core.commons.DBUtils;
  30. import org.openspcoop2.core.commons.Filtri;
  31. import org.openspcoop2.core.commons.ISearch;
  32. import org.openspcoop2.core.commons.Liste;
  33. import org.openspcoop2.core.commons.SearchUtils;
  34. import org.openspcoop2.core.constants.CostantiDB;
  35. import org.openspcoop2.core.constants.ProprietariProtocolProperty;
  36. import org.openspcoop2.core.id.IDAccordoCooperazione;
  37. import org.openspcoop2.core.id.IDSoggetto;
  38. import org.openspcoop2.core.registry.AccordoCooperazione;
  39. import org.openspcoop2.core.registry.AccordoCooperazionePartecipanti;
  40. import org.openspcoop2.core.registry.Documento;
  41. import org.openspcoop2.core.registry.IdSoggetto;
  42. import org.openspcoop2.core.registry.ProtocolProperty;
  43. import org.openspcoop2.core.registry.Soggetto;
  44. import org.openspcoop2.core.registry.constants.ProprietariDocumento;
  45. import org.openspcoop2.core.registry.constants.RuoliDocumento;
  46. import org.openspcoop2.core.registry.constants.StatiAccordo;
  47. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  48. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  49. import org.openspcoop2.core.registry.driver.FiltroRicercaAccordi;
  50. import org.openspcoop2.core.registry.driver.ValidazioneStatoPackageException;
  51. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  52. import org.openspcoop2.utils.sql.ISQLQueryObject;
  53. import org.openspcoop2.utils.sql.SQLObjectFactory;

  54. /**
  55.  * DriverRegistroServiziDB_accordiCooperazioneDriver
  56.  *
  57.  *
  58.  * @author Sandra Giangrandi (sandra@link.it)
  59.  * @author Stefano Corallo (corallo@link.it)
  60.  * @author $Author$
  61.  * @version $Rev$, $Date$
  62.  */
  63. public class DriverRegistroServiziDB_accordiCooperazioneDriver {

  64.     private DriverRegistroServiziDB driver = null;
  65.     private DriverRegistroServiziDB_protocolPropertiesDriver protocolPropertiesDriver = null;
  66.    
  67.     protected DriverRegistroServiziDB_accordiCooperazioneDriver(DriverRegistroServiziDB driver) {
  68.         this.driver = driver;
  69.         this.protocolPropertiesDriver = new DriverRegistroServiziDB_protocolPropertiesDriver(driver);
  70.     }
  71.    
  72.    
  73.     /* Accordi di Cooperazione */

  74.     protected org.openspcoop2.core.registry.AccordoCooperazione getAccordoCooperazione(IDAccordoCooperazione idAccordo) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  75.         return getAccordoCooperazione(idAccordo,false);
  76.     }
  77.     protected org.openspcoop2.core.registry.AccordoCooperazione getAccordoCooperazione(IDAccordoCooperazione idAccordo,boolean readContenutoAllegati) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{

  78.         // conrollo consistenza
  79.         if (idAccordo == null)
  80.             throw new DriverRegistroServiziException("[getAccordoCooperazione] Parametro idAccordo is null");
  81.         if (idAccordo.getNome() == null)
  82.             throw new DriverRegistroServiziException("[getAccordoCooperazione] Parametro idAccordo.getNome is null");
  83.         if (idAccordo.getNome().trim().equals(""))
  84.             throw new DriverRegistroServiziException("[getAccordoCooperazione] Parametro idAccordo.getNome non e' definito");

  85.         this.driver.logDebug("richiesto getAccordoCooperazione: " + idAccordo.toString());

  86.         org.openspcoop2.core.registry.AccordoCooperazione accordoCooperazione = null;
  87.         Connection con = null;
  88.         PreparedStatement stm = null;
  89.         ResultSet rs = null;
  90.         PreparedStatement stm2 = null;
  91.         ResultSet rs2 = null;

  92.         try {

  93.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  94.             // prendo la connessione dal pool
  95.             if (this.driver.atomica)
  96.                 con = this.driver.getConnectionFromDatasource("getAccordoCooperazione(idAccordo)");
  97.             else
  98.                 con = this.driver.globalConnection;

  99.             long idAccordoLong = DBUtils.getIdAccordoCooperazione(idAccordo, con, this.driver.tipoDB);
  100.             if(idAccordoLong<=0){
  101.                 throw new DriverRegistroServiziNotFound("[DriverRegistroServiziDB::getAccordoCooperazione] Accordo non trovato (id:"+idAccordoLong+")");
  102.             }


  103.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  104.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  105.             sqlQueryObject.addSelectField("*");
  106.             sqlQueryObject.addWhereCondition("id = ?");
  107.             sqlQueryObject.setANDLogicOperator(true);
  108.             String sqlQuery = sqlQueryObject.createSQLQuery();



  109.             stm = con.prepareStatement(sqlQuery);

  110.             stm.setLong(1, idAccordoLong);


  111.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idAccordoLong));
  112.             rs = stm.executeQuery();

  113.             if (rs.next()) {
  114.                 accordoCooperazione = new org.openspcoop2.core.registry.AccordoCooperazione();

  115.                 accordoCooperazione.setId(rs.getLong("id"));

  116.                 String tmp = rs.getString("nome");
  117.                 // se tmp==null oppure tmp=="" then setNome(null) else
  118.                 // setNome(tmp)
  119.                 accordoCooperazione.setNome(((tmp == null || tmp.equals("")) ? null : tmp));

  120.                 tmp = rs.getString("descrizione");
  121.                 accordoCooperazione.setDescrizione(((tmp == null || tmp.equals("")) ? null : tmp));

  122.                 // Soggetto referente
  123.                 long id_referente = rs.getLong("id_referente");
  124.                 if(id_referente>0) {
  125.                     IDSoggetto soggettoReferente = null;
  126.                     try {
  127.                         soggettoReferente = this.driver.getIdSoggetto(id_referente,con);
  128.                         if(soggettoReferente==null){
  129.                             throw new DriverRegistroServiziNotFound ("non esiste");
  130.                         }
  131.                     }catch(DriverRegistroServiziNotFound notFound) {
  132.                         throw new Exception ("Soggetto referente ["+id_referente+"] dell'accordo non esiste");
  133.                     }
  134.                     IdSoggetto assr = new IdSoggetto();
  135.                     assr.setTipo(soggettoReferente.getTipo());
  136.                     assr.setNome(soggettoReferente.getNome());
  137.                     accordoCooperazione.setSoggettoReferente(assr);
  138.                 }

  139.                 //Versione
  140.                 if(rs.getString("versione")!=null && !"".equals(rs.getString("versione")))
  141.                     accordoCooperazione.setVersione(rs.getInt("versione"));

  142.                 // Stato
  143.                 tmp = rs.getString("stato");
  144.                 accordoCooperazione.setStatoPackage(((tmp == null || tmp.equals("")) ? null : tmp));

  145.                 // Privato
  146.                 if(rs.getInt("privato")==1)
  147.                     accordoCooperazione.setPrivato(true);
  148.                 else
  149.                     accordoCooperazione.setPrivato(false);

  150.                 tmp = rs.getString("superuser");
  151.                 accordoCooperazione.setSuperUser(((tmp == null || tmp.equals("")) ? null : tmp));

  152.                 // Ora Registrazione
  153.                 if(rs.getTimestamp("ora_registrazione")!=null){
  154.                     accordoCooperazione.setOraRegistrazione(new Date(rs.getTimestamp("ora_registrazione").getTime()));
  155.                 }

  156.                 rs.close();
  157.                 stm.close();


  158.                 // Aggiungo ServiziComposti
  159.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  160.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_SERVIZI_COMPOSTO);
  161.                 sqlQueryObject.addSelectField("id_accordo");
  162.                 sqlQueryObject.addWhereCondition("id_accordo_cooperazione = ?");
  163.                 sqlQuery = sqlQueryObject.createSQLQuery();
  164.                 stm = con.prepareStatement(sqlQuery);
  165.                 stm.setLong(1, idAccordoLong);

  166.                 this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idAccordoLong));

  167.                 rs = stm.executeQuery();

  168.                 while (rs.next()) {

  169.                     long idAccServizioComposto = rs.getLong("id_accordo");

  170.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  171.                     sqlQueryObject.addFromTable(CostantiDB.ACCORDI);
  172.                     sqlQueryObject.addSelectField("nome");
  173.                     sqlQueryObject.addSelectField("versione");
  174.                     sqlQueryObject.addSelectField("id_referente");
  175.                     sqlQueryObject.addWhereCondition("id = ?");
  176.                     sqlQuery = sqlQueryObject.createSQLQuery();
  177.                     stm2 = con.prepareStatement(sqlQuery);
  178.                     stm2.setLong(1, idAccServizioComposto);
  179.                     rs2 = stm2.executeQuery();
  180.                     if(rs2.next()){
  181.                         IDSoggetto soggettoReferente = null;
  182.                         long id_referenteInternal = rs2.getLong("id_referente");
  183.                         if(id_referenteInternal>0){
  184.                             try {
  185.                                 soggettoReferente = this.driver.getIdSoggetto(id_referenteInternal,con);
  186.                                 if(soggettoReferente==null){
  187.                                     throw new DriverRegistroServiziNotFound ("non esiste");
  188.                                 }
  189.                             }catch(DriverRegistroServiziNotFound notFound) {
  190.                                 throw new Exception ("Soggetto referente ["+id_referenteInternal+"] dell'accordo non esiste");
  191.                             }
  192.                         }
  193.                         String uriAccordo = this.driver.idAccordoFactory.getUriFromValues(rs2.getString("nome"), soggettoReferente, rs2.getInt("versione"));
  194.                         accordoCooperazione.addUriServiziComposti(uriAccordo);
  195.                     }else{
  196.                         throw new DriverRegistroServiziException("IDAccordo con id ["+rs.getLong("id_accordo_servizio")+"] non presente");
  197.                     }
  198.                     rs2.close();
  199.                     stm2.close();

  200.                 }
  201.                 rs.close();
  202.                 stm.close();




  203.                 // Aggiungo Partecipanti
  204.                 sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  205.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  206.                 sqlQueryObject.addSelectField("*");
  207.                 sqlQueryObject.addWhereCondition("id_accordo_cooperazione = ?");
  208.                 sqlQuery = sqlQueryObject.createSQLQuery();
  209.                 stm = con.prepareStatement(sqlQuery);
  210.                 stm.setLong(1, idAccordoLong);

  211.                 this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idAccordoLong));

  212.                 rs = stm.executeQuery();

  213.                 IdSoggetto accCompPartecipante = null;
  214.                 AccordoCooperazionePartecipanti accCopPartecipanti = null;
  215.                 while (rs.next()) {

  216.                     accCompPartecipante = new IdSoggetto();
  217.                     accCompPartecipante.setIdSoggetto(rs.getLong("id_soggetto"));

  218.                     Soggetto s = this.driver.getSoggetto(rs.getLong("id_soggetto"),con);
  219.                     accCompPartecipante.setTipo(s.getTipo());
  220.                     accCompPartecipante.setNome(s.getNome());

  221.                     if(accCopPartecipanti==null){
  222.                         accCopPartecipanti = new AccordoCooperazionePartecipanti();
  223.                         accordoCooperazione.setElencoPartecipanti(accCopPartecipanti);
  224.                     }
  225.                     accordoCooperazione.getElencoPartecipanti().addSoggettoPartecipante(accCompPartecipante);

  226.                 }
  227.                 rs.close();
  228.                 stm.close();


  229.                 // read Documenti generici: i bytes non vengono ritornati se readContenutoAllegati==false, utilizzare il metodo apposta per averli:
  230.                 //                                               DriverRegistroServiziDB_LIB.getDocumento(id, readBytes, connection);
  231.                 try{
  232.                     List<?> allegati = DriverRegistroServiziDB_documentiLIB.getListaDocumenti(RuoliDocumento.allegato.toString(), idAccordoLong,
  233.                             ProprietariDocumento.accordoCooperazione,readContenutoAllegati, con, this.driver.tipoDB);
  234.                     for(int i=0; i<allegati.size();i++){
  235.                         accordoCooperazione.addAllegato((Documento) allegati.get(i));
  236.                     }
  237.                 }catch(DriverRegistroServiziNotFound dNotFound){}
  238.                 try{
  239.                     List<?> specificheSemiformali = DriverRegistroServiziDB_documentiLIB.getListaDocumenti(RuoliDocumento.specificaSemiformale.toString(),
  240.                             idAccordoLong, ProprietariDocumento.accordoCooperazione,readContenutoAllegati, con, this.driver.tipoDB);
  241.                     for(int i=0; i<specificheSemiformali.size();i++){
  242.                         accordoCooperazione.addSpecificaSemiformale((Documento) specificheSemiformali.get(i));
  243.                     }
  244.                 }catch(DriverRegistroServiziNotFound dNotFound){}

  245.                
  246.                 // Protocol Properties
  247.                 try{
  248.                     List<ProtocolProperty> listPP = DriverRegistroServiziDB_LIB.getListaProtocolProperty(idAccordoLong, ProprietariProtocolProperty.ACCORDO_COOPERAZIONE, con,
  249.                             this.driver.tipoDB, this.driver.getDriverUnwrapBYOK());
  250.                     if(listPP!=null && !listPP.isEmpty()){
  251.                         for (ProtocolProperty protocolProperty : listPP) {
  252.                             accordoCooperazione.addProtocolProperty(protocolProperty);
  253.                         }
  254.                     }
  255.                 }catch(DriverRegistroServiziNotFound dNotFound){
  256.                     // ignore
  257.                 }
  258.                

  259.             } else {
  260.                 throw new DriverRegistroServiziNotFound("[DriverRegistroServiziDB::getAccordoCooperazione] rs.next non ha restituito valori con la seguente interrogazione :\n" + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, idAccordoLong));
  261.             }


  262.             return accordoCooperazione;

  263.         }catch (DriverRegistroServiziNotFound e) {
  264.             throw e;
  265.         }catch (Exception se) {
  266.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getAccordoCooperazione] Exception :" + se.getMessage(),se);
  267.         } finally {

  268.             JDBCUtilities.closeResources(rs2, stm2);

  269.             JDBCUtilities.closeResources(rs, stm);

  270.             this.driver.closeConnection(con);

  271.         }
  272.     }

  273.     protected IDAccordoCooperazione getIdAccordoCooperazione(long id) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  274.         return getIdAccordoCooperazione(id,null);
  275.     }
  276.     protected IDAccordoCooperazione getIdAccordoCooperazione(long id,Connection conParam) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  277.         this.driver.logDebug("richiesto getIdAccordoCooperazione: " + id);
  278.         // conrollo consistenza
  279.         if (id <= 0)
  280.             return null;

  281.         Connection con = null;
  282.         PreparedStatement stm = null;
  283.         ResultSet rs = null;

  284.         IDAccordoCooperazione idAccordo = null;
  285.         try {
  286.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  287.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  288.             sqlQueryObject.addSelectField("nome");
  289.             sqlQueryObject.addSelectField("id_referente");
  290.             sqlQueryObject.addSelectField("versione");
  291.             sqlQueryObject.addWhereCondition("id = ?");
  292.             String sqlQuery = sqlQueryObject.createSQLQuery();

  293.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  294.             // prendo la connessione dal pool
  295.             if(conParam!=null){
  296.                 con = conParam;
  297.             }
  298.             else if (this.driver.atomica)
  299.                 con = this.driver.getConnectionFromDatasource("getIdAccordoCooperazione(longId)");
  300.             else
  301.                 con = this.driver.globalConnection;

  302.             stm = con.prepareStatement(sqlQuery);

  303.             stm.setLong(1, id);

  304.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, id));
  305.             rs = stm.executeQuery();


  306.             if (rs.next()) {

  307.                 IDSoggetto referente = null;
  308.                 long idReferente = rs.getLong("id_referente");
  309.                 if(idReferente>0){
  310.                     referente = this.driver.getIdSoggetto(idReferente,con);
  311.                     if(referente==null){
  312.                         throw new Exception("Soggetto referente non presente?");
  313.                     }
  314.                 }

  315.                 idAccordo = this.driver.idAccordoCooperazioneFactory.getIDAccordoFromValues(rs.getString("nome"),referente,rs.getInt("versione"));

  316.             } else {
  317.                 rs.close();
  318.                 stm.close();
  319.                 throw new DriverRegistroServiziNotFound("[DriverRegistroServiziDB::getIdAccordoCooperazione] rs.next non ha restituito valori con la seguente interrogazione :\n" + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, id));
  320.             }

  321.             rs.close();
  322.             stm.close();

  323.         } catch (SQLException se) {
  324.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getIdAccordoCooperazione] SQLException :" + se.getMessage(),se);
  325.         }catch (DriverRegistroServiziNotFound e) {
  326.             throw e;
  327.         }catch (Exception se) {
  328.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getIdAccordoCooperazione] Exception :" + se.getMessage(),se);
  329.         } finally {

  330.             //Chiudo statement and resultset
  331.             JDBCUtilities.closeResources(rs, stm);

  332.             this.driver.closeConnection(conParam, con);

  333.         }

  334.         return idAccordo;
  335.     }


  336.     protected AccordoCooperazione getAccordoCooperazione(long id) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  337.         return getAccordoCooperazione(id,null);
  338.     }
  339.     protected AccordoCooperazione getAccordoCooperazione(long id,Connection conParam) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  340.         this.driver.logDebug("richiesto getAccordoCooperazione: " + id);
  341.         // conrollo consistenza
  342.         if (id <= 0)
  343.             return null;

  344.         Connection con = null;
  345.         PreparedStatement stm = null;
  346.         ResultSet rs = null;

  347.         IDAccordoCooperazione idAccordo = null;
  348.         try {
  349.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  350.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  351.             sqlQueryObject.addSelectField("nome");
  352.             sqlQueryObject.addSelectField("id_referente");
  353.             sqlQueryObject.addSelectField("versione");
  354.             sqlQueryObject.addWhereCondition("id = ?");
  355.             String sqlQuery = sqlQueryObject.createSQLQuery();

  356.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  357.             // prendo la connessione dal pool
  358.             if(conParam!=null){
  359.                 con = conParam;
  360.             }
  361.             else if (this.driver.atomica)
  362.                 con = this.driver.getConnectionFromDatasource("getAccordoCooperazione(longId)");
  363.             else
  364.                 con = this.driver.globalConnection;

  365.             stm = con.prepareStatement(sqlQuery);

  366.             stm.setLong(1, id);

  367.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, id));
  368.             rs = stm.executeQuery();


  369.             if (rs.next()) {

  370.                 IDSoggetto referente = null;
  371.                 long idReferente = rs.getLong("id_referente");
  372.                 if(idReferente>0){
  373.                     referente = this.driver.getIdSoggetto(idReferente,con);
  374.                     if(referente==null){
  375.                         throw new Exception("Soggetto referente non presente?");
  376.                     }
  377.                 }
  378.                
  379.                 idAccordo = this.driver.idAccordoCooperazioneFactory.getIDAccordoFromValues(rs.getString("nome"),referente,rs.getInt("versione"));

  380.             } else {
  381.                 rs.close();
  382.                 stm.close();
  383.                 throw new DriverRegistroServiziNotFound("[DriverRegistroServiziDB::getAccordoCooperazione] rs.next non ha restituito valori con la seguente interrogazione :\n" + DriverRegistroServiziDB_LIB.formatSQLString(sqlQuery, id));
  384.             }

  385.             rs.close();
  386.             stm.close();

  387.         } catch (SQLException se) {
  388.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getAccordoCooperazione] SQLException :" + se.getMessage(),se);
  389.         }catch (DriverRegistroServiziNotFound e) {
  390.             throw e;
  391.         }catch (Exception se) {
  392.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::getAccordoCooperazione] Exception :" + se.getMessage(),se);
  393.         } finally {

  394.             //Chiudo statement and resultset
  395.             JDBCUtilities.closeResources(rs, stm);

  396.             this.driver.closeConnection(conParam, con);

  397.         }

  398.         return this.getAccordoCooperazione(idAccordo, false);
  399.     }

  400.     /**
  401.      * Ritorna gli identificatori degli accordi che rispettano il parametro di ricerca
  402.      *
  403.      * @param filtroRicerca
  404.      * @return Una lista di ID degli accordi trovati
  405.      * @throws DriverRegistroServiziException
  406.      * @throws DriverRegistroServiziNotFound
  407.      */
  408.     protected List<IDAccordoCooperazione> getAllIdAccordiCooperazione(FiltroRicercaAccordi filtroRicerca) throws DriverRegistroServiziException,DriverRegistroServiziNotFound{
  409.         Connection con = null;
  410.         PreparedStatement stm = null;
  411.         ResultSet rs = null;

  412.         this.driver.logDebug("getAllIdAccordiCooperazione...");

  413.         try {
  414.             this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  415.             // prendo la connessione dal pool
  416.             if (this.driver.atomica)
  417.                 con = this.driver.getConnectionFromDatasource("getAllIdAccordiCooperazione");
  418.             else
  419.                 con = this.driver.globalConnection;

  420.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  421.             sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  422.             sqlQueryObject.addFromTable(this.driver.tabellaSoggetti);

  423.             sqlQueryObject.addSelectField(CostantiDB.ACCORDI_COOPERAZIONE,"nome");
  424.             sqlQueryObject.addSelectField(CostantiDB.ACCORDI_COOPERAZIONE,"versione");
  425.             sqlQueryObject.addSelectField(this.driver.tabellaSoggetti,"tipo_soggetto");
  426.             sqlQueryObject.addSelectField(this.driver.tabellaSoggetti,"nome_soggetto");
  427.             sqlQueryObject.setSelectDistinct(true);
  428.             sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".id_referente="+this.driver.tabellaSoggetti+".id");
  429.             if(filtroRicerca!=null){
  430.                 // Filtro By Data
  431.                 if(filtroRicerca.getMinDate()!=null)
  432.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".ora_registrazione > ?");
  433.                 if(filtroRicerca.getMaxDate()!=null)
  434.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".ora_registrazione < ?");
  435.                 if(filtroRicerca.getNomeAccordo()!=null)
  436.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".nome = ?");
  437.                 if(filtroRicerca.getVersione()!=null)
  438.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".versione = ?");
  439.                 if(filtroRicerca.getTipoSoggettoReferente()!=null)
  440.                     sqlQueryObject.addWhereCondition(this.driver.tabellaSoggetti+".tipo_soggetto=?");
  441.                 if(filtroRicerca.getNomeSoggettoReferente()!=null)
  442.                     sqlQueryObject.addWhereCondition(this.driver.tabellaSoggetti+".nome_soggetto=?");
  443.                 this.protocolPropertiesDriver.setProtocolPropertiesForSearch(sqlQueryObject, filtroRicerca, CostantiDB.ACCORDI_COOPERAZIONE);
  444.             }

  445.             sqlQueryObject.setANDLogicOperator(true);
  446.             String sqlQuery = sqlQueryObject.createSQLQuery();
  447.             this.driver.logDebug("eseguo query : " + sqlQuery );
  448.             stm = con.prepareStatement(sqlQuery);
  449.             int indexStmt = 1;
  450.             if(filtroRicerca!=null){
  451.                 if(filtroRicerca.getMinDate()!=null){
  452.                     this.driver.logDebug("minDate stmt.setTimestamp("+filtroRicerca.getMinDate()+")");
  453.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMinDate().getTime()));
  454.                     indexStmt++;
  455.                 }
  456.                 if(filtroRicerca.getMaxDate()!=null){
  457.                     this.driver.logDebug("maxDate stmt.setTimestamp("+filtroRicerca.getMaxDate()+")");
  458.                     stm.setTimestamp(indexStmt, new Timestamp(filtroRicerca.getMaxDate().getTime()));
  459.                     indexStmt++;
  460.                 }  
  461.                 if(filtroRicerca.getNomeAccordo()!=null){
  462.                     this.driver.logDebug("nomeAccordo stmt.setString("+filtroRicerca.getNomeAccordo()+")");
  463.                     stm.setString(indexStmt, filtroRicerca.getNomeAccordo());
  464.                     indexStmt++;
  465.                 }  
  466.                 if(filtroRicerca.getVersione()!=null){
  467.                     this.driver.logDebug("versioneAccordo stmt.setString("+filtroRicerca.getVersione()+")");
  468.                     stm.setInt(indexStmt, filtroRicerca.getVersione());
  469.                     indexStmt++;
  470.                 }  
  471.                 if(filtroRicerca.getTipoSoggettoReferente()!=null){
  472.                     this.driver.logDebug("tipoSoggettoReferenteAccordo stmt.setString("+filtroRicerca.getTipoSoggettoReferente()+")");
  473.                     stm.setString(indexStmt, filtroRicerca.getTipoSoggettoReferente());
  474.                     indexStmt++;
  475.                 }
  476.                 if(filtroRicerca.getNomeSoggettoReferente()!=null){
  477.                     this.driver.logDebug("nomeSoggettoReferenteAccordo stmt.setString("+filtroRicerca.getNomeSoggettoReferente()+")");
  478.                     stm.setString(indexStmt, filtroRicerca.getNomeSoggettoReferente());
  479.                     indexStmt++;
  480.                 }  
  481.                 this.protocolPropertiesDriver.setProtocolPropertiesForSearch(stm, indexStmt, filtroRicerca, ProprietariProtocolProperty.ACCORDO_COOPERAZIONE);
  482.             }
  483.             rs = stm.executeQuery();
  484.             List<IDAccordoCooperazione> idAccordi = new ArrayList<IDAccordoCooperazione>();
  485.             while (rs.next()) {
  486.                 IDSoggetto idSoggetto = new IDSoggetto(rs.getString("tipo_soggetto"), rs.getString("nome_soggetto"));
  487.                 IDAccordoCooperazione idAccordo = this.driver.idAccordoCooperazioneFactory.getIDAccordoFromValues(rs.getString("nome"),idSoggetto,rs.getInt("versione"));
  488.                 idAccordi.add(idAccordo);
  489.             }
  490.             if(idAccordi.size()==0){
  491.                 if(filtroRicerca!=null)
  492.                     throw new DriverRegistroServiziNotFound("Accordi non trovati che rispettano il filtro di ricerca selezionato: "+filtroRicerca.toString());
  493.                 else
  494.                     throw new DriverRegistroServiziNotFound("Accordi non trovati");
  495.             }else{
  496.                 return idAccordi;
  497.             }
  498.         }catch(DriverRegistroServiziNotFound de){
  499.             throw de;
  500.         }
  501.         catch(Exception e){
  502.             throw new DriverRegistroServiziException("getAllIdAccordiCooperazione error",e);
  503.         } finally {

  504.             //Chiudo statement and resultset
  505.             JDBCUtilities.closeResources(rs, stm);

  506.             this.driver.closeConnection(con);

  507.         }
  508.     }




  509.     /**
  510.      * Crea un nuovo AccordoCooperazione
  511.      *
  512.      * @param accordoCooperazione
  513.      * @throws DriverRegistroServiziException
  514.      */
  515.     protected void createAccordoCooperazione(AccordoCooperazione accordoCooperazione) throws DriverRegistroServiziException{
  516.         if (accordoCooperazione == null)
  517.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createAccordoCooperazione] Parametro non valido.");

  518.         Connection con = null;
  519.         boolean error = false;

  520.         if (this.driver.atomica) {
  521.             try {
  522.                 con = this.driver.getConnectionFromDatasource("createAccordoCooperazione");
  523.                 con.setAutoCommit(false);
  524.             } catch (Exception e) {
  525.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createAccordoCooperazione] Exception accedendo al datasource :" + e.getMessage(),e);

  526.             }

  527.         } else
  528.             con = this.driver.globalConnection;

  529.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  530.         try {
  531.             this.driver.logDebug("CRUDServizio tupe=1");
  532.             // CREATE
  533.             DriverRegistroServiziDB_accordiCooperazioneLIB.CRUDAccordoCooperazione(CostantiDB.CREATE, accordoCooperazione, con,
  534.                     this.driver.tipoDB, this.driver.getDriverWrapBYOK());

  535.         } catch (Exception qe) {
  536.             error = true;
  537.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::createAccordoCooperazione] Errore durante la creazione dell'accordo : " + qe.getMessage(), qe);
  538.         } finally {
  539.             this.driver.closeConnection(error, con);
  540.         }
  541.     }

  542.     /**
  543.      * Verifica l'esistenza di un accordo registrato.
  544.      *
  545.      * @param idAccordo dell'accordo da verificare
  546.      * @return true se l'accordo esiste, false altrimenti
  547.      * @throws DriverRegistroServiziException
  548.      */    
  549.     protected boolean existsAccordoCooperazione(IDAccordoCooperazione idAccordo) throws DriverRegistroServiziException{
  550.         Connection connection;
  551.         if (this.driver.atomica) {
  552.             try {
  553.                 connection = this.driver.getConnectionFromDatasource("existsAccordoCooperazione");
  554.             } catch (Exception e) {
  555.                 throw new DriverRegistroServiziException("DriverRegistroServiziDB::createAccordoServizio] Exception accedendo al datasource :" + e.getMessage(),e);

  556.             }

  557.         } else
  558.             connection = this.driver.globalConnection;

  559.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);
  560.         try {
  561.             long idAccordoLong = DBUtils.getIdAccordoCooperazione(idAccordo, connection, this.driver.tipoDB);
  562.             if (idAccordoLong <= 0)
  563.                 return false;
  564.             else
  565.                 return true;
  566.         } catch (Exception e) {
  567.             throw new DriverRegistroServiziException(e.getMessage(),e);
  568.         } finally {
  569.             this.driver.closeConnection(connection);
  570.         }
  571.     }

  572.     /**
  573.      * Aggiorna l'AccordoCooperazione con i nuovi valori.
  574.      *  
  575.      * @param accordoCooperazione
  576.      * @throws DriverRegistroServiziException
  577.      */
  578.     protected void updateAccordoCooperazione(AccordoCooperazione accordoCooperazione) throws DriverRegistroServiziException{
  579.         if (accordoCooperazione == null)
  580.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateAccordoCooperazione] Parametro non valido.");
  581.         Connection con = null;
  582.         boolean error = false;

  583.         if (this.driver.atomica) {
  584.             try {
  585.                 con = this.driver.getConnectionFromDatasource("updateAccordoCooperazione");
  586.                 con.setAutoCommit(false);
  587.             } catch (Exception e) {
  588.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateAccordoCooperazione] Exception accedendo al datasource :" + e.getMessage(),e);

  589.             }

  590.         } else
  591.             con = this.driver.globalConnection;

  592.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  593.         try {

  594.             // UPDATE
  595.             DriverRegistroServiziDB_accordiCooperazioneLIB.CRUDAccordoCooperazione(CostantiDB.UPDATE, accordoCooperazione, con,
  596.                     this.driver.tipoDB, this.driver.getDriverWrapBYOK());

  597.         } catch (Exception qe) {
  598.             error = true;
  599.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::updateAccordoCooperazione] Errore durante l'update dell'accordo : " + qe.getMessage(),qe);
  600.         } finally {
  601.             this.driver.closeConnection(error, con);
  602.         }
  603.     }

  604.     /**
  605.      * Elimina un AccordoCooperazione
  606.      *  
  607.      * @param accordoCooperazione
  608.      * @throws DriverRegistroServiziException
  609.      */
  610.     protected void deleteAccordoCooperazione(AccordoCooperazione accordoCooperazione) throws DriverRegistroServiziException{
  611.         if (accordoCooperazione == null)
  612.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteAccordoCooperazione] Parametro non valido.");

  613.         Connection con = null;
  614.         boolean error = false;

  615.         if (this.driver.atomica) {
  616.             try {
  617.                 con = this.driver.getConnectionFromDatasource("deleteAccordoCooperazione");
  618.                 con.setAutoCommit(false);
  619.             } catch (Exception e) {
  620.                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteAccordoCooperazione] Exception accedendo al datasource :" + e.getMessage(),e);

  621.             }

  622.         } else
  623.             con = this.driver.globalConnection;

  624.         this.driver.logDebug("operazione atomica = " + this.driver.atomica);

  625.         try {
  626.             this.driver.logDebug("CRUDServizio type = 3");
  627.             // creo soggetto
  628.             DriverRegistroServiziDB_accordiCooperazioneLIB.CRUDAccordoCooperazione(CostantiDB.DELETE, accordoCooperazione, con,
  629.                     this.driver.tipoDB, this.driver.getDriverWrapBYOK());

  630.         } catch (Exception qe) {
  631.             error = true;
  632.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::deleteAccordoCooperazione] Errore durante la delete dell'accordo : " + qe.getMessage(),qe);
  633.         } finally {
  634.             this.driver.closeConnection(error, con);
  635.         }
  636.     }

  637.     protected List<AccordoCooperazione> accordiCooperazioneList(String superuser, ISearch ricerca) throws DriverRegistroServiziException {
  638.         String nomeMetodo = "accordiCooperazioneList";
  639.         int idLista = Liste.ACCORDI_COOPERAZIONE;
  640.         int offset;
  641.         int limit;
  642.         String search;
  643.         String queryString;

  644.         limit = ricerca.getPageSize(idLista);
  645.         offset = ricerca.getIndexIniziale(idLista);
  646.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  647.         ricerca.getSearchString(idLista);

  648.         String filterProtocollo = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_PROTOCOLLO);
  649.         String filterProtocolli = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_PROTOCOLLI);
  650.         List<String> tipoSoggettiProtocollo = null;
  651.         try {
  652.             tipoSoggettiProtocollo = Filtri.convertToTipiSoggetti(filterProtocollo, filterProtocolli);
  653.         }catch(Exception e) {
  654.             throw new DriverRegistroServiziException(e.getMessage(),e);
  655.         }

  656.         String filterStatoAccordo = SearchUtils.getFilter(ricerca, idLista, Filtri.FILTRO_STATO_ACCORDO);

  657.         this.driver.logDebug("search : " + search);
  658.         this.driver.logDebug("filterProtocollo : " + filterProtocollo);
  659.         this.driver.logDebug("filterProtocolli : " + filterProtocolli);
  660.         this.driver.logDebug("filterStatoAccordo : " + filterStatoAccordo);

  661.         Connection con = null;
  662.         PreparedStatement stmt = null;
  663.         ResultSet risultato = null;

  664.         ArrayList<IDAccordoCooperazione> idAccordi = new ArrayList<IDAccordoCooperazione>();

  665.         if (this.driver.atomica) {
  666.             try {
  667.                 con = this.driver.getConnectionFromDatasource("accordiCooperazioneList");

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

  670.             }

  671.         } else
  672.             con = this.driver.globalConnection;

  673.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  674.         try {

  675.             if (!search.equals("")) {
  676.                 //query con search
  677.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  678.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  679.                 sqlQueryObject.addSelectCountField("*", "cont");
  680.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  681.                     sqlQueryObject.addWhereCondition("superuser = ?");
  682.                 sqlQueryObject.addWhereCondition(false,
  683.                         sqlQueryObject.getWhereLikeCondition("nome", search, true, true));
  684.                         //sqlQueryObject.getWhereLikeCondition("versione", search, true, true)); // e' un intero
  685.                 if(tipoSoggettiProtocollo!=null && tipoSoggettiProtocollo.size()>0) {
  686.                     sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  687.                     sqlQueryObject.addWhereCondition("id_referente = "+CostantiDB.SOGGETTI+".id");
  688.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI+".tipo_soggetto", true, tipoSoggettiProtocollo.toArray(new String[1]));
  689.                 }
  690.                 if(filterStatoAccordo!=null && !filterStatoAccordo.equals("")) {
  691.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".stato = ?");
  692.                 }
  693.                 sqlQueryObject.setANDLogicOperator(true);
  694.                 queryString = sqlQueryObject.createSQLQuery();
  695.             } else {
  696.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  697.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  698.                 sqlQueryObject.addSelectCountField("*", "cont");
  699.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  700.                     sqlQueryObject.addWhereCondition("superuser = ?");
  701.                 if(tipoSoggettiProtocollo!=null && tipoSoggettiProtocollo.size()>0) {
  702.                     sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  703.                     sqlQueryObject.addWhereCondition("id_referente = "+CostantiDB.SOGGETTI+".id");
  704.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI+".tipo_soggetto", true, tipoSoggettiProtocollo.toArray(new String[1]));
  705.                 }
  706.                 if(filterStatoAccordo!=null && !filterStatoAccordo.equals("")) {
  707.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".stato = ?");
  708.                 }
  709.                 sqlQueryObject.setANDLogicOperator(true);
  710.                 queryString = sqlQueryObject.createSQLQuery();
  711.             }

  712.             stmt = con.prepareStatement(queryString);
  713.             int index = 1;
  714.             if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  715.                 stmt.setString(index++, superuser);
  716.             if(filterStatoAccordo!=null && !filterStatoAccordo.equals("")) {
  717.                 stmt.setString(index++, filterStatoAccordo);
  718.             }
  719.             risultato = stmt.executeQuery();
  720.             if (risultato.next())
  721.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  722.             risultato.close();
  723.             stmt.close();

  724.             // ricavo le entries
  725.             if (limit == 0) // con limit
  726.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  727.             if (!search.equals("")) { // con search
  728.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  729.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  730.                 sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI_COOPERAZIONE,"id","idAccordoCooperazione");
  731.                 sqlQueryObject.addSelectField("nome");
  732.                 sqlQueryObject.addSelectField("id_referente");
  733.                 sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI_COOPERAZIONE,"descrizione","descrizioneAccordoCooperazione");
  734.                 sqlQueryObject.addSelectField("versione");
  735.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  736.                     sqlQueryObject.addWhereCondition("superuser = ?");
  737.                 sqlQueryObject.addWhereCondition(false,
  738.                         sqlQueryObject.getWhereLikeCondition("nome", search, true, true));
  739.                         //sqlQueryObject.getWhereLikeCondition("versione", search, true, true));  // e' un intero
  740.                 if(tipoSoggettiProtocollo!=null && tipoSoggettiProtocollo.size()>0) {
  741.                     sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  742.                     sqlQueryObject.addWhereCondition("id_referente = "+CostantiDB.SOGGETTI+".id");
  743.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI+".tipo_soggetto", true, tipoSoggettiProtocollo.toArray(new String[1]));
  744.                 }
  745.                 if(filterStatoAccordo!=null && !filterStatoAccordo.equals("")) {
  746.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".stato = ?");
  747.                 }
  748.                 sqlQueryObject.setANDLogicOperator(true);
  749.                 sqlQueryObject.addOrderBy("nome");
  750.                 sqlQueryObject.addOrderBy("versione");
  751.                 sqlQueryObject.setSortType(true);
  752.                 sqlQueryObject.setLimit(limit);
  753.                 sqlQueryObject.setOffset(offset);
  754.                 queryString = sqlQueryObject.createSQLQuery();
  755.             } else {
  756.                 // senza search
  757.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  758.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  759.                 sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI_COOPERAZIONE,"id","idAccordoCooperazione");
  760.                 sqlQueryObject.addSelectField("nome");
  761.                 sqlQueryObject.addSelectField("id_referente");
  762.                 sqlQueryObject.addSelectAliasField(CostantiDB.ACCORDI_COOPERAZIONE,"descrizione","descrizioneAccordoCooperazione");
  763.                 sqlQueryObject.addSelectField("versione");
  764.                 if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  765.                     sqlQueryObject.addWhereCondition("superuser = ?");
  766.                 if(tipoSoggettiProtocollo!=null && tipoSoggettiProtocollo.size()>0) {
  767.                     sqlQueryObject.addFromTable(CostantiDB.SOGGETTI);
  768.                     sqlQueryObject.addWhereCondition("id_referente = "+CostantiDB.SOGGETTI+".id");
  769.                     sqlQueryObject.addWhereINCondition(CostantiDB.SOGGETTI+".tipo_soggetto", true, tipoSoggettiProtocollo.toArray(new String[1]));
  770.                 }
  771.                 if(filterStatoAccordo!=null && !filterStatoAccordo.equals("")) {
  772.                     sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE+".stato = ?");
  773.                 }
  774.                 sqlQueryObject.setANDLogicOperator(true);
  775.                 sqlQueryObject.addOrderBy("nome");
  776.                 sqlQueryObject.addOrderBy("versione");
  777.                 sqlQueryObject.setSortType(true);
  778.                 sqlQueryObject.setLimit(limit);
  779.                 sqlQueryObject.setOffset(offset);
  780.                 queryString = sqlQueryObject.createSQLQuery();
  781.             }


  782.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(queryString));

  783.             stmt = con.prepareStatement(queryString);
  784.             index = 1;
  785.             if(this.driver.useSuperUser && superuser!=null && (!superuser.equals("")))
  786.                 stmt.setString(index++, superuser);
  787.             if(filterStatoAccordo!=null && !filterStatoAccordo.equals("")) {
  788.                 stmt.setString(index++, filterStatoAccordo);
  789.             }
  790.             risultato = stmt.executeQuery();

  791.             while (risultato.next()) {

  792.                 String nomeAcc = risultato.getString("nome");
  793.                 int versioneAcc = risultato.getInt("versione");
  794.                 long idReferente = risultato.getLong("id_referente");

  795.                 IDSoggetto soggettoReferente = null;
  796.                 if(idReferente>0){
  797.                     Soggetto s = this.driver.getSoggetto(idReferente);
  798.                     soggettoReferente = new IDSoggetto(s.getTipo(),s.getNome());
  799.                 }
  800.                
  801.                 IDAccordoCooperazione id = this.driver.idAccordoCooperazioneFactory.getIDAccordoFromValues(nomeAcc, soggettoReferente, versioneAcc);

  802.                 idAccordi.add(id);

  803.             }

  804.             this.driver.logDebug("size lista :" + ((idAccordi == null) ? null : idAccordi.size()));


  805.         } catch (Exception se) {

  806.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  807.         } finally {

  808.             //Chiudo statement and resultset
  809.             JDBCUtilities.closeResources(risultato, stmt);

  810.             this.driver.closeConnection(con);
  811.         }


  812.         ArrayList<AccordoCooperazione> lista = new ArrayList<AccordoCooperazione>();
  813.         for(int i=-0; i<idAccordi.size(); i++){
  814.             try{
  815.                 lista.add(this.getAccordoCooperazione(idAccordi.get(i)));
  816.             }catch(DriverRegistroServiziNotFound dNot){
  817.                 throw new DriverRegistroServiziException("Accordo non trovato con id?: "+dNot.getMessage(),dNot);
  818.             }
  819.         }
  820.         return lista;
  821.     }

  822.     protected List<IDSoggetto> accordiCoopPartecipantiList(long idAccordo,ISearch ricerca) throws DriverRegistroServiziException {
  823.         String nomeMetodo = "accordiCoopPartecipantiList";
  824.         int idLista = Liste.ACCORDI_COOP_PARTECIPANTI;
  825.         int offset;
  826.         int limit;
  827.         String search;
  828.         String queryString;

  829.         limit = ricerca.getPageSize(idLista);
  830.         offset = ricerca.getIndexIniziale(idLista);
  831.         search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));
  832.         ricerca.getSearchString(idLista);

  833.         this.driver.logDebug("search : " + search);

  834.         Connection con = null;
  835.         PreparedStatement stmt = null;
  836.         ResultSet risultato = null;

  837.         ArrayList<IDSoggetto> idSoggetti = new ArrayList<IDSoggetto>();

  838.         if (this.driver.atomica) {
  839.             try {
  840.                 con = this.driver.getConnectionFromDatasource("accordiCoopPartecipantiList");

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

  843.             }

  844.         } else
  845.             con = this.driver.globalConnection;

  846.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  847.         try {

  848.             if (!search.equals("")) {
  849.                 ISQLQueryObject sqlQueryObjectSoggetti = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  850.                 sqlQueryObjectSoggetti.addFromTable(CostantiDB.SOGGETTI);
  851.                 sqlQueryObjectSoggetti.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  852.                 sqlQueryObjectSoggetti.addSelectCountField("*", "cont");
  853.                 sqlQueryObjectSoggetti.addWhereCondition(true,
  854.                         sqlQueryObjectSoggetti.getWhereLikeCondition(CostantiDB.SOGGETTI+".nome_soggetto", search, true, true),
  855.                             CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_soggetto="+CostantiDB.SOGGETTI+".id");
  856.                 sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_accordo_cooperazione=?");
  857.                 sqlQueryObjectSoggetti.setANDLogicOperator(true);
  858.                 queryString = sqlQueryObjectSoggetti.createSQLQuery();
  859.             }else {
  860.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  861.                 sqlQueryObject.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  862.                 sqlQueryObject.addSelectCountField("*", "cont");
  863.                 sqlQueryObject.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_accordo_cooperazione=?");
  864.                 sqlQueryObject.setANDLogicOperator(true);
  865.                 queryString = sqlQueryObject.createSQLQuery();
  866.             }

  867.             stmt = con.prepareStatement(queryString);
  868.             stmt.setLong(1, idAccordo);
  869.             risultato = stmt.executeQuery();
  870.             if (risultato.next())
  871.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  872.             risultato.close();
  873.             stmt.close();

  874.             // ricavo le entries
  875.             if (limit == 0) // con limit
  876.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;
  877.             if (!search.equals("")) { // con search
  878.                 ISQLQueryObject sqlQueryObjectSoggetti = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  879.                 sqlQueryObjectSoggetti.addFromTable(CostantiDB.SOGGETTI);
  880.                 sqlQueryObjectSoggetti.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  881.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.SOGGETTI, "nome_soggetto");
  882.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.SOGGETTI, "tipo_soggetto");
  883.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.SOGGETTI, "id");
  884.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI, "id_soggetto");
  885.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI, "id_accordo_cooperazione");

  886.                 sqlQueryObjectSoggetti.addWhereCondition(true,
  887.                         sqlQueryObjectSoggetti.getWhereLikeCondition(CostantiDB.SOGGETTI+".nome_soggetto", search, true, true),
  888.                         CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_soggetto="+CostantiDB.SOGGETTI+".id");

  889.                 sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_accordo_cooperazione=?");

  890.                 sqlQueryObjectSoggetti.setANDLogicOperator(true);
  891.                 sqlQueryObjectSoggetti.addOrderBy("tipo_soggetto");
  892.                 sqlQueryObjectSoggetti.addOrderBy("nome_soggetto");
  893.                 sqlQueryObjectSoggetti.setSortType(true);
  894.                 sqlQueryObjectSoggetti.setLimit(limit);
  895.                 sqlQueryObjectSoggetti.setOffset(offset);
  896.                 queryString = sqlQueryObjectSoggetti.createSQLQuery();
  897.             } else {
  898.                 // senza search
  899.                 ISQLQueryObject sqlQueryObjectSoggetti = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  900.                 sqlQueryObjectSoggetti.addFromTable(CostantiDB.SOGGETTI);
  901.                 sqlQueryObjectSoggetti.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  902.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.SOGGETTI, "nome_soggetto");
  903.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.SOGGETTI, "tipo_soggetto");
  904.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.SOGGETTI, "id");
  905.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI, "id_soggetto");
  906.                 sqlQueryObjectSoggetti.addSelectField(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI, "id_accordo_cooperazione");

  907.                 sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_accordo_cooperazione=?");
  908.                 sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_soggetto="+CostantiDB.SOGGETTI+".id");

  909.                 sqlQueryObjectSoggetti.setANDLogicOperator(true);
  910.                 sqlQueryObjectSoggetti.addOrderBy("tipo_soggetto");
  911.                 sqlQueryObjectSoggetti.addOrderBy("nome_soggetto");
  912.                 sqlQueryObjectSoggetti.setSortType(true);
  913.                 sqlQueryObjectSoggetti.setLimit(limit);
  914.                 sqlQueryObjectSoggetti.setOffset(offset);
  915.                 queryString = sqlQueryObjectSoggetti.createSQLQuery();
  916.             }


  917.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(queryString));

  918.             stmt = con.prepareStatement(queryString);
  919.             stmt.setLong(1, idAccordo);
  920.             risultato = stmt.executeQuery();

  921.             while (risultato.next()) {

  922.                 String tipo = risultato.getString("tipo_soggetto");
  923.                 String nome = risultato.getString("nome_soggetto");
  924.                 IDSoggetto idSogg = new IDSoggetto(tipo,nome);
  925.                 idSoggetti.add(idSogg);

  926.             }

  927.         } catch (Exception se) {

  928.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  929.         } finally {

  930.             //Chiudo statement and resultset
  931.             JDBCUtilities.closeResources(risultato, stmt);

  932.             this.driver.closeConnection(con);
  933.         }


  934.         return idSoggetti;
  935.     }

  936.     protected List<AccordoCooperazione> accordiCoopWithSoggettoPartecipante(IDSoggetto idSoggetto) throws DriverRegistroServiziException {
  937.         String nomeMetodo = "accordiCoopWithSoggettoPartecipante";

  938.         String queryString;

  939.         Connection con = null;
  940.         PreparedStatement stmt = null;
  941.         ResultSet risultato = null;

  942.         ArrayList<AccordoCooperazione> idAccordoCooperazione = new ArrayList<AccordoCooperazione>();

  943.         if (this.driver.atomica) {
  944.             try {
  945.                 con = this.driver.getConnectionFromDatasource("accordiCoopWithSoggettoPartecipante");

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

  948.             }

  949.         } else
  950.             con = this.driver.globalConnection;

  951.         this.driver.logDebug("operazione this.driver.atomica = " + this.driver.atomica);

  952.         try {
  953.             ISQLQueryObject sqlQueryObjectSoggetti = SQLObjectFactory.createSQLQueryObject(this.driver.tipoDB);
  954.             sqlQueryObjectSoggetti.addFromTable(CostantiDB.SOGGETTI);
  955.             sqlQueryObjectSoggetti.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI);
  956.             sqlQueryObjectSoggetti.addFromTable(CostantiDB.ACCORDI_COOPERAZIONE);
  957.             sqlQueryObjectSoggetti.addSelectAliasField(CostantiDB.ACCORDI_COOPERAZIONE, "id","idAccordoCooperazione");
  958.             sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_accordo_cooperazione="+CostantiDB.ACCORDI_COOPERAZIONE+".id");
  959.             sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.ACCORDI_COOPERAZIONE_PARTECIPANTI+".id_soggetto="+CostantiDB.SOGGETTI+".id");
  960.             sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.SOGGETTI+".tipo_soggetto=?");
  961.             sqlQueryObjectSoggetti.addWhereCondition(CostantiDB.SOGGETTI+".nome_soggetto=?");
  962.             sqlQueryObjectSoggetti.setANDLogicOperator(true);
  963.             queryString = sqlQueryObjectSoggetti.createSQLQuery();

  964.             this.driver.logDebug("eseguo query : " + DriverRegistroServiziDB_LIB.formatSQLString(queryString));

  965.             stmt = con.prepareStatement(queryString);
  966.             stmt.setString(1,idSoggetto.getTipo());
  967.             stmt.setString(2,idSoggetto.getNome());
  968.             risultato = stmt.executeQuery();

  969.             while (risultato.next()) {

  970.                 long idAccordoCooperazioneLong = risultato.getLong("idAccordoCooperazione");
  971.                 idAccordoCooperazione.add(this.getAccordoCooperazione(idAccordoCooperazioneLong));

  972.             }

  973.         } catch (Exception se) {

  974.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB::" + nomeMetodo + "] Exception: " + se.getMessage(),se);
  975.         } finally {

  976.             //Chiudo statement and resultset
  977.             JDBCUtilities.closeResources(risultato, stmt);

  978.             this.driver.closeConnection(con);
  979.         }


  980.         return idAccordoCooperazione;
  981.     }
  982.    
  983.     protected void validaStatoAccordoCooperazione(AccordoCooperazione ac) throws ValidazioneStatoPackageException{

  984.         ValidazioneStatoPackageException erroreValidazione =
  985.                 new ValidazioneStatoPackageException("AccordoCooperazione",ac.getStatoPackage(),null);

  986.         try{

  987.             // Controlli di visibilita
  988.             if(ac.getPrivato()==null || ac.getPrivato()==false){
  989.                 if(ac.getSoggettoReferente()!=null){
  990.                     IDSoggetto idS = new IDSoggetto(ac.getSoggettoReferente().getTipo(),ac.getSoggettoReferente().getNome());
  991.                     try{
  992.                         Soggetto s = this.driver.getSoggetto(idS);
  993.                         if(s.getPrivato()!=null && s.getPrivato()){
  994.                             erroreValidazione.addErroreValidazione("soggetto referente ["+idS+"] con visibilita' privata, in un accordo di cooperazione con visibilita' pubblica");
  995.                         }
  996.                     }catch(DriverRegistroServiziNotFound dNot){}
  997.                 }
  998.             }
  999.             if(ac.getElencoPartecipanti()!=null){
  1000.                 AccordoCooperazionePartecipanti partecipanti = ac.getElencoPartecipanti();
  1001.                 if(partecipanti.sizeSoggettoPartecipanteList()>=2){
  1002.                     for(int i=0; i<partecipanti.sizeSoggettoPartecipanteList(); i++){
  1003.                         IdSoggetto idSoggettoPartecipante = partecipanti.getSoggettoPartecipante(i);
  1004.                         if(idSoggettoPartecipante.getIdSoggetto()!=null && idSoggettoPartecipante.getIdSoggetto()>0){
  1005.                             try{
  1006.                                 Soggetto s = this.driver.getSoggetto(partecipanti.getSoggettoPartecipante(i).getIdSoggetto());
  1007.                                 if(s.getPrivato()!=null && s.getPrivato()){
  1008.                                     erroreValidazione.addErroreValidazione("soggetto partecipante ["+s.getTipo()+"/"+s.getNome()+"] con visibilita' privata, in un accordo di cooperazione con visibilita' pubblica");
  1009.                                 }
  1010.                             }catch(DriverRegistroServiziNotFound dNot){}
  1011.                         }
  1012.                         else if(idSoggettoPartecipante.getTipo()!=null && idSoggettoPartecipante.getNome()!=null){
  1013.                             try{
  1014.                                 Soggetto s = this.driver.getSoggetto(new IDSoggetto(idSoggettoPartecipante.getTipo(), idSoggettoPartecipante.getNome()));
  1015.                                 if(s.getPrivato()!=null && s.getPrivato()){
  1016.                                     erroreValidazione.addErroreValidazione("soggetto partecipante ["+s.getTipo()+"/"+s.getNome()+"] con visibilita' privata, in un accordo di cooperazione con visibilita' pubblica");
  1017.                                 }
  1018.                             }catch(DriverRegistroServiziNotFound dNot){}
  1019.                         }
  1020.                     }
  1021.                 }
  1022.             }

  1023.             // Controlli di stato
  1024.             if(StatiAccordo.bozza.toString().equals(ac.getStatoPackage()) == false){

  1025.                 // Validazione necessaria sia ad uno stato operativo che finale
  1026.                 if(ac.getElencoPartecipanti()==null){
  1027.                     erroreValidazione.addErroreValidazione("soggetti partecipanti non definiti");  
  1028.                 }
  1029.                 else if(ac.getElencoPartecipanti().sizeSoggettoPartecipanteList()<=0){
  1030.                     erroreValidazione.addErroreValidazione("soggetti partecipanti non definiti");  
  1031.                 }else{
  1032.                     if(ac.getElencoPartecipanti().sizeSoggettoPartecipanteList()<2){
  1033.                         erroreValidazione.addErroreValidazione("almeno 2 soggetti partecipanti devono essere definiti");    
  1034.                     }
  1035.                 }

  1036.             }

  1037.         }catch(Exception e){
  1038.             throw new ValidazioneStatoPackageException(e);
  1039.         }

  1040.         if(erroreValidazione.sizeErroriValidazione()>0){
  1041.             throw erroreValidazione;
  1042.         }
  1043.     }
  1044. }