DriverAuditDBAppender.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */


  20. package org.openspcoop2.web.lib.audit;

  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.sql.Timestamp;
  26. import java.text.SimpleDateFormat;
  27. import java.util.ArrayList;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.List;

  31. import org.openspcoop2.core.commons.ISearch;
  32. import org.openspcoop2.utils.TipiDatabase;
  33. import org.openspcoop2.utils.date.DateUtils;
  34. import org.openspcoop2.utils.jdbc.CustomKeyGeneratorObject;
  35. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKey;
  36. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyJDBCType;
  37. import org.openspcoop2.utils.jdbc.InsertAndGeneratedKeyObject;
  38. import org.openspcoop2.utils.sql.ISQLQueryObject;
  39. import org.openspcoop2.utils.sql.SQLObjectFactory;
  40. import org.openspcoop2.web.lib.audit.costanti.Costanti;
  41. import org.openspcoop2.web.lib.audit.log.Binary;
  42. import org.openspcoop2.web.lib.audit.log.Operation;
  43. import org.openspcoop2.web.lib.audit.log.constants.Stato;
  44. import org.openspcoop2.web.lib.audit.log.constants.Tipologia;

  45. /**
  46.  * Sono forniti metodi per la lettura dei dati di Users
  47.  *
  48.  *
  49.  * @author Andrea Poli (apoli@link.it)
  50.  * @author Stefano Corallo (corallo@link.it)
  51.  * @author Sandra Giangrandi (sandra@link.it)
  52.  * @author $Author$
  53.  * @version $Rev$, $Date$
  54.  *
  55.  */

  56. public class DriverAuditDBAppender {

  57.     /** Connessione al Database */
  58.     private Connection connectionDB;

  59.     // Tipo database passato al momento della creazione dell'oggetto
  60.     private String tipoDB = null;

  61.     public DriverAuditDBAppender(Connection con, String tipoDB) throws AuditException {
  62.         this.connectionDB = con;
  63.         if (con == null)
  64.             throw new AuditException("Connessione al Database non definita");

  65.         this.tipoDB = tipoDB;
  66.         if (tipoDB == null)
  67.             throw new AuditException("Il tipoDatabase non puo essere null.");
  68.     }

  69.    
  70.    
  71.    
  72.     public Operation getOperation(long id) throws AuditException {
  73.        
  74.         Operation operation = null;
  75.         PreparedStatement stm = null;
  76.         ResultSet rs = null;

  77.         if(id<=0){
  78.             throw new AuditException("[DriverAudit::getOperation]  Id non valido");
  79.         }
  80.        
  81.         try {
  82.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  83.             sqlQueryObject.addFromTable(Costanti.DB_AUDIT_OPERATIONS_TABLE);
  84.             sqlQueryObject.addSelectField("*");
  85.             sqlQueryObject.addWhereCondition("id=?");
  86.             String sqlQuery = sqlQueryObject.createSQLQuery();
  87.             stm = this.connectionDB.prepareStatement(sqlQuery);
  88.             stm.setLong(1, id);
  89.             rs = stm.executeQuery();
  90.             if (rs.next()) {
  91.                 operation = new Operation();
  92.                 operation.setTipologia(Tipologia.toEnumConstant(rs.getString("tipo_operazione")));
  93.                 operation.setTipoOggetto(rs.getString("tipo"));
  94.                 operation.setObjectId(rs.getString("object_id"));
  95.                 operation.setObjectOldId(rs.getString("object_old_id"));
  96.                 operation.setUtente(rs.getString("utente"));
  97.                 operation.setStato(Stato.toEnumConstant(rs.getString("stato")));
  98.                 operation.setObjectDetails(rs.getString("object_details"));
  99.                 operation.setObjectClass(rs.getString("object_class"));
  100.                 operation.setError(rs.getString("error"));
  101.                 operation.setTimeRequest(rs.getTimestamp("time_request"));
  102.                 operation.setTimeExecute(rs.getTimestamp("time_execute"));
  103.                 operation.setId(id);
  104.                
  105.                 // binaries
  106.                 ArrayList<Binary> list = this.getBinaries(id);
  107.                 if(list!=null && !list.isEmpty()){
  108.                     operation.setBinaryList(list);
  109.                 }  
  110.                
  111.             }else{
  112.                 throw new AuditException("[DriverAudit::getOperation] non presente");
  113.             }
  114.        
  115.             return operation;
  116.         } catch (SQLException se) {
  117.             throw new AuditException("[DriverAudit::getOperation] SqlException: " + se.getMessage(),se);
  118.         } catch (Exception ex) {
  119.             throw new AuditException("[DriverAudit::getOperation] Exception: " + ex.getMessage(),ex);
  120.         } finally {
  121.             try {
  122.                 if(rs!=null) {
  123.                     rs.close();
  124.                 }
  125.             } catch (Exception e) {
  126.                 // ignore exception
  127.             }
  128.             try {
  129.                 if(stm!=null) {
  130.                     stm.close();
  131.                 }
  132.             } catch (Exception e) {
  133.                 // ignore exception
  134.             }
  135.         }
  136.     }
  137.    
  138.     public ArrayList<Binary> getBinaries(long idOperation) throws AuditException {
  139.        
  140.         ArrayList<Binary> binaries = new ArrayList<Binary>();
  141.         PreparedStatement stm = null;
  142.         ResultSet rs = null;

  143.         if(idOperation<=0){
  144.             throw new AuditException("[DriverAudit::getBinaries]  Id non valido");
  145.         }
  146.        
  147.         try {
  148.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  149.             sqlQueryObject.addFromTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  150.             sqlQueryObject.addSelectField("*");
  151.             sqlQueryObject.addWhereCondition("id_audit_operation=?");
  152.             String sqlQuery = sqlQueryObject.createSQLQuery();
  153.             stm = this.connectionDB.prepareStatement(sqlQuery);
  154.             stm.setLong(1, idOperation);
  155.             rs = stm.executeQuery();
  156.             while (rs.next()) {
  157.                 binaries.add(this.getBinary(rs.getLong("id")));
  158.             }
  159.        
  160.             return binaries;
  161.         } catch (SQLException se) {
  162.             throw new AuditException("[DriverAudit::getBinaries] SqlException: " + se.getMessage(),se);
  163.         } catch (Exception ex) {
  164.             throw new AuditException("[DriverAudit::getBinaries] Exception: " + ex.getMessage(),ex);
  165.         } finally {
  166.             try {
  167.                 if(rs!=null) {
  168.                     rs.close();
  169.                 }
  170.             } catch (Exception e) {
  171.                 // ignore exception
  172.             }
  173.             try {
  174.                 if(stm!=null) {
  175.                     stm.close();
  176.                 }
  177.             } catch (Exception e) {
  178.                 // ignore exception
  179.             }
  180.         }
  181.     }
  182.    
  183.     public Binary getBinary(long id) throws AuditException {
  184.        
  185.         Binary binary = null;
  186.         PreparedStatement stm = null;
  187.         ResultSet rs = null;

  188.         if(id<=0){
  189.             throw new AuditException("[DriverAudit::getBinary]  Id non valido");
  190.         }
  191.        
  192.         try {
  193.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  194.             sqlQueryObject.addFromTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  195.             sqlQueryObject.addSelectField("*");
  196.             sqlQueryObject.addWhereCondition("id=?");
  197.             String sqlQuery = sqlQueryObject.createSQLQuery();
  198.             stm = this.connectionDB.prepareStatement(sqlQuery);
  199.             stm.setLong(1, id);
  200.             rs = stm.executeQuery();
  201.             if (rs.next()) {
  202.                 binary = new Binary();
  203.                
  204.                 binary.setBinaryId(rs.getString("binary_id"));
  205.                 binary.setChecksum(rs.getLong("checksum"));
  206.                 binary.setIdOperation(rs.getLong("id_audit_operation"));
  207.                 binary.setId(id);
  208.                
  209.             }else{
  210.                 throw new AuditException("[DriverAudit::getBinary] non presente");
  211.             }
  212.        
  213.             return binary;
  214.         } catch (SQLException se) {
  215.             throw new AuditException("[DriverAudit::getBinary] SqlException: " + se.getMessage(),se);
  216.         } catch (Exception ex) {
  217.             throw new AuditException("[DriverAudit::getBinary] Exception: " + ex.getMessage(),ex);
  218.         } finally {
  219.             try {
  220.                 if(rs!=null) {
  221.                     rs.close();
  222.                 }
  223.             } catch (Exception e) {
  224.                 // ignore exception
  225.             }
  226.             try {
  227.                 if(stm!=null) {
  228.                     stm.close();
  229.                 }
  230.             } catch (Exception e) {
  231.                 // ignore exception
  232.             }
  233.         }
  234.     }
  235.    
  236.    
  237.     public void createOperation(Operation operation) throws AuditException {
  238.        
  239.         if (operation == null)
  240.             throw new AuditException("[DriverAudit::createOperation] Parametro non valido.");

  241.        
  242.         try {
  243.            
  244.             //Inserimento della traccia nel DB
  245.             if(!TipiDatabase.isAMember(this.tipoDB)){
  246.                 throw new Exception("Tipo database ["+this.tipoDB+"] non supportato");
  247.             }
  248.             TipiDatabase tipo = TipiDatabase.toEnumConstant(this.tipoDB);
  249.             // ** Preparazione parametri
  250.             Timestamp timeRequestT = null;
  251.             if(operation.getTimeRequest()!=null){
  252.                 timeRequestT = new Timestamp(operation.getTimeRequest().getTime());
  253.             }
  254.             Timestamp timeExecuteT = null;
  255.             if(operation.getTimeExecute()!=null){
  256.                 timeExecuteT = new Timestamp(operation.getTimeExecute().getTime());
  257.             }
  258.             // ** Insert and return generated key
  259.             CustomKeyGeneratorObject customKeyGeneratorObject = new CustomKeyGeneratorObject(Costanti.DB_AUDIT_OPERATIONS_TABLE, Costanti.DB_AUDIT_OPERATIONS_TABLE_ID,
  260.                     Costanti.DB_AUDIT_OPERATIONS_TABLE_SEQUENCE, Costanti.DB_AUDIT_OPERATIONS_TABLE_FOR_ID_SEQUENCE);
  261.             long idoperazione = InsertAndGeneratedKey.insertAndReturnGeneratedKey(this.connectionDB, tipo, customKeyGeneratorObject,
  262.                         new InsertAndGeneratedKeyObject("tipo_operazione", operation.getTipologia()!=null ? operation.getTipologia().getValue() : null, InsertAndGeneratedKeyJDBCType.STRING),
  263.                         new InsertAndGeneratedKeyObject("tipo", operation.getTipoOggetto(), InsertAndGeneratedKeyJDBCType.STRING),
  264.                         new InsertAndGeneratedKeyObject("object_id", operation.getObjectId(), InsertAndGeneratedKeyJDBCType.STRING),
  265.                         new InsertAndGeneratedKeyObject("object_old_id", operation.getObjectOldId(), InsertAndGeneratedKeyJDBCType.STRING),
  266.                         new InsertAndGeneratedKeyObject("utente", operation.getUtente(), InsertAndGeneratedKeyJDBCType.STRING),
  267.                         new InsertAndGeneratedKeyObject("stato", operation.getStato()!=null ? operation.getStato().getValue() : null, InsertAndGeneratedKeyJDBCType.STRING),
  268.                         new InsertAndGeneratedKeyObject("object_details", operation.getObjectDetails(), InsertAndGeneratedKeyJDBCType.STRING),
  269.                         new InsertAndGeneratedKeyObject("object_class", operation.getObjectClass(), InsertAndGeneratedKeyJDBCType.STRING),
  270.                         new InsertAndGeneratedKeyObject("error", operation.getError(), InsertAndGeneratedKeyJDBCType.STRING),
  271.                         new InsertAndGeneratedKeyObject("time_request", timeRequestT, InsertAndGeneratedKeyJDBCType.TIMESTAMP),
  272.                         new InsertAndGeneratedKeyObject("time_execute", timeExecuteT, InsertAndGeneratedKeyJDBCType.TIMESTAMP));
  273.             if(idoperazione<=0){
  274.                 throw new Exception("ID autoincrementale non ottenuto");
  275.             }                      
  276.            
  277.             // binaries
  278.             for(int i=0; i<operation.sizeBinaryList(); i++){
  279.                 Binary binary = operation.getBinary(i);
  280.                 binary.setIdOperation(idoperazione);
  281.                 this.createBinary(binary);
  282.             }
  283.            
  284.             // Aggiorno l'id
  285.             operation.setId(idoperazione);
  286.            
  287.         } catch (Exception qe) {
  288.             throw new AuditException(qe.getMessage(),qe);
  289.         }
  290.     }
  291.    
  292.     public void createBinary(Binary binary) throws AuditException {
  293.         ResultSet rs = null;
  294.         PreparedStatement stm = null;
  295.         String sqlQuery = "";

  296.         if (binary == null)
  297.             throw new AuditException("[DriverAudit::createBinary] Parametro non valido.");
  298.         if (binary.getIdOperation() == null)
  299.             throw new AuditException("[DriverAudit::createBinary] IDOperation non valido.");
  300.         if (binary.getIdOperation() <= 0)
  301.             throw new AuditException("[DriverAudit::createBinary] IDOperation("+binary.getIdOperation()+") non valido.");
  302.        
  303.         try {
  304.            
  305.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  306.             sqlQueryObject.addInsertTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  307.             sqlQueryObject.addInsertField("binary_id", "?");
  308.             sqlQueryObject.addInsertField("checksum", "?");
  309.             sqlQueryObject.addInsertField("id_audit_operation", "?");
  310.             sqlQuery = sqlQueryObject.createSQLInsert();
  311.             stm = this.connectionDB.prepareStatement(sqlQuery);
  312.            
  313.             stm.setString(1, binary.getBinaryId());
  314.             stm.setLong(2, binary.getChecksum());
  315.             stm.setLong(3, binary.getIdOperation());
  316.             stm.executeUpdate();
  317.             stm.close();
  318.            
  319.             // Recupero id
  320.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  321.             sqlQueryObject.addFromTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  322.             sqlQueryObject.addSelectField(Costanti.DB_AUDIT_BINARIES_TABLE, "id");
  323.             sqlQueryObject.addWhereCondition("binary_id=?");
  324.             sqlQueryObject.addWhereCondition("id_audit_operation=?");
  325.             sqlQueryObject.setANDLogicOperator(true);
  326.             sqlQuery = sqlQueryObject.createSQLQuery();
  327.             stm = this.connectionDB.prepareStatement(sqlQuery);
  328.             stm.setString(1, binary.getBinaryId());
  329.             stm.setLong(2, binary.getIdOperation());
  330.             rs = stm.executeQuery();
  331.             if(rs.next()){
  332.                 binary.setId(rs.getLong("id"));
  333.             }else{
  334.                 throw new Exception("Identificativo dopo l'insert non recuperato");
  335.             }
  336.            
  337.         } catch (Exception qe) {
  338.             throw new AuditException(qe.getMessage(),qe);
  339.         } finally {

  340.             try {
  341.                 if(rs!=null) {
  342.                     rs.close();
  343.                 }
  344.             } catch (Exception e) {
  345.                 // ignore exception
  346.             }
  347.             try {
  348.                 if(stm!=null) {
  349.                     stm.close();
  350.                 }
  351.             } catch (Exception e) {
  352.                 // ignore exception
  353.             }
  354.         }
  355.     }
  356.    
  357.    
  358.     public void updateOperation(Operation operation,boolean updateBinaries) throws AuditException {
  359.         ResultSet rs = null;
  360.         PreparedStatement stm = null;
  361.         String sqlQuery = "";

  362.         if (operation == null)
  363.             throw new AuditException("[DriverAudit::updateOperation] Parametro non valido.");
  364.         if (operation.getId()<=0)
  365.             throw new AuditException("[DriverAudit::updateOperation] ID ("+operation.getId()+") non valido.");

  366.        
  367.         try {
  368.            
  369.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  370.             sqlQueryObject.addUpdateTable(Costanti.DB_AUDIT_OPERATIONS_TABLE);
  371.             sqlQueryObject.addUpdateField("tipo_operazione", "?");
  372.             sqlQueryObject.addUpdateField("tipo", "?");
  373.             sqlQueryObject.addUpdateField("object_id", "?");
  374.             sqlQueryObject.addUpdateField("object_old_id", "?");
  375.             sqlQueryObject.addUpdateField("utente", "?");
  376.             sqlQueryObject.addUpdateField("stato", "?");
  377.             sqlQueryObject.addUpdateField("object_details", "?");
  378.             sqlQueryObject.addUpdateField("object_class", "?");
  379.             sqlQueryObject.addUpdateField("error", "?");
  380.             sqlQueryObject.addUpdateField("time_request", "?");
  381.             sqlQueryObject.addUpdateField("time_execute", "?");
  382.             sqlQueryObject.addWhereCondition("id=?");
  383.             sqlQuery = sqlQueryObject.createSQLUpdate();
  384.             stm = this.connectionDB.prepareStatement(sqlQuery);
  385.            
  386.             stm.setString(1, operation.getTipologia()!=null ? operation.getTipologia().getValue() : null);
  387.             stm.setString(2, operation.getTipoOggetto());
  388.             stm.setString(3, operation.getObjectId());
  389.             stm.setString(4, operation.getObjectOldId());
  390.             stm.setString(5, operation.getUtente());
  391.             stm.setString(6, operation.getStato()!=null ? operation.getStato().getValue() : null);
  392.             stm.setString(7, operation.getObjectDetails());
  393.             stm.setString(8, operation.getObjectClass());
  394.             stm.setString(9, operation.getError());
  395.             stm.setTimestamp(10, new Timestamp(operation.getTimeRequest().getTime()));
  396.             stm.setTimestamp(11, new Timestamp(operation.getTimeExecute().getTime()));
  397.             stm.setLong(12,operation.getId());
  398.             stm.executeUpdate();
  399.            
  400.             if(updateBinaries){
  401.            
  402.                 // deleteAllBinaries
  403.                 this.deleteBinaries(operation.getId());
  404.                
  405.                 // binaries
  406.                 for(int i=0; i<operation.sizeBinaryList(); i++){
  407.                     Binary binary = operation.getBinary(i);
  408.                     binary.setIdOperation(operation.getId());
  409.                     this.createBinary(binary);
  410.                 }
  411.                
  412.             }
  413.            
  414.         } catch (Exception qe) {
  415.             throw new AuditException(qe.getMessage(),qe);
  416.         } finally {

  417.             //Chiudo statement and resultset
  418.             try {
  419.                 if (rs != null)
  420.                     rs.close();
  421.             } catch (Exception e) {
  422.                 //ignore
  423.             }
  424.             try {
  425.                 if (stm != null)
  426.                     stm.close();
  427.             } catch (Exception e) {
  428.                 //ignore
  429.             }
  430.         }
  431.     }
  432.    
  433.     public void updateBinary(Binary binary) throws AuditException {
  434.         PreparedStatement stm = null;
  435.         String sqlQuery = "";

  436.         if (binary == null)
  437.             throw new AuditException("[DriverAudit::updateBinary] Parametro non valido.");
  438.         if (binary.getIdOperation() == null)
  439.             throw new AuditException("[DriverAudit::updateBinary] IDOperation non valido.");
  440.         if (binary.getIdOperation() <= 0)
  441.             throw new AuditException("[DriverAudit::updateBinary] IDOperation("+binary.getIdOperation()+") non valido.");
  442.         if (binary.getId() == null)
  443.             throw new AuditException("[DriverAudit::updateBinary] ID non valido.");
  444.         if (binary.getId() <= 0)
  445.             throw new AuditException("[DriverAudit::updateBinary] ID("+binary.getId()+") non valido.");
  446.        
  447.         try {
  448.            
  449.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  450.             sqlQueryObject.addUpdateTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  451.             sqlQueryObject.addUpdateField("binary_id", "?");
  452.             sqlQueryObject.addUpdateField("checksum", "?");
  453.             sqlQueryObject.addUpdateField("id_audit_operation", "?");
  454.             sqlQueryObject.addWhereCondition("id=?");
  455.             sqlQuery = sqlQueryObject.createSQLUpdate();
  456.             stm = this.connectionDB.prepareStatement(sqlQuery);
  457.            
  458.             stm.setString(1, binary.getBinaryId());
  459.             stm.setLong(2, binary.getChecksum());
  460.             stm.setLong(3, binary.getIdOperation());
  461.             stm.setLong(4, binary.getId());
  462.             stm.executeUpdate();
  463.             stm.close();
  464.            
  465.         } catch (Exception qe) {
  466.             throw new AuditException(qe.getMessage(),qe);
  467.         } finally {

  468.             //Chiudo statement and resultset
  469.             try {
  470.                 if (stm != null)
  471.                     stm.close();
  472.             } catch (Exception e) {
  473.                 //ignore
  474.             }
  475.         }
  476.     }
  477.    
  478.    
  479.    
  480.    
  481.     public void deleteOperation(Operation operation) throws AuditException {
  482.         if (operation == null)
  483.             throw new AuditException("[DriverAudit::deleteOperation] Parametro non valido.");
  484.         if (operation.getId() <= 0)
  485.             throw new AuditException("[DriverAudit::deleteOperation] ID("+operation.getId()+") non valido.");
  486.         this.deleteOperation(operation.getId());
  487.     }
  488.    
  489.     public void deleteOperation(long id) throws AuditException {
  490.         PreparedStatement stm = null;
  491.         String sqlQuery = "";

  492.         if (id <= 0)
  493.             throw new AuditException("[DriverAudit::deleteBinary] ID("+id+") non valido.");
  494.        
  495.         try {
  496.            
  497.             this.deleteBinaries(id);
  498.            
  499.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  500.             sqlQueryObject.addDeleteTable(Costanti.DB_AUDIT_OPERATIONS_TABLE);
  501.             sqlQueryObject.addWhereCondition("id=?");
  502.             sqlQuery = sqlQueryObject.createSQLDelete();
  503.             stm = this.connectionDB.prepareStatement(sqlQuery);
  504.             stm.setLong(1, id);
  505.             stm.executeUpdate();
  506.             stm.close();
  507.            
  508.         } catch (Exception qe) {
  509.             throw new AuditException(qe.getMessage(),qe);
  510.         } finally {

  511.             //Chiudo statement and resultset
  512.             try {
  513.                 if (stm != null)
  514.                     stm.close();
  515.             } catch (Exception e) {
  516.                 //ignore
  517.             }
  518.         }
  519.     }
  520.    
  521.    
  522.     public void deleteBinaries(long idOperation) throws AuditException {
  523.         PreparedStatement stm = null;
  524.         String sqlQuery = "";

  525.         if (idOperation <= 0)
  526.             throw new AuditException("[DriverAudit::deleteBinary] IDOperation("+idOperation+") non valido.");
  527.        
  528.         try {
  529.            
  530.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  531.             sqlQueryObject.addDeleteTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  532.             sqlQueryObject.addWhereCondition("id_audit_operation=?");
  533.             sqlQuery = sqlQueryObject.createSQLDelete();
  534.             stm = this.connectionDB.prepareStatement(sqlQuery);
  535.             stm.setLong(1, idOperation);
  536.             stm.executeUpdate();
  537.             stm.close();
  538.            
  539.         } catch (Exception qe) {
  540.             throw new AuditException(qe.getMessage(),qe);
  541.         } finally {

  542.             //Chiudo statement and resultset
  543.             try {
  544.                 if (stm != null)
  545.                     stm.close();
  546.             } catch (Exception e) {
  547.                 //ignore
  548.             }
  549.         }
  550.     }
  551.    
  552.     public void deleteBinary(Binary binary) throws AuditException {
  553.         if (binary == null)
  554.             throw new AuditException("[DriverAudit::deleteBinary] Parametro non valido.");
  555.         if (binary.getId() == null)
  556.             throw new AuditException("[DriverAudit::deleteBinary] ID non valido.");
  557.         if (binary.getId() <= 0)
  558.             throw new AuditException("[DriverAudit::deleteBinary] ID("+binary.getId()+") non valido.");
  559.         this.deleteBinary(binary.getId());
  560.     }
  561.    
  562.     public void deleteBinary(long id) throws AuditException {
  563.         PreparedStatement stm = null;
  564.         String sqlQuery = "";

  565.         if (id <= 0)
  566.             throw new AuditException("[DriverAudit::deleteBinary] ID("+id+") non valido.");
  567.        
  568.         try {
  569.            
  570.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  571.             sqlQueryObject.addDeleteTable(Costanti.DB_AUDIT_BINARIES_TABLE);
  572.             sqlQueryObject.addWhereCondition("id=?");
  573.             sqlQuery = sqlQueryObject.createSQLDelete();
  574.             stm = this.connectionDB.prepareStatement(sqlQuery);
  575.             stm.setLong(1, id);
  576.             stm.executeUpdate();
  577.             stm.close();
  578.            
  579.         } catch (Exception qe) {
  580.             throw new AuditException(qe.getMessage(),qe);
  581.         } finally {

  582.             //Chiudo statement and resultset
  583.             try {
  584.                 if (stm != null)
  585.                     stm.close();
  586.             } catch (Exception e) {
  587.                 //ignore
  588.             }
  589.         }
  590.     }

  591.     public List<Operation> auditOperationList(ISearch ricerca, int idLista,
  592.             String datainizio, String datafine, String tipooperazione,
  593.             String tipooggetto, String id, String oldid, String utente,
  594.             String statooperazione, String contoggetto) throws AuditException {
  595.         String nomeMetodo = "auditOperationList";
  596.         int offset;
  597.         int limit;
  598.         //String search;
  599.         String queryString;

  600.         limit = ricerca.getPageSize(idLista);
  601.         offset = ricerca.getIndexIniziale(idLista);
  602.         //search = (org.openspcoop2.core.constants.Costanti.SESSION_ATTRIBUTE_VALUE_RICERCA_UNDEFINED.equals(ricerca.getSearchString(idLista)) ? "" : ricerca.getSearchString(idLista));

  603.         PreparedStatement stmt=null;
  604.         ResultSet risultato=null;
  605.         ArrayList<Operation> lista = new ArrayList<Operation>();

  606.         try {
  607.             // Conversione Data
  608.             SimpleDateFormat simpleDateFormat = DateUtils.getDefaultDateFormatter("yyyy-MM-dd");
  609.             Date dataInizioData = datainizio != null && !"".equals(datainizio) ?
  610.                     simpleDateFormat.parse(datainizio) : null;
  611.             Date dataFineData = datafine != null && !"".equals(datafine) ?
  612.                     simpleDateFormat.parse(datafine) : null;
  613.             // data fine inidica l intera giornata quinid aggiungo 1 giorno alla
  614.             // data
  615.             // inserita
  616.             if (dataFineData != null) {
  617.                 Calendar cal = Calendar.getInstance();
  618.                 cal.setTime(dataFineData);
  619.                 cal.add(Calendar.DAY_OF_WEEK, 1);
  620.                 dataFineData = cal.getTime();
  621.             }
  622.             Calendar cal_start = null;
  623.             if (dataInizioData != null) {
  624.                 cal_start = Calendar.getInstance();
  625.                 cal_start.setTime(dataInizioData);
  626.             }
  627.             Calendar cal_end = null;
  628.             if (dataFineData != null) {
  629.                 cal_end = Calendar.getInstance();
  630.                 cal_end.setTime(dataFineData);
  631.             }

  632.             ISQLQueryObject sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  633.             sqlQueryObject.addFromTable(Costanti.DB_AUDIT_OPERATIONS_TABLE);
  634.             sqlQueryObject.addSelectCountField("*", "cont");
  635.             if (datainizio != null && !datainizio.equals(""))
  636.                 sqlQueryObject.addWhereCondition("time_request >= ?");
  637.             if (datafine != null && !datafine.equals(""))
  638.                 sqlQueryObject.addWhereCondition("time_request <= ?");
  639.             if (tipooperazione != null && !tipooperazione.equals("-"))
  640.                 sqlQueryObject.addWhereCondition("tipo_operazione = ?");
  641.             if (tipooggetto != null && !tipooggetto.equals("-"))
  642.                 sqlQueryObject.addWhereCondition("tipo = ?");
  643.             if (utente != null && !utente.equals(""))
  644.                 sqlQueryObject.addWhereCondition("utente = ?");
  645.             if (statooperazione != null && !statooperazione.equals("-"))
  646.                 sqlQueryObject.addWhereCondition("stato = ?");
  647.             if (id != null && !id.equals("")) {
  648.                 //sqlQueryObject.addWhereCondition("object_id = ?");
  649.                 sqlQueryObject.addWhereLikeCondition("object_id",id, true, true);
  650.             }
  651.             if (oldid != null && !oldid.equals("")) {
  652.                 //sqlQueryObject.addWhereCondition("object_old_id = ?");
  653.                 sqlQueryObject.addWhereLikeCondition("object_old_id",id, true, true);
  654.             }
  655.             if (contoggetto != null && !contoggetto.equals(""))
  656.                 sqlQueryObject.addWhereLikeCondition("object_details",
  657.                         contoggetto, true, true);
  658.             sqlQueryObject.setANDLogicOperator(true);
  659.             queryString = sqlQueryObject.createSQLQuery();
  660.             stmt = this.connectionDB.prepareStatement(queryString);
  661.             int param_index = 0;
  662.             if (datainizio != null && !datainizio.equals(""))
  663.                 stmt.setTimestamp(++param_index, new Timestamp(cal_start.getTimeInMillis()));
  664.             if (datafine != null && !datafine.equals(""))
  665.                 stmt.setTimestamp(++param_index, new Timestamp(cal_end.getTimeInMillis()));
  666.             if (tipooperazione != null && !tipooperazione.equals("-"))
  667.                 stmt.setString(++param_index, tipooperazione);
  668.             if (tipooggetto != null && !tipooggetto.equals("-"))
  669.                 stmt.setString(++param_index, tipooggetto);
  670.             if (utente != null && !utente.equals(""))
  671.                 stmt.setString(++param_index, utente);
  672.             if (statooperazione != null && !statooperazione.equals("-"))
  673.                 stmt.setString(++param_index, statooperazione);
  674.             /*if (id != null && !id.equals(""))
  675.                 stmt.setString(++param_index, id);
  676.             if (oldid != null && !oldid.equals(""))
  677.                 stmt.setString(++param_index, oldid);*/
  678.             risultato = stmt.executeQuery();
  679.             if (risultato.next())
  680.                 ricerca.setNumEntries(idLista,risultato.getInt(1));
  681.             risultato.close();
  682.             stmt.close();

  683.             // ricavo le entries
  684.             if (limit == 0) // con limit
  685.                 limit = ISQLQueryObject.LIMIT_DEFAULT_VALUE;

  686.             sqlQueryObject = SQLObjectFactory.createSQLQueryObject(this.tipoDB);
  687.             sqlQueryObject.addFromTable(Costanti.DB_AUDIT_OPERATIONS_TABLE);
  688.             sqlQueryObject.addSelectField("id");
  689.             sqlQueryObject.addSelectField("time_execute");
  690.             if (datainizio != null && !datainizio.equals(""))
  691.                 sqlQueryObject.addWhereCondition("time_request >= ?");
  692.             if (datafine != null && !datafine.equals(""))
  693.                 sqlQueryObject.addWhereCondition("time_request <= ?");
  694.             if (tipooperazione != null && !tipooperazione.equals("-"))
  695.                 sqlQueryObject.addWhereCondition("tipo_operazione = ?");
  696.             if (tipooggetto != null && !tipooggetto.equals("-"))
  697.                 sqlQueryObject.addWhereCondition("tipo = ?");
  698.             if (utente != null && !utente.equals(""))
  699.                 sqlQueryObject.addWhereCondition("utente = ?");
  700.             if (statooperazione != null && !statooperazione.equals("-"))
  701.                 sqlQueryObject.addWhereCondition("stato = ?");
  702.             if (id != null && !id.equals("")) {
  703.                 //sqlQueryObject.addWhereCondition("object_id = ?");
  704.                 sqlQueryObject.addWhereLikeCondition("object_id",id, true, true);
  705.             }
  706.             if (oldid != null && !oldid.equals("")) {
  707.                 //sqlQueryObject.addWhereCondition("object_old_id = ?");
  708.                 sqlQueryObject.addWhereLikeCondition("object_old_id",id, true, true);
  709.             }
  710.             if (contoggetto != null && !contoggetto.equals(""))
  711.                 sqlQueryObject.addWhereLikeCondition("object_details",
  712.                         contoggetto, true, true);
  713.             sqlQueryObject.setANDLogicOperator(true);
  714.             sqlQueryObject.addOrderBy("time_execute");
  715.             sqlQueryObject.setSortType(false);
  716.             sqlQueryObject.setLimit(limit);
  717.             sqlQueryObject.setOffset(offset);
  718.             queryString = sqlQueryObject.createSQLQuery();
  719.             stmt = this.connectionDB.prepareStatement(queryString);
  720.             param_index = 0;
  721.             if (datainizio != null && !datainizio.equals(""))
  722.                 stmt.setTimestamp(++param_index, new Timestamp(cal_start.getTimeInMillis()));
  723.             if (datafine != null && !datafine.equals(""))
  724.                 stmt.setTimestamp(++param_index, new Timestamp(cal_end.getTimeInMillis()));
  725.             if (tipooperazione != null && !tipooperazione.equals("-"))
  726.                 stmt.setString(++param_index, tipooperazione);
  727.             if (tipooggetto != null && !tipooggetto.equals("-"))
  728.                 stmt.setString(++param_index, tipooggetto);
  729.             if (utente != null && !utente.equals(""))
  730.                 stmt.setString(++param_index, utente);
  731.             if (statooperazione != null && !statooperazione.equals("-"))
  732.                 stmt.setString(++param_index, statooperazione);
  733.             /*if (id != null && !id.equals(""))
  734.                 stmt.setString(++param_index, id);
  735.             if (oldid != null && !oldid.equals(""))
  736.                 stmt.setString(++param_index, oldid);*/
  737.             risultato = stmt.executeQuery();
  738.             while (risultato.next()) {
  739.                 lista.add(this.getOperation(risultato.getLong("id")));
  740.             }

  741.             return lista;

  742.         } catch (Exception qe) {
  743.             throw new AuditException("[DriverAuditDBAppender::" + nomeMetodo + "] Errore : " + qe.getMessage(),qe);
  744.         } finally {
  745.             try {
  746.                 if(risultato!=null) {
  747.                     risultato.close();
  748.                 }
  749.             } catch (Exception e) {
  750.                 // ignore exception
  751.             }
  752.             try {
  753.                 if(stmt!=null) {
  754.                     stmt.close();
  755.                 }
  756.             } catch (Exception e) {
  757.                 // ignore exception
  758.             }
  759.         }
  760.     }
  761. }