1 23 24 package org.infoglue.cms.util.workflow; 25 26 import java.io.ByteArrayInputStream ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import org.infoglue.cms.controllers.kernel.impl.simple.WorkflowDefinitionController; 33 import org.infoglue.cms.entities.workflow.WorkflowDefinitionVO; 34 import org.infoglue.cms.util.CmsPropertyHandler; 35 import org.infoglue.deliver.util.CacheController; 36 37 import com.opensymphony.workflow.FactoryException; 38 import com.opensymphony.workflow.InvalidWorkflowDescriptorException; 39 import com.opensymphony.workflow.loader.AbstractWorkflowFactory; 40 import com.opensymphony.workflow.loader.WorkflowDescriptor; 41 import com.opensymphony.workflow.loader.WorkflowLoader; 42 43 44 47 48 public class InfoGlueWorkflowFactory extends AbstractWorkflowFactory 49 { 50 protected boolean reload; 52 53 public void setLayout(String workflowName, Object layout) 54 { 55 } 56 57 public Object getLayout(String workflowName) 58 { 59 return null; 60 } 61 62 public boolean removeWorkflow(String name) throws FactoryException 63 { 64 throw new FactoryException("remove workflow not supported"); 65 } 66 67 public void renameWorkflow(String oldName, String newName) 68 { 69 } 70 71 public void save() 72 { 73 } 74 75 public boolean isModifiable(String name) 76 { 77 return true; 78 } 79 80 public String getName() 81 { 82 return ""; 83 } 84 85 public WorkflowDescriptor getWorkflow(String name, boolean validate) throws FactoryException 86 { 87 Map workflows = (Map )CacheController.getCachedObject("workflowCache", "workflowMap"); 88 89 if(workflows == null) 90 initDone(); 91 92 workflows = (Map )CacheController.getCachedObject("workflowCache", "workflowMap"); 93 94 WorkflowConfig c = (WorkflowConfig) workflows.get(name); 95 96 if (c == null) { 97 throw new FactoryException("Unknown workflow name \"" + name + "\""); 98 } 99 100 if (c.descriptor != null) 101 { 102 loadWorkflow(c, validate); 103 } 104 else 105 { 106 loadWorkflow(c, validate); 107 } 108 109 c.descriptor.setName(name); 110 111 return c.descriptor; 112 } 113 114 public void reload() throws FactoryException 115 { 116 initDone(); 117 } 118 119 public String [] getWorkflowNames() 120 { 121 Map workflows = (Map )CacheController.getCachedObject("workflowCache", "workflowMap"); 122 123 if(workflows == null) 124 { 125 try 126 { 127 initDone(); 128 } 129 catch (FactoryException e) 130 { 131 e.printStackTrace(); 132 } 133 } 134 135 workflows = (Map )CacheController.getCachedObject("workflowCache", "workflowMap"); 136 137 int i = 0; 138 String [] res = new String [workflows.keySet().size()]; 139 Iterator it = workflows.keySet().iterator(); 140 141 while (it.hasNext()) 142 { 143 res[i++] = (String ) it.next(); 144 } 145 146 return res; 147 } 148 149 public void createWorkflow(String name) 150 { 151 try 152 { 153 initDone(); 154 } 155 catch(Exception e) 156 { 157 e.printStackTrace(); 158 } 159 } 160 161 public void initDone() throws FactoryException 162 { 163 try 164 { 165 Map workflows = new HashMap (); 166 167 List list = WorkflowDefinitionController.getController().getWorkflowDefinitionVOList(); 168 169 Iterator listIterator = list.iterator(); 170 while(listIterator.hasNext()) 171 { 172 WorkflowDefinitionVO workflowDefinitionVO = (WorkflowDefinitionVO)listIterator.next(); 173 WorkflowConfig config = new WorkflowConfig(workflowDefinitionVO); 174 workflows.put(workflowDefinitionVO.getName(), config); 175 } 176 177 CacheController.cacheObject("workflowCache", "workflowMap", workflows); 178 } 179 catch (Exception e) 180 { 181 throw new InvalidWorkflowDescriptorException("Error in workflow config", e); 182 } 183 184 258 } 259 260 261 public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException 262 { 263 throw new FactoryException("Not supported..."); 264 } 265 266 267 private void loadWorkflow(WorkflowConfig c, boolean validate) throws FactoryException 268 { 269 try 270 { 271 String encoding = CmsPropertyHandler.getWorkflowEncoding(); 272 if(encoding == null || encoding.length() == 0 || encoding.equalsIgnoreCase("@workflowEncoding@")) 273 encoding = "UTF-8"; 274 275 c.descriptor = WorkflowLoader.load(new ByteArrayInputStream (c.workflowDefinitionVO.getValue().getBytes(encoding)) , validate); 278 } 279 catch (Exception e) 280 { 281 throw new FactoryException("Error in workflow descriptor: " + c.workflowDefinitionVO.getName(), e); 282 } 283 } 284 285 287 static class WorkflowConfig 288 { 289 WorkflowDescriptor descriptor; 290 WorkflowDefinitionVO workflowDefinitionVO; 291 292 public WorkflowConfig(WorkflowDefinitionVO workflowDefinitionVO) 293 { 294 this.workflowDefinitionVO = workflowDefinitionVO; 295 } 296 } 297 298 539 540 } | Popular Tags |