ReminderNotification.java

  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. package org.itracker.web.scheduler.tasks;


  19. import org.apache.log4j.Logger;
  20. import org.itracker.model.Issue;
  21. import org.itracker.model.Notification;
  22. import org.itracker.model.util.IssueUtilities;
  23. import org.itracker.services.ConfigurationService;
  24. import org.itracker.services.IssueService;
  25. import org.itracker.services.NotificationService;
  26. import org.itracker.web.util.ServletContextUtils;
  27. import org.quartz.JobExecutionContext;
  28. import org.quartz.JobExecutionException;
  29. import org.springframework.transaction.annotation.Transactional;

  30. import java.util.*;

  31. /**
  32.  * This class can be used to send reminder emails to owners/admins
  33.  * that issues need their attention.
  34.  *
  35.  */
  36. public class ReminderNotification extends BaseJob implements Runnable {

  37.     public static final String DEFAULT_BASE_URL = "http://localhost:8080/itracker";
  38.     public static final int DEFAULT_ISSUE_AGE = 30;

  39.     private final Logger logger;

  40.     public ReminderNotification() {
  41.         this.logger = Logger.getLogger(getClass());
  42.     }

  43.     @Override
  44.     @Transactional
  45.     public void run() {
  46.         this.performTask(new String[]{});
  47.     }

  48.     @Override
  49.     public void execute(JobExecutionContext context) throws JobExecutionException {
  50.         super.execute(context);
  51.         this.performTask((String[]) context.get("args"));
  52.     }

  53.     /**
  54.      * This method is called by the scheduler to send the reminder
  55.      * notifications.  The arguments can be used to configure which issues
  56.      * and projects are included in the notifications.  The args should
  57.      * include as the first parameter the base url of the server including
  58.      * the scheme, hostname, port, and context.  For example:
  59.      * <br>
  60.      * http://localhost:8080/itracker
  61.      * <br>
  62.      * If no other arguments are supplied it sends reminders to all
  63.      * owners/admins of unresolved issues in all projects that have not been
  64.      * modified in 30 days.  The second element of the array can be a number
  65.      * that represents the number of days to use to check the last modified
  66.      * date.  The third optional element is a number that represents the project
  67.      * id to limit the notifications to. A fourth optional argument is the severity
  68.      * to send the notification for.
  69.      *
  70.      * @param args optional arguments to configure the notification messages
  71.      */
  72.     public void performTask(String[] args) {
  73.         final List<Issue> issues;
  74.         final ConfigurationService configurationService = ServletContextUtils.getItrackerServices().getConfigurationService();

  75.         String baseURL = configurationService.getSystemBaseURL();
  76.         int notificationDays = configurationService.getIntegerProperty("reminder_notification_days",
  77.                         DEFAULT_ISSUE_AGE);

  78.         int projectId = -1;
  79.         int severity = -1;

  80.         // Process arguments.
  81.         if (args != null) {
  82.             if (args.length > 0 && args[0] != null) {
  83.                 baseURL = args[0];
  84.             }

  85.             if (null == baseURL) {
  86.                 baseURL = DEFAULT_BASE_URL;
  87.             }
  88.             if (args.length > 1) {
  89.                 try {
  90.                     notificationDays = Integer.parseInt(args[1]);
  91.                 } catch (NumberFormatException nfe) {
  92.                     logger.debug("Invalid issue age specified in ReminderNotification task.");
  93.                 }
  94.             }
  95.             if (args.length > 2) {
  96.                 try {
  97.                     projectId = Integer.parseInt(args[2]);
  98.                 } catch (NumberFormatException nfe) {
  99.                     logger.debug("Invalid projectId specified in ReminderNotification task.");
  100.                 }
  101.             }
  102.             if (args.length > 3) {
  103.                 try {
  104.                     severity = Integer.parseInt(args[3]);
  105.                 } catch (NumberFormatException nfe) {
  106.                     logger.debug("Invalid severity specified in ReminderNotification task.");
  107.                 }
  108.             }
  109.         }
  110.         if (notificationDays < 1) {
  111.             logger.info("Reminder notifications are disabled for project " + projectId );
  112.             return;
  113.         }
  114.         logger.debug("Reminder notifications being sent for project " + projectId + " with issues over " + notificationDays + " days old with severity " + severity + ".  Base URL = " + baseURL);

  115.         try {
  116.             IssueService issueService = ServletContextUtils.getItrackerServices().getIssueService();
  117.             NotificationService notificationService = ServletContextUtils.getItrackerServices().getNotificationService();
  118.             GregorianCalendar cal = new GregorianCalendar();
  119.             cal.add(Calendar.DAY_OF_MONTH, 0 - notificationDays);
  120.             Date threshold = cal.getTime();

  121.             if (projectId > 0) {
  122.                 issues = issueService.getIssuesByProjectId(projectId, IssueUtilities.STATUS_RESOLVED);
  123.             } else {
  124.                 issues = issueService.getIssuesWithStatusLessThan(IssueUtilities.STATUS_RESOLVED);
  125.             }
  126.             if (issues != null && issues.size() > 0) {
  127.                 for (Issue issue : issues) {
  128.                     if (severity >= 0 && issue.getSeverity() != severity) {
  129.                         continue;
  130.                     }
  131.                     if (issue.getLastModifiedDate() != null && issue.getLastModifiedDate().before(threshold)) {
  132.                         List<Notification> notifications = notificationService.getPrimaryIssueNotifications(issue);
  133.                         for (Notification notification : notifications) {
  134.                             if (notification.getUser().getEmail() != null
  135.                                     && notification.getUser().getEmail().indexOf('@') >= 0) {
  136.                                 if (logger.isDebugEnabled()) {
  137.                                     logger.debug("Sending reminder notification for issue " + issue.getId() + " to " + notification.getUser() + " users.");
  138.                                 }
  139.                                 notificationService.sendReminder(issue, notification.getUser(), baseURL, notificationDays);

  140.                             }
  141.                         }
  142.                     }
  143.                 }
  144.             }
  145.         } catch (Exception e) {
  146.             logger.error("Error sending reminder notifications. Message: ", e);
  147.             throw new RuntimeException("failed to send reminder notifications.", e);
  148.         }
  149.     }
  150. }