KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > audit > AuditSQLEntry


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jun 24, 2005
14  * @author Marc Batchelor
15  */

16
17 package org.pentaho.core.audit;
18
19 import java.math.BigDecimal JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.SQLException JavaDoc;
23 import java.sql.Types JavaDoc;
24
25 import org.pentaho.messages.Messages;
26 import org.pentaho.util.logging.Logger;
27
28 /**
29  * @author mbatchel
30  *
31  */

32 public class AuditSQLEntry implements IAuditEntry {
33     private static AuditConnection audc;
34
35     private static final String JavaDoc INSERT_STMT = Messages.getString("AUDSQLENT.CODE_AUDIT_INSERT_STATEMENT"); //$NON-NLS-1$
36

37     static {
38         try {
39             audc = new AuditConnection();
40             audc.initialize();
41         } catch (Exception JavaDoc ex) {
42             Logger.error(AuditHelper.class.getName(), Messages.getErrorString("AUDSQLENT.ERROR_0001_INVALID_CONNECTION"), ex); //$NON-NLS-1$
43
}
44     }
45
46     public AuditSQLEntry() {
47         // Do nothing
48
}
49
50     private void setString(PreparedStatement JavaDoc stmt, int num, String JavaDoc val) throws SQLException JavaDoc {
51         if (val != null) {
52             stmt.setString(num, val);
53         } else {
54             stmt.setNull(num, Types.VARCHAR);
55         }
56     }
57
58     private void setText(PreparedStatement JavaDoc stmt, int num, String JavaDoc val) throws SQLException JavaDoc {
59         if (val != null) {
60             stmt.setBytes(num, val.getBytes());
61         } else {
62             stmt.setNull(num, Types.CLOB);
63         }
64     }
65
66     private void setBigDec(PreparedStatement JavaDoc stmt, int num, BigDecimal JavaDoc val) throws SQLException JavaDoc {
67         if (val != null) {
68             stmt.setBigDecimal(num, val);
69         } else {
70             stmt.setNull(num, Types.DECIMAL);
71         }
72     }
73
74     /*
75      * private void setInteger(PreparedStatement stmt, int num, Integer val)
76      * throws SQLException{ if (val != null) { stmt.setInt(num, val.intValue()); }
77      * else { stmt.setNull(num, Types.INTEGER); } }
78      */

79     public void auditAll(String JavaDoc jobId, String JavaDoc instId, String JavaDoc objId, String JavaDoc objType, String JavaDoc actor, String JavaDoc messageType, String JavaDoc messageName, String JavaDoc messageTxtValue, BigDecimal JavaDoc messageNumValue, BigDecimal JavaDoc duration) throws AuditException {
80
81         try {
82             Connection JavaDoc con = audc.getAuditConnection();
83             try {
84                 PreparedStatement JavaDoc stmt = con.prepareStatement(INSERT_STMT);
85                 try {
86                     setString(stmt, 1, jobId);
87                     setString(stmt, 2, instId);
88                     setString(stmt, 3, objId);
89                     setString(stmt, 4, objType);
90                     setString(stmt, 5, actor);
91                     setString(stmt, 6, messageType);
92                     setString(stmt, 7, messageName);
93                     setText(stmt, 8, messageTxtValue);
94                     setBigDec(stmt, 9, messageNumValue);
95                     setBigDec(stmt, 10, duration);
96                     stmt.executeUpdate();
97                 } finally {
98                     stmt.close();
99                 }
100             } finally {
101                 con.close();
102             }
103         } catch (SQLException JavaDoc ex) {
104             throw new AuditException(ex);
105         }
106
107     }
108
109 }
110
Popular Tags