KickJava   Java API By Example, From Geeks To Geeks.

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


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: TransitionImpl.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.workflow.impl;
21
22 import org.apache.lenya.workflow.Action;
23 import org.apache.lenya.workflow.Condition;
24 import org.apache.lenya.workflow.Event;
25 import org.apache.lenya.workflow.Situation;
26 import org.apache.lenya.workflow.Transition;
27 import org.apache.lenya.workflow.WorkflowException;
28 import org.apache.lenya.workflow.WorkflowInstance;
29 import org.apache.log4j.Category;
30
31 import java.util.ArrayList JavaDoc;
32 import java.util.List JavaDoc;
33
34
35 /**
36  * Implementation of a transition.
37  */

38 public class TransitionImpl implements Transition {
39     
40     private static final Category log = Category.getInstance(TransitionImpl.class);
41     
42     /**
43      * Ctor.
44      * @param sourceState The source state.
45      * @param destinationState The destination state.
46      */

47     protected TransitionImpl(StateImpl sourceState, StateImpl destinationState) {
48         source = sourceState;
49         destination = destinationState;
50     }
51
52     private List JavaDoc actions = new ArrayList JavaDoc();
53     private boolean isSynchronized = false;
54
55     /**
56      * Returns the actions which are assigned tothis transition.
57      * @return An array of actions.
58      */

59     public Action[] getActions() {
60         return (Action[]) actions.toArray(new Action[actions.size()]);
61     }
62
63     /**
64      * Assigns an action to this transition.
65      * @param action The action.
66      */

67     public void addAction(Action action) {
68         actions.add(action);
69     }
70
71     private List JavaDoc conditions = new ArrayList JavaDoc();
72
73     /**
74      * Returns the conditions which are assigned to this transition.
75      * @return An array of conditions.
76      */

77     public Condition[] getConditions() {
78         return (Condition[]) conditions.toArray(new Condition[conditions.size()]);
79     }
80
81     /**
82      * Assigns a condition to this transition.
83      * @param condition The condition.
84      */

85     public void addCondition(Condition condition) {
86         conditions.add(condition);
87     }
88
89     private Event event;
90
91     /**
92      * Returns the event which invokes this transition.
93      * @return An event.
94      */

95     public Event getEvent() {
96         return event;
97     }
98
99     /**
100      * Sets the event to invoke this transition.
101      * @param anEvent An event.
102      */

103     public void setEvent(Event anEvent) {
104         event = anEvent;
105     }
106
107     private StateImpl source;
108
109     /**
110      * Returns the source state of this transition.
111      * @return A state.
112      */

113     public StateImpl getSource() {
114         return source;
115     }
116
117     private StateImpl destination;
118
119     /**
120      * Returns the destination state of this transition.
121      * @return A state.
122      */

123     public StateImpl getDestination() {
124         return destination;
125     }
126
127     /**
128      * Returns if the transition can fire in a certain situation.
129      * @param situation The situation.
130      * @param instance The workflow instance.
131      * @throws WorkflowException when an error occurs.
132      * @return A boolean value.
133      */

134     public boolean canFire(Situation situation, WorkflowInstance instance) throws WorkflowException {
135         Condition[] conditions = getConditions();
136         boolean canFire = true;
137
138         int i = 0;
139         while (canFire && i < conditions.length) {
140             canFire = canFire && conditions[i].isComplied(situation, instance);
141             if (log.isDebugEnabled()) {
142                 log.debug("Condition [" + conditions[i] + "] returns [" + canFire + "]");
143             }
144             i++;
145         }
146
147         return canFire;
148     }
149
150     /**
151      * @see java.lang.Object#toString()
152      */

153     public String JavaDoc toString() {
154         String JavaDoc string = getEvent().getName() + " [";
155         Condition[] conditions = getConditions();
156
157         for (int i = 0; i < conditions.length; i++) {
158             if (i > 0) {
159                 string += ", ";
160             }
161
162             string += conditions[i].toString();
163         }
164
165         string += "]";
166
167         Action[] actions = getActions();
168
169         if (actions.length > 0) {
170             string += " / ";
171
172             for (int i = 0; i < actions.length; i++) {
173                 if (i > 0) {
174                     string += ", ";
175                 }
176
177                 string += actions[i].toString();
178             }
179         }
180
181         return string;
182     }
183
184     /**
185      * Returns if this transition is synchronized.
186      * @return A boolean value.
187      */

188     public boolean isSynchronized() {
189         return isSynchronized;
190     }
191
192     /**
193      * Sets if this transition is synchronized.
194      * @param isSynchronized A boolean value.
195      */

196     protected void setSynchronized(boolean isSynchronized) {
197         this.isSynchronized = isSynchronized;
198     }
199
200 }
201
Popular Tags