1 package za.co.csir.icomtek.workflow; 2 3 import java.util.HashMap ; 4 import java.util.List ; 5 import java.util.Enumeration ; 6 import java.util.Iterator ; 7 import java.util.Collection ; 8 9 import java.io.InputStream ; 10 import java.io.ByteArrayInputStream ; 11 import java.io.InputStreamReader ; 12 import java.io.FileReader ; 13 import java.io.FileInputStream ; 14 import java.io.IOException ; 15 import java.io.File ; 16 17 import org.apache.commons.beanutils.PropertyUtils; 18 19 import za.co.csir.icomtek.workflow.factories.WorkflowFinderFactory; 20 import za.co.csir.icomtek.workflow.handlers.ActionHandler; 21 import za.co.csir.icomtek.workflow.handlers.ComparatorHandler; 22 import za.co.csir.icomtek.workflow.handlers.RulesHandler; 23 import za.co.csir.icomtek.workflow.interfaces.WorkflowContext; 24 import za.co.csir.icomtek.workflow.model.*; 25 26 public class WorkflowEvaluator { 27 28 private static HashMap workflows = new HashMap (); 29 30 static { 31 try { 32 List flows = WorkflowFinderFactory.instance() 33 .getWorkflowFinder().getAllWorkflows(); 34 for (Iterator it = flows.iterator(); it.hasNext(); ) { 35 Workflow pw = (Workflow)it.next(); 36 workflows.put(pw.getName(), pw); 37 try { 38 ComparatorHandler.registerComparators(pw.getComparatorAsReference()); 39 } catch (Exception e) { 40 e.printStackTrace(); 41 throw new RuntimeException ("The comparator does not exist"); 42 } 43 } 44 } catch (Exception e) { 45 System.err.println("<< constructor, unable to load the xml"); 46 e.printStackTrace(); 47 } 48 } 49 50 53 public static Collection getAllRegisteredWorkflows () { 54 return workflows.values(); 55 } 56 57 65 public static Workflow getWorkflow (String workflowName) { 66 return (Workflow)workflows.get(workflowName); 67 } 68 69 76 public static State getDefaultStateForWorkflow (String workflowName) { 77 State retVal = null; 78 Workflow pw = getWorkflow(workflowName); 79 if (pw != null) { 80 for (Enumeration e = pw.enumerateState(); e.hasMoreElements(); ) { 81 State state = (State)e.nextElement(); 82 if (state.getStartState()) { 83 retVal = state; 84 } 85 } 86 } 87 return retVal; 88 } 89 90 99 public static State getState (String workflowName, String stateName) 100 throws Exception 101 { 102 return retrieveStateByName(stateName, workflowName); 103 } 104 105 114 public static State determineState (WorkflowContext ctx, WorkflowData wd) 115 throws Exception 116 { 117 State currentState = null; 118 State nextState = null; 119 boolean transitioning = false; 120 if (wd != null && wd.getCurrentState() == null 121 && wd.getWorkflowName() != null) { 122 nextState = transition(ctx, null, wd); 124 } else { 125 currentState = retrieveStateByName(wd.getCurrentState(), 126 wd.getWorkflowName()); 127 if (!currentState.hasStopState()) { 128 for (Enumeration e = currentState.enumerateStateTransition(); 130 e.hasMoreElements() && !transitioning; ) { 131 StateTransition st = (StateTransition)e.nextElement(); 132 transitioning = RulesHandler.evaluateRules(st.getRules(), 133 ctx); 134 if (transitioning) { 135 nextState = transition(ctx, st, wd); 136 } 137 } 138 } 139 } 140 return (nextState == null) ? currentState : nextState; 141 } 142 143 public static boolean isModuleViewableForState (WorkflowContext ctx, 144 WorkflowData wd, 145 String moduleName) 146 throws Exception 147 { 148 boolean retVal = false; 149 Module currentModule = new Module(); 150 currentModule.setName(moduleName); 151 if (wd != null) { 152 State state = retrieveStateByName(wd.getCurrentState().trim(), 153 wd.getWorkflowName().trim()); 154 if (state != null) { 155 retVal = state.getVisibleModules() 156 .getModuleAsReference().contains(currentModule); 157 } 158 } 159 return retVal; 160 } 161 162 public static void registerWorkflow (Workflow pw) { 163 workflows.put(pw.getName(), pw); 164 } 165 166 public static void deregisterWorkflow (String workflowName) { 167 workflows.remove(workflowName); 168 } 169 170 private static State retrieveStateByName(String stateName, 171 String workflowName) 172 { 173 State retVal = null; 174 System.err.println("<< workflows size: "+workflows.size()); 175 Workflow pw = 176 (Workflow)workflows.get(workflowName); 177 if (pw != null) { 178 for (Enumeration e = pw.enumerateState(); e.hasMoreElements(); ) { 179 State state = (State)e.nextElement(); 180 if (stateName == null && state.hasStartState()) { 181 retVal = state; 182 break; 183 } else if (state.getName().equals(stateName)) { 184 retVal = state; 185 break; 186 } 187 } 188 if (retVal == null) { 189 throw new RuntimeException ("The state, " + stateName 190 + "does not exist for " 191 + "project workflow, " 192 + pw.getName()); 193 } 194 } else { 195 throw new RuntimeException ("workflow, " 196 + workflowName + 197 " does not exist"); 198 } 199 return retVal; 200 } 201 202 207 private static byte[] getBytesFromFile(File file) throws IOException { 208 209 InputStream is = new FileInputStream (file); 210 System.out.println("\nDEBUG: FileInputStream is " + file); 211 212 long length = file.length(); 214 System.out.println("DEBUG: Length of " + file + " is " + length + "\n"); 215 216 221 if (length > Integer.MAX_VALUE) { 222 System.out.println("File is too large to process"); 223 return null; 224 } 225 226 byte[] bytes = new byte[(int)length]; 228 229 int offset = 0; 231 int numRead = 0; 232 while ( (offset < bytes.length) 233 && 234 ( (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) ) { 235 236 offset += numRead; 237 238 } 239 240 if (offset < bytes.length) { 242 throw new IOException ("Could not completely read file " + file.getName()); 243 } 244 245 is.close(); 246 return bytes; 247 248 } 249 250 253 public static State transition (WorkflowContext ctx, StateTransition st, 254 WorkflowData wd) 255 throws Exception 256 { 257 return transition(ctx, st, wd, true); 258 } 259 260 264 public static State transition (WorkflowContext ctx, StateTransition st, 265 WorkflowData wd, boolean performActions) 266 throws Exception 267 { 268 State nextState = null; 269 if (st == null) { 270 nextState = retrieveStateByName(null, wd.getWorkflowName()); 271 } else { 272 nextState = retrieveStateByName(st.getDestination(), 273 wd.getWorkflowName()); 274 } 275 System.err.println("<< transitioning to state: " 276 + nextState.getName()); 277 wd.setCurrentState(nextState.getName()); 278 wd.setCurrentStateDescription(nextState.getDescription()); 279 280 if (st != null) { 281 if (performActions) { 283 ActionHandler.performActions(ctx, st); 284 } 285 } 286 287 if (performActions) { 289 ActionHandler.performActions(ctx, nextState); 290 } 291 292 return determineState(ctx, wd); 294 } 295 296 } 297 | Popular Tags |