001: package org.tigris.scarab.notification;
002:
003: import java.util.Collection;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Set;
007: import java.util.List;
008:
009: import org.apache.log4j.Logger;
010: import org.apache.torque.TorqueException;
011: import org.apache.turbine.Turbine;
012: import org.tigris.scarab.om.Activity;
013: import org.tigris.scarab.om.ActivitySet;
014: import org.tigris.scarab.notification.ActivityType;
015: import org.tigris.scarab.om.Attachment;
016: import org.tigris.scarab.om.AttributePeer;
017: import org.tigris.scarab.om.Issue;
018: import org.tigris.scarab.om.ScarabUser;
019: import org.tigris.scarab.util.Email;
020: import org.tigris.scarab.util.EmailContext;
021: import org.tigris.scarab.util.Log;
022:
023: /**
024: * This class provides the default implementation for the provided notification
025: * manager. It just send the notifications by email in the moment they are added.
026: *
027: * @author jorgeuriarte
028: */
029: public class ScarabOldNotificationManager implements
030: NotificationManager {
031: public static Logger log = Log
032: .get(ScarabOldNotificationManager.class.getName());
033:
034: private static final Integer NOTIFICATION_MANAGER_ID = new Integer(
035: 1);
036:
037: public Integer getManagerId() {
038: return NOTIFICATION_MANAGER_ID;
039: }
040:
041: /**
042: * Receives an activitySet from which to generate notification. Current
043: * implementation does only online email sending, with no aggregation or
044: * filtering.
045: */
046: public void addActivityNotification(ActivityType event,
047: ActivitySet activitySet, Issue issue, ScarabUser fromUser) {
048: this .addActivityNotification(event, activitySet, issue, null,
049: null, fromUser);
050: }
051:
052: /**
053: * Long version of the addActivityNotification method, allowing to pass the sets of
054: * users involved as 'To' or 'CC'.
055: */
056: public void addActivityNotification(ActivityType event,
057: ActivitySet activitySet, Issue issue, Set toUsers,
058: Set ccUsers, ScarabUser fromUser) {
059: if (log.isDebugEnabled())
060: log.debug("addActivityNotification: " + issue.getIdPrefix()
061: + issue.getIssueId() + "-" + event);
062:
063: Attachment activityAttch = null;
064: Set changes = null;
065: try {
066: activityAttch = activitySet.getAttachment();
067: List activityList = activitySet.getActivityList();
068: changes = new HashSet(activityList.size());
069: for (Iterator itr = activityList.iterator(); itr.hasNext();) {
070: Activity activity = (Activity) itr.next();
071: if (activity.getIssue().equals(issue)) {
072: changes.add(activity);
073: }
074: }
075: } catch (Exception e) {
076: log
077: .error("addActivityNotification: Error accessing activityset: "
078: + e);
079: }
080: EmailContext ectx = new EmailContext();
081: ectx.setIssue(issue);
082: ectx.put("attachment", activityAttch);
083: ectx.put("uniqueActivityDescriptions", changes);
084:
085: String template = configureEmailContext(ectx, event,
086: activitySet, issue);
087:
088: try {
089: // FIXME: This should really be 'queued', delaying the sending
090: // untill sendPendingNotifications is called
091: this .sendEmail(changes, activityAttch, ectx, issue,
092: activitySet.getCreator(), toUsers, ccUsers,
093: template);
094: } catch (Exception e) {
095: log.error("addNotification: " + e);
096: }
097: }
098:
099: /**
100: * Does nothing, because this implementation currently send the
101: * notifications online in the moment they are generated calling
102: * addActivityNotification methods.
103: */
104: public void sendPendingNotifications() {
105: log
106: .debug("sendPendingNotifications(): No implementation required.");
107: }
108:
109: /**
110: * Decides what templates to use for email, and set needed values in the
111: * context, depending on the event that originated the notification.
112: *
113: * @param event
114: * The NotificationEvent originated
115: * @param ectx
116: * Returned initialized/updated by reference
117: * @return The name of the email template to be used
118: */
119: private String configureEmailContext(EmailContext ectx,
120: ActivityType event, ActivitySet acttivitySet, Issue issue) {
121: String template = null;
122:
123: // Select appropiate template depending on the event
124: if (event == ActivityType.USER_ATTRIBUTE_CHANGED) {
125: template = Turbine.getConfiguration().getString(
126: "scarab.email.assignissue.template",
127: "ModifyIssue.vm");
128:
129: ectx.setSubjectTemplate("AssignIssueModifyIssueSubject.vm");
130: } else if (event == ActivityType.ISSUE_CREATED) {
131: template = Turbine.getConfiguration().getString(
132: "scarab.email.reportissue.template",
133: "NewIssueNotification.vm");
134: } else if (event == ActivityType.ISSUE_MOVED
135: || event == ActivityType.ISSUE_COPIED) {
136: template = Turbine.getConfiguration().getString(
137: "scarab.email.moveissue.template", "MoveIssue.vm");
138: ectx.setDefaultTextKey("MovedIssueEmailSubject");
139:
140: // placed in the context for the email to be able to access them
141: // from within the email template
142: try {
143: Issue newIssue = ((Activity) acttivitySet
144: .getActivitys().get(0)).getIssue();
145: ectx.put("issue", newIssue);
146: ectx.put("module", newIssue.getModule());
147: ectx.setModule(newIssue.getModule());
148: Attachment reason = acttivitySet.getAttachment();
149: ectx.put("reason",
150: (reason == null) ? "[no reason provided]"
151: : reason.getData());
152: ectx.put("oldModule", issue.getModule());
153: ectx.put("oldIssueType", issue.getIssueType());
154: ectx.put("oldIssue", issue);
155: if (event == ActivityType.ISSUE_COPIED)
156: ectx.put("action", "copy");
157: else
158: ectx.put("action", "move");
159: } catch (TorqueException te) {
160: Log.get(this .getClass().getName()).error(
161: "configureEmailContext: Can't get the issues from activitySet="
162: + acttivitySet.getActivitySetId());
163: }
164:
165: if (event == ActivityType.ISSUE_COPIED)
166: ectx.setDefaultTextKey("CopiedIssueEmailSubject");
167: else
168: ectx.setDefaultTextKey("MovedIssueEmailSubject");
169: } else if (event == ActivityType.ATTACHMENT_CREATED) {
170: template = Turbine.getConfiguration().getString(
171: "scarab.email.modifyissue.template",
172: "ModifyIssue.vm");
173: } else {
174: /** Rest of cases will use, by default, ModifyIssue.vm * */
175: template = Turbine.getConfiguration().getString(
176: "scarab.email.modifyissue.template",
177: "ModifyIssue.vm");
178: ectx.setDefaultTextKey("DefaultModifyIssueEmailSubject");
179: }
180: return template;
181: }
182:
183: /**
184: * Sends email to the users associated with the issue. That is associated
185: * with this activitySet. If no subject and template specified, assume
186: * modify issue action. throws Exception
187: *
188: * @param activityDesc
189: * Set containing the different descriptions of this activityset
190: * @param attachment
191: * Attachment of the activityset (if present)
192: * @param context
193: * Any contextual information for the message.
194: * @param issue
195: * The issue
196: * @param creator
197: * The user originating the notification event
198: * @param toUsers
199: * @param ccUsers
200: * @param template
201: * The name of the velocity template containing the mail text
202: */
203: private void sendEmail(Set activityDesc, Attachment attachment,
204: EmailContext context, Issue issue, ScarabUser creator,
205: Collection toUsers, Collection ccUsers, String template)
206: throws Exception {
207: // add data to context
208:
209: if (toUsers == null) {
210: // Then add users who are assigned to "email-to" attributes
211: toUsers = issue.getAllUsersToEmail(AttributePeer.EMAIL_TO);
212: }
213:
214: if (ccUsers == null) {
215: // add users to cc field of email
216: ccUsers = issue.getAllUsersToEmail(AttributePeer.CC_TO);
217: }
218:
219: String[] replyToUser = issue.getModule().getSystemEmail();
220:
221: if (Turbine.getConfiguration().getString(
222: "scarab.email.replyto.sender").equals("true")) {
223: Email.sendEmail(context, issue.getModule(), creator,
224: creator, toUsers, ccUsers, template);
225: } else {
226: Email.sendEmail(context, issue.getModule(), creator,
227: replyToUser, toUsers, ccUsers, template);
228: }
229: }
230:
231: }
|