View Javadoc

1   /*
2    * Copyright 1999-2001,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  package org.apache.commons.workflow.base;
18  
19  
20  import java.util.ArrayList;
21  import org.apache.commons.workflow.Activity;
22  import org.apache.commons.workflow.Step;
23  
24  
25  /**
26   * <p><strong>BaseActivity</strong> is a convenient base class for more
27   * sophisticated <code>Activity</code> implementations.  It includes
28   * management of the static relationships of Steps to each other as part
29   * of an owning Activity.</p>
30   *
31   * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
32   * @author Craig R. McClanahan
33   */
34  
35  public class BaseActivity implements Activity {
36  
37  
38      // ----------------------------------------------------- Instance Variables
39  
40  
41      /**
42       * The first Step associated with this Activity.
43       */
44      protected Step firstStep = null;
45  
46  
47      /**
48       * The unique identifier of this Activity.
49       */
50      protected String id = null;
51  
52  
53      /**
54       * The last Step associated with this Activity.
55       */
56      protected Step lastStep = null;
57  
58  
59      // ------------------------------------------------------------- Properties
60  
61  
62      /**
63       * Return the first Step associated with this Activity.
64       */
65      public Step getFirstStep() {
66  
67          return (this.firstStep);
68  
69      }
70  
71  
72      /**
73       * Return the unique identifier of this Activity.
74       */
75      public String getId() {
76  
77          return (this.id);
78  
79      }
80  
81  
82      /**
83       * Set the unique identifier of this Activity.
84       *
85       * @param id The new unique identifier
86       */
87      public void setId(String id) {
88  
89          this.id = id;
90  
91      }
92  
93  
94      /**
95       * Return the last Step associated with this Activity.
96       */
97      public Step getLastStep() {
98  
99          return (this.lastStep);
100 
101     }
102 
103 
104     // ---------------------------------------------------------- Owner Methods
105 
106 
107     /**
108      * Add a new Step to the end of the sequence of Steps associated with
109      * this Activity.
110      *
111      * @param step The new step to be added
112      */
113     public void addStep(Step step) {
114 
115         step.setOwner(this);
116         if (firstStep == null) {
117             step.setPreviousStep(null);
118             step.setNextStep(null);
119             firstStep = step;
120             lastStep = step;
121         } else {
122             step.setPreviousStep(lastStep);
123             step.setNextStep(null);
124             lastStep.setNextStep(step);
125             lastStep = step;
126         }
127 
128     }
129 
130 
131     /**
132      * Clear any existing Steps associated with this Activity.
133      */
134     public void clearSteps() {
135 
136         Step current = firstStep;
137         while (current != null) {
138             Step next = current.getNextStep();
139             current.setOwner(null);
140             current.setPreviousStep(null);
141             current.setNextStep(null);
142             current = next;
143         }
144         firstStep = null;
145         lastStep = null;
146 
147     }
148 
149 
150     /**
151      * Return the identified Step from this Activity, if it exists.
152      * Otherwise, return <code>null</code>.
153      *
154      * @param id Identifier of the desired Step
155      */
156     public Step findStep(String id) {
157 
158         Step current = getFirstStep();
159         while (current != null) {
160             if (id.equals(current.getId()))
161                 return (current);
162             current = current.getNextStep();
163         }
164         return (null);
165 
166     }
167 
168 
169     /**
170      * Return the set of Steps associated with this Activity.
171      */
172     public Step[] getSteps() {
173 
174         ArrayList list = new ArrayList();
175         Step currentStep = firstStep;
176         while (currentStep != null) {
177             list.add(currentStep);
178             currentStep = currentStep.getNextStep();
179         }
180         Step steps[] = new Step[list.size()];
181         return ((Step[]) list.toArray(steps));
182 
183     }
184 
185 
186     /**
187      * Set the set of Steps associated with this Activity, replacing any
188      * existing ones.
189      *
190      * @param steps The new set of steps.
191      */
192     public void setSteps(Step steps[]) {
193 
194         clearSteps();
195         for (int i = 0; i < steps.length; i++)
196             addStep(steps[i]);
197 
198     }
199 
200 
201 }