1 package com.opensymphony.workflow.loader; 2 3 import java.io.*; 4 import java.util.*; 5 import java.net.URL ; 6 import java.net.MalformedURLException ; 7 8 import com.opensymphony.workflow.FactoryException; 9 import com.opensymphony.workflow.designer.Layout; 10 import com.opensymphony.workflow.designer.Prefs; 11 import com.opensymphony.workflow.designer.ResourceManager; 12 13 18 public class Workspace extends XMLWorkflowFactory 19 { 20 private Map layouts = new HashMap(); 21 private File workflowsXML; 22 private File configFile; 23 24 public Workspace() 25 { 26 workflows = new HashMap(); 27 } 28 29 public void initDone() throws FactoryException 30 { 31 String name = getProperties().getProperty("resource", null); 32 if(name!=null) name = name.replace('\\', '/'); 33 if(name!=null && name.indexOf('/')==-1) 34 { 35 try 37 { 38 configFile = new File(new URL (Prefs.INSTANCE.get(Prefs.LAST_WORKSPACE, null)).getFile()); 39 } 40 catch(MalformedURLException e) 41 { 42 throw new FactoryException(e); 43 } 44 try 45 { 46 workflowsXML = new File(configFile.getParentFile(), name); 47 getProperties().setProperty("resource", workflowsXML.toURL().toString()); 48 } 49 catch(MalformedURLException e) 50 { 51 e.printStackTrace(); 52 } 53 } 54 super.initDone(); 55 String dir = workflowsXML.getParent(); 56 Iterator iter = workflows.values().iterator(); 57 while(iter.hasNext()) 58 { 59 WorkflowConfig config = (WorkflowConfig)iter.next(); 60 if(config.location.indexOf('/')==-1 && config.location.indexOf('\\')==-1) 61 { 62 try 63 { 64 File file = new File(dir, config.location); 65 config.url = file.toURL(); 66 if(file.exists()) 67 config.lastModified = file.lastModified(); 68 } 69 catch(MalformedURLException e) 70 { 71 e.printStackTrace(); 72 } 73 } 74 } 75 iter = workflows.entrySet().iterator(); 77 while(iter.hasNext()) 78 { 79 Map.Entry entry = (Map.Entry)iter.next(); 80 String workflowName = entry.getKey().toString(); 81 WorkflowConfig config = (WorkflowConfig)entry.getValue(); 82 String layoutUrl = getLayoutURL(config, workflowName); 83 layouts.put(workflowName, layoutUrl); 84 } 85 } 86 87 private String getLayoutURL(XMLWorkflowFactory.WorkflowConfig config, String workflowName) 88 { 89 try 90 { 91 if(config.url==null) config.url = new File(workflowsXML.getParentFile(), workflowName + ".xml").toURL(); 92 } 93 catch(MalformedURLException e) 94 { 95 e.printStackTrace(); 96 } 97 String layoutUrl = config.url.toString(); 98 layoutUrl = layoutUrl.substring(0, layoutUrl.lastIndexOf('/')+1); 99 layoutUrl = layoutUrl + workflowName + ".lyt"; 100 return layoutUrl; 101 } 102 103 public String getName() 104 { 105 return configFile!=null ? configFile.getName().substring(0, configFile.getName().lastIndexOf('.')) : "<new>"; 106 } 107 108 public boolean isNew() 109 { 110 return workflowsXML==null; 111 } 112 113 public void importDescriptor(String name, InputStream is) 114 { 115 WorkflowConfig config = new WorkflowConfig("file", name + ".xml"); 116 try 117 { 118 File file = new File(workflowsXML.getParentFile(), name+".xml"); 119 config.url = file.toURL(); 120 config.lastModified = file.lastModified(); 121 workflows.put(name, config); 122 } 123 catch(MalformedURLException e) 124 { 125 e.printStackTrace(); 127 } 128 } 129 130 public Layout getLayout(String workflowName) 131 { 132 Object obj = layouts.get(workflowName); 133 if(obj==null) return null; 134 if(obj instanceof Layout) 135 return (Layout)obj; 136 String url = obj.toString(); 137 try 138 { 139 InputStream is = new URL (url).openStream(); 140 if(is!=null) 141 { 142 Layout layout = new Layout(is); 143 layout.setUrl(url); 144 layouts.put(workflowName, layout); 145 return layout; 146 } 147 } 148 catch(FileNotFoundException ex) 149 { 150 } 152 catch(java.io.IOException e) 153 { 154 e.printStackTrace(); 155 } 156 return null; 157 } 158 159 public void save() 160 { 161 if(workflowsXML==null) 162 { 163 return; 164 } 165 try 166 { 167 if(!configFile.exists()) 168 { 169 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(configFile))); 171 out.println("<osworkflow>"); 172 out.println(" <persistence class=\"com.opensymphony.workflow.spi.memory.MemoryWorkflowStore\"/>"); 173 out.println(" <factory class=\"com.opensymphony.workflow.loader.Workspace\">"); 174 out.println(" <property key=\"resource\" value=\"workflows.xml\" />"); 175 out.println(" </factory>"); 176 out.println("</osworkflow>"); 177 out.flush(); 178 out.close(); 179 } 180 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(workflowsXML))); 181 out.println("<workflows>"); 182 Iterator iter = workflows.entrySet().iterator(); 183 while(iter.hasNext()) 184 { 185 Map.Entry entry = (Map.Entry)iter.next(); 186 WorkflowConfig config = (WorkflowConfig)entry.getValue(); 187 if(config.location==null) config.location = entry.getKey() + ".xml"; 188 out.println(" <workflow name=\"" + entry.getKey() + "\" type=\"file\" location=\"" + config.location + "\" />"); 189 } 190 out.println("</workflows>"); 191 out.flush(); 192 out.close(); 193 } 194 catch(IOException e) 195 { 196 e.printStackTrace(); 197 } 198 } 199 200 public boolean removeWorkflow(String name) throws FactoryException 201 { 202 WorkflowConfig removed = (WorkflowConfig)workflows.remove(name); 203 save(); 204 if(removed.url != null && removed.url.getProtocol().equals("file")) 205 { 206 return new File(removed.url.getFile()).delete(); 207 } 208 return removed != null; 209 } 210 211 public WorkflowDescriptor getWorkflow(String name) throws FactoryException 212 { 213 WorkflowConfig config = (WorkflowConfig)workflows.get(name); 214 if(config==null) return null; 215 if(config.url ==null) 216 { 217 return config.descriptor; 218 } 219 return super.getWorkflow(name); 220 } 221 222 public boolean saveWorkflow(String name, WorkflowDescriptor descriptor, boolean replace) throws FactoryException 223 { 224 Object obj = layouts.get(name); 225 if(obj instanceof Layout) 226 { 227 Layout layout = (Layout)obj; 228 try 229 { 230 WorkflowConfig config = (WorkflowConfig)workflows.get(name); 231 URL url = new URL (getLayoutURL(config, name)); 232 File file = new File(url.getFile()); 233 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file))); 234 layout.writeXML(out, 0); 235 out.flush(); 236 out.close(); 237 if(config.location==null) 238 { 239 config.location = name + ".xml"; 240 config.url = new File(workflowsXML.getParentFile(), config.location).toURL(); 241 } 242 } 243 catch(IOException e) 244 { 245 e.printStackTrace(); 246 return false; 247 } 248 } 249 if(descriptor!=null) 250 { 251 WorkflowConfig config = (WorkflowConfig)workflows.get(name); 252 if(config.url!=null) 253 { 254 descriptor.getMetaAttributes().put("generator", "OSWOrkflow Designer"); 255 descriptor.getMetaAttributes().put("lastModified", new Date()); 256 return super.saveWorkflow(name, descriptor, replace); 257 } 258 else 259 { 260 System.out.println("WARN: *** saveWorkflow called with config.location=" + config.location + " url is null"); 261 } 262 } 263 return false; 264 } 265 266 public void setLayout(String workflowName, Layout layout) 267 { 268 layouts.put(workflowName, layout); 269 } 270 271 public File getLocation() 272 { 273 return workflowsXML; 274 } 275 276 public void setLocation(File file) 277 { 278 this.workflowsXML = new File(file.getParentFile(), "workflows.xml"); 279 this.configFile = file; 280 } 281 282 public void createWorkflow(String name) 283 { 284 WorkflowConfig config = new WorkflowConfig("file", null); 285 config.descriptor = new WorkflowDescriptor(); 286 ActionDescriptor initialAction = new ActionDescriptor(); 287 initialAction.setName(ResourceManager.getString("action.initial.start")); 288 config.descriptor.getInitialActions().add(initialAction); 289 config.descriptor.getMetaAttributes().put("created", new Date()); 290 workflows.put(name, config); 291 } 292 293 public void renameWorkflow(String oldName, String newName) 294 { 295 WorkflowConfig config = (WorkflowConfig)workflows.get(oldName); 297 config.location = newName + ".xml"; 298 try 299 { 300 config.url = new File(workflowsXML.getParentFile(), config.location).toURL(); 301 } 302 catch(MalformedURLException e) 303 { 304 e.printStackTrace(); 306 } 307 workflows.remove(oldName); 308 workflows.put(newName, config); 309 } 310 311 public void deleteWorkflow(String name) 312 { 313 WorkflowConfig config = (WorkflowConfig)workflows.remove(name); 314 } 315 } 316 | Popular Tags |