1 20 21 package com.methodhead.event; 22 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Timestamp ; 26 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import com.methodhead.persistable.ConnectionSingleton; 34 import com.methodhead.persistable.Persistable; 35 import com.methodhead.persistable.PersistableException; 36 import com.methodhead.MhfException; 37 38 import org.apache.commons.beanutils.DynaClass; 39 import org.apache.commons.beanutils.DynaProperty; 40 import org.apache.commons.beanutils.BasicDynaClass; 41 42 import com.methodhead.sitecontext.SiteContextCapable; 43 import com.methodhead.sitecontext.SiteContext; 44 45 import org.apache.log4j.Logger; 46 import org.apache.commons.lang.exception.ExceptionUtils; 47 import com.methodhead.persistable.ConnectionSingleton; 48 import com.methodhead.test.TestUtils; 49 50 51 52 62 public class Event 63 extends 64 Persistable 65 implements 66 SiteContextCapable { 67 68 private static DynaClass dynaClass_ = null; 69 70 static { 71 DynaProperty[] dynaProperties = 72 new DynaProperty[] { 73 new DynaProperty( "sitecontext_id", Integer .class ), 74 new DynaProperty( "eventTime", Date .class ), 75 new DynaProperty( "userName", String .class ), 76 new DynaProperty( "source", String .class ), 77 new DynaProperty( "description", String .class ) 78 }; 79 80 dynaClass_ = 81 new BasicDynaClass( 82 "mh_event", Event.class, dynaProperties ); 83 } 84 85 87 public Event() { 88 super( dynaClass_ ); 89 setInt( "sitecontext_id", 0 ); 90 setDate( "eventTime", TestUtils.getCurrentDate() ); 91 setString( "userName", "" ); 92 setString( "source", "" ); 93 setString( "description", "" ); 94 } 95 96 public Event( 97 DynaClass dynaClass ) { 98 super( dynaClass ); 99 } 100 101 103 105 107 110 public void saveNew() { 111 setInt( "sitecontext_id", getSiteContext().getInt( "id" ) ); 112 super.saveNew(); 113 } 114 115 118 public void log( 119 String userName, 120 String source, 121 String description ) { 122 123 Event e = new Event(); 124 e.setInt( "sitecontext_id", getSiteContext().getInt( "id" ) ); 125 e.setString( "userName", userName ); 126 e.setString( "source", source ); 127 e.setString( "description", description ); 128 e.saveNew(); 129 } 130 131 134 public static void log( 135 SiteContext siteContext, 136 String userName, 137 String source, 138 String description ) { 139 140 Event e = new Event(); 141 e.setSiteContext( siteContext ); 142 e.setString( "userName", userName ); 143 e.setString( "source", source ); 144 e.setString( "description", description ); 145 e.saveNew(); 146 } 147 148 152 public void log( 153 String description ) { 154 155 log( "", "", description ); 156 } 157 158 162 public static void log( 163 SiteContext siteContext, 164 String description ) { 165 166 log( siteContext, "", "", description ); 167 } 168 169 176 public List getEvents( 177 Date from, 178 Date to, 179 int page, 180 int pageSize ) { 181 182 String whereClause = null; 186 187 193 194 if ( ( from != null ) && ( to != null ) ) 195 whereClause = 196 "eventTime BETWEEN " + getSqlLiteral( from ) + " AND " + 197 getSqlLiteral( to ); 198 199 else if ( ( from == null ) && ( to != null ) ) 200 whereClause = "eventTime < " + getSqlLiteral( to ); 201 202 else if ( ( from != null ) && ( to == null ) ) 203 whereClause = "eventTime > " + getSqlLiteral( from ); 204 205 if ( whereClause == null ) 206 whereClause = "sitecontext_id=" + getSiteContext().getInt( "id" ); 207 else 208 whereClause += " AND sitecontext_id=" + getSiteContext().getInt( "id" ); 209 210 String sql = 214 "SELECT " + 215 " eventTime, " + 216 " userName, " + 217 " source, " + 218 " description " + 219 "FROM " + 220 " mh_event "; 221 222 if ( whereClause != null ) 223 sql += " WHERE " + whereClause; 224 225 sql += " ORDER BY eventTime DESC "; 226 227 if ( page >= 0 ) { 228 if ( ConnectionSingleton.DBTYPE_MYSQL.equals( 229 ConnectionSingleton.getDatabaseType() ) ) 230 sql += " LIMIT " + ( pageSize * page ) + ", " + pageSize + " "; 231 else 232 sql += " LIMIT " + pageSize + " OFFSET " + ( pageSize * page ); 233 } 234 235 List events = new ArrayList (); 239 240 ResultSet rs = null; 241 try { 242 rs = ConnectionSingleton.runQuery( sql ); 243 244 if ( rs == null ) { 245 throw new SQLException ( "Null result set." ); 246 } 247 248 while ( rs.next() ) { 249 Event event = new Event(); 250 event.setDate( "eventTime", rs.getTimestamp( "eventTime" ) ); 251 event.setString( "userName", rs.getString( "userName" ) ); 252 event.setString( "source", rs.getString( "source" ) ); 253 event.setString( "description", rs.getString( "description" ) ); 254 events.add( event ); 255 } 256 } 257 catch ( SQLException e ) { 258 String msg = "Getting events from \"" + from + "\" to \"" + to + "\". " + ExceptionUtils.getStackTrace( e ); 259 logger_.error( msg ); 260 throw new RuntimeException ( msg ); 261 } 262 finally { 263 ConnectionSingleton.close( rs ); 264 } 265 266 return events; 267 } 268 269 275 public List getEvents( 276 Date from, 277 Date to ) { 278 279 String whereClause = null; 283 284 if ( ( from != null ) && ( to != null ) ) 285 whereClause = 286 "eventTime BETWEEN " + getSqlLiteral( from ) + " AND " + 287 getSqlLiteral( to ); 288 289 else if ( ( from == null ) && ( to != null ) ) 290 whereClause = "eventTime < " + getSqlLiteral( to ); 291 292 else if ( ( from != null ) && ( to == null ) ) 293 whereClause = "eventTime > " + getSqlLiteral( from ); 294 295 if ( whereClause == null ) 296 whereClause = "sitecontext_id=" + getSiteContext().getInt( "id" ); 297 else 298 whereClause += " AND sitecontext_id=" + getSiteContext().getInt( "id" ); 299 300 String sql = 304 "SELECT " + 305 " eventTime, " + 306 " userName, " + 307 " source, " + 308 " description " + 309 "FROM " + 310 " mh_event "; 311 312 if ( whereClause != null ) 313 sql += " WHERE " + whereClause; 314 315 sql += " ORDER BY eventTime DESC "; 316 317 List events = new ArrayList (); 321 322 ResultSet rs = null; 323 try { 324 rs = ConnectionSingleton.runQuery( sql ); 325 326 while ( rs.next() ) { 327 Event event = new Event(); 328 event.setDate( "eventTime", rs.getTimestamp( "eventTime" ) ); 329 event.setString( "userName", rs.getString( "userName" ) ); 330 event.setString( "source", rs.getString( "source" ) ); 331 event.setString( "description", rs.getString( "description" ) ); 332 events.add( event ); 333 } 334 335 ConnectionSingleton.close( rs ); 336 } 337 catch ( SQLException e ) { 338 if ( rs != null ) 339 ConnectionSingleton.close( rs ); 340 341 throw new PersistableException( e.toString() ); 342 } 343 344 return events; 345 } 346 347 350 public void deleteBefore( 351 Date date ) { 352 353 deleteAll( 354 dynaClass_, 355 "eventTime < " + getSqlLiteral( date ) + " AND sitecontext_id=" + 356 getSiteContext().getInt( "id" ) ); 357 } 358 359 361 367 public SiteContext getSiteContext() { 368 if ( siteContext_ == null ) 369 siteContext_ = SiteContext.getDefaultContext(); 370 371 return siteContext_; 372 } 373 374 public void setSiteContext( 375 SiteContext siteContext ) { 376 siteContext_ = siteContext; 377 } 378 379 382 public void deleteAll() { 383 deleteAll( 384 dynaClass_, "sitecontext_id=" + getSiteContext().getInt( "id" ) ); 385 } 386 387 389 private SiteContext siteContext_ = null; 390 391 private static Logger logger_ = Logger.getLogger( Event.class ); 392 } 393 | Popular Tags |