001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Class created on Jul 15, 2005
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.summary;
044:
045: import java.util.ArrayList;
046: import java.util.Calendar;
047: import java.util.Date;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import net.jforum.dao.DataAccessDriver;
052: import net.jforum.dao.SummaryDAO;
053: import net.jforum.entities.User;
054: import net.jforum.util.mail.Spammer;
055: import net.jforum.util.preferences.ConfigKeys;
056: import net.jforum.util.preferences.SystemGlobals;
057: import net.jforum.view.forum.common.ViewCommon;
058:
059: import org.apache.log4j.Logger;
060:
061: import freemarker.template.SimpleHash;
062:
063: /**
064: * Manage the Summary sends.
065: *
066: * @see net.jforum.summary.SummaryJob
067: * @see net.jforum.summary.SummaryScheduler
068: *
069: * @author Franklin S. Dattein (<a href="mailto:franklin@hp.com">franklin@hp.com</a>)
070: * @version $Id: SummaryModel.java,v 1.11 2007/09/08 02:36:36 andowson Exp $
071: */
072: public class SummaryModel extends Spammer {
073: private SummaryDAO dao;
074:
075: private static Logger logger = Logger.getLogger(SummaryModel.class);
076:
077: public SummaryModel() {
078: this .dao = DataAccessDriver.getInstance().newSummaryDAO();
079: }
080:
081: public void sendPostsSummary(List recipients) {
082: logger.info("Sending Weekly summary...");
083:
084: // Gets a Date seven days before now
085: int daysBefore = Integer.parseInt(SystemGlobals
086: .getValue(ConfigKeys.SUMMARY_DAYS_BEFORE));
087:
088: // New date "X" days before now, where "X" is the number set on the variable daysBefore
089: long dateBefore = Calendar.getInstance().getTimeInMillis()
090: - (daysBefore * 1000 * 60 * 60 * 24);
091:
092: List posts = listPosts(new Date(dateBefore), new Date());
093:
094: String forumLink = ViewCommon.getForumLink();
095:
096: SimpleHash params = new SimpleHash();
097: params.put("posts", posts);
098: params.put("url", forumLink);
099: params.put("extension", SystemGlobals
100: .getValue(ConfigKeys.SERVLET_EXTENSION));
101:
102: String subject = SystemGlobals
103: .getValue(ConfigKeys.MAIL_SUMMARY_SUBJECT);
104:
105: this .setUsers(this .recipientsAsUsers(recipients));
106: this .setTemplateParams(params);
107:
108: this .prepareMessage(subject, SystemGlobals
109: .getValue(ConfigKeys.MAIL_SUMMARY_FILE));
110: super .dispatchMessages();
111: }
112:
113: private List recipientsAsUsers(List recipients) {
114: List l = new ArrayList();
115:
116: for (Iterator iter = recipients.iterator(); iter.hasNext();) {
117: String email = (String) iter.next();
118:
119: User u = new User();
120: u.setEmail(email);
121:
122: l.add(u);
123: }
124:
125: return l;
126: }
127:
128: /**
129: * List all recipients able to receive posts summaries.
130: *
131: * @return List of users
132: */
133: public List listRecipients() {
134: return this .dao.listRecipients();
135: }
136:
137: /**
138: * List last posts of a period like a week, day or month.
139: *
140: * @param firstDate First date of a period.
141: * @param lastDate Last date of a period.
142: * @return List of Posts
143: */
144: public List listPosts(Date firstDate, Date lastDate) {
145: return this.dao.selectLastPosts(firstDate, lastDate);
146: }
147: }
|