1
2
3
4
5
6 package net.kwfgrid.gwui.taskframework;
7
8 import java.util.*;
9 import javax.swing.Icon;
10
11 import net.kwfgrid.gwui.*;
12 import net.kwfgrid.gwui.workflow.*;
13 import net.kwfgrid.gwui.gui.*;
14 import net.kwfgrid.gwui.util.HashMapOfLists;
15 import net.kwfgrid.gworkflowdl.structure.*;
16 import net.kwfgrid.gworkflowdl.protocol.structure.*;
17
18 import org.glassbox.Theme;
19 import org.glassbox.executor.Executor;
20
21 import org.apache.log4j.Logger;
22 /***
23 Theme properties:
24 <ul>
25 <li>kwfgrid.InputTaskProducer4Decision.icon</li>
26 </ul>
27 */
28 public class InputTaskProducer4Decision implements UserTaskProducer {
29 private static final Logger logger = Logger.getLogger(InputTaskProducer4Decision.class);
30
31 public static final String DECISION_KEY_PREFIX = "decision.";
32
33 protected static final String TASK_ICON_KEY = "kwfgrid.InputTaskProducer4Decision.icon";
34 protected static final Icon TASK_ICON = Theme.getIcon(TASK_ICON_KEY);
35
36 private Executor _executor;
37 private UAA _uaa;
38 private ProtocolWorkflow _workflow;
39 private Browser _browser;
40 private HashMapOfLists _tasksbyplace;
41 private LinkedList _tasks;
42
43 public InputTaskProducer4Decision(Executor executor, UAA uaa, Browser browser) {
44 _executor = executor;
45 _uaa = uaa;
46 _workflow = null;
47 _browser = browser;
48 _tasksbyplace = new HashMapOfLists();
49 _tasks = new LinkedList();
50 }
51
52
53
54
55
56 protected void setTaskInvalid(InputTask4Decision task) {
57 _tasksbyplace.getList(task.getPlace()).remove(task);
58 }
59
60 protected void setTaskDone(InputTask4Decision task) {
61 _tasksbyplace.getList(task.getPlace()).remove(task);
62 }
63
64 protected boolean hasValidTask(Place p) {
65 return !_tasksbyplace.getList(p).isEmpty();
66 }
67
68 protected List getValidTasks(Place p) {
69 return _tasksbyplace.getList(p);
70 }
71
72 protected InputTask4Decision createTaskForDecisionProperty(Workflow wf, Property prop) {
73 if (prop.getKey().indexOf(DECISION_KEY_PREFIX)==0) {
74 String decisionkey = prop.getKey();
75 String decisiontype = prop.getValue();
76 Transition[] transitions = null;
77 Place place = null;
78 Form form = null;
79 String title = null;
80 String tooltip = null;
81
82
83 Place[] allplaces = wf.getPlaces();
84 for (int i=0; i<allplaces.length; i++) {
85 String pprop = allplaces[i].getProperties().get(decisionkey);
86 if (pprop != null && !"".equals(pprop)) {
87 place = allplaces[i];
88 break;
89 }
90 }
91
92 if (place == null) return null;
93
94
95 Transition[] alltransitions = wf.getTransitions();
96 List transitionlist = new LinkedList();
97 for (int i=0; i<alltransitions.length; i++) {
98 String tprop = alltransitions[i].getProperties().get(decisionkey);
99 if (tprop != null && !"".equals(tprop)) {
100 transitionlist.add(alltransitions[i]);
101 }
102 }
103
104 if (transitionlist.size() < 2) return null;
105 transitions = (Transition[])transitionlist.toArray(new Transition[transitionlist.size()]);
106
107 title = "Please select next operation at " + place.getID() + ".";
108 tooltip = title;
109
110 form = new InputForm4Decision(_executor, place, transitions);
111 InputTask4Decision task = new InputTask4Decision(this,
112 title,
113 tooltip,
114 form,
115 decisionkey,
116 place,
117 transitions,
118 _uaa);
119
120 logger.debug("Adding task to internal list for place ."+place);
121
122 _tasksbyplace.put(place, task);
123
124 return task;
125 } else {
126 return null;
127 }
128 }
129
130
131
132
133
134 private void createTasksForDecisionProperties(Workflow wf, List properties, Collection tasks) {
135 Iterator i = properties.iterator(); while (i.hasNext()) {
136 UserTask task = createTaskForDecisionProperty(wf, (Property)i.next());
137 if (task != null) tasks.add(task);
138 }
139 }
140
141
142
143
144
145 public UserTask[] newDocument(IRootObject document) {
146 _workflow =(ProtocolWorkflow) document;
147 LinkedList tasks = new LinkedList();
148 Property[] props = _workflow.getProperties().getProperties();
149 for (int i=0; i<props.length; i++) {
150 UserTask task = createTaskForDecisionProperty(_workflow, props[i]);
151 if (task != null) tasks.add(task);
152 }
153 return toUserTaskArray(tasks);
154 }
155
156 public UserTask[] objectsAdded(IStructureObject parent, String namespace, String name, List objects) {
157 if (isWorkflowProperty(parent, namespace, name)) {
158 _tasks.clear();
159 createTasksForDecisionProperties((Workflow)parent.getRoot(), objects, _tasks);
160 return toUserTaskArray(_tasks);
161 }
162 return null;
163 }
164
165 public UserTask[] objectsRemoved(IStructureObject parent, String namespace, String name, List objects) {
166 return null;
167 }
168
169 public UserTask[] propertyChanged(IStructureObject parent, String namespace, String name, Object newvalue) {
170 if (isValueOfWorkflowProperty(parent, namespace, name) ||
171 isKeyOfWorkflowProperty(parent, namespace, name)) {
172 UserTask task = createTaskForDecisionProperty((Workflow)parent.getRoot(), (Property)parent);
173 if (task != null) return new UserTask[] { task };
174 }
175 return null;
176 }
177
178 protected boolean isWorkflowProperty(IStructureObject parent, String namespace, String name) {
179 if (ProtocolProperty.NAME.equals(name) &&
180 XMLUtilities.equalNamespace(ProtocolProperty.NAMESPACE, namespace)) {
181 if (parent instanceof ProtocolProperties) {
182 Object pp = ((ProtocolProperties)parent).getParent();
183 return (pp instanceof Workflow);
184 }
185 }
186 return false;
187 }
188
189 private boolean isValueOfWorkflowProperty(IStructureObject parent, String namespace, String name) {
190 if (ProtocolProperty.NAME.equals(name) &&
191 XMLUtilities.equalNamespace(ProtocolProperty.NAMESPACE, namespace)) {
192 if (parent instanceof ProtocolProperty) {
193 ProtocolProperties pp = (ProtocolProperties)((ProtocolProperty)parent).getParent();
194 Object ppp = pp.getParent();
195 return (ppp instanceof Workflow);
196 }
197 }
198 return false;
199 }
200
201 private boolean isKeyOfWorkflowProperty(IStructureObject parent, String namespace, String name) {
202 if (ProtocolProperty.NAME_KEY.equals(name) &&
203 XMLUtilities.equalNamespace(ProtocolProperty.NAMESPACE_PROPERTIES, namespace)) {
204 if (parent instanceof ProtocolProperty) {
205 ProtocolProperties pp = (ProtocolProperties)((ProtocolProperty)parent).getParent();
206 Object ppp = pp.getParent();
207 return (ppp instanceof Workflow);
208 }
209 }
210 return false;
211 }
212
213
214 private UserTask[] toUserTaskArray(Collection tasks) {
215 return tasks.size()==0?null:(UserTask[])tasks.toArray(new UserTask[tasks.size()]);
216 }
217 }
218