Notification.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;

  19. import org.apache.commons.lang.builder.CompareToBuilder;
  20. import org.apache.commons.lang.builder.ToStringBuilder;

  21. import java.io.Serializable;
  22. import java.util.Comparator;

  23. /**
  24.  * A notification to a user about an Issue.
  25.  * <p/>
  26.  * <p>
  27.  * An Notification can only belong to 1 Issue (composition).
  28.  * </p>
  29.  *
  30.  * @author ready
  31.  */
  32. public class Notification extends AbstractEntity implements Comparable<Entity> {

  33.     /**
  34.      *
  35.      */
  36.     private static final long serialVersionUID = 1L;

  37.     public static final Comparator<Notification> TYPE_COMPARATOR = new RoleComparator();

  38.     public static final Comparator<Notification> USER_COMPARATOR = new UserComparator();

  39.     public static final Comparator<Notification> ISSUE_USER_ROLE_COMPARATOR = new IssueUserRoleComparator();

  40.     private Issue issue;

  41.     private User user;

  42.     private Role role;

  43.     /**
  44.      * Default constructor (required by Hibernate).
  45.      * <p/>
  46.      * <p>
  47.      * PENDING: should be <code>private</code> so that it can only be used by
  48.      * Hibernate, to ensure that the fields which form an instance's identity
  49.      * are always initialized/never <tt>null</tt>.
  50.      * </p>
  51.      */
  52.     public Notification() {
  53.     }

  54.     /**
  55.      * @deprecated use Role instead int for role
  56.      */
  57.     public Notification(User user, Issue issue, int role) {
  58.         this.setUser(user);
  59.         this.setIssue(issue);
  60.         this.setNotificationRole(role);

  61.     }

  62.     public Notification(User user, Issue issue, Role role) {
  63.         this.setUser(user);
  64.         this.setIssue(issue);
  65.         this.setRole(role);
  66.     }

  67.     public Issue getIssue() {
  68.         return issue;
  69.     }

  70.     public void setIssue(Issue issue) {
  71.         if (issue == null) {
  72.             throw new IllegalArgumentException("null issue");
  73.         }
  74.         this.issue = issue;
  75.     }

  76.     public User getUser() {
  77.         return user;
  78.     }

  79.     public void setUser(User user) {
  80.         if (user == null) {
  81.             throw new IllegalArgumentException("null user");
  82.         }
  83.         this.user = user;
  84.     }

  85.     /**
  86.      * @deprecated use getRole instead
  87.      */
  88.     public int getNotificationRole() {
  89.         Role r = getRole();
  90.         if (null == r) {
  91.             r = Role.ANY;
  92.         }
  93.         return r.code;
  94.     }

  95.     /**
  96.      * @deprecated
  97.      */
  98.     public void setNotificationRole(int role) {
  99.         Role r = getRoleForCode(role);
  100.         if (null == r) {
  101.             r = Role.ANY;
  102.         }
  103.         this.setRole(r);
  104.     }

  105.     private Role getRoleForCode(int code) {
  106.         for (int i = 0; i < Role.values().length; i++) {
  107.             if (Role.values()[i].code == code) {
  108.                 return Role.values()[i];
  109.             }
  110.         }
  111.         return Role.ANY;
  112.     }

  113.     public Role getRole() {
  114.         return this.role;
  115.     }

  116.     public void setRole(Role role) {
  117.         this.role = role;
  118.     }

  119.     @Override
  120.     public String toString() {
  121.         return new ToStringBuilder(this).append("id", getId()).append("issue",
  122.                 getIssue()).append("user", getUser()).append("role", getRole())
  123.                 .toString();
  124.     }

  125.     /**
  126.      * Compares by properties issue, user, role.
  127.      *
  128.      * @author ranks
  129.      */
  130.     public static final class IssueUserRoleComparator implements
  131.             Comparator<Notification>, Serializable {
  132.         /**
  133.          *
  134.          */
  135.         private static final long serialVersionUID = 1L;

  136.         public int compare(Notification o1, Notification o2) {
  137.             return new CompareToBuilder().append(o1.getIssue(), o2.getIssue())
  138.                     .append(o1.getUser(), o2.getUser(), User.NAME_COMPARATOR)
  139.                     .append(o1.getRole().getCode(), o2.getRole().getCode())
  140.                     .toComparison();
  141.         }
  142.     }

  143.     private static class UserComparator implements Comparator<Notification>,
  144.             Serializable {
  145.         /**
  146.          *
  147.          */
  148.         private static final long serialVersionUID = 1L;

  149.         public int compare(Notification a, Notification b) {
  150.             return new CompareToBuilder().append(a.getUser(), b.getUser(),
  151.                     User.NAME_COMPARATOR).append(a.getRole(), b.getRole(),
  152.                     Notification.TYPE_COMPARATOR).toComparison();
  153.         }

  154.     }

  155.     private static class RoleComparator implements Comparator<Notification>,
  156.             Serializable {
  157.         /**
  158.          *
  159.          */
  160.         private static final long serialVersionUID = 1L;

  161.         public int compare(Notification a, Notification b) {
  162.             if (null == a.getRole()) {
  163.                 return null == b.getRole() ? 0 : -1;
  164.             } else if (b.getRole() == null) {
  165.                 return 1;
  166.             }
  167.             return new CompareToBuilder().append(a.getRole().getCode(),
  168.                     b.getRole().getCode()).toComparison();
  169.         }

  170.     }

  171.     public static enum Role {

  172.         ANY(-1),

  173.         CREATOR(1),

  174.         OWNER(2),

  175.         CONTRIBUTER(3),

  176.         QA(4),

  177.         PM(5),

  178.         PO(6),

  179.         CO(7),

  180.         VO(8),

  181.         IP(9);

  182.         private final int code;

  183.         private Role(int code) {
  184.             this.code = code;
  185.         }

  186.         public Integer getCode() {
  187.             return this.code;
  188.         }

  189.     }

  190.     public static enum Type {

  191.         CREATED(1),

  192.         UPDATED(2),

  193.         ASSIGNED(3),

  194.         CLOSED(4),

  195.         SELF_REGISTER(5),

  196.         ISSUE_REMINDER(6);

  197.         private final int code;

  198.         private Type(int code) {
  199.             this.code = code;
  200.         }

  201.         public Integer getCode() {
  202.             return code;
  203.         }

  204.     }

  205. }