001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.portlet.news;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.util.StringTokenizer;
024: import java.util.Timer;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletException;
029:
030: import org.w3c.dom.Element;
031:
032: import com.nabhinc.portlet.mvcportlet.core.ActionConfig;
033: import com.nabhinc.portlet.mvcportlet.core.ActionProcessor;
034: import com.nabhinc.portlet.mvcportlet.core.BaseRequestProcessor;
035: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
036: import com.nabhinc.util.IOUtil;
037: import com.nabhinc.util.XMLUtil;
038: import com.nabhinc.util.scheduling.DailyTask;
039: import com.nabhinc.util.scheduling.MonthlyTask;
040: import com.nabhinc.util.scheduling.WeeklyTask;
041:
042: /**
043: *
044: *
045: * @author Padmanabh Dabke
046: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
047: */
048: public class NewsNotifier extends BaseRequestProcessor implements
049: ActionProcessor {
050: private Timer nnTimer = new Timer();
051: private DailyTask nnDailyTask = null;
052: private long nnDailySendTime = 12 * 60;
053: private long nnWeeklySendTime = 13 * 60;
054: private long nnMonthlySendTime = 14 * 60;
055: private int nnWeeklySendDay = 1;
056: private int nnMonthlySendDay = 1;
057:
058: private String nnFromEmail = "news.admin@stringbeans.com";
059: private String nnDailyEmailSubject = "Daily news update from nabh.com ($currentDate)";
060: private String nnWeeklyEmailSubject = "Weekly news update from nabh.com ($currentDate)";
061: private String nnMonthlyEmailSubject = "Monthly news update from nabh.com ($currentDate)";
062: private String nnEmailTemplate = "<html><body><headlines><b>$headline</b><br/>$summary<p/></headlines></body></html>";
063: private String nnBaseNewsDir = "/portlet/news";
064: private String nnBaseURL = null;
065:
066: private String nnStoriesSQL = null;
067: private String nnSubscriberSQL = null;
068: private String nnUserEmailSQL = null;
069:
070: private long getTimeParameter(Element config, String paramName,
071: long defaul) throws PortletException {
072: String temp = XMLUtil.getSubElementText(config, paramName);
073: if (temp == null) {
074: return defaul;
075: } else {
076: try {
077: StringTokenizer st = new StringTokenizer(temp, ":");
078: int hour = Integer.parseInt(st.nextToken());
079: if (hour < 0 || hour > 23) {
080: throw new PortletException(
081: "Invalid value for parameter "
082: + paramName
083: + ". Hour must be between 0 and 23.");
084: }
085: int minute = 0;
086: if (st.hasMoreTokens()) {
087: minute = Integer.parseInt(st.nextToken());
088: if (minute < 0 || minute > 59) {
089: throw new PortletException(
090: "Invalid value for parameter "
091: + paramName
092: + ". Minutes must be between 0 and 59.");
093: }
094: }
095: return hour * 3600000L + minute * 60000L;
096: } catch (PortletException ex) {
097: throw ex;
098: } catch (Exception ex) {
099: throw new PortletException("Invalid value " + temp
100: + " for parameter " + paramName);
101: }
102: }
103: }
104:
105: private int getWeekdayParameter(Element config, String paramName,
106: int defaul) throws PortletException {
107: String temp = XMLUtil.getSubElementText(config, paramName);
108: if (temp == null) {
109: return defaul;
110: } else {
111: try {
112: int weekday = Integer.parseInt(temp);
113: if (weekday < 0 || weekday > 6) {
114: throw new PortletException(
115: "Weekday number must be between 0 and 6.");
116: }
117: return weekday;
118: } catch (PortletException ex) {
119: throw ex;
120: } catch (Exception ex) {
121: throw new PortletException("Invalid value " + temp
122: + " for parameter " + paramName);
123: }
124: }
125: }
126:
127: private int getMonthdayParameter(Element config, String paramName,
128: int defaul) throws PortletException {
129: String temp = XMLUtil.getSubElementText(config, paramName);
130: if (temp == null) {
131: return defaul;
132: } else {
133: try {
134: int monthday = Integer.parseInt(temp);
135: if (monthday < 1 || monthday > 31) {
136: throw new PortletException(
137: "Monthday number must be between 1 and 31.");
138: }
139: return monthday;
140: } catch (PortletException ex) {
141: throw ex;
142: } catch (Exception ex) {
143: throw new PortletException("Invalid value " + temp
144: + " for parameter " + paramName);
145: }
146: }
147: }
148:
149: public void init(Element config, ControllerPortletConfig conConfig)
150: throws PortletException {
151: super .init(config, conConfig);
152:
153: nnBaseURL = XMLUtil.getSubElementText(config, "base-url");
154: if (nnBaseURL == null)
155: throw new PortletException(
156: "Missing required base url parameter: base-url, in NewsNotifier action processor.");
157:
158: String temp = conConfig.getParameter("base-news-dir");
159: if (temp != null) {
160: nnBaseNewsDir = temp;
161: }
162:
163: // Set time and day on which the notifications are to be sent.
164: nnDailySendTime = getTimeParameter(config, "daily-send-time",
165: nnDailySendTime);
166: nnWeeklySendTime = getTimeParameter(config, "weekly-send-time",
167: nnWeeklySendTime);
168: nnMonthlySendTime = getTimeParameter(config,
169: "monthly-send-time", nnMonthlySendTime);
170: nnWeeklySendDay = getWeekdayParameter(config,
171: "weekly-send-day", nnWeeklySendDay);
172: nnMonthlySendDay = getMonthdayParameter(config,
173: "monthly-send-day", nnMonthlySendDay);
174:
175: nnStoriesSQL = XMLUtil.getSubElementText(config, "stories-sql");
176: nnSubscriberSQL = XMLUtil.getSubElementText(config,
177: "subscriber-sql");
178: nnUserEmailSQL = XMLUtil.getSubElementText(config,
179: "user-email-sql");
180: nnFromEmail = XMLUtil.getSubElementText(config,
181: "news-admin-email", nnFromEmail);
182:
183: String templateFileStr = XMLUtil.getSubElementText(config,
184: "email-template");
185: if (templateFileStr != null) {
186: if (!templateFileStr.startsWith("/"))
187: templateFileStr = "/" + templateFileStr;
188: templateFileStr = getRealPath(
189: conConfig.getPortletContext(), templateFileStr);
190: File templateFile = new File(templateFileStr);
191: if (!templateFile.exists()) {
192: throw new PortletException(
193: "News update email template file does not exist.");
194: } else {
195: try {
196: nnEmailTemplate = IOUtil
197: .getFileContentAsString(templateFile);
198: } catch (Exception ex) {
199: throw new PortletException(
200: "Failed to read template file.", ex);
201: }
202: }
203: }
204:
205: nnDailyEmailSubject = XMLUtil.getSubElementText(config,
206: "daily-email-subject", nnDailyEmailSubject);
207: nnWeeklyEmailSubject = XMLUtil.getSubElementText(config,
208: "weekly-email-subject", nnWeeklyEmailSubject);
209: nnMonthlyEmailSubject = XMLUtil.getSubElementText(config,
210: "monthly-email-subject", nnMonthlyEmailSubject);
211:
212: new DailyTask(nnTimer, new NewsEmailSender(conConfig
213: .getDataSource(), NewsEmailSender.DAILY, nnFromEmail,
214: nnDailyEmailSubject, nnEmailTemplate, nnStoriesSQL,
215: nnSubscriberSQL, nnUserEmailSQL, nnBaseURL
216: + nnBaseNewsDir), nnDailySendTime);
217:
218: new WeeklyTask(nnTimer, new NewsEmailSender(conConfig
219: .getDataSource(), NewsEmailSender.WEEKLY, nnFromEmail,
220: nnWeeklyEmailSubject, nnEmailTemplate, nnStoriesSQL,
221: nnSubscriberSQL, nnUserEmailSQL, nnBaseURL
222: + nnBaseNewsDir), nnWeeklySendDay,
223: nnWeeklySendTime);
224:
225: new MonthlyTask(nnTimer, new NewsEmailSender(conConfig
226: .getDataSource(), NewsEmailSender.MONTHLY, nnFromEmail,
227: nnMonthlyEmailSubject, nnEmailTemplate, nnStoriesSQL,
228: nnSubscriberSQL, nnUserEmailSQL, nnBaseURL
229: + nnBaseNewsDir), nnMonthlySendDay,
230: nnMonthlySendTime);
231:
232: }
233:
234: /* (non-Javadoc)
235: * @see com.nabhinc.portlet.mvcportlet.core.ActionProcessor#process(javax.portlet.ActionRequest, javax.portlet.ActionResponse, com.nabhinc.portlet.mvcportlet.core.ActionConfig)
236: */
237: public String process(ActionRequest request,
238: ActionResponse response, ActionConfig actionConfig)
239: throws PortletException, IOException {
240:
241: throw new PortletException("You are not supposed to call this.");
242: }
243:
244: public void destroy() {
245: nnTimer.cancel();
246: }
247:
248: }
|