1 19 package org.openharmonise.rm.logging; 20 21 import java.sql.*; 22 import java.util.*; 23 import java.util.Date ; 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 38 public class DBEventLogger extends AbstractEventLogger implements EventLogger { 39 40 43 public static final String NAME_VALUE_SEPARATOR = ":"; 44 45 48 public static final String PAIR_SEPARATOR = ";"; 49 50 54 private static final String TBL_EVENT = "event"; 55 56 59 private static final String CLMN_EVENT_ID = "id"; 60 61 64 private static final String CLMN_USER_ID = "user_id"; 65 66 69 private static final String CLMN_SESSION_ID = "session_id"; 70 71 74 private static final String CLMN_OBJECT_ID = "object_id"; 75 76 79 private static final String CLMN_OBJECT_TYPE = "object_type"; 80 81 84 private static final String CLMN_ACTION = "action"; 85 86 89 private static final String CLMN_TIMESTAMP = "timestamp"; 90 91 94 private static final String CLMN_IP_ADDRESS = "ip_address"; 95 96 99 private static final String CLMN_USER_AGENT = "user_agent"; 100 101 104 private static final String CLMN_REFERER = "referer"; 105 106 109 private static final String CLMN_HEADER_INFO = "header_info"; 110 111 114 private static final String SEQ_EVENT = "seq_event"; 115 116 119 private AbstractDataStoreInterface m_dsi = null; 120 121 124 public DBEventLogger(AbstractDataStoreInterface dsi) { 125 super(); 126 m_dsi = dsi; 127 } 128 129 130 133 protected void saveData( 134 int nUserId, 135 String sSessionId, 136 int nObjectId, 137 String sObjectType, 138 String sAction, 139 Date timestamp, 140 String sIP, 141 Map headers) 142 throws LogException { 143 String sUserAgent = null; 144 String sReferer = null; 145 String sHeaderInfo = null; 146 147 if(headers != null) { 148 sUserAgent = (String ) headers.get(HttpRequestManager.HEADER_USER_AGENT); 149 sReferer = (String ) 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 163 private String getStringFromMap(Map map) { 164 StringBuffer strBuf = new StringBuffer (); 165 166 Set keys = map.keySet(); 167 168 Iterator iter = keys.iterator(); 169 170 while (iter.hasNext()) { 171 String sKey = (String ) 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 186 public static String getColumnAction() { 187 return CLMN_ACTION; 188 } 189 190 195 public static String getColumnEventId() { 196 return CLMN_EVENT_ID; 197 } 198 199 204 public static String getColumnHeaderInfo() { 205 return CLMN_HEADER_INFO; 206 } 207 208 213 public static String getColumnIPAddress() { 214 return CLMN_IP_ADDRESS; 215 } 216 217 222 public static String getColumnObjectId() { 223 return CLMN_OBJECT_ID; 224 } 225 226 231 public static String getColumnReferer() { 232 return CLMN_REFERER; 233 } 234 235 240 public static String getColumnSessionId() { 241 return CLMN_SESSION_ID; 242 } 243 244 249 public static String getColumnUserAgent() { 250 return CLMN_USER_AGENT; 251 } 252 253 258 public static String getColumnUserId() { 259 return CLMN_USER_ID; 260 } 261 262 267 public static String getTableName() { 268 return TBL_EVENT; 269 } 270 271 276 public static String getColumnTimestamp() { 277 return CLMN_TIMESTAMP; 278 } 279 280 285 public static String getColumnObjectType() { 286 return CLMN_OBJECT_TYPE; 287 } 288 289 290 293 protected void saveData(int nUserId, String sSessionId, int nObjectId, String sObjectType, String sAction, Date timestamp, String sIP, String sAdditional) throws LogException { 294 String sUserAgent = null; 295 String 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 |