1 /***
2 * Task.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.permissioning;
27
28 import java.util.HashSet;
29 import java.util.Hashtable;
30 import java.util.Iterator;
31
32 import org.jcreme.enumerations.Enumeration;
33
34 /***
35 * A Task represents an action from a functional point of view. It is associated
36 * to a purpose that defines the intention of the action. Each task is also
37 * associated to a Role. Users that hold that role are the only legitimate Users
38 * that can accomplish this Task.
39 *
40 * @author $Author: dbregeon $
41 * @version $Revision: 1.1 $
42 */
43 public class Task extends Enumeration {
44
45 protected static final Hashtable fromName = new Hashtable();
46
47 protected Purpose purpose = null;
48
49 protected HashSet subTasks = new HashSet();
50
51 protected Role role = null;
52
53 protected Task(String name) {
54 super(name);
55 }
56
57 public void setPurpose(Purpose p) {
58 this.purpose = p;
59 }
60
61 public Purpose getPurpose() {
62 return this.purpose;
63 }
64
65 public void setRole(Role r) {
66 if (this.role != r) {
67 if (this.role != null) {
68 this.role.removeTask(this);
69 }
70 this.role = r;
71 if (this.role != null) {
72 this.role.addTask(this);
73 }
74 }
75 }
76
77 public Role getRole() {
78 return this.role;
79 }
80
81 public Task[] getSubTasks() {
82 return (Task[]) this.subTasks.toArray(new Task[0]);
83 }
84
85 public void addSubTask(Task t) throws IllegalArgumentException {
86 if (t != null) {
87 if (t.isSubTask(this)) {
88 throw new IllegalArgumentException("Cycle detected.");
89 }
90 this.subTasks.add(t);
91 }
92 }
93
94 public void removeSubTask(Task t) {
95 if (t != null) {
96 this.subTasks.remove(t);
97 }
98 }
99
100 public boolean isSubTask(Task t) {
101 boolean result = (t != null) && (this.subTasks.contains(t));
102 if (!result) {
103 Iterator iter = this.subTasks.iterator();
104 while ((iter.hasNext()) && (!result)) {
105 result = ((Task) iter.next()).isSubTask(t);
106 }
107 }
108 return result;
109 }
110
111 public boolean isLeaf() {
112 return this.subTasks.isEmpty();
113 }
114
115 public static Task get(String name) {
116 Task result = null;
117 if (name != null) {
118 result = (Task) fromName.get(name);
119 if (result == null) {
120 result = new Task(name);
121 }
122 }
123 return result;
124 }
125
126 public static boolean contains(String name) {
127 return (name != null) && (fromName.containsKey(name));
128 }
129
130 public static void remove(String name) {
131 if (name != null) {
132 fromName.remove(name);
133 }
134 }
135
136 /***
137 * @see org.jcreme.enumerations.Enumeration#getFromName()
138 */
139 protected Hashtable getFromName() {
140 return fromName;
141 }
142
143 public static Task[] getTasks() {
144 return (Task[]) fromName.values().toArray(new Task[0]);
145 }
146
147 public static Enumeration[] getValues() {
148 return (Enumeration[]) fromName.values().toArray(new Enumeration[0]);
149 }
150
151 }