001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.event;
022:
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.sql.Timestamp;
026:
027: import java.util.ArrayList;
028: import java.util.Date;
029: import java.util.HashMap;
030: import java.util.List;
031: import java.util.Map;
032:
033: import com.methodhead.persistable.ConnectionSingleton;
034: import com.methodhead.persistable.Persistable;
035: import com.methodhead.persistable.PersistableException;
036: import com.methodhead.MhfException;
037:
038: import org.apache.commons.beanutils.DynaClass;
039: import org.apache.commons.beanutils.DynaProperty;
040: import org.apache.commons.beanutils.BasicDynaClass;
041:
042: import com.methodhead.sitecontext.SiteContextCapable;
043: import com.methodhead.sitecontext.SiteContext;
044:
045: import org.apache.log4j.Logger;
046: import org.apache.commons.lang.exception.ExceptionUtils;
047: import com.methodhead.persistable.ConnectionSingleton;
048: import com.methodhead.test.TestUtils;
049:
050: /**
051: * An event. The following fields are defined:
052: * <ul>
053: * <li><tt>int sitecontext_id = 0</tt></li>
054: * <li><tt>Date eventTime = new Date()</tt></li>
055: * <li><tt>String userName = ""</tt></li>
056: * <li><tt>String source = ""</tt></li>
057: * <li><tt>String description = ""</tt></li>
058: * </ul>
059: */
060: public class Event extends Persistable implements SiteContextCapable {
061:
062: private static DynaClass dynaClass_ = null;
063:
064: static {
065: DynaProperty[] dynaProperties = new DynaProperty[] {
066: new DynaProperty("sitecontext_id", Integer.class),
067: new DynaProperty("eventTime", Date.class),
068: new DynaProperty("userName", String.class),
069: new DynaProperty("source", String.class),
070: new DynaProperty("description", String.class) };
071:
072: dynaClass_ = new BasicDynaClass("mh_event", Event.class,
073: dynaProperties);
074: }
075:
076: // constructors /////////////////////////////////////////////////////////////
077:
078: public Event() {
079: super (dynaClass_);
080: setInt("sitecontext_id", 0);
081: setDate("eventTime", TestUtils.getCurrentDate());
082: setString("userName", "");
083: setString("source", "");
084: setString("description", "");
085: }
086:
087: public Event(DynaClass dynaClass) {
088: super (dynaClass);
089: }
090:
091: // constants ////////////////////////////////////////////////////////////////
092:
093: // classes //////////////////////////////////////////////////////////////////
094:
095: // methods //////////////////////////////////////////////////////////////////
096:
097: /**
098: * Sets <tt>sitecontext_id</tt> before calling super-class method.
099: */
100: public void saveNew() {
101: setInt("sitecontext_id", getSiteContext().getInt("id"));
102: super .saveNew();
103: }
104:
105: /**
106: * Logs an event.
107: */
108: public void log(String userName, String source, String description) {
109:
110: Event e = new Event();
111: e.setInt("sitecontext_id", getSiteContext().getInt("id"));
112: e.setString("userName", userName);
113: e.setString("source", source);
114: e.setString("description", description);
115: e.saveNew();
116: }
117:
118: /**
119: * Logs an event.
120: */
121: public static void log(SiteContext siteContext, String userName,
122: String source, String description) {
123:
124: Event e = new Event();
125: e.setSiteContext(siteContext);
126: e.setString("userName", userName);
127: e.setString("source", source);
128: e.setString("description", description);
129: e.saveNew();
130: }
131:
132: /**
133: * Logs an event; <tt>userName</tt> and <tt>source</tt> default to empty
134: * strings;
135: */
136: public void log(String description) {
137:
138: log("", "", description);
139: }
140:
141: /**
142: * Logs an event; <tt>userName</tt> and <tt>source</tt> default to empty
143: * strings;
144: */
145: public static void log(SiteContext siteContext, String description) {
146:
147: log(siteContext, "", "", description);
148: }
149:
150: /**
151: * Loads all events between <tt>from</tt> <tt>to</tt> in reverse
152: * chronological order; the result set is limited to <tt>pageSize</tt> events
153: * and the <tt>page</tt>th page is returned. If <tt>from</tt> or
154: * <tt>to</tt>, the result set in not bounded in that direction.
155: * @deprecated
156: */
157: public List getEvents(Date from, Date to, int page, int pageSize) {
158:
159: //
160: // build where clause
161: //
162: String whereClause = null;
163:
164: /*
165: if ( ( from != null ) && ( to != null ) )
166: whereClause =
167: "eventTime BETWEEN " + getSqlLiteral( from ) + " AND " +
168: getSqlLiteral( to );
169: */
170:
171: if ((from != null) && (to != null))
172: whereClause = "eventTime BETWEEN " + getSqlLiteral(from)
173: + " AND " + getSqlLiteral(to);
174:
175: else if ((from == null) && (to != null))
176: whereClause = "eventTime < " + getSqlLiteral(to);
177:
178: else if ((from != null) && (to == null))
179: whereClause = "eventTime > " + getSqlLiteral(from);
180:
181: if (whereClause == null)
182: whereClause = "sitecontext_id="
183: + getSiteContext().getInt("id");
184: else
185: whereClause += " AND sitecontext_id="
186: + getSiteContext().getInt("id");
187:
188: //
189: // build sql statement
190: //
191: String sql = "SELECT " + " eventTime, " + " userName, "
192: + " source, " + " description " + "FROM "
193: + " mh_event ";
194:
195: if (whereClause != null)
196: sql += " WHERE " + whereClause;
197:
198: sql += " ORDER BY eventTime DESC ";
199:
200: if (page >= 0) {
201: if (ConnectionSingleton.DBTYPE_MYSQL
202: .equals(ConnectionSingleton.getDatabaseType()))
203: sql += " LIMIT " + (pageSize * page) + ", " + pageSize
204: + " ";
205: else
206: sql += " LIMIT " + pageSize + " OFFSET "
207: + (pageSize * page);
208: }
209:
210: //
211: // get events
212: //
213: List events = new ArrayList();
214:
215: ResultSet rs = null;
216: try {
217: rs = ConnectionSingleton.runQuery(sql);
218:
219: if (rs == null) {
220: throw new SQLException("Null result set.");
221: }
222:
223: while (rs.next()) {
224: Event event = new Event();
225: event
226: .setDate("eventTime", rs
227: .getTimestamp("eventTime"));
228: event.setString("userName", rs.getString("userName"));
229: event.setString("source", rs.getString("source"));
230: event.setString("description", rs
231: .getString("description"));
232: events.add(event);
233: }
234: } catch (SQLException e) {
235: String msg = "Getting events from \"" + from + "\" to \""
236: + to + "\". " + ExceptionUtils.getStackTrace(e);
237: logger_.error(msg);
238: throw new RuntimeException(msg);
239: } finally {
240: ConnectionSingleton.close(rs);
241: }
242:
243: return events;
244: }
245:
246: /**
247: * Loads all events between <tt>from</tt> <tt>to</tt> in reverse
248: * chronological order; the result set is limited to <tt>pageSize</tt> events
249: * and the <tt>page</tt>th page is returned. If <tt>from</tt> or
250: * <tt>to</tt>, the result set in not bounded in that direction.
251: */
252: public List getEvents(Date from, Date to) {
253:
254: //
255: // build where clause
256: //
257: String whereClause = null;
258:
259: if ((from != null) && (to != null))
260: whereClause = "eventTime BETWEEN " + getSqlLiteral(from)
261: + " AND " + getSqlLiteral(to);
262:
263: else if ((from == null) && (to != null))
264: whereClause = "eventTime < " + getSqlLiteral(to);
265:
266: else if ((from != null) && (to == null))
267: whereClause = "eventTime > " + getSqlLiteral(from);
268:
269: if (whereClause == null)
270: whereClause = "sitecontext_id="
271: + getSiteContext().getInt("id");
272: else
273: whereClause += " AND sitecontext_id="
274: + getSiteContext().getInt("id");
275:
276: //
277: // build sql statement
278: //
279: String sql = "SELECT " + " eventTime, " + " userName, "
280: + " source, " + " description " + "FROM "
281: + " mh_event ";
282:
283: if (whereClause != null)
284: sql += " WHERE " + whereClause;
285:
286: sql += " ORDER BY eventTime DESC ";
287:
288: //
289: // get events
290: //
291: List events = new ArrayList();
292:
293: ResultSet rs = null;
294: try {
295: rs = ConnectionSingleton.runQuery(sql);
296:
297: while (rs.next()) {
298: Event event = new Event();
299: event
300: .setDate("eventTime", rs
301: .getTimestamp("eventTime"));
302: event.setString("userName", rs.getString("userName"));
303: event.setString("source", rs.getString("source"));
304: event.setString("description", rs
305: .getString("description"));
306: events.add(event);
307: }
308:
309: ConnectionSingleton.close(rs);
310: } catch (SQLException e) {
311: if (rs != null)
312: ConnectionSingleton.close(rs);
313:
314: throw new PersistableException(e.toString());
315: }
316:
317: return events;
318: }
319:
320: /**
321: * Deletes all events that occured before <tt>date</tt>.
322: */
323: public void deleteBefore(Date date) {
324:
325: deleteAll(dynaClass_, "eventTime < " + getSqlLiteral(date)
326: + " AND sitecontext_id="
327: + getSiteContext().getInt("id"));
328: }
329:
330: // properties ///////////////////////////////////////////////////////////////
331:
332: /**
333: * Returns the site context associated with this object; if no site context
334: * has been set, the default context (as returned by {@link
335: * com.methodhead.sitecontext.SiteContext#getDefaultContext()
336: * SiteContext.getDefaultContext()}) is returned.
337: */
338: public SiteContext getSiteContext() {
339: if (siteContext_ == null)
340: siteContext_ = SiteContext.getDefaultContext();
341:
342: return siteContext_;
343: }
344:
345: public void setSiteContext(SiteContext siteContext) {
346: siteContext_ = siteContext;
347: }
348:
349: /**
350: * Deletes all events for the site context.
351: */
352: public void deleteAll() {
353: deleteAll(dynaClass_, "sitecontext_id="
354: + getSiteContext().getInt("id"));
355: }
356:
357: // attributes ///////////////////////////////////////////////////////////////
358:
359: private SiteContext siteContext_ = null;
360:
361: private static Logger logger_ = Logger.getLogger(Event.class);
362: }
|