001: /*
002: * Copyright 2002-2005 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package info.jtrac.domain;
018:
019: import static info.jtrac.Constants.*;
020:
021: import info.jtrac.util.XmlUtils;
022:
023: import java.io.Serializable;
024: import java.util.HashMap;
025: import java.util.Map;
026: import org.dom4j.Element;
027:
028: /**
029: * In addition to definition of custom fields, the Metadata
030: * for a Space may contain a bunch of Role defintions as well.
031: * Roles do the following
032: * - define the State Transitions possible (i.e. from status --> to status)
033: * - for each State (from status) define the access permissions that this Role has per Field
034: */
035: public class Role implements Serializable {
036:
037: private String name;
038: private String description;
039: private Map<Integer, State> states = new HashMap<Integer, State>();
040:
041: public Role(String name) {
042: this .name = name;
043: }
044:
045: public Role(Element e) {
046: name = e.attributeValue(NAME);
047: for (Object o : e.elements(STATE)) {
048: State state = new State((Element) o);
049: states.put(state.getStatus(), state);
050: }
051: }
052:
053: /* append this object onto an existing XML document */
054: public void addAsChildOf(Element parent) {
055: Element e = parent.addElement(ROLE);
056: copyTo(e);
057: }
058:
059: /* marshal this object into a fresh new XML Element */
060: public Element getAsElement() {
061: Element e = XmlUtils.getNewElement(ROLE);
062: copyTo(e);
063: return e;
064: }
065:
066: /* copy object values into an existing XML Element */
067: private void copyTo(Element e) {
068: // appending empty strings to create new objects for "clone" support
069: e.addAttribute(NAME, name + "");
070: for (State state : states.values()) {
071: state.addAsChildOf(e);
072: }
073: }
074:
075: //=======================================================================
076:
077: public void add(State state) {
078: states.put(state.getStatus(), state);
079: }
080:
081: public void removeState(int stateId) {
082: states.remove(stateId);
083: for (State s : states.values()) {
084: s.removeTransition(stateId);
085: }
086: }
087:
088: public boolean hasTransitionsFromState(int stateKey) {
089: return states.get(stateKey).getTransitions().size() > 0;
090: }
091:
092: //=======================================================================
093:
094: public Map<Integer, State> getStates() {
095: return states;
096: }
097:
098: public void setStates(Map<Integer, State> states) {
099: this .states = states;
100: }
101:
102: public String getDescription() {
103: return description;
104: }
105:
106: public void setDescription(String description) {
107: this .description = description;
108: }
109:
110: public String getName() {
111: return name;
112: }
113:
114: public void setName(String name) {
115: this .name = name;
116: }
117:
118: @Override
119: public String toString() {
120: StringBuffer sb = new StringBuffer();
121: sb.append("name [").append(name);
122: sb.append("]; states [").append(states);
123: sb.append("]");
124: return sb.toString();
125: }
126:
127: }
|