1 /***
2 * Purpose.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 * This enumeration represent the Purpose that drives a Task. Purposes define a
36 * hierarchy. Purposes are linked to the permissions to access data as they
37 * reflect the intent rather than to the actual action.
38 *
39 * @author $Author: dbregeon $
40 * @version $Revision: 1.1 $
41 */
42 public class Purpose extends Enumeration {
43
44 protected static final Hashtable fromName = new Hashtable();
45
46 protected HashSet parents = new HashSet();
47
48 protected Purpose(String name) {
49 super(name);
50 }
51
52 public void addParent(Purpose p) throws IllegalArgumentException {
53 if (p != null) {
54 if (p.hasParent(this)) {
55 throw new IllegalArgumentException("Cycle detected.");
56 }
57 this.parents.add(p);
58 }
59 }
60
61 public void removeParent(Purpose p) {
62 if (p != null) {
63 this.parents.remove(p);
64 }
65 }
66
67 public Purpose[] getParents() {
68 return (Purpose[]) this.parents.toArray(new Purpose[0]);
69 }
70
71 public boolean hasParent(Purpose p) {
72 boolean result = (p != null) && (this.parents.contains(p));
73 if (!result) {
74 Iterator iter = this.parents.iterator();
75 while ((iter.hasNext()) && (!result)) {
76 result = ((Purpose) iter.next()).hasParent(p);
77 }
78 }
79 return result;
80 }
81
82 public static Purpose get(String name) {
83 Purpose result = null;
84 if (name != null) {
85 result = (Purpose) fromName.get(name);
86 if (result == null) {
87 result = new Purpose(name);
88 }
89 }
90 return result;
91 }
92
93 public static boolean contains(String name) {
94 return (name != null) && (fromName.containsKey(name));
95 }
96
97 public static void remove(String name) {
98 if (name != null) {
99 fromName.remove(name);
100 }
101 }
102
103 /***
104 * @see org.jcreme.enumerations.Enumeration#getFromName()
105 */
106 protected Hashtable getFromName() {
107 return fromName;
108 }
109
110 public static Purpose[] getPurposes() {
111 return (Purpose[]) fromName.values().toArray(new Purpose[0]);
112 }
113
114 public static Enumeration[] getValues() {
115 return (Enumeration[]) fromName.values().toArray(new Enumeration[0]);
116 }
117
118 }