]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/process/NewSlcExecutionController.java
Introduction of annotation to handle MVC flows
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / process / NewSlcExecutionController.java
1 package org.argeo.slc.web.mvc.process;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.InputStream;
6 import java.util.UUID;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.apache.commons.io.IOUtils;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 import org.argeo.slc.core.attachment.Attachment;
15 import org.argeo.slc.core.attachment.AttachmentsStorage;
16 import org.argeo.slc.core.attachment.SimpleAttachment;
17 import org.argeo.slc.msg.MsgConstants;
18 import org.argeo.slc.msg.ObjectList;
19 import org.argeo.slc.process.SlcExecution;
20 import org.argeo.slc.process.SlcExecutionStep;
21 import org.argeo.slc.runtime.SlcAgent;
22 import org.argeo.slc.runtime.SlcAgentFactory;
23 import org.argeo.slc.services.SlcExecutionService;
24 import org.argeo.slc.web.mvc.AbstractServiceController;
25 import org.springframework.oxm.Marshaller;
26 import org.springframework.oxm.Unmarshaller;
27 import org.springframework.util.Assert;
28 import org.springframework.web.servlet.ModelAndView;
29 import org.springframework.xml.transform.StringResult;
30 import org.springframework.xml.transform.StringSource;
31
32 /** Send a new SlcExecution. */
33 public class NewSlcExecutionController extends AbstractServiceController {
34 private final static Log log = LogFactory
35 .getLog(NewSlcExecutionController.class);
36
37 private SlcAgentFactory agentFactory;
38 private Unmarshaller unmarshaller;
39 private Marshaller marshaller;
40 private SlcExecutionService slcExecutionService;
41
42 private AttachmentsStorage attachmentsStorage;
43
44 @Override
45 protected void handleServiceRequest(HttpServletRequest request,
46 HttpServletResponse response, ModelAndView modelAndView)
47 throws Exception {
48
49 if (log.isTraceEnabled()) {
50 log.debug("Content-Type: " + request.getContentType());
51 log.debug("Content-Length: " + request.getContentLength());
52 }
53
54 String agentId = request
55 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
56 Assert.notNull(agentId, "agent id");
57
58 String answer = request.getParameter("body");
59 if (answer == null) {
60 // lets read the message body instead
61 BufferedReader reader = request.getReader();
62 StringBuffer buffer = new StringBuffer();
63 String line = null;
64 while (((line = reader.readLine()) != null)) {
65 buffer.append(line);
66 }
67 answer = buffer.toString();
68 }
69
70 if (log.isTraceEnabled())
71 log.debug("Received message:\n" + answer);
72
73 StringSource source = new StringSource(answer);
74 SlcExecution slcExecution = (SlcExecution) unmarshaller
75 .unmarshal(source);
76
77 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
78 if (slcExecution.getUuid() == null
79 || slcExecution.getUuid().length() < 8)
80 slcExecution.setUuid(UUID.randomUUID().toString());
81
82 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
83 slcExecution.getSteps().add(
84 new SlcExecutionStep(SlcExecutionStep.TYPE_START,
85 "Process started from the Web UI"));
86
87 // ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
88 // StringResult result = new StringResult();
89 // marshaller.marshal(ol, result);
90 // slcExecution.setRealizedFlowsXml(result.toString());
91 storeRealizedFlows(slcExecution);
92
93 slcExecutionService.newExecution(slcExecution);
94
95 SlcAgent agent = agentFactory.getAgent(agentId);
96 agent.runSlcExecution(slcExecution);
97 }
98
99 protected void storeRealizedFlows(SlcExecution slcExecution) {
100 Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()
101 .toString(), slcExecution);
102 InputStream in = null;
103 try {
104
105 ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());
106 StringResult result = new StringResult();
107 marshaller.marshal(ol, result);
108
109 in = new ByteArrayInputStream(result.toString().getBytes());
110 attachmentsStorage.storeAttachment(attachment, in);
111
112 slcExecution.setRealizedFlowsXml(attachment.getUuid());
113
114 } catch (Exception e) {
115 log.error("Could not store realized flows as attachment #"
116 + attachment.getUuid(), e);
117 } finally {
118 IOUtils.closeQuietly(in);
119 }
120 }
121
122 public void setUnmarshaller(Unmarshaller unmarshaller) {
123 this.unmarshaller = unmarshaller;
124 }
125
126 public void setAgentFactory(SlcAgentFactory agentFactory) {
127 this.agentFactory = agentFactory;
128 }
129
130 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
131 this.slcExecutionService = slcExecutionService;
132 }
133
134 public void setMarshaller(Marshaller marshaller) {
135 this.marshaller = marshaller;
136 }
137
138 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
139 this.attachmentsStorage = attachmentsStorage;
140 }
141
142 /** Unify labelling in the package */
143 static Attachment realizedFlowsAttachment(String attachmentUuid,
144 SlcExecution slcExecution) {
145 return new SimpleAttachment(attachmentUuid,
146 "RealizedFlows of SlcExecution #" + slcExecution.getUuid(),
147 "text/xml");
148 }
149 }