1 package org.weda.workflow.impl;
2
3 import java.io.Serializable;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Stack;
7 import org.weda.action.ActionRegistry;
8 import org.weda.cache.CacheEntity;
9 import org.weda.cache.CacheManager;
10 import org.weda.cache.CacheService;
11 import org.weda.action.ExecutedActionInfo;
12 import org.weda.workflow.EntryException;
13 import org.weda.workflow.Path;
14 import org.weda.workflow.PathAdapter;
15 import org.weda.workflow.Workflow;
16 import org.weda.workflow.WorkflowException;
17
18 /**
19 *
20 * @author tim
21 */
22 public class WorkflowImpl
23 extends BasicEntryContainer<PageEntry>
24 implements Workflow
25 {
26 private final static String ID=PathStackEntity.class.getName();
27 private CacheManager cacheManager;
28 private ActionRegistry actionRegistry;
29 private PathAdapter pathAdapter;
30 private List<PageEntry> pageEntries;
31 private boolean usePathAdapter = true;
32
33 public void init() throws EntryException {
34 try{
35 if (pageEntries!=null)
36 for (PageEntry entry: pageEntries){
37 PageEntry selfEntry = getEntryByName(entry.getName());
38 if (selfEntry==null)
39 getEntries().add(entry);
40 else
41 selfEntry.merge(entry);
42 }
43 setName("workflow");
44 setWorkflow(this);
45 super.init();
46 }catch(EntryException e){
47 throw new WorkflowException(
48 "Can't initialize workflow engine"
49 , e);
50 }
51 }
52
53 public Object getNextPage(String currentPageName, Map pathAdapterParams)
54 throws WorkflowException
55 {
56 try{
57 Stack<String> expressions = new Stack<String>();
58 ExecutedActionInfo actionInfo =
59 actionRegistry.getLastExecutedActionInfo();
60 Object actionResult = actionInfo.getActionResult();
61 expressions.push(
62 actionResult == null ? null : actionResult.toString());
63 expressions.push(
64 actionInfo.isSuccessExecution()?
65 ResultTypeEntry.SUCCESS_RESULT
66 : ResultTypeEntry.ERROR_RESULT);
67 expressions.push(actionInfo.getTargetName());
68 expressions.push(actionInfo.getTargetClass().getName());
69 expressions.push(actionInfo.getActionClass().getName());
70 expressions.push(currentPageName);
71 operate(expressions);
72 Path path = getPathStack().pop();
73 Object adaptedPath = path;
74 if (isUsePathAdapter())
75 adaptedPath = pathAdapter.adaptPath(path, pathAdapterParams);
76 return adaptedPath;
77 }catch(EntryException e){
78 throw new WorkflowException(
79 "Can't determine next page in workflow"
80 , e);
81 }
82 }
83
84 public void setCacheManager(CacheManager cacheManager) {
85 this.cacheManager = cacheManager;
86 }
87
88 public void pushPath(Path path){
89 getPathStack().push(path);
90 }
91
92 private Stack<Path> getPathStack(){
93 PathStackEntity entity =
94 (PathStackEntity)cacheManager.getCacheService().get(ID);
95 if (entity==null){
96 entity = new PathStackEntity();
97 cacheManager.getCacheService().setCacheEntity(ID, entity);
98 }
99 return entity.getStack();
100 }
101
102 public void setPageEntries(List<PageEntry> pageEntries) {
103 this.pageEntries = pageEntries;
104 }
105
106 private class PathStackEntity implements CacheEntity, Serializable{
107 private Stack<Path> stack = new Stack<Path>();
108
109 public void release() {
110 stack.clear();
111 }
112
113 public Object getValue() {
114 return stack;
115 }
116
117 public Stack<Path> getStack(){
118 return stack;
119 }
120 }
121
122 public void setActionRegistry(ActionRegistry actionRegistry) {
123 this.actionRegistry = actionRegistry;
124 }
125
126 public void setPathAdapter(PathAdapter pathAdapter) {
127 this.pathAdapter = pathAdapter;
128 }
129
130 public boolean isUsePathAdapter() {
131 return usePathAdapter;
132 }
133
134 public void setUsePathAdapter(boolean usePathAdapter) {
135 this.usePathAdapter = usePathAdapter;
136 }
137
138 public String getCurrentPage() {
139 return pathAdapter.getCurrentPage();
140 }
141
142 }