DriverRegistroServiziDB_documentiLIB.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 static org.openspcoop2.core.constants.CostantiDB.CREATE;
  22. import static org.openspcoop2.core.constants.CostantiDB.DELETE;
  23. import static org.openspcoop2.core.constants.CostantiDB.UPDATE;

  24. import java.sql.Connection;
  25. import java.sql.PreparedStatement;
  26. import java.sql.ResultSet;
  27. import java.sql.SQLException;
  28. import java.sql.Timestamp;
  29. import java.util.ArrayList;
  30. import java.util.Date;
  31. import java.util.List;

  32. import org.openspcoop2.core.commons.DBUtils;
  33. import org.openspcoop2.core.constants.CostantiDB;
  34. import org.openspcoop2.core.registry.Documento;
  35. import org.openspcoop2.core.registry.constants.ProprietariDocumento;
  36. import org.openspcoop2.core.registry.driver.BeanUtilities;
  37. import org.openspcoop2.core.registry.driver.DriverRegistroServiziException;
  38. import org.openspcoop2.core.registry.driver.DriverRegistroServiziNotFound;
  39. import org.openspcoop2.utils.jdbc.IJDBCAdapter;
  40. import org.openspcoop2.utils.jdbc.JDBCAdapterFactory;
  41. import org.openspcoop2.utils.jdbc.JDBCUtilities;
  42. import org.openspcoop2.utils.sql.ISQLQueryObject;
  43. import org.openspcoop2.utils.sql.SQLObjectFactory;

  44. /**
  45.  * Classe utilizzata per effettuare query ad un registro dei servizi openspcoop
  46.  * formato db.
  47.  *
  48.  *
  49.  * @author Sandra Giangrandi (sandra@link.it)
  50.  * @author Stefano Corallo (corallo@link.it)
  51.  * @author $Author$
  52.  * @version $Rev$, $Date$
  53.  */
  54. public class DriverRegistroServiziDB_documentiLIB {
  55.        
  56.    
  57.     public static void CRUDDocumento(int type, List<Documento> documenti, long idProprietario,
  58.             ProprietariDocumento tipologiaProprietarioDocumento, Connection connection,
  59.             String tipoDatabase) throws DriverRegistroServiziException {
  60.        
  61.         // NOTA: l'update dei documenti, essendo mega di documenti non puo' essere implementata come delete + create
  62.        
  63.         PreparedStatement stm = null;
  64.         ResultSet rs=null;
  65.         String sqlQuery;

  66.         if(idProprietario <= 0 )
  67.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] id proprietario non definito");
  68.        
  69.         IJDBCAdapter jdbcAdapter = null;
  70.        
  71.         try {

  72.             jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  73.            
  74.             switch (type) {
  75.             case CREATE:

  76.                 if((documenti == null))
  77.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] L'oggetto documenti non puo essere null");
  78.                
  79.                 for(int i=0; i<documenti.size(); i++){
  80.                
  81.                     Documento doc = documenti.get(i);
  82.                     if(doc.getFile()==null || "".equals(doc.getFile()))
  83.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Nome non definito per documento ["+i+"]");
  84.                     if(doc.getRuolo()==null || "".equals(doc.getRuolo()))
  85.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Ruolo non definito per documento ["+doc.getFile()+"]");
  86.                     if(doc.getTipo()==null || "".equals(doc.getTipo()))
  87.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Tipo non definito per documento ["+doc.getFile()+"]");
  88.                     if(doc.getByteContenuto()==null || doc.getByteContenuto().length<=0)
  89.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Contenuto non definito per documento ["+doc.getFile()+"]");
  90.                     BeanUtilities.validateTipoRuolo(doc.getTipo(),doc.getRuolo());
  91.                    
  92.                     byte[] contenuto = doc.getByteContenuto();
  93.                     if(contenuto.length<3){
  94.                         String test = new String(contenuto);
  95.                         if("".equals(test.trim().replaceAll("\n", ""))){
  96.                             // eliminare \n\n
  97.                             contenuto = null;  
  98.                         }
  99.                     }
  100.                    
  101.                    
  102.                     // create
  103.                     ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  104.                     sqlQueryObject.addInsertTable(CostantiDB.DOCUMENTI);
  105.                     sqlQueryObject.addInsertField("ruolo", "?");
  106.                     sqlQueryObject.addInsertField("tipo", "?");
  107.                     sqlQueryObject.addInsertField("nome", "?");
  108.                     sqlQueryObject.addInsertField("id_proprietario", "?");
  109.                     sqlQueryObject.addInsertField("tipo_proprietario", "?");
  110.                     if(contenuto!=null)
  111.                         sqlQueryObject.addInsertField("contenuto", "?");
  112.                     if(doc.getOraRegistrazione()!=null)
  113.                         sqlQueryObject.addInsertField("ora_registrazione", "?");
  114.                     sqlQuery = sqlQueryObject.createSQLInsert();
  115.                     stm = connection.prepareStatement(sqlQuery);
  116.                     stm.setString(1, doc.getRuolo());
  117.                     stm.setString(2, doc.getTipo());
  118.                     stm.setString(3, doc.getFile());
  119.                     stm.setLong(4, idProprietario);
  120.                     stm.setString(5, tipologiaProprietarioDocumento.toString());
  121.                     int index = 6;
  122.                     if(contenuto!=null){
  123.                         jdbcAdapter.setBinaryData(stm,index,contenuto);
  124.                         index++;
  125.                     }
  126.                     if(doc.getOraRegistrazione()!=null){
  127.                         stm.setTimestamp(index, new Timestamp(doc.getOraRegistrazione().getTime()));
  128.                         index++;
  129.                     }
  130.                    
  131.                     DriverRegistroServiziDB_LIB.log.debug("CRUDConnettore CREATE : \n" + DBUtils.formatSQLString(sqlQuery, doc.getRuolo(), doc.getTipo(), doc.getFile(), idProprietario, tipologiaProprietarioDocumento.toString()));
  132.    
  133.                     int n = stm.executeUpdate();
  134.                     stm.close();
  135.                     DriverRegistroServiziDB_LIB.log.debug("Inserted " + n + " row(s)");
  136.        
  137.                     sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  138.                     sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  139.                     sqlQueryObject.addSelectField("id");
  140.                     sqlQueryObject.addSelectField("ora_registrazione");
  141.                     sqlQueryObject.addWhereCondition("ruolo = ?");
  142.                     sqlQueryObject.addWhereCondition("tipo = ?");
  143.                     sqlQueryObject.addWhereCondition("nome = ?");
  144.                     sqlQueryObject.addWhereCondition("id_proprietario = ?");
  145.                     sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  146.                     sqlQueryObject.setANDLogicOperator(true);
  147.                     sqlQuery = sqlQueryObject.createSQLQuery();
  148.                     stm = connection.prepareStatement(sqlQuery);
  149.                     stm.setString(1, doc.getRuolo());
  150.                     stm.setString(2, doc.getTipo());
  151.                     stm.setString(3, doc.getFile());
  152.                     stm.setLong(4, idProprietario);
  153.                     stm.setString(5, tipologiaProprietarioDocumento.toString());
  154.    
  155.                     DriverRegistroServiziDB_LIB.log.debug("Recupero idConnettore e oraRegistrazione inserito : \n" + DBUtils.formatSQLString(sqlQuery,doc.getRuolo(), doc.getTipo(), doc.getFile(), idProprietario, tipologiaProprietarioDocumento.toString()));
  156.    
  157.                     rs = stm.executeQuery();
  158.    
  159.                     if (rs.next()) {
  160.                         documenti.get(i).setId(rs.getLong("id"));
  161.                         documenti.get(i).setOraRegistrazione(rs.getTimestamp("ora_registrazione"));
  162.                     } else {
  163.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] Errore tentanto di effettuare la select dopo una create, non riesco a recuperare l'id!");
  164.                     }
  165.    
  166.                     rs.close();
  167.                     stm.close();
  168.                    
  169.                 }
  170.                 break;

  171.             case UPDATE:
  172.                
  173.                 if((documenti == null))
  174.                     throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] L'oggetto documenti non puo essere null");
  175.                
  176.                 // Prelevo vecchia lista
  177.                 List<Documento> oldLista = null;
  178.                 try{
  179.                     oldLista = DriverRegistroServiziDB_documentiLIB.getListaDocumenti(idProprietario,tipologiaProprietarioDocumento, false, connection, tipoDatabase);
  180.                 }catch(DriverRegistroServiziNotFound dNotFound){
  181.                     oldLista = new ArrayList<Documento>();
  182.                 }
  183.                
  184.                 // Gestico la nuova immagine
  185.                 for(int i=0; i<documenti.size(); i++){
  186.                    
  187.                     Documento doc = documenti.get(i);
  188.                     if(doc.getFile()==null || "".equals(doc.getFile()))
  189.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Nome non definito per documento ["+i+"]");
  190.                     if(doc.getRuolo()==null || "".equals(doc.getRuolo()))
  191.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Ruolo non definito per documento ["+doc.getFile()+"]");
  192.                     if(doc.getTipo()==null || "".equals(doc.getTipo()))
  193.                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Tipo non definito per documento ["+doc.getFile()+"]");
  194.                     BeanUtilities.validateTipoRuolo(doc.getTipo(),doc.getRuolo());
  195.                    
  196.                     //if(doc.getId()<=0){
  197.                     // Rileggo sempre id, puo' essere diverso (es. importato tramite sincronizzazioni)
  198.                     String tipologiaProprietarioDocumentoString = null;
  199.                     if(tipologiaProprietarioDocumento!=null){
  200.                         tipologiaProprietarioDocumentoString = tipologiaProprietarioDocumento.toString();
  201.                     }
  202.                     doc.setId(DBUtils.getIdDocumento(doc.getFile(), doc.getTipo(), doc.getRuolo(), idProprietario, connection,
  203.                             DriverRegistroServiziDB_LIB.tipoDB, tipologiaProprietarioDocumentoString));
  204.                     //}
  205.                     // L'ora di registrazione deve essere impostata solo se non presente, in tale caso siamo in un caso di creazione del nuovo documento
  206.                     if(doc.getOraRegistrazione()==null){
  207.                         doc.setOraRegistrazione(new Date());
  208.                     }
  209.                    
  210.                     // Assegno corretto idProprietario se id e' diverso (es. importato tramite sincronizzazioni)
  211.                     doc.setIdProprietarioDocumento(idProprietario);
  212.                    
  213.                     boolean documentoGiaPresente = false;
  214.                     boolean documentoDaAggiornare = false;
  215.                     if(doc.getId()>0){
  216.                         for(int j=0; j<oldLista.size(); j++){
  217.                             Documento old = oldLista.get(j);
  218.                             /*System.out.println("OLD["+old.getRuolo()+"]==ATTUALE["+doc.getRuolo()+"] ("+doc.getRuolo().equals(old.getRuolo())+")");
  219.                             System.out.println("OLD["+old.getTipo()+"]==ATTUALE["+doc.getTipo()+"] ("+doc.getTipo().equals(old.getTipo())+")");
  220.                             System.out.println("OLD["+old.getFile()+"]==ATTUALE["+doc.getFile()+"] ("+doc.getFile().equals(old.getFile())+")");
  221.                             System.out.println("OLD["+old.getIdProprietarioDocumento().longValue()+"]==ATTUALE["+doc.getIdProprietarioDocumento().longValue()+"] ("+(doc.getIdProprietarioDocumento().longValue() == old.getIdProprietarioDocumento().longValue())+")");
  222.                             System.out.println("OLD["+old.getTipoProprietarioDocumento()+"]==ATTUALE["+tipologiaProprietarioDocumento+"] ("+tipologiaProprietarioDocumento.equals(old.getTipoProprietarioDocumento())+")");
  223.                             if(doc.getRuolo().equals(old.getRuolo()) &&
  224.                                     doc.getTipo().equals(old.getTipo()) &&
  225.                                     doc.getFile().equals(old.getFile()) &&
  226.                                     ( (doc.getIdProprietarioDocumento().longValue()) == (old.getIdProprietarioDocumento().longValue()) ) &&
  227.                                     tipologiaProprietarioDocumento.equals(old.getTipoProprietarioDocumento()) ){*/
  228.                            
  229.                             //System.out.println("OLD["+old.getId().longValue()+"]==ATTUALE["+doc.getId().longValue()+"] ("+((doc.getId().longValue() == old.getId().longValue()))+")");
  230.                             if(doc.getId().longValue() == old.getId().longValue()){    
  231.                                     documentoGiaPresente = true; // non devo fare una insert, ma una update...
  232.                                    
  233.                                     if(doc.getOraRegistrazione()==null)
  234.                                         throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] OraRegistrazione non definita per documento, precedentemente gia inserito, ["+doc.getFile()+"] ["+doc.getTipo()+"] ["+tipologiaProprietarioDocumento+"] ["+idProprietario+"]");
  235.                                            
  236.                                     // rimuovo la vecchia immagine del documento dalla lista dei doc vecchi
  237.                                     oldLista.remove(j);
  238.                                    
  239.                                     // Il documento e' lo stesso, rimane da verificare che sia un documento successivo di modifica.
  240.                                     if(doc.getOraRegistrazione().after(old.getOraRegistrazione())){
  241.                                         documentoDaAggiornare = true;
  242.                                     }
  243.                             }
  244.                         }
  245.                     }

  246.                     byte[] contenuto = null;
  247.                     if( (documentoGiaPresente == false) || (documentoDaAggiornare) ){
  248.                         // Il contenuto ci deve essere
  249.                         if(doc.getByteContenuto()==null || doc.getByteContenuto().length<=0)
  250.                             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Contenuto non definito per documento ["+doc.getFile()+"] ["+doc.getTipo()+"] ["+tipologiaProprietarioDocumento+"] ["+idProprietario+"]");

  251.                         contenuto = doc.getByteContenuto();
  252.                         if(contenuto.length<3){
  253.                             String test = new String(contenuto);
  254.                             if("".equals(test.trim().replaceAll("\n", ""))){
  255.                                 // eliminare \n\n
  256.                                 contenuto = null;  
  257.                             }
  258.                         }
  259.                     }
  260.                    
  261.                     if(documentoGiaPresente){
  262.                         if(documentoDaAggiornare){
  263.                             // update
  264.                             long idDocumento = doc.getId();
  265.                             if(idDocumento<=0){
  266.                                 throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] ID non definito per documento da aggiorare ["+doc.getFile()+"] ["+doc.getTipo()+"] ["+tipologiaProprietarioDocumento+"] ["+idProprietario+"]");
  267.                             }
  268.                             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  269.                             sqlQueryObject.addUpdateTable(CostantiDB.DOCUMENTI);
  270.                             sqlQueryObject.addUpdateField("tipo", "?");
  271.                             sqlQueryObject.addUpdateField("nome", "?");
  272.                             sqlQueryObject.addUpdateField("contenuto", "?");
  273.                             sqlQueryObject.addUpdateField("ora_registrazione", "?");
  274.                             sqlQueryObject.addWhereCondition("id=?");
  275.                             sqlQuery = sqlQueryObject.createSQLUpdate();
  276.                             stm = connection.prepareStatement(sqlQuery);
  277.                             stm.setString(1, doc.getTipo());
  278.                             stm.setString(2, doc.getFile());
  279.                             jdbcAdapter.setBinaryData(stm,3,contenuto);
  280.                             stm.setTimestamp(4, new Timestamp(doc.getOraRegistrazione().getTime()));
  281.                             stm.setLong(5, idDocumento);
  282.                             stm.executeUpdate();
  283.                             stm.close();
  284.                         }
  285.                     }else{
  286.                         // insert
  287.                         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  288.                         sqlQueryObject.addInsertTable(CostantiDB.DOCUMENTI);
  289.                         sqlQueryObject.addInsertField("ruolo", "?");
  290.                         sqlQueryObject.addInsertField("tipo", "?");
  291.                         sqlQueryObject.addInsertField("nome", "?");
  292.                         sqlQueryObject.addInsertField("id_proprietario", "?");
  293.                         sqlQueryObject.addInsertField("tipo_proprietario", "?");
  294.                         if(contenuto!=null)
  295.                             sqlQueryObject.addInsertField("contenuto", "?");
  296.                         if(doc.getOraRegistrazione()!=null)
  297.                             sqlQueryObject.addInsertField("ora_registrazione", "?");
  298.                         sqlQuery = sqlQueryObject.createSQLInsert();
  299.                         stm = connection.prepareStatement(sqlQuery);
  300.                         stm.setString(1, doc.getRuolo());
  301.                         stm.setString(2, doc.getTipo());
  302.                         stm.setString(3, doc.getFile());
  303.                         stm.setLong(4, idProprietario);
  304.                         stm.setString(5, tipologiaProprietarioDocumento.toString());
  305.                         int index = 6;
  306.                         if(contenuto!=null){
  307.                             jdbcAdapter.setBinaryData(stm,index,contenuto);
  308.                             index++;
  309.                         }
  310.                         if(doc.getOraRegistrazione()!=null){
  311.                             stm.setTimestamp(index, new Timestamp(doc.getOraRegistrazione().getTime()));
  312.                             index++;
  313.                         }
  314.                        
  315.                         DriverRegistroServiziDB_LIB.log.debug("CRUDDocumento UPDATE : \n" + DBUtils.formatSQLString(sqlQuery, doc.getRuolo(), doc.getTipo(), doc.getFile(), idProprietario, tipologiaProprietarioDocumento.toString()));
  316.        
  317.                         int n = stm.executeUpdate();
  318.                         stm.close();
  319.                         DriverRegistroServiziDB_LIB.log.debug("Inserted " + n + " row(s)");
  320.            
  321.                         sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  322.                         sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  323.                         sqlQueryObject.addSelectField("id");
  324.                         sqlQueryObject.addWhereCondition("ruolo = ?");
  325.                         sqlQueryObject.addWhereCondition("tipo = ?");
  326.                         sqlQueryObject.addWhereCondition("nome = ?");
  327.                         sqlQueryObject.addWhereCondition("id_proprietario = ?");
  328.                         sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  329.                         sqlQueryObject.setANDLogicOperator(true);
  330.                         sqlQuery = sqlQueryObject.createSQLQuery();
  331.                         stm = connection.prepareStatement(sqlQuery);
  332.                         stm.setString(1, doc.getRuolo());
  333.                         stm.setString(2, doc.getTipo());
  334.                         stm.setString(3, doc.getFile());
  335.                         stm.setLong(4, idProprietario);
  336.                         stm.setString(5, tipologiaProprietarioDocumento.toString());
  337.        
  338.                         DriverRegistroServiziDB_LIB.log.debug("Recupero idConnettore inserito : \n" + DBUtils.formatSQLString(sqlQuery,doc.getRuolo(), doc.getTipo(), doc.getFile(), idProprietario,tipologiaProprietarioDocumento.toString()));
  339.        
  340.                         rs = stm.executeQuery();
  341.        
  342.                         if (rs.next()) {
  343.                             documenti.get(i).setId(rs.getLong("id"));
  344.                         } else {
  345.                             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDConnettore] Errore tentanto di effettuare la select dopo una create, non riesco a recuperare l'id!");
  346.                         }
  347.        
  348.                         rs.close();
  349.                         stm.close();
  350.                     }
  351.                    
  352.                 }
  353.                
  354.                 if(oldLista.size()>0){
  355.                     // Qualche documento e' stato cancellato.
  356.                     // Non e' piu' presente.
  357.                     for(int j=0; j<oldLista.size(); j++){
  358.                         Documento old = oldLista.get(j);
  359.                        
  360.                         ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  361.                         sqlQueryObject.addDeleteTable(CostantiDB.DOCUMENTI);
  362.                         sqlQueryObject.addWhereCondition("id=?");
  363.                         sqlQuery = sqlQueryObject.createSQLDelete();
  364.                         stm = connection.prepareStatement(sqlQuery);
  365.                         stm.setLong(1, old.getId());
  366.                         stm.executeUpdate();
  367.                         stm.close();
  368.                     }
  369.                 }
  370.                
  371.                 break;

  372.             case DELETE:
  373.                 // delete
  374.                
  375.                 ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  376.                 sqlQueryObject.addDeleteTable(CostantiDB.DOCUMENTI);
  377.                 sqlQueryObject.addWhereCondition("id_proprietario=?");
  378.                 sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  379.                 sqlQueryObject.setANDLogicOperator(true);
  380.                 sqlQuery = sqlQueryObject.createSQLDelete();
  381.                 stm = connection.prepareStatement(sqlQuery);
  382.                 stm.setLong(1, idProprietario);
  383.                 stm.setString(2, tipologiaProprietarioDocumento.toString());
  384.                 stm.executeUpdate();
  385.                 stm.close();

  386.                 DriverRegistroServiziDB_LIB.log.debug("CRUDDocumento DELETE : \n" + DBUtils.formatSQLString(sqlQuery, idProprietario));

  387.                 break;
  388.             }

  389.         } catch (SQLException se) {
  390.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] SQLException : " + se.getMessage(),se);
  391.         }catch (Exception se) {
  392.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::CRUDDocumento] Exception : " + se.getMessage(),se);
  393.         } finally {
  394.             JDBCUtilities.closeResources(rs, stm);
  395.         }
  396.     }
  397.    
  398.    
  399.     public static List<Documento> getListaDocumenti(long idProprietario, ProprietariDocumento tipologiaProprietarioDocumento, boolean readBytes,
  400.             Connection connection,
  401.             String tipoDatabase) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  402.         return DriverRegistroServiziDB_documentiLIB.getListaDocumenti(null,idProprietario,tipologiaProprietarioDocumento,readBytes,connection,tipoDatabase);
  403.     }
  404.        
  405.     public static List<Documento> getListaDocumenti(String ruoloDocumenti, long idProprietario,
  406.             ProprietariDocumento tipologiaProprietarioDocumento, boolean readBytes, Connection connection,
  407.             String tipoDatabase) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  408.        
  409.         PreparedStatement stm = null;
  410.         ResultSet rs=null;
  411.         String sqlQuery;

  412.         if(idProprietario <= 0 )
  413.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::getListaDocumenti] id proprietario non definito");
  414.        
  415.         try {
  416.        
  417.             List<Documento> documenti = new ArrayList<Documento>();
  418.            
  419.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  420.             sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  421.             sqlQueryObject.addSelectField("id");
  422.             sqlQueryObject.addWhereCondition("id_proprietario = ?");
  423.             sqlQueryObject.addWhereCondition("tipo_proprietario = ?");
  424.             sqlQueryObject.setANDLogicOperator(true);
  425.             if(ruoloDocumenti!=null)
  426.                 sqlQueryObject.addWhereCondition("ruolo = ?");
  427.             sqlQueryObject.setANDLogicOperator(true);
  428.             sqlQuery = sqlQueryObject.createSQLQuery();
  429.             stm = connection.prepareStatement(sqlQuery);
  430.             stm.setLong(1, idProprietario);
  431.             stm.setString(2, tipologiaProprietarioDocumento.toString());
  432.             if(ruoloDocumenti!=null)
  433.                 stm.setString(3, ruoloDocumenti);
  434.             rs = stm.executeQuery();
  435.            
  436.             while(rs.next()){
  437.                 Documento doc = DriverRegistroServiziDB_documentiLIB.getDocumento(rs.getLong("id"),readBytes, connection,tipoDatabase);
  438.                 documenti.add(doc);
  439.             }
  440.            
  441.             if(documenti.size()<=0)
  442.                 throw new DriverRegistroServiziNotFound("Documenti con ruolo["+ruoloDocumenti+"] e idProprietario["+idProprietario+"] non trovati");
  443.            
  444.             return documenti;

  445.         } catch (SQLException se) {
  446.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::getListaDocumenti] SQLException : " + se.getMessage(),se);
  447.         }catch (DriverRegistroServiziNotFound dnf) {
  448.             throw dnf;
  449.         }catch (Exception se) {
  450.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::getListaDocumenti] Exception : " + se.getMessage(),se);
  451.         } finally {
  452.             JDBCUtilities.closeResources(rs, stm);
  453.         }
  454.     }
  455.    
  456.     public static Documento getDocumento(long id, boolean readBytes, Connection connection, String tipoDatabase) throws DriverRegistroServiziException,DriverRegistroServiziNotFound {
  457.        
  458.         PreparedStatement stm = null;
  459.         ResultSet rs=null;
  460.         String sqlQuery;

  461.         if(id <= 0 )
  462.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::getDocumento] id non definito");
  463.        
  464.         try {
  465.        
  466.             IJDBCAdapter jdbcAdapter = null;
  467.             if(readBytes){
  468.                 // jdbcAdapter = org.openspcoop.utils.jdbc.JDBCAdapterFactory.createJDBCAdapter(DriverRegistroServiziDB_LIB.tipoDB);
  469.                 jdbcAdapter = JDBCAdapterFactory.createJDBCAdapter(tipoDatabase);
  470.             }
  471.                
  472.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(DriverRegistroServiziDB_LIB.tipoDB);
  473.             sqlQueryObject.addFromTable(CostantiDB.DOCUMENTI);
  474.             sqlQueryObject.addSelectField("ruolo");
  475.             sqlQueryObject.addSelectField("tipo");
  476.             sqlQueryObject.addSelectField("nome");
  477.             sqlQueryObject.addSelectField("id_proprietario");
  478.             sqlQueryObject.addSelectField("tipo_proprietario");
  479.             sqlQueryObject.addSelectField("ora_registrazione");
  480.             sqlQueryObject.addSelectField("id");
  481.             if(readBytes)
  482.                 sqlQueryObject.addSelectField("contenuto");
  483.             sqlQueryObject.addWhereCondition("id = ?");
  484.             sqlQueryObject.setANDLogicOperator(true);
  485.             sqlQuery = sqlQueryObject.createSQLQuery();
  486.             stm = connection.prepareStatement(sqlQuery);
  487.             stm.setLong(1, id);
  488.             rs = stm.executeQuery();
  489.            
  490.             Documento doc = null;
  491.             if(rs.next()){
  492.                 doc = new Documento();
  493.                 doc.setRuolo(rs.getString("ruolo"));
  494.                 doc.setTipo(rs.getString("tipo"));
  495.                 doc.setFile(rs.getString("nome"));
  496.                 doc.setIdProprietarioDocumento(rs.getLong("id_proprietario"));
  497.                 doc.setTipoProprietarioDocumento(rs.getString("tipo_proprietario"));
  498.                 doc.setOraRegistrazione(rs.getTimestamp("ora_registrazione"));
  499.                 doc.setId(rs.getLong("id"));
  500.                 if(readBytes){
  501.                     doc.setByteContenuto(jdbcAdapter.getBinaryData(rs,8));
  502.                 }
  503.             }
  504.            
  505.             if(doc==null)
  506.                 throw new DriverRegistroServiziNotFound("Documento con id["+id+"] non trovato");
  507.            
  508.             return doc;

  509.         } catch (SQLException se) {
  510.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::getDocumento] SQLException : " + se.getMessage(),se);
  511.         }catch (DriverRegistroServiziNotFound dnf) {
  512.             throw dnf;
  513.         }catch (Exception se) {
  514.             throw new DriverRegistroServiziException("[DriverRegistroServiziDB_LIB::getDocumento] Exception : " + se.getMessage(),se);
  515.         } finally {
  516.             JDBCUtilities.closeResources(rs, stm);
  517.         }
  518.     }
  519.    
  520. }