KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > workflow > impl > SynchronizedWorkflowInstancesImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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
18 /* $Id: SynchronizedWorkflowInstancesImpl.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.workflow.impl;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.lenya.workflow.Event;
27 import org.apache.lenya.workflow.Situation;
28 import org.apache.lenya.workflow.State;
29 import org.apache.lenya.workflow.SynchronizedWorkflowInstances;
30 import org.apache.lenya.workflow.WorkflowException;
31 import org.apache.lenya.workflow.WorkflowInstance;
32 import org.apache.log4j.Category;
33
34 /**
35  * An object of this class encapsulates a set of synchronized
36  * workflow instances.
37  */

38 public class SynchronizedWorkflowInstancesImpl implements SynchronizedWorkflowInstances {
39
40     private static final Category log = Category.getInstance(SynchronizedWorkflowInstancesImpl.class);
41
42     /**
43      * Ctor.
44      */

45     public SynchronizedWorkflowInstancesImpl() {
46     }
47
48     /**
49      * Ctor.
50      * @param instances The set of workflow instances to synchronize.
51      * @param mainInstance The main workflow instance to invoke for non-synchronized transitions.
52      */

53     public SynchronizedWorkflowInstancesImpl(
54         WorkflowInstance[] instances,
55         WorkflowInstance mainInstance) {
56         setInstances(instances);
57         setMainInstance(mainInstance);
58     }
59
60     /**
61      * Sets the main workflow instance.
62      * @param mainInstance The main workflow instance to invoke for non-synchronized transitions.
63      */

64     public void setMainInstance(WorkflowInstance mainInstance) {
65         this.mainInstance = mainInstance;
66     }
67
68     private WorkflowInstance[] instances;
69     private WorkflowInstance mainInstance;
70
71     public void setInstances(WorkflowInstance[] instances) {
72         this.instances = instances;
73     }
74
75     public WorkflowInstance[] getInstances() {
76         return instances;
77     }
78
79     /**
80      * Returns all executable events.
81      * @see org.apache.lenya.workflow.WorkflowInstance#getExecutableEvents(org.apache.lenya.workflow.Situation)
82      */

83     public Event[] getExecutableEvents(Situation situation) throws WorkflowException {
84         if (log.isDebugEnabled()) {
85             log.debug("Resolving executable events");
86         }
87
88         WorkflowInstance[] instances = getInstances();
89         if (instances.length == 0) {
90             throw new WorkflowException("The set must contain at least one workflow instance!");
91         }
92
93         Event[] events = mainInstance.getExecutableEvents(situation);
94         Set JavaDoc executableEvents = new HashSet JavaDoc(Arrays.asList(events));
95
96         for (int i = 0; i < events.length; i++) {
97             Event event = events[i];
98             if (mainInstance.isSynchronized(event)) {
99
100                 boolean canFire = true;
101                 if (log.isDebugEnabled()) {
102                     log.debug(" Transition for event [" + event + "] is synchronized.");
103                 }
104
105                 boolean sameState = true;
106                 State currentState = mainInstance.getCurrentState();
107                 int j = 0;
108                 while (j < instances.length && sameState) {
109                     sameState = instances[j].getCurrentState().equals(currentState);
110                     j++;
111                 }
112                 if (log.isDebugEnabled()) {
113                     log.debug(" All instances are in the same state: [" + sameState + "]");
114                 }
115
116                 if (sameState) {
117                     for (int k = 0; k < instances.length; k++) {
118                         WorkflowInstanceImpl instance = (WorkflowInstanceImpl) instances[k];
119                         if (instance != mainInstance && !instance.getNextTransition(event).canFire(situation, instance)) {
120                             canFire = false;
121                             if (log.isDebugEnabled()) {
122                                 log.debug(" Workflow instance [" + instance + "] can not fire.");
123                             }
124                         }
125                     }
126                 } else {
127                     canFire = false;
128                 }
129
130                 if (!canFire) {
131                     executableEvents.remove(event);
132                     if (log.isDebugEnabled()) {
133                         log.debug(" Event [" + event + "] can not fire - removing from executable events.");
134                     }
135                 }
136             }
137         }
138
139         if (log.isDebugEnabled()) {
140             log.debug(" Resolving executable events completed.");
141         }
142
143         return (Event[]) executableEvents.toArray(new Event[executableEvents.size()]);
144     }
145
146     /**
147      * Invokes an event on all documents.
148      * @see org.apache.lenya.workflow.WorkflowInstance#invoke(org.apache.lenya.workflow.Situation, org.apache.lenya.workflow.Event)
149      */

150     public void invoke(Situation situation, Event event) throws WorkflowException {
151         
152         if (mainInstance.isSynchronized(event)) {
153             for (int i = 0; i < instances.length; i++) {
154                 instances[i].invoke(situation, event);
155             }
156         }
157         else {
158             mainInstance.invoke(situation, event);
159         }
160     }
161
162 }
163
Popular Tags