001: /*
002: * Copyright 2007 Outerthought bvba and Schaubroeck nv
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: package org.outerj.daisy.workflow;
017:
018: import java.util.*;
019:
020: /**
021: * Holds the information with which to update a task.
022: * Information missing in this object is simply left untouched in the task
023: * when updating it. Note that to remove variables, you need to explicitely
024: * call {@link #deleteVariable}.
025: */
026: public class TaskUpdateData {
027: private Map<VariableKey, VariableValue> variables = new HashMap<VariableKey, VariableValue>();
028: private Set<VariableKey> deletedVariables = new HashSet<VariableKey>();
029: private TaskPriority priority;
030: private Date dueDate;
031: private boolean clearDueDate = false;
032:
033: public void setVariable(String name, VariableScope scope,
034: WfValueType type, Object value) {
035: if (name == null)
036: throw new IllegalArgumentException("Null argument: name");
037: if (scope == null)
038: throw new IllegalArgumentException("Null argument: scope");
039: if (value == null)
040: throw new IllegalArgumentException("Null argument: value");
041: if (type == null)
042: throw new IllegalArgumentException("Null argument: type");
043:
044: if (!type.getTypeClass().isAssignableFrom(value.getClass()))
045: throw new IllegalArgumentException(
046: "Illegal value for given variable type, expected a "
047: + type.getTypeClass().getName()
048: + " but got a "
049: + value.getClass().getName());
050:
051: VariableKey key = new VariableKey(name, scope);
052: deletedVariables.remove(key);
053: variables.put(key, new VariableValue(type, value));
054: }
055:
056: public Object getVariable(String name, VariableScope scope) {
057: if (name == null)
058: throw new IllegalArgumentException("Null argument: name");
059: if (scope == null)
060: throw new IllegalArgumentException("Null argument: scope");
061:
062: VariableValue variableValue = variables.get(new VariableKey(
063: name, scope));
064: return variableValue != null ? variableValue.getValue() : null;
065: }
066:
067: public void deleteVariable(String name, VariableScope scope) {
068: if (name == null)
069: throw new IllegalArgumentException("Null argument: name");
070: if (scope == null)
071: throw new IllegalArgumentException("Null argument: scope");
072:
073: VariableKey key = new VariableKey(name, scope);
074: variables.remove(key);
075: deletedVariables.add(key);
076: }
077:
078: public boolean isVariableDeleted(String name, VariableScope scope) {
079: return deletedVariables.contains(new VariableKey(name, scope));
080: }
081:
082: /**
083: * Clears information about set and deleted variables.
084: */
085: public void clearVariables() {
086: variables.clear();
087: deletedVariables.clear();
088: }
089:
090: public void setPriority(TaskPriority priority) {
091: this .priority = priority;
092: }
093:
094: public TaskPriority getPriority() {
095: return priority;
096: }
097:
098: public void setDueDate(Date dueDate) {
099: this .dueDate = dueDate;
100: }
101:
102: public Date getDueDate() {
103: return dueDate;
104: }
105:
106: public void clearDueDate() {
107: this .dueDate = null;
108: this .clearDueDate = true;
109: }
110:
111: public boolean getClearDueDate() {
112: return clearDueDate;
113: }
114:
115: public Map<VariableKey, VariableValue> getVariables() {
116: return Collections.unmodifiableMap(variables);
117: }
118:
119: public Set<VariableKey> getDeletedVariables() {
120: return Collections.unmodifiableSet(deletedVariables);
121: }
122:
123: public final class VariableKey {
124: private final String name;
125: private final VariableScope scope;
126: private final int hash;
127:
128: public VariableKey(String name, VariableScope scope) {
129: this .name = name;
130: this .scope = scope;
131: this .hash = (name + scope).hashCode();
132: }
133:
134: public String getName() {
135: return name;
136: }
137:
138: public VariableScope getScope() {
139: return scope;
140: }
141:
142: public int hashCode() {
143: return hash;
144: }
145:
146: public boolean equals(Object obj) {
147: VariableKey other = (VariableKey) obj;
148: return name.equals(other.name) && scope == other.scope;
149: }
150: }
151:
152: public final class VariableValue {
153: private final WfValueType type;
154: private final Object value;
155:
156: public VariableValue(WfValueType type, Object value) {
157: this .type = type;
158: this .value = value;
159: }
160:
161: public WfValueType getType() {
162: return type;
163: }
164:
165: public Object getValue() {
166: return value;
167: }
168: }
169: }
|