]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/process/NewSlcExecutionController.java
Add license headers
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / process / NewSlcExecutionController.java
index 7b03f3b9bed0b4d7454bdd75dfae9eb659574250..4c6249a0a90d8a19da2d3cbae5a4b3ac360c79a8 100644 (file)
@@ -1,23 +1,48 @@
+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
 package org.argeo.slc.web.mvc.process;\r
 \r
 import java.io.BufferedReader;\r
+import java.io.ByteArrayInputStream;\r
+import java.io.InputStream;\r
 import java.util.UUID;\r
 \r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
 \r
+import org.apache.commons.io.IOUtils;\r
 import org.apache.commons.logging.Log;\r
 import org.apache.commons.logging.LogFactory;\r
+import org.argeo.slc.core.attachment.Attachment;\r
+import org.argeo.slc.core.attachment.AttachmentsStorage;\r
+import org.argeo.slc.core.attachment.SimpleAttachment;\r
 import org.argeo.slc.msg.MsgConstants;\r
+import org.argeo.slc.msg.ObjectList;\r
 import org.argeo.slc.process.SlcExecution;\r
 import org.argeo.slc.process.SlcExecutionStep;\r
 import org.argeo.slc.runtime.SlcAgent;\r
 import org.argeo.slc.runtime.SlcAgentFactory;\r
 import org.argeo.slc.services.SlcExecutionService;\r
 import org.argeo.slc.web.mvc.AbstractServiceController;\r
+import org.springframework.oxm.Marshaller;\r
 import org.springframework.oxm.Unmarshaller;\r
 import org.springframework.util.Assert;\r
 import org.springframework.web.servlet.ModelAndView;\r
+import org.springframework.xml.transform.StringResult;\r
 import org.springframework.xml.transform.StringSource;\r
 \r
 /** Send a new SlcExecution. */\r
@@ -27,8 +52,11 @@ public class NewSlcExecutionController extends AbstractServiceController {
 \r
        private SlcAgentFactory agentFactory;\r
        private Unmarshaller unmarshaller;\r
+       private Marshaller marshaller;\r
        private SlcExecutionService slcExecutionService;\r
 \r
+       private AttachmentsStorage attachmentsStorage;\r
+\r
        @Override\r
        protected void handleServiceRequest(HttpServletRequest request,\r
                        HttpServletResponse response, ModelAndView modelAndView)\r
@@ -71,12 +99,42 @@ public class NewSlcExecutionController extends AbstractServiceController {
                slcExecution.getSteps().add(\r
                                new SlcExecutionStep(SlcExecutionStep.TYPE_START,\r
                                                "Process started from the Web UI"));\r
+\r
+               // ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());\r
+               // StringResult result = new StringResult();\r
+               // marshaller.marshal(ol, result);\r
+               // slcExecution.setRealizedFlowsXml(result.toString());\r
+               storeRealizedFlows(slcExecution);\r
+\r
                slcExecutionService.newExecution(slcExecution);\r
 \r
                SlcAgent agent = agentFactory.getAgent(agentId);\r
                agent.runSlcExecution(slcExecution);\r
        }\r
 \r
+       protected void storeRealizedFlows(SlcExecution slcExecution) {\r
+               Attachment attachment = realizedFlowsAttachment(UUID.randomUUID()\r
+                               .toString(), slcExecution);\r
+               InputStream in = null;\r
+               try {\r
+\r
+                       ObjectList ol = new ObjectList(slcExecution.getRealizedFlows());\r
+                       StringResult result = new StringResult();\r
+                       marshaller.marshal(ol, result);\r
+\r
+                       in = new ByteArrayInputStream(result.toString().getBytes());\r
+                       attachmentsStorage.storeAttachment(attachment, in);\r
+\r
+                       slcExecution.setRealizedFlowsXml(attachment.getUuid());\r
+\r
+               } catch (Exception e) {\r
+                       log.error("Could not store realized flows as attachment #"\r
+                                       + attachment.getUuid(), e);\r
+               } finally {\r
+                       IOUtils.closeQuietly(in);\r
+               }\r
+       }\r
+\r
        public void setUnmarshaller(Unmarshaller unmarshaller) {\r
                this.unmarshaller = unmarshaller;\r
        }\r
@@ -89,4 +147,19 @@ public class NewSlcExecutionController extends AbstractServiceController {
                this.slcExecutionService = slcExecutionService;\r
        }\r
 \r
+       public void setMarshaller(Marshaller marshaller) {\r
+               this.marshaller = marshaller;\r
+       }\r
+\r
+       public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {\r
+               this.attachmentsStorage = attachmentsStorage;\r
+       }\r
+\r
+       /** Unify labelling in the package */\r
+       static Attachment realizedFlowsAttachment(String attachmentUuid,\r
+                       SlcExecution slcExecution) {\r
+               return new SimpleAttachment(attachmentUuid,\r
+                               "RealizedFlows of SlcExecution #" + slcExecution.getUuid(),\r
+                               "text/xml");\r
+       }\r
 }\r