]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/process/ExecutionServiceController.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 / ExecutionServiceController.java
1 package org.argeo.slc.web.mvc.process;
2
3 import java.io.BufferedReader;
4 import java.util.Comparator;
5 import java.util.List;
6 import java.util.SortedSet;
7 import java.util.TreeSet;
8 import java.util.UUID;
9
10 import javax.servlet.http.HttpServletRequest;
11
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;
34
35 @Controller
36 public class ExecutionServiceController {
37
38 public final static String KEY_ANSWER = "__answer";
39 private final static Log log = LogFactory
40 .getLog(ExecutionServiceController.class);
41
42 private SlcExecutionDao slcExecutionDao;
43 private SlcAgentFactory agentFactory;
44 private Unmarshaller unmarshaller;
45 private Marshaller marshaller;
46 private SlcExecutionService slcExecutionService;
47 private AttachmentsStorage attachmentsStorage;
48
49 private SlcExecutionManager slcExecutionManager;
50
51 @RequestMapping("/listSlcExecutions.service")
52 protected String listSlcExecutions(Model model) {
53
54 if (log.isDebugEnabled())
55 log.debug("In SlcServiceController :: listSlcExecutions.service");
56
57 List<SlcExecution> list = slcExecutionDao.listSlcExecutions();
58 model.addAttribute("list", new ObjectList(list));
59 return "list";
60 }
61
62 @RequestMapping("/getExecutionDescriptor.service")
63 protected String getExecutionDescriptor(@RequestParam String agentId,
64 @RequestParam String moduleName, @RequestParam String version,
65 Model model) {
66
67 if (log.isDebugEnabled())
68 log.debug(":: SlcServiceController :: getExecutionDescriptor");
69
70 SlcAgent slcAgent = agentFactory.getAgent(agentId);
71
72 ExecutionModuleDescriptor md = slcAgent.getExecutionModuleDescriptor(
73 moduleName, version);
74 model.addAttribute(md);
75 return "executionModuleDescriptor";
76 }
77
78 @RequestMapping("/listModulesDescriptors.service")
79 protected String listModulesDescriptors(@RequestParam String agentId,
80 Model model) {
81
82 if (log.isDebugEnabled())
83 log.debug(":: SlcServiceController :: listModulesDescriptors");
84
85 // TODO: use centralized agentId property (from MsgConstants)?
86 SlcAgent slcAgent = agentFactory.getAgent(agentId);
87
88 List<ExecutionModuleDescriptor> descriptors = slcAgent
89 .listExecutionModuleDescriptors();
90 SortedSet<ExecutionModuleDescriptor> set = new TreeSet<ExecutionModuleDescriptor>(
91 new Comparator<ExecutionModuleDescriptor>() {
92
93 public int compare(ExecutionModuleDescriptor md1,
94 ExecutionModuleDescriptor md2) {
95 String str1 = md1.getLabel() != null ? md1.getLabel()
96 : md1.getName();
97 String str2 = md2.getLabel() != null ? md2.getLabel()
98 : md2.getName();
99 return str1.compareTo(str2);
100 }
101 });
102 set.addAll(descriptors);
103
104 model.addAttribute(new ObjectList(set));
105 return "objectList";
106 }
107
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");
113
114 SlcExecution slcExecution = slcExecutionDao.getSlcExecution(uuid);
115
116 initializeSEM();
117 slcExecutionManager.retrieveRealizedFlows(slcExecution);
118 modelAndView.addObject(slcExecution);
119 modelAndView.addObject(KEY_ANSWER, ExecutionAnswer
120 .ok("Execution completed properly"));
121 }
122
123 @RequestMapping("/newSlcExecution.service")
124 protected String newSlcExecution(HttpServletRequest request,
125 Model model) throws Exception {
126
127 if (log.isDebugEnabled())
128 log.debug("In ExecutionServiceController :: newSlcExecution.service");
129
130 String agentId = request
131 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
132 Assert.notNull(agentId, "agent id");
133
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();
139 String line = null;
140 while (((line = reader.readLine()) != null)) {
141 buffer.append(line);
142 }
143 answer = buffer.toString();
144 }
145
146 if (log.isTraceEnabled())
147 log.debug("Received message:\n" + answer);
148
149 StringSource source = new StringSource(answer);
150 SlcExecution slcExecution = (SlcExecution) unmarshaller
151 .unmarshal(source);
152
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());
157
158 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
159 slcExecution.getSteps().add(
160 new SlcExecutionStep(SlcExecutionStep.TYPE_START,
161 "Process started from the Web UI"));
162
163 initializeSEM();
164 slcExecutionManager.storeRealizedFlows(slcExecution);
165 slcExecutionService.newExecution(slcExecution);
166 SlcAgent agent = agentFactory.getAgent(agentId);
167 agent.runSlcExecution(slcExecution);
168
169 log.debug("After Everything has been done !");
170
171 model.addAttribute(KEY_ANSWER, ExecutionAnswer
172 .ok("Execution completed properly"));
173 return KEY_ANSWER;
174 }
175
176 private void initializeSEM() {
177 slcExecutionManager = new SlcExecutionManager(agentFactory,
178 unmarshaller, marshaller, slcExecutionService,
179 attachmentsStorage);
180
181 }
182
183 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao) {
184 this.slcExecutionDao = slcExecutionDao;
185 }
186
187 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
188 this.slcExecutionService = slcExecutionService;
189 }
190
191 public void setUnmarshaller(Unmarshaller unmarshaller) {
192 this.unmarshaller = unmarshaller;
193 }
194
195 public void setMarshaller(Marshaller marshaller) {
196 this.marshaller = marshaller;
197 }
198
199 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
200 this.attachmentsStorage = attachmentsStorage;
201 }
202
203 public void setAgentFactory(SlcAgentFactory agentFactory) {
204 this.agentFactory = agentFactory;
205 }
206
207 }