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   * 
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 Integer 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  	/** The user who's granted this permission. */
56  	private User user;
57  
58  	/**
59  	 * Default constructor (required by Hibernate).
60  	 * 
61  	 * <p>
62  	 * PENDING: should be <code>private</code> so that it can only be used by
63  	 * Hibernate, to ensure that the fields which form an instance's identity
64  	 * are always initialized/never <tt>null</tt>.
65  	 * </p>
66  	 */
67  	public Permission() {
68  	}
69  
70  	/**
71  	 * Grants permissions on all projects to the given user.
72  	 * 
73  	 * @param type
74  	 *            permission type
75  	 * @param user
76  	 *            grantee
77  	 */
78  	public Permission(Integer 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
86  	 *            permission type
87  	 * @param user
88  	 *            grantee
89  	 * @param project
90  	 *            on which permission is granted, or <tt>null</tt> for all
91  	 *            projects
92  	 */
93  	public Permission(Integer type, User user, Project project) {
94  		setPermissionType(type);
95  		setUser(user);
96  		setProject(project);
97  	}
98  
99  	public Integer getPermissionType() {
100 		return type;
101 	}
102 
103 	public void setPermissionType(Integer type) {
104 		this.type = type;
105 	}
106 
107 	public User getUser() {
108 		return user;
109 	}
110 
111 	public void setUser(User user) {
112 		if (user == null) {
113 			throw new IllegalArgumentException("null user");
114 		}
115 		this.user = user;
116 	}
117 
118 	public Project getProject() {
119 		return project;
120 	}
121 
122 	/** May be null to indicate a permission on all projects. */
123 	public void setProject(Project project) {
124 		this.project = project;
125 	}
126 
127 
128 	@Override
129 	public String toString() {
130 		return new ToStringBuilder(this).append("id", getId()).append("type", getPermissionType())
131 				.append("user", getUser()).append("project", getProject()).toString();
132 	}
133 
134 	
135 	public static final class PermissionPropertiesComparator implements java.util.Comparator<Permission> {
136 		public int compare(Permission lhs, Permission rhs) {
137 			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();
138 	    }
139 	}
140 }