KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > beans > message > NotificationMessageBean


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.beans.message;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.jms.*;
26 import javax.naming.*;
27 import javax.mail.*;
28 import javax.mail.internet.*;
29 import javax.rmi.*;
30
31 import cowsultants.itracker.ejb.util.*;
32 import cowsultants.itracker.ejb.client.util.*;
33 import cowsultants.itracker.ejb.client.models.*;
34 import cowsultants.itracker.ejb.client.interfaces.*;
35 import cowsultants.itracker.ejb.client.resources.*;
36
37 public class NotificationMessageBean implements MessageDrivenBean, MessageListener {
38     public static final String JavaDoc DEFAULT_CONNECTION_FACTORY = "jms/QueueConnectionFactory";
39     public static final String JavaDoc DEFAULT_QUEUE_NAME = "jms/ITrackerNotificationQueue";
40
41     public static final String JavaDoc DEFAULT_FROM_ADDRESS = "itracker@localhost";
42     public static final String JavaDoc DEFAULT_REPLYTO_ADDRESS = "itracker@localhost";
43     public static final String JavaDoc DEFAULT_SMTP_HOST = "localhost";
44
45     MessageDrivenContext ejbContext;
46     Context jndiContext;
47     IssueHandlerHome ihHome;
48
49     String JavaDoc fromAddress = "";
50     String JavaDoc replyToAddress = "";
51     String JavaDoc smtpHost = "";
52
53     public void setMessageDrivenContext(MessageDrivenContext mdc) {
54         ejbContext = mdc;
55
56         try {
57             jndiContext = new InitialContext();
58
59             Object JavaDoc ihRef = jndiContext.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
60             ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
61
62             Object JavaDoc scRef = jndiContext.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
63             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
64             SystemConfiguration sc = scHome.create();
65
66             fromAddress = sc.getProperty("notification_from_address", DEFAULT_FROM_ADDRESS);
67             replyToAddress = sc.getProperty("notification_replyto_address", DEFAULT_REPLYTO_ADDRESS);
68             smtpHost = sc.getProperty("notification_smtp_host", DEFAULT_SMTP_HOST);
69             if(Logger.isLoggingDebug()) {
70                 Logger.logDebug("Notification Init: From Address set to: " + fromAddress);
71                 Logger.logDebug("Notification Init: ReplyTo Address set to: " + replyToAddress);
72                 Logger.logDebug("Notification Init: SMTP server set to: " + smtpHost);
73             }
74         } catch(CreateException ce) {
75             throw new EJBException(ce);
76         } catch(NamingException ne) {
77             throw new EJBException(ne);
78         }
79     }
80
81     public void onMessage(javax.jms.Message JavaDoc message) {
82         try {
83             MapMessage notificationMsg = (MapMessage) message;
84
85             int type = notificationMsg.getInt("type");
86             String JavaDoc url = notificationMsg.getString("baseURL");
87
88             if(type == NotificationUtilities.TYPE_SELF_REGISTER) {
89                 handleSelfRegistrationNotification(notificationMsg, url);
90             } else {
91                 handleIssueNotification(notificationMsg, type, url);
92             }
93         } catch(Exception JavaDoc e) {
94             throw new EJBException(e);
95         }
96     }
97
98     private void handleSelfRegistrationNotification(MapMessage notificationMsg, String JavaDoc url) {
99         try {
100             String JavaDoc toAddress = (String JavaDoc) notificationMsg.getObject("toAddress");
101
102             if(toAddress != null && ! "".equals(toAddress)) {
103                 String JavaDoc subject = ITrackerResources.getString("itracker.email.selfreg.subject");
104                 String JavaDoc msgText = ITrackerResources.getString("itracker.email.selfreg.body", ITrackerResources.getDefaultLocale(),
105                                  new Object JavaDoc[] {(String JavaDoc) notificationMsg.getObject("login"), url + "/login.jsp"});
106                 EmailHandler.sendEmail(toAddress, subject, msgText);
107             }
108         } catch(Exception JavaDoc e) {
109             throw new EJBException(e);
110         }
111     }
112
113     private void handleIssueNotification(MapMessage notificationMsg, int type, String JavaDoc url) {
114         try {
115             HashSet addresses = null;
116             NotificationModel[] notifications;
117             Integer JavaDoc issueId = (Integer JavaDoc) notificationMsg.getObject("issueId");
118
119             if(issueId != null) {
120                 byte[] addressesBytes = (byte[]) notificationMsg.getObject("addresses");
121                 if(addressesBytes != null) {
122                     try {
123                         ByteArrayInputStream bais = new ByteArrayInputStream(addressesBytes);
124                         ObjectInputStream ois = new ObjectInputStream(bais);
125                         addresses = (HashSet) ois.readObject();
126                     } catch(Exception JavaDoc e) {
127                         Logger.logDebug("Unable to read address list for notification: " + e.getMessage());
128                     }
129                 }
130                 Integer JavaDoc lastModifiedDays = (Integer JavaDoc) notificationMsg.getObject("lastModifiedDays");
131                 if(lastModifiedDays == null || lastModifiedDays.intValue() < 0) {
132                     lastModifiedDays = new Integer JavaDoc(cowsultants.itracker.web.scheduler.tasks.ReminderNotification.DEFAULT_ISSUE_AGE);
133                 }
134
135                 IssueHandler ih = ihHome.create();
136                 if(addresses == null) {
137                     addresses = new HashSet();
138                     notifications = ih.getIssueNotifications(issueId);
139                     for(int i = 0; i < notifications.length; i++) {
140                         if(notifications[i].getUserEmail() != null && notifications[i].getUserEmail().indexOf('@') >= 0) {
141                             addresses.add(notifications[i].getUserEmail());
142                         }
143                     }
144                 }
145
146                 IssueModel issue = ih.getIssue(issueId);
147                 IssueActivityModel[] activity = ih.getIssueActivity(issueId, false);
148                 IssueHistoryModel history = ih.getLastIssueHistory(issueId);
149                 ComponentModel[] components = ih.getIssueComponents(issueId);
150                 VersionModel[] versions = ih.getIssueVersions(issueId);
151                 if(addresses.size() > 0) {
152                     String JavaDoc subject = "";
153                     if(type == NotificationUtilities.TYPE_CREATED) {
154                         subject = ITrackerResources.getString("itracker.email.issue.subject.created",
155                                       new Object JavaDoc[] {issue.getId(), issue.getProjectName(), lastModifiedDays});
156                     } else if(type == NotificationUtilities.TYPE_ASSIGNED) {
157                         subject = ITrackerResources.getString("itracker.email.issue.subject.assigned",
158                                       new Object JavaDoc[] {issue.getId(), issue.getProjectName(), lastModifiedDays});
159                     } else if(type == NotificationUtilities.TYPE_CLOSED) {
160                         subject = ITrackerResources.getString("itracker.email.issue.subject.closed",
161                                       new Object JavaDoc[] {issue.getId(), issue.getProjectName(), lastModifiedDays});
162                     } else if(type == NotificationUtilities.TYPE_ISSUE_REMINDER) {
163                         subject = ITrackerResources.getString("itracker.email.issue.subject.reminder",
164                                       new Object JavaDoc[] {issue.getId(), issue.getProjectName(), lastModifiedDays});
165                     } else {
166                         subject = ITrackerResources.getString("itracker.email.issue.subject.updated",
167                                       new Object JavaDoc[] {issue.getId(), issue.getProjectName(), lastModifiedDays});
168                     }
169
170
171                     String JavaDoc activityString = "";
172                     String JavaDoc componentString = "";
173                     String JavaDoc versionString = "";
174                     for(int i = 0; i < activity.length; i++) {
175                         activityString += IssueUtilities.getActivityName(activity[i].getType()) + ": " + activity[i].getDescription() + "\n";
176                     }
177                     for(int i = 0; i < components.length; i++) {
178                         componentString += (i != 0 ? ", " : "") + components[i].getName();
179                     }
180                     for(int i = 0; i < versions.length; i++) {
181                         versionString += (i != 0 ? ", " : "") + versions[i].getNumber();
182                     }
183
184                     String JavaDoc msgText = "";
185                     if(type == NotificationUtilities.TYPE_ISSUE_REMINDER) {
186                         msgText = ITrackerResources.getString("itracker.email.issue.body.reminder",
187                                     new Object JavaDoc[] {url + "/view_issue.jsp?id=" + issue.getId(),
188                                                   issue.getProjectName(),
189                                                   issue.getDescription(),
190                                                   IssueUtilities.getStatusName(issue.getStatus()),
191                                                   IssueUtilities.getSeverityName(issue.getSeverity()),
192                                                   (issue.getOwnerFirstName() != null ? issue.getOwnerFirstName() : "") + " " + (issue.getOwnerLastName() != null ? issue.getOwnerLastName() : ""),
193                                                   componentString,
194                                                   (history == null ? "" : history.getUserFirstName() + " " + history.getUserLastName()),
195                                                   (history == null ? "" : HTMLUtilities.removeMarkup(history.getDescription())),
196                                                   lastModifiedDays,
197                                                   activityString
198                                                   });
199                     } else {
200                         msgText = ITrackerResources.getString("itracker.email.issue.body.standard",
201                                     new Object JavaDoc[] {url + "/view_issue.jsp?id=" + issue.getId(),
202                                                   issue.getProjectName(),
203                                                   issue.getDescription(),
204                                                   IssueUtilities.getStatusName(issue.getStatus()),
205                                                   issue.getResolution(),
206                                                   IssueUtilities.getSeverityName(issue.getSeverity()),
207                                                   (issue.getOwnerFirstName() != null ? issue.getOwnerFirstName() : "") + " " + (issue.getOwnerLastName() != null ? issue.getOwnerLastName() : ""),
208                                                   componentString,
209                                                   (history == null ? "" : history.getUserFirstName() + " " + history.getUserLastName()),
210                                                   (history == null ? "" : HTMLUtilities.removeMarkup(history.getDescription())),
211                                                   activityString
212                                                   });
213                     }
214                     EmailHandler.sendEmail(addresses, subject, msgText);
215                     ih.updateIssueActivityNotification(issue.getId(), true);
216                 }
217             }
218         } catch(Exception JavaDoc e) {
219             throw new EJBException(e);
220         }
221     }
222
223     public void ejbCreate() {}
224
225     public void ejbRemove() {
226         try {
227             jndiContext.close();
228             ejbContext = null;
229         } catch(NamingException ne) {
230         }
231     }
232 }
Popular Tags