001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.logging;
020:
021: import java.sql.*;
022: import java.util.*;
023: import java.util.Date;
024:
025: import org.openharmonise.commons.dsi.*;
026: import org.openharmonise.commons.dsi.dml.*;
027: import org.openharmonise.rm.view.servlet.utils.*;
028:
029: /**
030: * The <code>EventLogger</code> implementation which handles logging Harmonise
031: * event data to a database.
032: *
033: * @author Michael Bell
034: * @version $Revision: 1.2 $
035: *
036: */
037: public class DBEventLogger extends AbstractEventLogger implements
038: EventLogger {
039:
040: /**
041: * Separator constant for separating names and values in a string list of name-value pairs
042: */
043: public static final String NAME_VALUE_SEPARATOR = ":";
044:
045: /**
046: * Separator constant for separating name-value pairs in a string list of name-value pairs
047: */
048: public static final String PAIR_SEPARATOR = ";";
049:
050: //DB constants
051: /**
052: * The event database table name
053: */
054: private static final String TBL_EVENT = "event";
055:
056: /**
057: * The event id column name
058: */
059: private static final String CLMN_EVENT_ID = "id";
060:
061: /**
062: * The user id column name
063: */
064: private static final String CLMN_USER_ID = "user_id";
065:
066: /**
067: * The session id column name
068: */
069: private static final String CLMN_SESSION_ID = "session_id";
070:
071: /**
072: * The object id column name
073: */
074: private static final String CLMN_OBJECT_ID = "object_id";
075:
076: /**
077: * The object type column name
078: */
079: private static final String CLMN_OBJECT_TYPE = "object_type";
080:
081: /**
082: * The action column name
083: */
084: private static final String CLMN_ACTION = "action";
085:
086: /**
087: * The timestamp column name
088: */
089: private static final String CLMN_TIMESTAMP = "timestamp";
090:
091: /**
092: * The IP address column name
093: */
094: private static final String CLMN_IP_ADDRESS = "ip_address";
095:
096: /**
097: * The user agent column name
098: */
099: private static final String CLMN_USER_AGENT = "user_agent";
100:
101: /**
102: * The referer column name
103: */
104: private static final String CLMN_REFERER = "referer";
105:
106: /**
107: * The header info column
108: */
109: private static final String CLMN_HEADER_INFO = "header_info";
110:
111: /**
112: * The event id sequence name
113: */
114: private static final String 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: /* (non-Javadoc)
130: * @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)
131: */
132: protected void saveData(int nUserId, String sSessionId,
133: int nObjectId, String sObjectType, String sAction,
134: Date timestamp, String sIP, Map headers)
135: throws LogException {
136: String sUserAgent = null;
137: String sReferer = null;
138: String sHeaderInfo = null;
139:
140: if (headers != null) {
141: sUserAgent = (String) headers
142: .get(HttpRequestManager.HEADER_USER_AGENT);
143: sReferer = (String) headers
144: .get(HttpRequestManager.HEADER_REFERER);
145: sHeaderInfo = getStringFromMap(headers);
146: }
147:
148: saveData(nUserId, sSessionId, nObjectId, sObjectType, sAction,
149: timestamp, sIP, sHeaderInfo);
150:
151: }
152:
153: /**
154: * Returns a string representation of the contents of the given <code>Map</code>.
155: *
156: * @param map the <code>Map</code> to serialise
157: * @return a string representation of the contents of the given <code>Map</code>
158: */
159: private String getStringFromMap(Map map) {
160: StringBuffer strBuf = new StringBuffer();
161:
162: Set keys = map.keySet();
163:
164: Iterator iter = keys.iterator();
165:
166: while (iter.hasNext()) {
167: String sKey = (String) iter.next();
168:
169: strBuf.append(sKey).append(NAME_VALUE_SEPARATOR).append(
170: map.get(sKey)).append(PAIR_SEPARATOR);
171: }
172:
173: return strBuf.toString();
174: }
175:
176: /**
177: * Returns the column name for the event 'action' database column.
178: * .
179: * @return the column name for the event 'action' database column
180: */
181: public static String getColumnAction() {
182: return CLMN_ACTION;
183: }
184:
185: /**
186: * Returns the column name for the event 'id' database column.
187: *
188: * @return the column name for the event 'id' database column
189: */
190: public static String getColumnEventId() {
191: return CLMN_EVENT_ID;
192: }
193:
194: /**
195: * Returns the column name for the event 'header info' database column.
196: *
197: * @return the column name for the event 'header info' database column
198: */
199: public static String getColumnHeaderInfo() {
200: return CLMN_HEADER_INFO;
201: }
202:
203: /**
204: * Returns the column name for the event 'IP address' database column.
205: *
206: * @return the column name for the event 'IP address' database column
207: */
208: public static String getColumnIPAddress() {
209: return CLMN_IP_ADDRESS;
210: }
211:
212: /**
213: * Returns the column name for the event 'object id' database column.
214: *
215: * @return the column name for the event 'object id' database column
216: */
217: public static String getColumnObjectId() {
218: return CLMN_OBJECT_ID;
219: }
220:
221: /**
222: * Returns the column name for the event 'referer' database column.
223: *
224: * @return the column name for the event 'referer' database column
225: */
226: public static String getColumnReferer() {
227: return CLMN_REFERER;
228: }
229:
230: /**
231: * Returns the column name for the event 'session id' database column.
232: *
233: * @return the column name for the event 'session id' database column
234: */
235: public static String getColumnSessionId() {
236: return CLMN_SESSION_ID;
237: }
238:
239: /**
240: * Returns the column name for the event 'user agent' database column.
241: *
242: * @return the column name for the event 'user agent' database column
243: */
244: public static String getColumnUserAgent() {
245: return CLMN_USER_AGENT;
246: }
247:
248: /**
249: * Returns the column name for the event 'user id' database column.
250: *
251: * @return the column name for the event 'user id' database column
252: */
253: public static String getColumnUserId() {
254: return CLMN_USER_ID;
255: }
256:
257: /**
258: * Returns the event database table name.
259: *
260: * @return the event database table name
261: */
262: public static String getTableName() {
263: return TBL_EVENT;
264: }
265:
266: /**
267: * Returns the column name for the event 'timestamp' database column.
268: *
269: * @return the column name for the event 'timestamp' database column
270: */
271: public static String getColumnTimestamp() {
272: return CLMN_TIMESTAMP;
273: }
274:
275: /**
276: * Returns the column name for the event 'object type' database column.
277: *
278: * @return the column name for the event 'object type' database column
279: */
280: public static String getColumnObjectType() {
281: return CLMN_OBJECT_TYPE;
282: }
283:
284: /* (non-Javadoc)
285: * @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)
286: */
287: protected void saveData(int nUserId, String sSessionId,
288: int nObjectId, String sObjectType, String sAction,
289: Date timestamp, String sIP, String sAdditional)
290: throws LogException {
291: String sUserAgent = null;
292: String sReferer = null;
293:
294: try {
295: InsertStatement insert = new InsertStatement();
296:
297: int nEventId = m_dsi.getSequenceNextValue(SEQ_EVENT);
298:
299: insert.addColumnValue(new ColumnRef(TBL_EVENT,
300: CLMN_EVENT_ID, ColumnRef.NUMBER), nEventId);
301:
302: insert.addColumnValue(new ColumnRef(TBL_EVENT,
303: CLMN_USER_ID, ColumnRef.NUMBER), nUserId);
304:
305: if (sSessionId != null) {
306: insert.addColumnValue(new ColumnRef(TBL_EVENT,
307: CLMN_SESSION_ID, ColumnRef.TEXT), sSessionId);
308: }
309:
310: if (nObjectId > 0) {
311: insert.addColumnValue(new ColumnRef(TBL_EVENT,
312: CLMN_OBJECT_ID, ColumnRef.NUMBER), nObjectId);
313: insert.addColumnValue(new ColumnRef(TBL_EVENT,
314: CLMN_OBJECT_TYPE, ColumnRef.TEXT), sObjectType);
315: }
316:
317: insert.addColumnValue(new ColumnRef(TBL_EVENT, CLMN_ACTION,
318: ColumnRef.TEXT), sAction);
319:
320: insert.addColumnValue(new ColumnRef(TBL_EVENT,
321: CLMN_TIMESTAMP, ColumnRef.DATE), timestamp);
322:
323: if (sIP != null) {
324: insert.addColumnValue(new ColumnRef(TBL_EVENT,
325: CLMN_IP_ADDRESS, ColumnRef.TEXT), sIP);
326: }
327:
328: if (sUserAgent != null) {
329: insert.addColumnValue(new ColumnRef(TBL_EVENT,
330: CLMN_USER_AGENT, ColumnRef.TEXT), sUserAgent);
331: }
332:
333: if (sReferer != null) {
334: insert.addColumnValue(new ColumnRef(TBL_EVENT,
335: CLMN_REFERER, ColumnRef.TEXT), sReferer);
336: }
337:
338: if (sAdditional != null) {
339: insert.addColumnValue(new ColumnRef(TBL_EVENT,
340: CLMN_HEADER_INFO, ColumnRef.TEXT), sAdditional);
341: }
342:
343: m_dsi.execute(insert);
344: } catch (DataStoreException e) {
345: throw new LogException("DS Error inserting data in DB", e);
346: } catch (SQLException e) {
347: throw new LogException("SQL Error inserting data in DB", e);
348: }
349:
350: }
351:
352: }
|