001: /*
002: * Copyright (c) 2006 Your Corporation. All Rights Reserved.
003: */
004:
005: package com.technoetic.xplanner.mail;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.ResourceBundle;
012: import java.util.Set;
013: import java.util.Properties;
014:
015: import org.apache.log4j.Logger;
016:
017: import com.technoetic.xplanner.DomainSpecificPropertiesFactory;
018: import com.technoetic.xplanner.XPlannerProperties;
019: import com.technoetic.xplanner.domain.Person;
020: import com.technoetic.xplanner.domain.Project;
021: import com.technoetic.xplanner.domain.Task;
022: import com.technoetic.xplanner.domain.UserStory;
023:
024: /**
025: * User: mprokopowicz
026: * Date: Feb 3, 2006
027: * Time: 5:36:56 PM
028: */
029: public class EmailNotificationSupport {
030: private EmailFormatter emailFormatter;
031: private DomainSpecificPropertiesFactory propertiesFactory;
032: private static final Logger log = Logger
033: .getLogger(EmailNotificationSupport.class);
034: private EmailMessageFactory emailMessageFactory;
035: static final String XPLANNER_MAIL_FROM_KEY = "xplanner.mail.from";
036:
037: public EmailNotificationSupport(EmailFormatter emailFormatter,
038: EmailMessageFactory emailMessageFactory,
039: DomainSpecificPropertiesFactory propertiesFactory) {
040: this .emailFormatter = emailFormatter;
041: this .emailMessageFactory = emailMessageFactory;
042: this .propertiesFactory = propertiesFactory;
043: }
044:
045: public void sendNotifications(Map notificationEmails,
046: String emailHeaderKey, String subjectKey) {
047: Set keySet = notificationEmails.keySet();
048: Iterator iterator = keySet.iterator();
049: ResourceBundle bundle = ResourceBundle
050: .getBundle("ResourceBundle");
051: String subject = bundle.getString(subjectKey);
052: String emailHeader = bundle.getString(emailHeaderKey);
053: String emailFooter = bundle
054: .getString(MissingTimeEntryNotifier.EMAIL_BODY_FOOTER);
055: String emailTaskHeader = bundle
056: .getString(MissingTimeEntryNotifier.EMAIL_TASK_HEADER);
057: String emailStoryHeader = bundle
058: .getString(MissingTimeEntryNotifier.EMAIL_STORY_HEADER);
059: while (iterator.hasNext()) {
060: Integer id = (Integer) iterator.next();
061: List bodyEntryList = (List) notificationEmails.get(id);
062: try {
063: EmailMessage emailMessage = emailMessageFactory
064: .createMessage();
065: emailMessage.setRecipient(id.intValue());
066: emailMessage.setSubject(subject);
067: emailMessage.setFrom(propertiesFactory
068: .getDefaultProperties().getProperty(
069: XPLANNER_MAIL_FROM_KEY));
070: String formatedText = emailFormatter.formatEmailEntry(
071: emailHeader, emailFooter, emailStoryHeader,
072: emailTaskHeader, bodyEntryList);
073: emailMessage.setBody(formatedText);
074: emailMessage.send();
075: } catch (Exception e) {
076: log.error("Error sending email: ", e);
077: }
078: }
079: }
080:
081: public void compileEmail(Map notificationEmails, int receiverId,
082: Person acceptor, Task task, UserStory story) {
083: List emailBodyList;
084: if (notificationEmails.containsKey(new Integer(receiverId))) {
085: emailBodyList = (List) notificationEmails.get(new Integer(
086: receiverId));
087: } else {
088: emailBodyList = new ArrayList();
089: notificationEmails.put(new Integer(receiverId),
090: emailBodyList);
091: }
092: List entryList = new ArrayList();
093: entryList.add(task);
094: entryList.add(story);
095: if (acceptor != null) {
096: entryList.add(acceptor.getName());
097: }
098: emailBodyList.add(entryList);
099: }
100:
101: public boolean isProjectToBeNotified(Map projectsToBeNotified,
102: Project project) {
103: Boolean isNotified = (Boolean) projectsToBeNotified
104: .get(new Integer(project.getId()));
105: if (isNotified == null) {
106: isNotified = Boolean
107: .valueOf(isProjectToBeNotified(project));
108: projectsToBeNotified.put(new Integer(project.getId()),
109: isNotified);
110: }
111: return isNotified.booleanValue();
112: }
113:
114: public boolean isProjectToBeNotified(Project project) {
115: Boolean isNotified;
116: Properties projectDynamicProperties = propertiesFactory
117: .createPropertiesFor(project);
118: String stringValue = projectDynamicProperties.getProperty(
119: XPlannerProperties.SEND_NOTIFICATION_KEY, Boolean.TRUE
120: .toString());
121: return Boolean.valueOf(stringValue).booleanValue();
122: }
123: }
|