1 package org
.argeo
.slc
.web
.mvc
.process
;
3 import java
.io
.BufferedReader
;
4 import java
.util
.Comparator
;
6 import java
.util
.SortedSet
;
7 import java
.util
.TreeSet
;
10 import javax
.servlet
.http
.HttpServletRequest
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.core
.attachment
.AttachmentsStorage
;
15 import org
.argeo
.slc
.dao
.process
.SlcExecutionDao
;
16 import org
.argeo
.slc
.execution
.ExecutionModuleDescriptor
;
17 import org
.argeo
.slc
.msg
.ExecutionAnswer
;
18 import org
.argeo
.slc
.msg
.MsgConstants
;
19 import org
.argeo
.slc
.msg
.ObjectList
;
20 import org
.argeo
.slc
.process
.SlcExecution
;
21 import org
.argeo
.slc
.process
.SlcExecutionStep
;
22 import org
.argeo
.slc
.runtime
.SlcAgent
;
23 import org
.argeo
.slc
.runtime
.SlcAgentFactory
;
24 import org
.argeo
.slc
.services
.SlcExecutionService
;
25 import org
.springframework
.oxm
.Marshaller
;
26 import org
.springframework
.oxm
.Unmarshaller
;
27 import org
.springframework
.stereotype
.Controller
;
28 import org
.springframework
.ui
.Model
;
29 import org
.springframework
.util
.Assert
;
30 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
31 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
32 import org
.springframework
.xml
.transform
.StringSource
;
35 public class ExecutionServiceController
{
37 public final static String KEY_ANSWER
= "__answer";
38 private final static Log log
= LogFactory
39 .getLog(ExecutionServiceController
.class);
41 private SlcExecutionDao slcExecutionDao
;
42 private SlcAgentFactory agentFactory
;
43 private Unmarshaller unmarshaller
;
44 private Marshaller marshaller
;
45 private SlcExecutionService slcExecutionService
;
46 private AttachmentsStorage attachmentsStorage
;
48 private SlcExecutionManager slcExecutionManager
;
50 @RequestMapping("/listSlcExecutions.service")
51 protected ObjectList
listSlcExecutions() {
52 List
<SlcExecution
> list
= slcExecutionDao
.listSlcExecutions();
53 return new ObjectList(list
);
56 @RequestMapping("/getExecutionDescriptor.service")
57 protected ExecutionModuleDescriptor
getExecutionDescriptor(
58 @RequestParam String agentId
, @RequestParam String moduleName
,
59 @RequestParam String version
) {
61 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
63 ExecutionModuleDescriptor md
= slcAgent
.getExecutionModuleDescriptor(
68 @RequestMapping("/listModulesDescriptors.service")
69 protected ObjectList
listModulesDescriptors(@RequestParam String agentId
) {
70 // TODO: use centralized agentId property (from MsgConstants)?
71 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
73 List
<ExecutionModuleDescriptor
> descriptors
= slcAgent
74 .listExecutionModuleDescriptors();
75 SortedSet
<ExecutionModuleDescriptor
> set
= new TreeSet
<ExecutionModuleDescriptor
>(
76 new Comparator
<ExecutionModuleDescriptor
>() {
78 public int compare(ExecutionModuleDescriptor md1
,
79 ExecutionModuleDescriptor md2
) {
80 String str1
= md1
.getLabel() != null ? md1
.getLabel()
82 String str2
= md2
.getLabel() != null ? md2
.getLabel()
84 return str1
.compareTo(str2
);
87 set
.addAll(descriptors
);
88 return new ObjectList(set
);
91 @RequestMapping("/getSlcExecution.service")
92 protected SlcExecution
getSlcExecution(@RequestParam String uuid
) {
93 SlcExecution slcExecution
= slcExecutionDao
.getSlcExecution(uuid
);
95 slcExecutionManager
.retrieveRealizedFlows(slcExecution
);
99 @RequestMapping("/newSlcExecution.service")
100 protected ExecutionAnswer
newSlcExecution(HttpServletRequest request
,
101 Model model
) throws Exception
{
103 String agentId
= request
104 .getParameter(MsgConstants
.PROPERTY_SLC_AGENT_ID
);
105 Assert
.notNull(agentId
, "agent id");
107 String answer
= request
.getParameter("body");
108 if (answer
== null) {
109 // lets read the message body instead
110 BufferedReader reader
= request
.getReader();
111 StringBuffer buffer
= new StringBuffer();
113 while (((line
= reader
.readLine()) != null)) {
116 answer
= buffer
.toString();
119 if (log
.isTraceEnabled())
120 log
.debug("Received message:\n" + answer
);
122 StringSource source
= new StringSource(answer
);
123 SlcExecution slcExecution
= (SlcExecution
) unmarshaller
126 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
127 if (slcExecution
.getUuid() == null
128 || slcExecution
.getUuid().length() < 8)
129 slcExecution
.setUuid(UUID
.randomUUID().toString());
131 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
132 slcExecution
.getSteps().add(
133 new SlcExecutionStep(SlcExecutionStep
.TYPE_START
,
134 "Process started from the Web UI"));
137 slcExecutionManager
.storeRealizedFlows(slcExecution
);
138 slcExecutionService
.newExecution(slcExecution
);
139 SlcAgent agent
= agentFactory
.getAgent(agentId
);
140 agent
.runSlcExecution(slcExecution
);
142 return ExecutionAnswer
.ok("Execution completed properly");
145 private void initializeSEM() {
146 slcExecutionManager
= new SlcExecutionManager(agentFactory
,
147 unmarshaller
, marshaller
, slcExecutionService
,
152 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao
) {
153 this.slcExecutionDao
= slcExecutionDao
;
156 public void setSlcExecutionService(SlcExecutionService slcExecutionService
) {
157 this.slcExecutionService
= slcExecutionService
;
160 public void setUnmarshaller(Unmarshaller unmarshaller
) {
161 this.unmarshaller
= unmarshaller
;
164 public void setMarshaller(Marshaller marshaller
) {
165 this.marshaller
= marshaller
;
168 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
169 this.attachmentsStorage
= attachmentsStorage
;
172 public void setAgentFactory(SlcAgentFactory agentFactory
) {
173 this.agentFactory
= agentFactory
;