View Javadoc
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.model;
20  
21  import org.apache.commons.lang.builder.CompareToBuilder;
22  import org.apache.commons.lang.builder.ToStringBuilder;
23  
24  /**
25   * A user permission on a project.
26   * <p/>
27   * <p>
28   * The permission type tells what kind of action the user is allowed perform.
29   * </p>
30   *
31   * @author ready
32   */
33  public class Permission extends AbstractEntity {
34  
35      /**
36       * Comparator for comparing the main properties type, user, project
37       */
38      public static final PermissionPropertiesComparator PERMISSION_PROPERTIES_COMPARATOR = new PermissionPropertiesComparator();
39      /**
40       *
41       */
42      private static final long serialVersionUID = 1L;
43  
44      /**
45       * The type of permission granted. TODO: use PermissionType enum
46       */
47      private PermissionType type;
48  
49      /**
50       * The project on which this permission is granted. May be <tt>null</tt>
51       * to indicate the permission is granted on all projects.
52       */
53      private Project project;
54  
55      /**
56       * The user who's granted this permission.
57       */
58      private User user;
59  
60      /**
61       * Default constructor (required by Hibernate).
62       * <p/>
63       * <p>
64       * PENDING: should be <code>private</code> so that it can only be used by
65       * Hibernate, to ensure that the fields which form an instance's identity
66       * are always initialized/never <tt>null</tt>.
67       * </p>
68       */
69      public Permission() {
70      }
71  
72      /**
73       * Grants permissions on all projects to the given user.
74       *
75       * @param type permission type
76       * @param user grantee
77       */
78      public Permission(PermissionType type, User user) {
79          this(type, user, null);
80      }
81  
82      /**
83       * Grants permissions on all projects to the given user.
84       *
85       * @param type    permission type
86       * @param user    grantee
87       * @param project on which permission is granted, or <tt>null</tt> for all
88       *                projects
89       */
90      public Permission(PermissionType type, User user, Project project) {
91          setPermissionType(type);
92          setUser(user);
93          setProject(project);
94      }
95  
96      public PermissionType getPermissionType() {
97          return type;
98      }
99  
100     public void setPermissionType(PermissionType type) {
101         this.type = type;
102     }
103 
104     public User getUser() {
105         return user;
106     }
107 
108     public void setUser(User user) {
109         if (user == null) {
110             throw new IllegalArgumentException("null user");
111         }
112         this.user = user;
113     }
114 
115     public Project getProject() {
116         return project;
117     }
118 
119     /**
120      * May be null to indicate a permission on all projects.
121      */
122     public void setProject(Project project) {
123         this.project = project;
124     }
125 
126 
127     @Override
128     public String toString() {
129         return new ToStringBuilder(this).append("id", getId()).append("type", getPermissionType())
130                 .append("user", getUser()).append("project", getProject()).toString();
131     }
132 
133 
134     public static final class PermissionPropertiesComparator implements java.util.Comparator<Permission> {
135         public int compare(Permission"../../../org/itracker/model/Permission.html#Permission">Permission lhs, Permission rhs) {
136             return new CompareToBuilder().append(lhs.type, rhs.type).append(lhs.user, rhs.user, User.NAME_COMPARATOR).append(lhs.project, rhs.project, Project.PROJECT_COMPARATOR).toComparison();
137         }
138     }
139 }