001 /* 002 * Copyright 1999-2001,2004 The Apache Software Foundation. 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 017 package org.apache.commons.workflow.base; 018 019 020 import java.util.ArrayList; 021 import org.apache.commons.workflow.Activity; 022 import org.apache.commons.workflow.Step; 023 024 025 /** 026 * <p><strong>BaseActivity</strong> is a convenient base class for more 027 * sophisticated <code>Activity</code> implementations. It includes 028 * management of the static relationships of Steps to each other as part 029 * of an owning Activity.</p> 030 * 031 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $ 032 * @author Craig R. McClanahan 033 */ 034 035 public class BaseActivity implements Activity { 036 037 038 // ----------------------------------------------------- Instance Variables 039 040 041 /** 042 * The first Step associated with this Activity. 043 */ 044 protected Step firstStep = null; 045 046 047 /** 048 * The unique identifier of this Activity. 049 */ 050 protected String id = null; 051 052 053 /** 054 * The last Step associated with this Activity. 055 */ 056 protected Step lastStep = null; 057 058 059 // ------------------------------------------------------------- Properties 060 061 062 /** 063 * Return the first Step associated with this Activity. 064 */ 065 public Step getFirstStep() { 066 067 return (this.firstStep); 068 069 } 070 071 072 /** 073 * Return the unique identifier of this Activity. 074 */ 075 public String getId() { 076 077 return (this.id); 078 079 } 080 081 082 /** 083 * Set the unique identifier of this Activity. 084 * 085 * @param id The new unique identifier 086 */ 087 public void setId(String id) { 088 089 this.id = id; 090 091 } 092 093 094 /** 095 * Return the last Step associated with this Activity. 096 */ 097 public Step getLastStep() { 098 099 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 }