NotificationUtilities.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.model.util;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.itracker.core.resources.ITrackerResources;
  21. import org.itracker.model.Notification;
  22. import org.itracker.model.Notification.Role;
  23. import org.itracker.model.User;

  24. import java.util.*;

  25. public class NotificationUtilities {

  26.     /**
  27.      * @deprecated, use enumeration Notification.Role instead
  28.      */
  29.     private static HashMap<Locale, HashMap<Role, String>> roleNames = new HashMap<Locale, HashMap<Role, String>>();

  30.     public NotificationUtilities() {
  31.         super();
  32.     }

  33.     /**
  34.      * @deprecated
  35.      */
  36.     public static String getRoleName(int value) {
  37.         return getRoleName(value, ITrackerResources.getLocale());
  38.     }

  39.     /**
  40.      * @deprecated
  41.      */
  42.     public static String getRoleName(int value, Locale locale) {
  43.         return ITrackerResources.getString("itracker.notification.role."
  44.                 + value, locale);
  45.     }

  46.     public static String getRoleName(Role role) {
  47.         return getRoleName(role, ITrackerResources.getLocale());
  48.     }

  49.     public static String getRoleName(Role role, Locale locale) {
  50.         String s;
  51.         if (null != (s = ITrackerResources.getString(
  52.                 "itracker.notification.role." + role, locale))) {
  53.             return s;
  54.         }

  55.         return ITrackerResources.getString("itracker.notification.role."
  56.                 + role.getCode(), locale);
  57.     }


  58.     public static HashMap<Role, String> getRoleNames(Locale locale) {
  59.         HashMap<Role, String> roles = roleNames.get(locale);
  60.         if (roles == null) {
  61.             roles = new HashMap<Role, String>();
  62.             roles.put(Notification.Role.CREATOR,
  63.                     getRoleName(Notification.Role.CREATOR, locale));
  64.             roles.put(Notification.Role.OWNER, getRoleName(Role.OWNER, locale));
  65.             roles.put(Notification.Role.CONTRIBUTER, getRoleName(
  66.                     Notification.Role.CONTRIBUTER, locale));
  67.             roles.put(Notification.Role.QA, getRoleName(Role.QA, locale));
  68.             roles.put(Notification.Role.PM, getRoleName(Role.PM, locale));
  69.             roles.put(Notification.Role.PO, getRoleName(Role.PO, locale));
  70.             roles.put(Notification.Role.CO, getRoleName(Role.CO, locale));
  71.             roles.put(Notification.Role.VO, getRoleName(Role.VO, locale));
  72.             roles.put(Notification.Role.IP, getRoleName(Role.IP, locale));
  73.         }
  74.         roleNames.put(locale, roles);
  75.         return roles;
  76.     }


  77.     public static final Map<User, Set<Notification.Role>> mappedRoles(List<Notification> notifications) {

  78.         Map<User, Set<Role>> mapping = new Hashtable<User, Set<Role>>();
  79.         Iterator<Notification> notificationIt = notifications.iterator();
  80.         while (notificationIt.hasNext()) {
  81.             Notification notification = (Notification) notificationIt.next();
  82.             Set<Role> roles;
  83.             if (mapping.keySet().contains(notification.getUser())) {
  84.                 roles = mapping.get(notification.getUser());
  85.                 roles.add(notification.getRole());
  86.             } else {
  87.                 roles = new TreeSet<Role>(new Comparator<Role>() {
  88.                     public int compare(Role o1, Role o2) {
  89.                         return new CompareToBuilder().append(o1.getCode(), o2.getCode()).toComparison();
  90.                     }
  91.                 });
  92.                 roles.add(notification.getRole());
  93.                 mapping.put(notification.getUser(), roles);
  94.             }
  95.         }


  96.         return mapping;
  97.     }

  98. }