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
.web
.servlet
.ModelAndView
;
33 import org
.springframework
.xml
.transform
.StringSource
;
36 public class ExecutionServiceController
{
38 public final static String KEY_ANSWER
= "__answer";
39 private final static Log log
= LogFactory
40 .getLog(ExecutionServiceController
.class);
42 private SlcExecutionDao slcExecutionDao
;
43 private SlcAgentFactory agentFactory
;
44 private Unmarshaller unmarshaller
;
45 private Marshaller marshaller
;
46 private SlcExecutionService slcExecutionService
;
47 private AttachmentsStorage attachmentsStorage
;
49 private SlcExecutionManager slcExecutionManager
;
51 @RequestMapping("/listSlcExecutions.service")
52 protected String
listSlcExecutions(Model model
) {
54 if (log
.isDebugEnabled())
55 log
.debug("In SlcServiceController :: listSlcExecutions.service");
57 List
<SlcExecution
> list
= slcExecutionDao
.listSlcExecutions();
58 model
.addAttribute("list", new ObjectList(list
));
62 @RequestMapping("/getExecutionDescriptor.service")
63 protected String
getExecutionDescriptor(@RequestParam String agentId
,
64 @RequestParam String moduleName
, @RequestParam String version
,
67 if (log
.isDebugEnabled())
68 log
.debug(":: SlcServiceController :: getExecutionDescriptor");
70 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
72 ExecutionModuleDescriptor md
= slcAgent
.getExecutionModuleDescriptor(
74 model
.addAttribute(md
);
75 return "executionModuleDescriptor";
78 @RequestMapping("/listModulesDescriptors.service")
79 protected String
listModulesDescriptors(@RequestParam String agentId
,
82 if (log
.isDebugEnabled())
83 log
.debug(":: SlcServiceController :: listModulesDescriptors");
85 // TODO: use centralized agentId property (from MsgConstants)?
86 SlcAgent slcAgent
= agentFactory
.getAgent(agentId
);
88 List
<ExecutionModuleDescriptor
> descriptors
= slcAgent
89 .listExecutionModuleDescriptors();
90 SortedSet
<ExecutionModuleDescriptor
> set
= new TreeSet
<ExecutionModuleDescriptor
>(
91 new Comparator
<ExecutionModuleDescriptor
>() {
93 public int compare(ExecutionModuleDescriptor md1
,
94 ExecutionModuleDescriptor md2
) {
95 String str1
= md1
.getLabel() != null ? md1
.getLabel()
97 String str2
= md2
.getLabel() != null ? md2
.getLabel()
99 return str1
.compareTo(str2
);
102 set
.addAll(descriptors
);
104 model
.addAttribute(new ObjectList(set
));
108 @RequestMapping("/getSlcExecution.service")
109 protected void getSlcExecution(@RequestParam String uuid
,
110 ModelAndView modelAndView
) {
111 if (log
.isDebugEnabled())
112 log
.debug("In ExecutionServiceController :: getSlcExecution.service");
114 SlcExecution slcExecution
= slcExecutionDao
.getSlcExecution(uuid
);
117 slcExecutionManager
.retrieveRealizedFlows(slcExecution
);
118 modelAndView
.addObject(slcExecution
);
119 modelAndView
.addObject(KEY_ANSWER
, ExecutionAnswer
120 .ok("Execution completed properly"));
123 @RequestMapping("/newSlcExecution.service")
124 protected String
newSlcExecution(HttpServletRequest request
,
125 Model model
) throws Exception
{
127 if (log
.isDebugEnabled())
128 log
.debug("In ExecutionServiceController :: newSlcExecution.service");
130 String agentId
= request
131 .getParameter(MsgConstants
.PROPERTY_SLC_AGENT_ID
);
132 Assert
.notNull(agentId
, "agent id");
134 String answer
= request
.getParameter("body");
135 if (answer
== null) {
136 // lets read the message body instead
137 BufferedReader reader
= request
.getReader();
138 StringBuffer buffer
= new StringBuffer();
140 while (((line
= reader
.readLine()) != null)) {
143 answer
= buffer
.toString();
146 if (log
.isTraceEnabled())
147 log
.debug("Received message:\n" + answer
);
149 StringSource source
= new StringSource(answer
);
150 SlcExecution slcExecution
= (SlcExecution
) unmarshaller
153 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
154 if (slcExecution
.getUuid() == null
155 || slcExecution
.getUuid().length() < 8)
156 slcExecution
.setUuid(UUID
.randomUUID().toString());
158 slcExecution
.setStatus(SlcExecution
.STATUS_SCHEDULED
);
159 slcExecution
.getSteps().add(
160 new SlcExecutionStep(SlcExecutionStep
.TYPE_START
,
161 "Process started from the Web UI"));
164 slcExecutionManager
.storeRealizedFlows(slcExecution
);
165 slcExecutionService
.newExecution(slcExecution
);
166 SlcAgent agent
= agentFactory
.getAgent(agentId
);
167 agent
.runSlcExecution(slcExecution
);
169 log
.debug("After Everything has been done !");
171 model
.addAttribute(KEY_ANSWER
, ExecutionAnswer
172 .ok("Execution completed properly"));
176 private void initializeSEM() {
177 slcExecutionManager
= new SlcExecutionManager(agentFactory
,
178 unmarshaller
, marshaller
, slcExecutionService
,
183 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao
) {
184 this.slcExecutionDao
= slcExecutionDao
;
187 public void setSlcExecutionService(SlcExecutionService slcExecutionService
) {
188 this.slcExecutionService
= slcExecutionService
;
191 public void setUnmarshaller(Unmarshaller unmarshaller
) {
192 this.unmarshaller
= unmarshaller
;
195 public void setMarshaller(Marshaller marshaller
) {
196 this.marshaller
= marshaller
;
199 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
200 this.attachmentsStorage
= attachmentsStorage
;
203 public void setAgentFactory(SlcAgentFactory agentFactory
) {
204 this.agentFactory
= agentFactory
;