KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > jtrac > domain > Role


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package info.jtrac.domain;
18
19 import static info.jtrac.Constants.*;
20
21 import info.jtrac.util.XmlUtils;
22
23 import java.io.Serializable JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import org.dom4j.Element;
27
28 /**
29  * In addition to definition of custom fields, the Metadata
30  * for a Space may contain a bunch of Role defintions as well.
31  * Roles do the following
32  * - define the State Transitions possible (i.e. from status --> to status)
33  * - for each State (from status) define the access permissions that this Role has per Field
34  */

35 public class Role implements Serializable JavaDoc {
36     
37     private String JavaDoc name;
38     private String JavaDoc description;
39     private Map JavaDoc<Integer JavaDoc, State> states = new HashMap JavaDoc<Integer JavaDoc, State>();
40     
41     public Role(String JavaDoc name) {
42         this.name = name;
43     }
44     
45     public Role(Element e) {
46         name = e.attributeValue(NAME);
47         for (Object JavaDoc o : e.elements(STATE)) {
48             State state = new State((Element) o);
49             states.put(state.getStatus(), state);
50         }
51     }
52     
53     /* append this object onto an existing XML document */
54     public void addAsChildOf(Element parent) {
55         Element e = parent.addElement(ROLE);
56         copyTo(e);
57     }
58     
59     /* marshal this object into a fresh new XML Element */
60     public Element getAsElement() {
61         Element e = XmlUtils.getNewElement(ROLE);
62         copyTo(e);
63         return e;
64     }
65     
66     /* copy object values into an existing XML Element */
67     private void copyTo(Element e) {
68         // appending empty strings to create new objects for "clone" support
69
e.addAttribute(NAME, name + "");
70         for (State state : states.values()) {
71             state.addAsChildOf(e);
72         }
73     }
74     
75     //=======================================================================
76

77     public void add(State state) {
78         states.put(state.getStatus(), state);
79     }
80     
81     public void removeState(int stateId) {
82         states.remove(stateId);
83         for(State s : states.values()) {
84             s.removeTransition(stateId);
85         }
86     }
87     
88     //=======================================================================
89

90     public Map JavaDoc<Integer JavaDoc, State> getStates() {
91         return states;
92     }
93     
94     public void setStates(Map JavaDoc<Integer JavaDoc, State> states) {
95         this.states = states;
96     }
97     
98     public String JavaDoc getDescription() {
99         return description;
100     }
101     
102     public void setDescription(String JavaDoc description) {
103         this.description = description;
104     }
105     
106     public String JavaDoc getName() {
107         return name;
108     }
109     
110     public void setName(String JavaDoc name) {
111         this.name = name;
112     }
113     
114     @Override JavaDoc
115     public String JavaDoc toString() {
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117         sb.append("name [").append(name);
118         sb.append("]; states [").append(states);
119         sb.append("]");
120         return sb.toString();
121     }
122     
123 }
124
Popular Tags