KickJava   Java API By Example, From Geeks To Geeks.

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


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 info.jtrac.util.XmlUtils;
20
21 import java.util.HashSet JavaDoc;
22
23 import static info.jtrac.Constants.*;
24
25 import java.io.Serializable JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import org.dom4j.Element;
31
32 /**
33  * State as in "State Transition"
34  * holds a set of possible future states to transition to
35  * also holds a map of [ field name = integer "mask" ]
36  * to represent permissions (view or edit) that the role owning this state
37  * has for each field for an item which is in this particular state
38  *
39  * For example, consider a state FOO and a role BAR.
40  * When a user with role BAR views an item that is having the status FOO:
41  * ie. when item.status == FOO.status, the fields that can be viewed on screen
42  * will be the entries in FOO.fields where the value == MASK_VIEW (or 1)
43  */

44 public class State implements Serializable JavaDoc {
45     
46     private int status;
47     private Set JavaDoc<Integer JavaDoc> transitions = new HashSet JavaDoc<Integer JavaDoc>();
48     private Map JavaDoc<Field.Name, Integer JavaDoc> fields = new HashMap JavaDoc<Field.Name, Integer JavaDoc>();
49     
50     public static final int NEW = 0;
51     public static final int OPEN = 1;
52     public static final int CLOSED = 99;
53     
54     public static final int MASK_HIDE = 0;
55     public static final int MASK_VIEW = 1;
56     public static final int MASK_EDIT = 2;
57     
58     public State() {
59         // zero arg constructor
60
}
61     
62     public State(int s) {
63         this.status = s;
64     }
65     
66     public State(Element e) {
67         this.status = Integer.parseInt(e.attributeValue(STATUS));
68         for (Object JavaDoc o : e.elements(TRANSITION)) {
69             Element t = (Element) o;
70             transitions.add(new Integer JavaDoc(t.attributeValue(STATUS)));
71         }
72         for (Object JavaDoc o : e.elements(FIELD)) {
73             Element f = (Element) o;
74             String JavaDoc fieldName = f.attributeValue(NAME);
75             fields.put(Field.convertToName(fieldName), new Integer JavaDoc(f.attributeValue(MASK)));
76         }
77     }
78     
79     /* append this object onto an existing XML document */
80     public void addAsChildOf(Element parent) {
81         Element e = parent.addElement(STATE);
82         copyTo(e);
83     }
84     
85     /* marshal this object into a fresh new XML Element */
86     public Element getAsElement() {
87         Element e = XmlUtils.getNewElement(STATE);
88         copyTo(e);
89         return e;
90     }
91     
92     /* copy object values into an existing XML Element */
93     private void copyTo(Element e) {
94         // appending empty strings to create new objects for "clone" support
95
e.addAttribute(STATUS, status + "");
96         for (Integer JavaDoc toStatus : transitions) {
97             Element t = e.addElement(TRANSITION);
98             t.addAttribute(STATUS, toStatus + "");
99         }
100         for (Map.Entry JavaDoc<Field.Name, Integer JavaDoc> entry : fields.entrySet()) {
101             Element f = e.addElement(FIELD);
102             f.addAttribute(NAME, entry.getKey() + "");
103             f.addAttribute(MASK, entry.getValue() + "");
104         }
105     }
106     
107     //=======================================================================
108

109     public void add(Collection JavaDoc<Field.Name> fieldNames) {
110         for (Field.Name fieldName : fieldNames) {
111             add(fieldName);
112         }
113     }
114     
115     public void add(Field.Name fieldName) {
116         int mask = MASK_VIEW;
117         // for NEW states, normally all Fields on the Item are editable
118
if (status == NEW) {
119             mask = MASK_EDIT;
120         }
121         fields.put(fieldName, mask);
122     }
123     
124     public void remove(Field.Name fieldName) {
125         fields.remove(fieldName);
126     }
127     
128     public void addTransition(int toStatus) {
129         transitions.add(toStatus);
130     }
131     
132     public void removeTransition(int toStatus) {
133         transitions.remove(toStatus);
134     }
135     
136     /**
137      * to make JSTL EL easier
138      * create Map on the fly but with boolean true values for keys that are present
139      */

140     public Map JavaDoc<Integer JavaDoc, Boolean JavaDoc> getTransitionMap() {
141         Map JavaDoc<Integer JavaDoc, Boolean JavaDoc> map = new HashMap JavaDoc<Integer JavaDoc, Boolean JavaDoc>();
142         for (Integer JavaDoc i : transitions) {
143             map.put(i, true);
144         }
145         return map;
146     }
147     
148     //=======================================================================
149

150     public Map JavaDoc<Field.Name, Integer JavaDoc> getFields() {
151         return fields;
152     }
153
154     public void setFields(Map JavaDoc<Field.Name, Integer JavaDoc> fields) {
155         this.fields = fields;
156     }
157
158     public int getStatus() {
159         return status;
160     }
161
162     public void setStatus(int status) {
163         this.status = status;
164     }
165     
166     public Set JavaDoc<Integer JavaDoc> getTransitions() {
167         return transitions;
168     }
169
170     public void setTransitions(Set JavaDoc<Integer JavaDoc> transitions) {
171         this.transitions = transitions;
172     }
173     
174     @Override JavaDoc
175     public String JavaDoc toString() {
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
177         sb.append("status [").append(status);
178         sb.append("]; transitions [").append(transitions);
179         sb.append("]; fields [").append(fields);
180         sb.append("]");
181         return sb.toString();
182     }
183     
184 }
185
Popular Tags