KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > dbobj > Event


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 /**
66  * Event.java
67  *
68  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.services.dbobj;
71
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.dbobj.RequestContext;
74 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
75 import com.jcorporate.expresso.core.logging.LogException;
76 import com.jcorporate.expresso.core.misc.ConfigManager;
77 import com.jcorporate.expresso.core.misc.ConfigurationException;
78 import com.jcorporate.expresso.core.misc.EMailSender;
79 import com.jcorporate.expresso.core.misc.StringUtil;
80 import com.jcorporate.expresso.core.security.User;
81 import com.jcorporate.expresso.kernel.util.FastStringBuffer;
82 import org.apache.log4j.Logger;
83
84 import java.util.Enumeration JavaDoc;
85 import java.util.Iterator JavaDoc;
86 import java.util.Vector JavaDoc;
87
88
89 /**
90  * Event
91  *
92  * @author Michael Nash
93  */

94 public class Event
95         extends SecuredDBObject {
96     private static Logger log = Logger.getLogger(Event.class);
97
98     /**
99      * use this as theEvent parameter in order to generate admin email
100      *
101      * @see #Event(java.lang.String, java.lang.String, java.lang.String, boolean)
102      */

103     public static final String JavaDoc SYSTEM_ERROR = "SYSERROR";
104
105     /**
106      * @see com.jcorporate.expresso.core.dbobj.SecuredDBObject
107      */

108     public Event()
109             throws DBException {
110         super();
111     } /* Event() */
112
113     /**
114      * @param uid the expresso uid
115      * @throws DBException upon initialization error
116      */

117     public Event(int uid)
118             throws DBException {
119         super(uid);
120     }
121
122     /**
123      * For using DBObjects within Controllers. Initializes based upon the current
124      * user and the requested db. [Of course this can be modified later]
125      *
126      * @param request - The request context handed to you by the framework.
127      * @throws DBException upon initialization error
128      */

129     public Event(RequestContext request)
130             throws DBException {
131         super(request);
132     }
133
134     /**
135      * The one-liner version of event via this special constructor
136      * This method is used to create the event and send the notifications all
137      * in one step.
138      *
139      * @param dbName the database context name
140      * @param theEvent Code of the event to trigger
141      * @param theMessage Detail message associated for the event
142      * @param success True if the event is the success of a task,
143      * false if it indicates failure
144      * @throws DBException If there is problem sending the notification
145      */

146     public Event(String JavaDoc dbName, String JavaDoc theEvent, String JavaDoc theMessage,
147                  boolean success)
148             throws DBException, LogException {
149         this(SecuredDBObject.SYSTEM_ACCOUNT);
150         setDataContext(dbName);
151         setField("Event", theEvent);
152
153         /* if we can't find the event, send a system error notice */
154         try {
155             retrieve();
156         } catch (DBException de) {
157
158             /* unless of course we can't find the syserror event! */
159             if (!theEvent.equalsIgnoreCase(SYSTEM_ERROR)) {
160                 new Event(dbName, SYSTEM_ERROR,
161                         "Could not find event '" + theEvent + "'", false);
162                 throw new DBException("No such event '" + theEvent + "'");
163             }
164         }
165
166         Vector JavaDoc mailMessage = new Vector JavaDoc(1);
167         mailMessage.addElement(theMessage);
168         sendMail(mailMessage, success);
169     } /* Event(String, String, String, boolean) */
170
171     /**
172      * @return Vector of valid values
173      * @throws DBException If there is a problem retrieving the values
174      */

175     public Vector JavaDoc getValues()
176             throws DBException {
177         return getValuesDefault("Event", "Descrip");
178     } /* getValues() */
179
180
181     /**
182      * Once an Event has been retrieved, this method allows the e-mail
183      * notifications to be sent to each of the appropriate users
184      *
185      * @param theMessage A Vector of strings containing the text of
186      * the message to be sent
187      * @param success Is this message a notification of success?
188      * @throws DBException on any error, including a mail system error
189      */

190     public void sendMail(Vector JavaDoc theMessage, boolean success)
191             throws DBException, LogException {
192         EventMail myEventMails = new EventMail(SecuredDBObject.SYSTEM_ACCOUNT);
193         myEventMails.setDataContext(getDataContext());
194
195         EventMail oneEventMail = null;
196         User oneUser = new User();
197         oneUser.setDataContext(getDataContext());
198
199         FastStringBuffer bigString = new FastStringBuffer(256);
200         String JavaDoc oneRecipient = ("");
201
202         for (Enumeration JavaDoc e = theMessage.elements(); e.hasMoreElements();) {
203             bigString.append((String JavaDoc) e.nextElement());
204             bigString.append("\n");
205         }
206
207         bigString.append("\nFrom Server:");
208         bigString.append(Setup.getValueRequired(getDataContext(), "HTTPServ"));
209         bigString.append(", Database/context:");
210         bigString.append(getDataContext());
211
212         String JavaDoc dbDescrip = "";
213
214         try {
215             dbDescrip = StringUtil.notNull(ConfigManager.getContext(getDataContext()).getDescription());
216         } catch (ConfigurationException ce) {
217             throw new DBException(ce);
218         }
219         if (!dbDescrip.equals("")) {
220             bigString.append(" (");
221             bigString.append(dbDescrip);
222             bigString.append(")");
223         }
224
225         myEventMails.setField("Event", this.getField("Event"));
226
227         if (success) {
228             myEventMails.setField("Success", "Y");
229         } else {
230             myEventMails.setField("Success", "N");
231         }
232
233         int theCount = 0;
234
235         for (Iterator JavaDoc em = myEventMails.searchAndRetrieveList().iterator();
236              em.hasNext();) {
237             oneEventMail = (EventMail) em.next();
238             theCount++;
239             oneUser.clear();
240             oneUser.setUid(oneEventMail.getField("ExpUid"));
241
242             if (!oneUser.find()) {
243                 log.error("Attempting to send event notification " +
244                         "for event '" + getField("Event") + "' to " +
245                         "user " + oneEventMail.getField("ExpUid") +
246                         ", but this user not found in the '" + getDataContext() +
247                         "' context.");
248                 continue;
249             }
250
251             oneRecipient = oneUser.getEmail();
252
253             if (log.isInfoEnabled()) {
254                 log.info("Notifying " + oneRecipient + ":" +
255                         this.getField("Descrip"));
256             }
257
258             try {
259                 String JavaDoc subject = null;
260
261                 if (success) {
262                     subject = (this.getField("Descrip"));
263                 } else {
264                     subject = ("ERROR:" + getField("Descrip"));
265                 }
266
267                 EMailSender ems = new EMailSender();
268                 ems.setDBName(getDataContext());
269                 ems.send(oneRecipient, subject, bigString.toString());
270             } catch (Exception JavaDoc e) {
271                 throw new DBException("Error sending e-mail", e);
272             }
273         } /* for each recipient */
274
275
276         if (log.isInfoEnabled()) {
277             log.info("Sent " + theCount + " e-mail notifications");
278         }
279
280         if (theCount == 0) {
281             String JavaDoc successFlag = "N";
282
283             if (success) {
284                 successFlag = "Y";
285             }
286
287             log.warn("No users in db/context '" + getDataContext() + "' were set " +
288                     "to receive notice of event '" + getField("Event") +
289                     "' with " + " success flag '" + successFlag + "', so " +
290                     "no notices were sent.");
291         }
292     } /* sendMail(Vector, boolean) */
293
294
295     /**
296      * Set up the database fields for this object
297      */

298     protected synchronized void setupFields()
299             throws DBException {
300         setTargetTable("EVENT");
301         setDescription("DBevent");
302         setCharset("ISO-8859-1");
303         addField("Event", "char", 30, false, "eventCode");
304         addField("Descrip", "varchar", 80, false, "eventDescrip");
305         setStringFilter("Event", "stripFilter");
306         setStringFilter("Descrip", "standardFilter");
307         addKey("Event");
308         addDetail("com.jcorporate.expresso.services.dbobj.EventMail", "Event",
309                 "Event");
310     } /* setupFields() */
311
312
313     /**
314      * @throws DBException
315      */

316     public synchronized void populateDefaultValues()
317             throws DBException {
318         if (log.isInfoEnabled()) {
319             log.info("Populating default values for Events in db '" + getDataContext() +
320                     "'");
321         }
322
323         Event oneEvent = new Event();
324         oneEvent.setDataContext(getDataContext());
325         oneEvent.setField("Event", SYSTEM_ERROR);
326
327         if (!oneEvent.find()) {
328             oneEvent.setField("Descrip", "System Error");
329             oneEvent.add();
330             if (log.isInfoEnabled()) {
331                 log.info("Added 'SYSERROR' event");
332             }
333         } else {
334             if (log.isInfoEnabled()) {
335                 log.info("'SYSERROR' event already set up");
336             }
337         }
338
339         oneEvent.clear();
340         oneEvent.setField("Event", "HEALTH");
341
342         if (!oneEvent.find()) {
343             oneEvent.setField("Descrip", "System Health Check");
344             oneEvent.add();
345             if (log.isInfoEnabled()) {
346                 log.info("Added 'HEALTH' event");
347             }
348         } else {
349             if (log.isInfoEnabled()) {
350                 log.info("'HEALTH' event already set up");
351             }
352         }
353
354         oneEvent.clear();
355         oneEvent.setField("Event", "DOWNLOAD");
356
357         if (!oneEvent.find()) {
358             oneEvent.setField("Descrip", "User Download Activity");
359             oneEvent.add();
360             if (log.isInfoEnabled()) {
361                 log.info("Added 'DOWNLOAD' event");
362             }
363         } else {
364             if (log.isInfoEnabled()) {
365                 log.info("'DOWNLOAD' event already set up");
366             }
367         }
368
369         oneEvent.clear();
370         oneEvent.setField("Event", "REGISTER");
371
372         if (!oneEvent.find()) {
373             oneEvent.setField("Descrip", "User Registration Activity");
374             oneEvent.add();
375             if (log.isInfoEnabled()) {
376                 log.info("Added 'REGISTER' event");
377             }
378         } else {
379             if (log.isInfoEnabled()) {
380                 log.info("'REGISTER' event already set up");
381             }
382         }
383     } /* populateDefaultValues() */
384
385
386 } /* Event */
387
Popular Tags