Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
53   162   25   26.5
28   103   0.47   2
2     12.5  
1    
 
 
  ReminderNotification       Line # 46 53 25 0% 0.0
 
No Tests
 
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 org.itracker.web.scheduler.tasks;
20   
21   
22    import java.util.ArrayList;
23    import java.util.Calendar;
24    import java.util.Date;
25    import java.util.GregorianCalendar;
26    import java.util.HashSet;
27    import java.util.List;
28   
29    import javax.mail.internet.InternetAddress;
30   
31    import org.apache.log4j.Logger;
32    import org.itracker.model.Issue;
33    import org.itracker.model.Notification;
34    import org.itracker.model.Notification.Type;
35    import org.itracker.services.ConfigurationService;
36    import org.itracker.services.IssueService;
37    import org.itracker.services.NotificationService;
38    import org.itracker.services.util.IssueUtilities;
39    import org.itracker.web.util.ServletContextUtils;
40   
41    /**
42    * This class can be used to send reminder emails to owners/admins
43    * that issues need their attention.
44    * @see SchedulableTask
45    */
 
46    public class ReminderNotification extends BaseJob {
47   
48    public static final String DEFAULT_BASE_URL = "http://localhost:8080/itracker";
49    public static final int DEFAULT_ISSUE_AGE = 30;
50   
51    private final Logger logger;
52   
 
53  0 toggle public ReminderNotification() {
54  0 this.logger = Logger.getLogger(getClass());
55    }
56   
57    /**
58    * This method is called by the scheduler to send the reminder
59    * notifications. The arguments can be used to configure which issues
60    * and projects are included in the notifications. The args should
61    * include as the first parameter the base url of the server including
62    * the scheme, hostname, port, and context. For example:
63    * <br>
64    * http://localhost:8080/itracker
65    * <br>
66    * If no other arguments are supplied it sends reminders to all
67    * owners/admins of unresolved issues in all projects that have not been
68    * modified in 30 days. The second element of the array can be a number
69    * that represents the number of days to use to check the last modified
70    * date. The third optional element is a number that represents the project
71    * id to limit the notifications to. A fourth optional argument is the severity
72    * to send the notification for.
73    * @param args optional arguments to configure the notification messages
74    * @see SchedulableTask#performTask
75    */
 
76  0 toggle public void performTask(String[] args) {
77  0 List<Issue> issues;
78  0 String baseURL = DEFAULT_BASE_URL;
79  0 int issueAge = DEFAULT_ISSUE_AGE;
80  0 int projectId = -1;
81  0 int severity = -1;
82  0 ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();
83   
84    // Process arguments.
85  0 if(args != null) {
86  0 if(args.length > 0 && args[0] != null) {
87  0 baseURL = args[0];
88    }
89   
90  0 if (null == baseURL) {
91  0 baseURL = configurationService.getSystemBaseURL();
92    }
93  0 if(args.length > 1) {
94  0 try {
95  0 issueAge = Integer.parseInt(args[1]);
96    } catch(NumberFormatException nfe) {
97  0 logger.debug("Invalid issue age specified in ReminderNotification task.");
98    }
99    }
100  0 if(args.length > 2) {
101  0 try {
102  0 projectId = Integer.parseInt(args[2]);
103    } catch(NumberFormatException nfe) {
104  0 logger.debug("Invalid projectId specified in ReminderNotification task.");
105    }
106    }
107  0 if(args.length > 3) {
108  0 try {
109  0 severity = Integer.parseInt(args[3]);
110    } catch(NumberFormatException nfe) {
111  0 logger.debug("Invalid severity specified in ReminderNotification task.");
112    }
113    }
114    }
115  0 logger.debug("Reminder Notifications being sent for project " + projectId + " with issues over " + issueAge + " days old with severity " + severity + ". Base URL = " + baseURL);
116   
117  0 try {
118  0 IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
119  0 NotificationService notificationService = ServletContextUtils.getItrackerServices().getNotificationService();
120  0 GregorianCalendar cal = new GregorianCalendar();
121  0 cal.add(Calendar.DAY_OF_MONTH, 0 - issueAge);
122  0 Date oldDate = cal.getTime();
123  0 Date currentDate = new Date();
124   
125  0 if(projectId > 0) {
126  0 issues = issueService.getIssuesByProjectId(projectId, IssueUtilities.STATUS_RESOLVED);
127    } else {
128  0 issues = issueService.getIssuesWithStatusLessThan(IssueUtilities.STATUS_RESOLVED);
129    }
130  0 if(issues != null && issues.size() > 0) {
131  0 for(int i = 0; i < issues.size(); i++) {
132  0 if(severity >= 0 && issues.get(i).getSeverity() != severity) {
133  0 continue;
134    }
135  0 if(issues.get(i).getLastModifiedDate() != null && issues.get(i).getLastModifiedDate().before(oldDate)) {
136  0 HashSet<InternetAddress> addresses = new HashSet<InternetAddress>();
137  0 long numMillis = currentDate.getTime() - issues.get(i).getLastModifiedDate().getTime();
138  0 int numDays = (int) (numMillis / (24 * 60 * 60 * 1000));
139   
140  0 List<Notification> notifications = notificationService.getPrimaryIssueNotifications(issues.get(i));
141  0 for(int j = 0; j < notifications.size(); j++) {
142  0 if(notifications.get(j).getUser().getEmail() != null
143    && notifications.get(j).getUser().getEmail().indexOf('@') >= 0) {
144  0 addresses.add(notifications.get(j).getUser().getEmailAddress());
145    }
146    }
147  0 InternetAddress[] addressesArray = new ArrayList<InternetAddress>(addresses).toArray(new InternetAddress[]{});
148   
149  0 if (logger.isDebugEnabled()) {
150  0 logger.debug("Sending reminder notification for issue " + issues.get(i).getId() + " to " + addressesArray.length + " users.");
151    }
152  0 notificationService.sendNotification(issues.get(i), Type.ISSUE_REMINDER, baseURL, addressesArray, numDays);
153   
154    }
155    }
156    }
157    } catch(Exception e) {
158  0 logger.error("Error sending reminder notifications. Message: ", e);
159  0 throw new RuntimeException("failed to send reminder notifications.", e);
160    }
161    }
162    }