001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: /**
066: * Event.java
067: *
068: * Copyright 1999, 2000, 2001 Jcorporate Ltd.
069: */package com.jcorporate.expresso.services.dbobj;
070:
071: import com.jcorporate.expresso.core.db.DBException;
072: import com.jcorporate.expresso.core.dbobj.RequestContext;
073: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
074: import com.jcorporate.expresso.core.logging.LogException;
075: import com.jcorporate.expresso.core.misc.ConfigManager;
076: import com.jcorporate.expresso.core.misc.ConfigurationException;
077: import com.jcorporate.expresso.core.misc.EMailSender;
078: import com.jcorporate.expresso.core.misc.StringUtil;
079: import com.jcorporate.expresso.core.security.User;
080: import com.jcorporate.expresso.kernel.util.FastStringBuffer;
081: import org.apache.log4j.Logger;
082:
083: import java.util.Enumeration;
084: import java.util.Iterator;
085: import java.util.Vector;
086:
087: /**
088: * Event
089: *
090: * @author Michael Nash
091: */
092: public class Event extends SecuredDBObject {
093: private static Logger log = Logger.getLogger(Event.class);
094:
095: /**
096: * use this as theEvent parameter in order to generate admin email
097: *
098: * @see #Event(java.lang.String, java.lang.String, java.lang.String, boolean)
099: */
100: public static final String SYSTEM_ERROR = "SYSERROR";
101:
102: /**
103: * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
104: */
105: public Event() throws DBException {
106: super ();
107: } /* Event() */
108:
109: /**
110: * @param uid the expresso uid
111: * @throws DBException upon initialization error
112: */
113: public Event(int uid) throws DBException {
114: super (uid);
115: }
116:
117: /**
118: * For using DBObjects within Controllers. Initializes based upon the current
119: * user and the requested db. [Of course this can be modified later]
120: *
121: * @param request - The request context handed to you by the framework.
122: * @throws DBException upon initialization error
123: */
124: public Event(RequestContext request) throws DBException {
125: super (request);
126: }
127:
128: /**
129: * The one-liner version of event via this special constructor
130: * This method is used to create the event and send the notifications all
131: * in one step.
132: *
133: * @param dbName the database context name
134: * @param theEvent Code of the event to trigger
135: * @param theMessage Detail message associated for the event
136: * @param success True if the event is the success of a task,
137: * false if it indicates failure
138: * @throws DBException If there is problem sending the notification
139: */
140: public Event(String dbName, String theEvent, String theMessage,
141: boolean success) throws DBException, LogException {
142: this (SecuredDBObject.SYSTEM_ACCOUNT);
143: setDataContext(dbName);
144: setField("Event", theEvent);
145:
146: /* if we can't find the event, send a system error notice */
147: try {
148: retrieve();
149: } catch (DBException de) {
150:
151: /* unless of course we can't find the syserror event! */
152: if (!theEvent.equalsIgnoreCase(SYSTEM_ERROR)) {
153: new Event(dbName, SYSTEM_ERROR,
154: "Could not find event '" + theEvent + "'",
155: false);
156: throw new DBException("No such event '" + theEvent
157: + "'");
158: }
159: }
160:
161: Vector mailMessage = new Vector(1);
162: mailMessage.addElement(theMessage);
163: sendMail(mailMessage, success);
164: } /* Event(String, String, String, boolean) */
165:
166: /**
167: * @return Vector of valid values
168: * @throws DBException If there is a problem retrieving the values
169: */
170: public Vector getValues() throws DBException {
171: return getValuesDefault("Event", "Descrip");
172: } /* getValues() */
173:
174: /**
175: * Once an Event has been retrieved, this method allows the e-mail
176: * notifications to be sent to each of the appropriate users
177: *
178: * @param theMessage A Vector of strings containing the text of
179: * the message to be sent
180: * @param success Is this message a notification of success?
181: * @throws DBException on any error, including a mail system error
182: */
183: public void sendMail(Vector theMessage, boolean success)
184: throws DBException, LogException {
185: EventMail myEventMails = new EventMail(
186: SecuredDBObject.SYSTEM_ACCOUNT);
187: myEventMails.setDataContext(getDataContext());
188:
189: EventMail oneEventMail = null;
190: User oneUser = new User();
191: oneUser.setDataContext(getDataContext());
192:
193: FastStringBuffer bigString = new FastStringBuffer(256);
194: String oneRecipient = ("");
195:
196: for (Enumeration e = theMessage.elements(); e.hasMoreElements();) {
197: bigString.append((String) e.nextElement());
198: bigString.append("\n");
199: }
200:
201: bigString.append("\nFrom Server:");
202: bigString.append(Setup.getValueRequired(getDataContext(),
203: "HTTPServ"));
204: bigString.append(", Database/context:");
205: bigString.append(getDataContext());
206:
207: String dbDescrip = "";
208:
209: try {
210: dbDescrip = StringUtil.notNull(ConfigManager.getContext(
211: getDataContext()).getDescription());
212: } catch (ConfigurationException ce) {
213: throw new DBException(ce);
214: }
215: if (!dbDescrip.equals("")) {
216: bigString.append(" (");
217: bigString.append(dbDescrip);
218: bigString.append(")");
219: }
220:
221: myEventMails.setField("Event", this .getField("Event"));
222:
223: if (success) {
224: myEventMails.setField("Success", "Y");
225: } else {
226: myEventMails.setField("Success", "N");
227: }
228:
229: int theCount = 0;
230:
231: for (Iterator em = myEventMails.searchAndRetrieveList()
232: .iterator(); em.hasNext();) {
233: oneEventMail = (EventMail) em.next();
234: theCount++;
235: oneUser.clear();
236: oneUser.setUid(oneEventMail.getField("ExpUid"));
237:
238: if (!oneUser.find()) {
239: log.error("Attempting to send event notification "
240: + "for event '" + getField("Event") + "' to "
241: + "user " + oneEventMail.getField("ExpUid")
242: + ", but this user not found in the '"
243: + getDataContext() + "' context.");
244: continue;
245: }
246:
247: oneRecipient = oneUser.getEmail();
248:
249: if (log.isInfoEnabled()) {
250: log.info("Notifying " + oneRecipient + ":"
251: + this .getField("Descrip"));
252: }
253:
254: try {
255: String subject = null;
256:
257: if (success) {
258: subject = (this .getField("Descrip"));
259: } else {
260: subject = ("ERROR:" + getField("Descrip"));
261: }
262:
263: EMailSender ems = new EMailSender();
264: ems.setDBName(getDataContext());
265: ems.send(oneRecipient, subject, bigString.toString());
266: } catch (Exception e) {
267: throw new DBException("Error sending e-mail", e);
268: }
269: } /* for each recipient */
270:
271: if (log.isInfoEnabled()) {
272: log.info("Sent " + theCount + " e-mail notifications");
273: }
274:
275: if (theCount == 0) {
276: String successFlag = "N";
277:
278: if (success) {
279: successFlag = "Y";
280: }
281:
282: log.warn("No users in db/context '" + getDataContext()
283: + "' were set " + "to receive notice of event '"
284: + getField("Event") + "' with " + " success flag '"
285: + successFlag + "', so " + "no notices were sent.");
286: }
287: } /* sendMail(Vector, boolean) */
288:
289: /**
290: * Set up the database fields for this object
291: */
292: protected synchronized void setupFields() throws DBException {
293: setTargetTable("EVENT");
294: setDescription("DBevent");
295: setCharset("ISO-8859-1");
296: addField("Event", "char", 30, false, "eventCode");
297: addField("Descrip", "varchar", 80, false, "eventDescrip");
298: setStringFilter("Event", "stripFilter");
299: setStringFilter("Descrip", "standardFilter");
300: addKey("Event");
301: addDetail("com.jcorporate.expresso.services.dbobj.EventMail",
302: "Event", "Event");
303: } /* setupFields() */
304:
305: /**
306: * @throws DBException
307: */
308: public synchronized void populateDefaultValues() throws DBException {
309: if (log.isInfoEnabled()) {
310: log.info("Populating default values for Events in db '"
311: + getDataContext() + "'");
312: }
313:
314: Event oneEvent = new Event();
315: oneEvent.setDataContext(getDataContext());
316: oneEvent.setField("Event", SYSTEM_ERROR);
317:
318: if (!oneEvent.find()) {
319: oneEvent.setField("Descrip", "System Error");
320: oneEvent.add();
321: if (log.isInfoEnabled()) {
322: log.info("Added 'SYSERROR' event");
323: }
324: } else {
325: if (log.isInfoEnabled()) {
326: log.info("'SYSERROR' event already set up");
327: }
328: }
329:
330: oneEvent.clear();
331: oneEvent.setField("Event", "HEALTH");
332:
333: if (!oneEvent.find()) {
334: oneEvent.setField("Descrip", "System Health Check");
335: oneEvent.add();
336: if (log.isInfoEnabled()) {
337: log.info("Added 'HEALTH' event");
338: }
339: } else {
340: if (log.isInfoEnabled()) {
341: log.info("'HEALTH' event already set up");
342: }
343: }
344:
345: oneEvent.clear();
346: oneEvent.setField("Event", "DOWNLOAD");
347:
348: if (!oneEvent.find()) {
349: oneEvent.setField("Descrip", "User Download Activity");
350: oneEvent.add();
351: if (log.isInfoEnabled()) {
352: log.info("Added 'DOWNLOAD' event");
353: }
354: } else {
355: if (log.isInfoEnabled()) {
356: log.info("'DOWNLOAD' event already set up");
357: }
358: }
359:
360: oneEvent.clear();
361: oneEvent.setField("Event", "REGISTER");
362:
363: if (!oneEvent.find()) {
364: oneEvent.setField("Descrip", "User Registration Activity");
365: oneEvent.add();
366: if (log.isInfoEnabled()) {
367: log.info("Added 'REGISTER' event");
368: }
369: } else {
370: if (log.isInfoEnabled()) {
371: log.info("'REGISTER' event already set up");
372: }
373: }
374: } /* populateDefaultValues() */
375:
376: } /* Event */
|