KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > scheduler > tasks > ReminderNotification


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.web.scheduler.tasks;
20
21 import java.util.*;
22 import java.rmi.*;
23 import javax.naming.*;
24 import javax.rmi.*;
25
26 import cowsultants.itracker.ejb.client.interfaces.*;
27 import cowsultants.itracker.ejb.client.models.*;
28 import cowsultants.itracker.ejb.client.util.*;
29 import cowsultants.itracker.web.scheduler.*;
30
31 /**
32   * This class can be used to send reminder emails to owners/admins
33   * that issues need their attention.
34   * @see SchedulableTask
35   */

36 public class ReminderNotification implements SchedulableTask {
37     public static final String JavaDoc DEFAULT_BASE_URL = "http://localhost:8080/itracker";
38     public static final int DEFAULT_ISSUE_AGE = 30;
39
40     public ReminderNotification() {
41     }
42
43     /**
44       * This method is called by the scheduler to send the reminder
45       * notifications. The arguments can be used to configure which issues
46       * and projects are included in the notifications. The args should
47       * include as the first parameter the base url of the server including
48       * the scheme, hostname, port, and context. For example:
49       * <br>
50       * http://localhost:8080/itracker
51       * <br>
52       * If no other arguments are supplied it sends reminders to all
53       * owners/admins of unresolved issues in all projects that have not been
54       * modified in 30 days. The second element of the array can be a number
55       * that represents the number of days to use to check the last modified
56       * date. The third optional element is a number that represents the project
57       * id to limit the notifications to. A fourth optional argument is the severity
58       * to send the notification for.
59       * @param args optional arguments to configure the notification messages
60       * @see SchedulableTask#performTask
61       */

62     public void performTask(String JavaDoc[] args) {
63         IssueModel[] issues;
64         String JavaDoc baseURL = DEFAULT_BASE_URL;
65         int issueAge = DEFAULT_ISSUE_AGE;
66         int projectId = -1;
67         int severity = -1;
68
69         // Process arguments.
70
if(args != null) {
71             if(args.length > 0 && args[0] != null) {
72                 baseURL = args[0];
73             }
74             if(args.length > 1) {
75                 try {
76                     issueAge = Integer.parseInt(args[1]);
77                 } catch(NumberFormatException JavaDoc nfe) {
78                     Logger.logDebug("Invalid issue age specified in ReminderNotification task.");
79                 }
80             }
81             if(args.length > 2) {
82                 try {
83                     projectId = Integer.parseInt(args[2]);
84                 } catch(NumberFormatException JavaDoc nfe) {
85                     Logger.logDebug("Invalid projectId specified in ReminderNotification task.");
86                 }
87             }
88             if(args.length > 3) {
89                 try {
90                     severity = Integer.parseInt(args[3]);
91                 } catch(NumberFormatException JavaDoc nfe) {
92                     Logger.logDebug("Invalid severity specified in ReminderNotification task.");
93                 }
94             }
95         }
96         Logger.logDebug("Reminder Notifications being sent for project " + projectId + " with issues over " + issueAge + " days old with severity " + severity + ". Base URL = " + baseURL);
97
98         try {
99             InitialContext ic = new InitialContext();
100             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
101             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
102             IssueHandler ih = ihHome.create();
103
104             GregorianCalendar cal = new GregorianCalendar();
105             cal.add(Calendar.DAY_OF_MONTH, 0 - issueAge);
106             Date oldDate = cal.getTime();
107             Date currentDate = new Date();
108
109             if(projectId > 0) {
110                 issues = ih.getIssuesByProjectId(new Integer JavaDoc(projectId), IssueUtilities.STATUS_RESOLVED);
111             } else {
112                 issues = ih.getIssuesWithStatusLessThan(IssueUtilities.STATUS_RESOLVED);
113             }
114             if(issues != null && issues.length > 0) {
115                 for(int i = 0; i < issues.length; i++) {
116                     if(severity >= 0 && issues[i].getSeverity() != severity) {
117                         continue;
118                     }
119                     if(issues[i].getLastModifiedDate() != null && issues[i].getLastModifiedDate().before(oldDate)) {
120                         HashSet addresses = new HashSet();
121                         long numMillis = currentDate.getTime() - issues[i].getLastModifiedDate().getTime();
122                         int numDays = (int) (numMillis / (24 * 60 * 60 * 1000));
123
124                         NotificationModel[] notifications = ih.getPrimaryIssueNotifications(issues[i].getId());
125                         for(int j = 0; j < notifications.length; j++) {
126                             if(notifications[j].getUserEmail() != null && notifications[j].getUserEmail().indexOf('@') >= 0) {
127                                 addresses.add(notifications[j].getUserEmail());
128                             }
129                         }
130                         Logger.logDebug("Sending reminder notification for issue " + issues[i].getId() + " to " + addresses.size() + " users.");
131                         ih.sendNotification(issues[i].getId(), NotificationUtilities.TYPE_ISSUE_REMINDER, baseURL, addresses, new Integer JavaDoc(numDays));
132                     }
133                 }
134             }
135         } catch(Exception JavaDoc e) {
136             Logger.logError("Error sending reminder notifications. Message: " + e.getMessage());
137         }
138     }
139 }
140
Popular Tags