KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > logging > DBEventLogger


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.logging;
20
21 import java.sql.*;
22 import java.util.*;
23 import java.util.Date JavaDoc;
24
25 import org.openharmonise.commons.dsi.*;
26 import org.openharmonise.commons.dsi.dml.*;
27 import org.openharmonise.rm.view.servlet.utils.*;
28
29
30 /**
31  * The <code>EventLogger</code> implementation which handles logging Harmonise
32  * event data to a database.
33  *
34  * @author Michael Bell
35  * @version $Revision: 1.2 $
36  *
37  */

38 public class DBEventLogger extends AbstractEventLogger implements EventLogger {
39
40     /**
41      * Separator constant for separating names and values in a string list of name-value pairs
42      */

43     public static final String JavaDoc NAME_VALUE_SEPARATOR = ":";
44     
45     /**
46      * Separator constant for separating name-value pairs in a string list of name-value pairs
47      */

48     public static final String JavaDoc PAIR_SEPARATOR = ";";
49
50     //DB constants
51
/**
52      * The event database table name
53      */

54     private static final String JavaDoc TBL_EVENT = "event";
55     
56     /**
57      * The event id column name
58      */

59     private static final String JavaDoc CLMN_EVENT_ID = "id";
60     
61     /**
62      * The user id column name
63      */

64     private static final String JavaDoc CLMN_USER_ID = "user_id";
65     
66     /**
67      * The session id column name
68      */

69     private static final String JavaDoc CLMN_SESSION_ID = "session_id";
70     
71     /**
72      * The object id column name
73      */

74     private static final String JavaDoc CLMN_OBJECT_ID = "object_id";
75     
76     /**
77      * The object type column name
78      */

79     private static final String JavaDoc CLMN_OBJECT_TYPE = "object_type";
80     
81     /**
82      * The action column name
83      */

84     private static final String JavaDoc CLMN_ACTION = "action";
85     
86     /**
87      * The timestamp column name
88      */

89     private static final String JavaDoc CLMN_TIMESTAMP = "timestamp";
90     
91     /**
92      * The IP address column name
93      */

94     private static final String JavaDoc CLMN_IP_ADDRESS = "ip_address";
95     
96     /**
97      * The user agent column name
98      */

99     private static final String JavaDoc CLMN_USER_AGENT = "user_agent";
100     
101     /**
102      * The referer column name
103      */

104     private static final String JavaDoc CLMN_REFERER = "referer";
105     
106     /**
107      * The header info column
108      */

109     private static final String JavaDoc CLMN_HEADER_INFO = "header_info";
110     
111     /**
112      * The event id sequence name
113      */

114     private static final String JavaDoc SEQ_EVENT = "seq_event";
115     
116     /**
117      * The data store interface
118      */

119     private AbstractDataStoreInterface m_dsi = null;
120
121     /**
122      * Creates an instance of a database event logger
123      */

124     public DBEventLogger(AbstractDataStoreInterface dsi) {
125         super();
126         m_dsi = dsi;
127     }
128
129     
130     /* (non-Javadoc)
131      * @see org.openharmonise.rm.logging.AbstractEventLogger#saveData(int, java.lang.String, int, java.lang.String, java.lang.String, java.util.Date, java.lang.String, java.util.Map)
132      */

133     protected void saveData(
134         int nUserId,
135         String JavaDoc sSessionId,
136         int nObjectId,
137         String JavaDoc sObjectType,
138         String JavaDoc sAction,
139         Date JavaDoc timestamp,
140         String JavaDoc sIP,
141         Map headers)
142         throws LogException {
143         String JavaDoc sUserAgent = null;
144         String JavaDoc sReferer = null;
145         String JavaDoc sHeaderInfo = null;
146         
147         if(headers != null) {
148             sUserAgent = (String JavaDoc) headers.get(HttpRequestManager.HEADER_USER_AGENT);
149             sReferer = (String JavaDoc) headers.get(HttpRequestManager.HEADER_REFERER);
150             sHeaderInfo = getStringFromMap(headers);
151         }
152
153         saveData(nUserId, sSessionId, nObjectId, sObjectType, sAction, timestamp, sIP, sHeaderInfo);
154
155     }
156     
157     /**
158      * Returns a string representation of the contents of the given <code>Map</code>.
159      *
160      * @param map the <code>Map</code> to serialise
161      * @return a string representation of the contents of the given <code>Map</code>
162      */

163     private String JavaDoc getStringFromMap(Map map) {
164         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc();
165
166         Set keys = map.keySet();
167
168         Iterator iter = keys.iterator();
169
170         while (iter.hasNext()) {
171             String JavaDoc sKey = (String JavaDoc) iter.next();
172
173             strBuf.append(sKey).append(NAME_VALUE_SEPARATOR).append(
174                 map.get(sKey)).append(
175                 PAIR_SEPARATOR);
176         }
177
178         return strBuf.toString();
179     }
180
181     /**
182      * Returns the column name for the event 'action' database column.
183      * .
184      * @return the column name for the event 'action' database column
185      */

186     public static String JavaDoc getColumnAction() {
187         return CLMN_ACTION;
188     }
189
190     /**
191      * Returns the column name for the event 'id' database column.
192      *
193      * @return the column name for the event 'id' database column
194      */

195     public static String JavaDoc getColumnEventId() {
196         return CLMN_EVENT_ID;
197     }
198
199     /**
200      * Returns the column name for the event 'header info' database column.
201      *
202      * @return the column name for the event 'header info' database column
203      */

204     public static String JavaDoc getColumnHeaderInfo() {
205         return CLMN_HEADER_INFO;
206     }
207
208     /**
209      * Returns the column name for the event 'IP address' database column.
210      *
211      * @return the column name for the event 'IP address' database column
212      */

213     public static String JavaDoc getColumnIPAddress() {
214         return CLMN_IP_ADDRESS;
215     }
216
217     /**
218      * Returns the column name for the event 'object id' database column.
219      *
220      * @return the column name for the event 'object id' database column
221      */

222     public static String JavaDoc getColumnObjectId() {
223         return CLMN_OBJECT_ID;
224     }
225
226     /**
227      * Returns the column name for the event 'referer' database column.
228      *
229      * @return the column name for the event 'referer' database column
230      */

231     public static String JavaDoc getColumnReferer() {
232         return CLMN_REFERER;
233     }
234
235     /**
236      * Returns the column name for the event 'session id' database column.
237      *
238      * @return the column name for the event 'session id' database column
239      */

240     public static String JavaDoc getColumnSessionId() {
241         return CLMN_SESSION_ID;
242     }
243
244     /**
245      * Returns the column name for the event 'user agent' database column.
246      *
247      * @return the column name for the event 'user agent' database column
248      */

249     public static String JavaDoc getColumnUserAgent() {
250         return CLMN_USER_AGENT;
251     }
252
253     /**
254      * Returns the column name for the event 'user id' database column.
255      *
256      * @return the column name for the event 'user id' database column
257      */

258     public static String JavaDoc getColumnUserId() {
259         return CLMN_USER_ID;
260     }
261
262     /**
263      * Returns the event database table name.
264      *
265      * @return the event database table name
266      */

267     public static String JavaDoc getTableName() {
268         return TBL_EVENT;
269     }
270   
271     /**
272      * Returns the column name for the event 'timestamp' database column.
273      *
274      * @return the column name for the event 'timestamp' database column
275      */

276     public static String JavaDoc getColumnTimestamp() {
277         return CLMN_TIMESTAMP;
278     }
279
280     /**
281      * Returns the column name for the event 'object type' database column.
282      *
283      * @return the column name for the event 'object type' database column
284      */

285     public static String JavaDoc getColumnObjectType() {
286         return CLMN_OBJECT_TYPE;
287     }
288
289
290     /* (non-Javadoc)
291      * @see org.openharmonise.rm.logging.AbstractEventLogger#saveData(int, java.lang.String, int, java.lang.String, java.lang.String, java.util.Date, java.lang.String, java.lang.String)
292      */

293     protected void saveData(int nUserId, String JavaDoc sSessionId, int nObjectId, String JavaDoc sObjectType, String JavaDoc sAction, Date JavaDoc timestamp, String JavaDoc sIP, String JavaDoc sAdditional) throws LogException {
294         String JavaDoc sUserAgent = null;
295         String JavaDoc sReferer = null;
296
297         try {
298             InsertStatement insert = new InsertStatement();
299             
300             int nEventId = m_dsi.getSequenceNextValue(SEQ_EVENT);
301             
302             insert.addColumnValue(
303                 new ColumnRef(TBL_EVENT, CLMN_EVENT_ID, ColumnRef.NUMBER),
304                 nEventId);
305                 
306             insert.addColumnValue(
307                             new ColumnRef(TBL_EVENT, CLMN_USER_ID, ColumnRef.NUMBER),
308                             nUserId);
309                 
310             if(sSessionId != null) {
311                 insert.addColumnValue(
312                             new ColumnRef(TBL_EVENT, CLMN_SESSION_ID, ColumnRef.TEXT),
313                             sSessionId);
314             }
315             
316             if(nObjectId > 0) {
317                 insert.addColumnValue(
318                             new ColumnRef(TBL_EVENT, CLMN_OBJECT_ID, ColumnRef.NUMBER),
319                             nObjectId);
320                 insert.addColumnValue(
321                             new ColumnRef(TBL_EVENT, CLMN_OBJECT_TYPE, ColumnRef.TEXT),
322                             sObjectType);
323             }
324             
325             insert.addColumnValue(
326                 new ColumnRef(TBL_EVENT, CLMN_ACTION, ColumnRef.TEXT),
327                 sAction);
328                 
329             insert.addColumnValue(
330                     new ColumnRef(TBL_EVENT, CLMN_TIMESTAMP, ColumnRef.DATE),
331                     timestamp);
332             
333             if(sIP != null) {
334                 insert.addColumnValue(
335                                     new ColumnRef(TBL_EVENT, CLMN_IP_ADDRESS, ColumnRef.TEXT),
336                                     sIP);
337             }
338             
339             if(sUserAgent != null) {
340                 insert.addColumnValue(
341                                     new ColumnRef(TBL_EVENT, CLMN_USER_AGENT, ColumnRef.TEXT),
342                                     sUserAgent);
343             }
344             
345             if(sReferer != null) {
346                 insert.addColumnValue(new ColumnRef(TBL_EVENT,CLMN_REFERER, ColumnRef.TEXT),
347                             sReferer);
348             }
349             
350             if(sAdditional != null) {
351                 insert.addColumnValue(new ColumnRef(TBL_EVENT,CLMN_HEADER_INFO, ColumnRef.TEXT),
352                         sAdditional);
353             }
354             
355             m_dsi.execute(insert);
356         } catch (DataStoreException e) {
357             throw new LogException("DS Error inserting data in DB",e);
358         } catch (SQLException e) {
359             throw new LogException("SQL Error inserting data in DB",e);
360         }
361
362         
363     }
364
365 }
366
Popular Tags