001: package com.opensymphony.workflow.loader;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: import com.opensymphony.workflow.FactoryException;
007: import com.opensymphony.workflow.designer.Layout;
008: import com.opensymphony.workflow.designer.ResourceManager;
009: import com.opensymphony.workflow.designer.WorkflowGraph;
010: import com.opensymphony.workflow.designer.DesignerService;
011:
012: /**
013: * @author Hani Suleiman (hani@formicary.net)
014: * Date: May 15, 2003
015: * Time: 7:56:36 PM
016: */
017: public class RemoteWorkspace extends HTTPWorkflowFactory {
018: private Map layouts = new HashMap();
019: private DesignerService service = null;
020:
021: public RemoteWorkspace(DesignerService service) {
022: this .service = service;
023: workflows = new HashMap();
024: }
025:
026: public boolean isModifiable(String name) {
027: return true;
028: }
029:
030: public void initDone() throws FactoryException {
031: super .initDone();
032:
033: Map flows = service.getWorkflows();
034: Iterator iter = flows.entrySet().iterator();
035: while (iter.hasNext()) {
036: Map.Entry entry = (Map.Entry) iter.next();
037: HTTPWorkflowConfig config = new HTTPWorkflowConfig(service
038: .getRemoteAddress(), entry.getValue().toString(),
039: entry.getKey().toString());
040: workflows.put(entry.getValue(), config);
041: }
042:
043: // now read in all layouts
044: iter = workflows.entrySet().iterator();
045: while (iter.hasNext()) {
046: Map.Entry entry = (Map.Entry) iter.next();
047: String workflowName = entry.getKey().toString();
048: HTTPWorkflowConfig config = (HTTPWorkflowConfig) entry
049: .getValue();
050: String docId = config.docId;
051: layouts.put(workflowName, docId);
052: }
053: }
054:
055: public String getName() {
056: return service.getWorkspaceName();
057: }
058:
059: public boolean isNew() {
060: //return workflowsXML==null;
061: return false;
062: }
063:
064: public void importDescriptor(String name, InputStream is) {
065: /*
066: WorkflowConfig config = new WorkflowConfig("file", name + ".xml");
067: try
068: {
069: File file = new File(workflowsXML.getParentFile(), name+".xml");
070: config.url = file.toURL();
071: config.lastModified = file.lastModified();
072: workflows.put(name, config);
073: }
074: catch(MalformedURLException e)
075: {
076: //can't really happen
077: e.printStackTrace();
078: }
079: */
080: }
081:
082: public Object getLayout(String workflowName) {
083: Object obj = layouts.get(workflowName);
084: if (obj == null)
085: return null;
086: if (obj instanceof Layout)
087: return (Layout) obj;
088: String docId = obj.toString();
089: try {
090: String layBuffer = readLayoutBuffer(service
091: .getRemoteAddress(), docId);
092: if (layBuffer != null) {
093: Layout layout = new Layout(layBuffer);
094: layouts.put(workflowName, layout);
095: return layout;
096: }
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100: return null;
101: }
102:
103: public void save() {
104: }
105:
106: public boolean removeWorkflow(String name) throws FactoryException {
107: HTTPWorkflowConfig removed = (HTTPWorkflowConfig) workflows
108: .remove(name);
109: save();
110: return removed != null;
111: }
112:
113: public WorkflowDescriptor getWorkflow(String name, boolean validate)
114: throws FactoryException {
115: HTTPWorkflowConfig config = (HTTPWorkflowConfig) workflows
116: .get(name);
117: if (config == null)
118: return null;
119:
120: if (config.descriptor == null) {
121: String docId = config.docId;
122: try {
123: String workflowBuffer = readWorkflowBuffer(service
124: .getRemoteAddress(), docId);
125: if (workflowBuffer != null) {
126: config.descriptor = WorkflowLoader.load(
127: new ByteArrayInputStream(workflowBuffer
128: .getBytes()), validate);
129: if (config.descriptor != null) {
130: /*
131: String metaName = (String)config.descriptor.getMetaAttributes().get("descr");
132: if ((metaName!=null)&&(metaName.length()>0))
133: name = metaName;
134: */
135: config.descriptor.setName(name);
136: }
137: return config.descriptor;
138: }
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142: return null;
143: }
144: return config.descriptor;
145: }
146:
147: public boolean saveWorkflow(String name,
148: WorkflowDescriptor descriptor, WorkflowGraph graph,
149: boolean replace) throws Exception {
150: String layoutBuffer = "";
151: String workflowBuffer;
152:
153: Object obj = layouts.get(name);
154: if (obj instanceof Layout) {
155: Layout layout = (Layout) obj;
156: try {
157: HTTPWorkflowConfig config = (HTTPWorkflowConfig) workflows
158: .get(name);
159:
160: StringWriter sw = new StringWriter();
161: PrintWriter out = new PrintWriter(
162: new BufferedWriter(sw));
163: //PrintWriter out = new PrintWriter(new ByteArrayOutputStream());
164: layout.writeXML(out, 0, graph);
165: out.flush();
166: out.close();
167: layoutBuffer = sw.getBuffer().toString();
168: //layoutBuffer = out.toString();
169: } catch (Exception e) {
170: e.printStackTrace();
171: return false;
172: }
173: }
174: if (descriptor != null) {
175: HTTPWorkflowConfig config = (HTTPWorkflowConfig) workflows
176: .get(name);
177: descriptor.getMetaAttributes().put("generator",
178: "OSWOrkflow Designer");
179: descriptor.getMetaAttributes().put("lastModified",
180: (new Date()).toString());
181: StringWriter sw = new StringWriter();
182: PrintWriter writer = new PrintWriter(new BufferedWriter(sw));
183: writer.println(WorkflowDescriptor.XML_HEADER);
184: writer.println(WorkflowDescriptor.DOCTYPE_DECL);
185: descriptor.writeXML(writer, 0);
186: writer.flush();
187: writer.close();
188: //workflowBuffer = writer.toString();
189: workflowBuffer = sw.getBuffer().toString();
190: String ret = writeWorkflowDescriptor(service
191: .getRemoteAddress(), config.docId, name,
192: workflowBuffer);
193: System.out.println("workflow ret = " + ret);
194: if (ret.length() == 0)
195: return false;
196: ret = writeWorkflowLayout(service.getRemoteAddress(),
197: config.docId, name, layoutBuffer);
198: System.out.println("layout ret = " + ret);
199: if (ret.length() == 0)
200: return false;
201: config.docId = ret; // set the new document ID
202: return true;
203: }
204: return false;
205: }
206:
207: public void setLayout(String workflowName, Object layout) {
208: layouts.put(workflowName, layout);
209: }
210:
211: /*
212: public String getLocation()
213: {
214: //return workflowsXML;
215: return null;
216: }
217:
218: public void setLocation(String service)
219: {
220: this.workflowsXML = new File(file.getParentFile(), "workflows.xml");
221: this.configFile = file;
222: }
223: */
224:
225: public void createWorkflow(String name) {
226: HTTPWorkflowConfig config = new HTTPWorkflowConfig(service
227: .getRemoteAddress(), name, "");
228: config.descriptor = new WorkflowDescriptor();
229: config.descriptor.setName(name);
230: ActionDescriptor initialAction = new ActionDescriptor();
231: initialAction.setName(ResourceManager
232: .getString("action.initial.start"));
233: config.descriptor.getInitialActions().add(initialAction);
234: config.descriptor.getMetaAttributes().put("created",
235: (new Date()).toString());
236: workflows.put(name, config);
237: }
238:
239: public void renameWorkflow(String oldName, String newName) {
240: //todo need to keep track of deleted workflows and delete their files on save
241: HTTPWorkflowConfig config = (HTTPWorkflowConfig) workflows
242: .get(oldName);
243: config.name = newName;
244: workflows.remove(oldName);
245: workflows.put(newName, config);
246: }
247: }
|