KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > event > Event


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.event;
22
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
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 /**
53  * An event. The following fields are defined:
54  * <ul>
55  * <li><tt>int sitecontext_id = 0</tt></li>
56  * <li><tt>Date eventTime = new Date()</tt></li>
57  * <li><tt>String userName = ""</tt></li>
58  * <li><tt>String source = ""</tt></li>
59  * <li><tt>String description = ""</tt></li>
60  * </ul>
61  */

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 JavaDoc.class ),
74         new DynaProperty( "eventTime", Date JavaDoc.class ),
75         new DynaProperty( "userName", String JavaDoc.class ),
76         new DynaProperty( "source", String JavaDoc.class ),
77         new DynaProperty( "description", String JavaDoc.class )
78       };
79
80     dynaClass_ =
81       new BasicDynaClass(
82         "mh_event", Event.class, dynaProperties );
83   }
84
85   // constructors /////////////////////////////////////////////////////////////
86

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   // constants ////////////////////////////////////////////////////////////////
102

103   // classes //////////////////////////////////////////////////////////////////
104

105   // methods //////////////////////////////////////////////////////////////////
106

107   /**
108    * Sets <tt>sitecontext_id</tt> before calling super-class method.
109    */

110   public void saveNew() {
111     setInt( "sitecontext_id", getSiteContext().getInt( "id" ) );
112     super.saveNew();
113   }
114
115   /**
116    * Logs an event.
117    */

118   public void log(
119     String JavaDoc userName,
120     String JavaDoc source,
121     String JavaDoc 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   /**
132    * Logs an event.
133    */

134   public static void log(
135     SiteContext siteContext,
136     String JavaDoc userName,
137     String JavaDoc source,
138     String JavaDoc 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   /**
149    * Logs an event; <tt>userName</tt> and <tt>source</tt> default to empty
150    * strings;
151    */

152   public void log(
153     String JavaDoc description ) {
154
155     log( "", "", description );
156   }
157
158   /**
159    * Logs an event; <tt>userName</tt> and <tt>source</tt> default to empty
160    * strings;
161    */

162   public static void log(
163     SiteContext siteContext,
164     String JavaDoc description ) {
165
166     log( siteContext, "", "", description );
167   }
168
169   /**
170    * Loads all events between <tt>from</tt> <tt>to</tt> in reverse
171    * chronological order; the result set is limited to <tt>pageSize</tt> events
172    * and the <tt>page</tt>th page is returned. If <tt>from</tt> or
173    * <tt>to</tt>, the result set in not bounded in that direction.
174    * @deprecated
175    */

176   public List JavaDoc getEvents(
177     Date JavaDoc from,
178     Date JavaDoc to,
179     int page,
180     int pageSize ) {
181
182     //
183
// build where clause
184
//
185
String JavaDoc whereClause = null;
186
187 /*
188     if ( ( from != null ) && ( to != null ) )
189       whereClause =
190         "eventTime BETWEEN " + getSqlLiteral( from ) + " AND " +
191         getSqlLiteral( to );
192 */

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     //
211
// build sql statement
212
//
213
String JavaDoc 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     //
236
// get events
237
//
238
List JavaDoc events = new ArrayList JavaDoc();
239
240     ResultSet JavaDoc rs = null;
241     try {
242       rs = ConnectionSingleton.runQuery( sql );
243
244       if ( rs == null ) {
245         throw new SQLException JavaDoc( "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 JavaDoc e ) {
258       String JavaDoc msg = "Getting events from \"" + from + "\" to \"" + to + "\". " + ExceptionUtils.getStackTrace( e );
259       logger_.error( msg );
260       throw new RuntimeException JavaDoc( msg );
261     }
262     finally {
263       ConnectionSingleton.close( rs );
264     }
265
266     return events;
267   }
268
269   /**
270    * Loads all events between <tt>from</tt> <tt>to</tt> in reverse
271    * chronological order; the result set is limited to <tt>pageSize</tt> events
272    * and the <tt>page</tt>th page is returned. If <tt>from</tt> or
273    * <tt>to</tt>, the result set in not bounded in that direction.
274    */

275   public List JavaDoc getEvents(
276     Date JavaDoc from,
277     Date JavaDoc to ) {
278
279     //
280
// build where clause
281
//
282
String JavaDoc 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     //
301
// build sql statement
302
//
303
String JavaDoc 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     //
318
// get events
319
//
320
List JavaDoc events = new ArrayList JavaDoc();
321
322     ResultSet JavaDoc 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 JavaDoc e ) {
338       if ( rs != null )
339         ConnectionSingleton.close( rs );
340
341       throw new PersistableException( e.toString() );
342     }
343
344     return events;
345   }
346
347   /**
348    * Deletes all events that occured before <tt>date</tt>.
349    */

350   public void deleteBefore(
351     Date JavaDoc date ) {
352
353     deleteAll(
354       dynaClass_,
355       "eventTime < " + getSqlLiteral( date ) + " AND sitecontext_id=" +
356         getSiteContext().getInt( "id" ) );
357   }
358
359   // properties ///////////////////////////////////////////////////////////////
360

361   /**
362    * Returns the site context associated with this object; if no site context
363    * has been set, the default context (as returned by {@link
364    * com.methodhead.sitecontext.SiteContext#getDefaultContext()
365    * SiteContext.getDefaultContext()}) is returned.
366    */

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   /**
380    * Deletes all events for the site context.
381    */

382   public void deleteAll() {
383     deleteAll(
384       dynaClass_, "sitecontext_id=" + getSiteContext().getInt( "id" ) );
385   }
386
387   // attributes ///////////////////////////////////////////////////////////////
388

389   private SiteContext siteContext_ = null;
390
391   private static Logger logger_ = Logger.getLogger( Event.class );
392 }
393
Popular Tags