KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > job > SendNotice


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  * SendNotice.java
67  *
68  * Copyright 1999, 2000, 2001 Jcorporate Ltd.
69  */

70 package com.jcorporate.expresso.ext.job;
71
72 import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
73 import com.jcorporate.expresso.core.job.Job;
74 import com.jcorporate.expresso.core.security.User;
75 import com.jcorporate.expresso.ext.dbobj.DownloadLog;
76 import com.jcorporate.expresso.services.dbobj.Event;
77 import com.jcorporate.expresso.services.dbobj.JobQueue;
78 import com.jcorporate.expresso.services.dbobj.JobQueueParam;
79 import org.apache.log4j.Logger;
80
81 import javax.servlet.ServletException JavaDoc;
82 import java.util.Enumeration JavaDoc;
83 import java.util.Hashtable JavaDoc;
84 import java.util.Iterator JavaDoc;
85
86
87 /**
88  * SendNotice allows an email notification to be sent to a group of
89  * users that have downloaded a file.
90  *
91  * @author Michael Nash
92  */

93 public class SendNotice
94         extends Job {
95     private static final String JavaDoc thisClass = SendNotice.class.getName() + ".";
96     private static Logger log = Logger.getLogger("expresso.services.job.SendNotice");;
97
98     /**
99      * Default Constructor
100      */

101     public SendNotice() {
102         super();
103         addParameter("Subject", "Message Subject");
104         addParameter("FileNumber", "File Number");
105         addParameter("NoticeText", "Notice Text");
106     } /* SendNotice() */
107
108     /**
109      * Triggers the SYSERROR event and sends the message specified by parameter
110      * msg.
111      *
112      * @param msg The message to send for the notifications
113      */

114     private void errorNotify(String JavaDoc msg) {
115         String JavaDoc myName = (thisClass + "errorNotify()");
116
117         try {
118             Event myEvent = new Event(getJobQueueEntry().getDataContext(),
119                     "SYSERROR",
120                     myName +
121                     ":Unable to send notifications:" + msg +
122                     " Job:" + getJobNumber() + " User:" +
123                     getUser(), false);
124         } catch (Exception JavaDoc e) {
125             log.error(e);
126         }
127     } /* errorNotify(String) */
128
129     /**
130      * Retrieve the title of this job
131      *
132      * @return java.lang.String The Title of the controller
133      */

134     public String JavaDoc getTitle() {
135         return ("Send Notices");
136     } /* getTitle() */
137
138     /**
139      * Receive the notification request and send the emails
140      */

141     public void run() {
142         super.run();
143
144         String JavaDoc myName = (thisClass + "run()");
145
146         try {
147             StringBuffer JavaDoc mailMsg = new StringBuffer JavaDoc("");
148             JobQueue jq = getJobQueueEntry();
149             String JavaDoc uid = jq.getField("ExpUid");
150             String JavaDoc subject = jq.getParamValue("Subject");
151             String JavaDoc fileNumber = jq.getParamValue("FileNumber");
152             StringBuffer JavaDoc noticeTextBuffer = new StringBuffer JavaDoc();
153             JobQueueParam jqp = new JobQueueParam(SecuredDBObject.SYSTEM_ACCOUNT);
154             jqp.setField("JobNumber", jq.getField("JobNumber"));
155             jqp.setField("ParamCode", "NoticeText%");
156
157             JobQueueParam oneJqp = null;
158
159             for (Iterator JavaDoc je = jqp.searchAndRetrieveList("ParamNumber").iterator();
160                  je.hasNext();) {
161                 oneJqp = (JobQueueParam) je.next();
162                 noticeTextBuffer.append(oneJqp.getField("ParamValue"));
163                 noticeTextBuffer.append("\n");
164             } /* for */
165
166
167             String JavaDoc noticeText = noticeTextBuffer.toString();
168
169             if (fileNumber.equals("")) {
170                 throw new ServletException JavaDoc(myName +
171                         ":File number must not be " +
172                         "blank");
173             }
174
175             Hashtable JavaDoc h = new Hashtable JavaDoc(10);
176             User oneUser = new User();
177
178             /* Build the unique list of people who have downloaded */
179             DownloadLog dll = new DownloadLog(SecuredDBObject.SYSTEM_ACCOUNT);
180             dll.setField("FileNumber", fileNumber);
181
182             DownloadLog oneDl = null;
183
184             for (Iterator JavaDoc e = dll.searchAndRetrieveList().iterator();
185                  e.hasNext();) {
186                 oneDl = (DownloadLog) e.next();
187
188                 if (h.get(oneDl.getField("ExpUid")) == null) {
189                     h.put(oneDl.getField("ExpUid"), oneDl.getField("ExpUid"));
190                 }
191             }
192
193             int noticeCount = 0;
194
195             for (Enumeration JavaDoc e = h.keys(); e.hasMoreElements();) {
196                 oneUser.clear();
197                 oneUser.setUid((String JavaDoc) e.nextElement());
198
199                 if (oneUser.find()) {
200                     mailMsg.append("Sending notice to " +
201                             oneUser.getLoginName() + " (" +
202                             oneUser.getUid() + ")\n");
203                     oneUser.notify(subject, noticeText);
204                     noticeCount++;
205                 } else {
206                     mailMsg.append("Unable to find user '" +
207                             oneDl.getField("ExpUid") +
208                             " in user table");
209                 }
210             }
211
212             mailMsg.append("" + noticeCount + " Notifications Sent");
213             oneUser.clear();
214             oneUser.setUid(uid);
215             oneUser.retrieve();
216             oneUser.notify("Sent Notifications", mailMsg.toString());
217             if (log.isInfoEnabled()) {
218                 log.info("Job completed - removing queue entry");
219             }
220             finish("Send Notices Job Completed");
221         } catch (Exception JavaDoc de) {
222             finish("Unable to Send Notices", de);
223         }
224     } /* run() */
225
226 } /* SendNotice */
227
Popular Tags