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: * SendNotice.java
067: *
068: * Copyright 1999, 2000, 2001 Jcorporate Ltd.
069: */
070: package com.jcorporate.expresso.ext.job;
071:
072: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
073: import com.jcorporate.expresso.core.job.Job;
074: import com.jcorporate.expresso.core.security.User;
075: import com.jcorporate.expresso.ext.dbobj.DownloadLog;
076: import com.jcorporate.expresso.services.dbobj.Event;
077: import com.jcorporate.expresso.services.dbobj.JobQueue;
078: import com.jcorporate.expresso.services.dbobj.JobQueueParam;
079: import org.apache.log4j.Logger;
080:
081: import javax.servlet.ServletException;
082: import java.util.Enumeration;
083: import java.util.Hashtable;
084: import java.util.Iterator;
085:
086: /**
087: * SendNotice allows an email notification to be sent to a group of
088: * users that have downloaded a file.
089: *
090: * @author Michael Nash
091: */
092: public class SendNotice extends Job {
093: private static final String this Class = SendNotice.class.getName()
094: + ".";
095: private static Logger log = Logger
096: .getLogger("expresso.services.job.SendNotice");;
097:
098: /**
099: * 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 msg) {
115: String myName = (this Class + "errorNotify()");
116:
117: try {
118: Event myEvent = new Event(getJobQueueEntry()
119: .getDataContext(), "SYSERROR", myName
120: + ":Unable to send notifications:" + msg + " Job:"
121: + getJobNumber() + " User:" + getUser(), false);
122: } catch (Exception e) {
123: log.error(e);
124: }
125: } /* errorNotify(String) */
126:
127: /**
128: * Retrieve the title of this job
129: *
130: * @return java.lang.String The Title of the controller
131: */
132: public String getTitle() {
133: return ("Send Notices");
134: } /* getTitle() */
135:
136: /**
137: * Receive the notification request and send the emails
138: */
139: public void run() {
140: super .run();
141:
142: String myName = (this Class + "run()");
143:
144: try {
145: StringBuffer mailMsg = new StringBuffer("");
146: JobQueue jq = getJobQueueEntry();
147: String uid = jq.getField("ExpUid");
148: String subject = jq.getParamValue("Subject");
149: String fileNumber = jq.getParamValue("FileNumber");
150: StringBuffer noticeTextBuffer = new StringBuffer();
151: JobQueueParam jqp = new JobQueueParam(
152: SecuredDBObject.SYSTEM_ACCOUNT);
153: jqp.setField("JobNumber", jq.getField("JobNumber"));
154: jqp.setField("ParamCode", "NoticeText%");
155:
156: JobQueueParam oneJqp = null;
157:
158: for (Iterator je = jqp.searchAndRetrieveList("ParamNumber")
159: .iterator(); je.hasNext();) {
160: oneJqp = (JobQueueParam) je.next();
161: noticeTextBuffer.append(oneJqp.getField("ParamValue"));
162: noticeTextBuffer.append("\n");
163: } /* for */
164:
165: String noticeText = noticeTextBuffer.toString();
166:
167: if (fileNumber.equals("")) {
168: throw new ServletException(myName
169: + ":File number must not be " + "blank");
170: }
171:
172: Hashtable h = new Hashtable(10);
173: User oneUser = new User();
174:
175: /* Build the unique list of people who have downloaded */
176: DownloadLog dll = new DownloadLog(
177: SecuredDBObject.SYSTEM_ACCOUNT);
178: dll.setField("FileNumber", fileNumber);
179:
180: DownloadLog oneDl = null;
181:
182: for (Iterator e = dll.searchAndRetrieveList().iterator(); e
183: .hasNext();) {
184: oneDl = (DownloadLog) e.next();
185:
186: if (h.get(oneDl.getField("ExpUid")) == null) {
187: h.put(oneDl.getField("ExpUid"), oneDl
188: .getField("ExpUid"));
189: }
190: }
191:
192: int noticeCount = 0;
193:
194: for (Enumeration e = h.keys(); e.hasMoreElements();) {
195: oneUser.clear();
196: oneUser.setUid((String) e.nextElement());
197:
198: if (oneUser.find()) {
199: mailMsg.append("Sending notice to "
200: + oneUser.getLoginName() + " ("
201: + oneUser.getUid() + ")\n");
202: oneUser.notify(subject, noticeText);
203: noticeCount++;
204: } else {
205: mailMsg.append("Unable to find user '"
206: + oneDl.getField("ExpUid")
207: + " in user table");
208: }
209: }
210:
211: mailMsg.append("" + noticeCount + " Notifications Sent");
212: oneUser.clear();
213: oneUser.setUid(uid);
214: oneUser.retrieve();
215: oneUser.notify("Sent Notifications", mailMsg.toString());
216: if (log.isInfoEnabled()) {
217: log.info("Job completed - removing queue entry");
218: }
219: finish("Send Notices Job Completed");
220: } catch (Exception de) {
221: finish("Unable to Send Notices", de);
222: }
223: } /* run() */
224:
225: } /* SendNotice */
|