]> 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 // Class cleaning
[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.xml.transform.StringSource;
33
34 @Controller
35 public class ExecutionServiceController {
36
37 public final static String KEY_ANSWER = "__answer";
38 private final static Log log = LogFactory
39 .getLog(ExecutionServiceController.class);
40
41 private SlcExecutionDao slcExecutionDao;
42 private SlcAgentFactory agentFactory;
43 private Unmarshaller unmarshaller;
44 private Marshaller marshaller;
45 private SlcExecutionService slcExecutionService;
46 private AttachmentsStorage attachmentsStorage;
47
48 private SlcExecutionManager slcExecutionManager;
49
50 @RequestMapping("/listSlcExecutions.service")
51 protected ObjectList listSlcExecutions() {
52 List<SlcExecution> list = slcExecutionDao.listSlcExecutions();
53 return new ObjectList(list);
54 }
55
56 @RequestMapping("/getExecutionDescriptor.service")
57 protected ExecutionModuleDescriptor getExecutionDescriptor(
58 @RequestParam String agentId, @RequestParam String moduleName,
59 @RequestParam String version) {
60
61 SlcAgent slcAgent = agentFactory.getAgent(agentId);
62
63 ExecutionModuleDescriptor md = slcAgent.getExecutionModuleDescriptor(
64 moduleName, version);
65 return md;
66 }
67
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);
72
73 List<ExecutionModuleDescriptor> descriptors = slcAgent
74 .listExecutionModuleDescriptors();
75 SortedSet<ExecutionModuleDescriptor> set = new TreeSet<ExecutionModuleDescriptor>(
76 new Comparator<ExecutionModuleDescriptor>() {
77
78 public int compare(ExecutionModuleDescriptor md1,
79 ExecutionModuleDescriptor md2) {
80 String str1 = md1.getLabel() != null ? md1.getLabel()
81 : md1.getName();
82 String str2 = md2.getLabel() != null ? md2.getLabel()
83 : md2.getName();
84 return str1.compareTo(str2);
85 }
86 });
87 set.addAll(descriptors);
88 return new ObjectList(set);
89 }
90
91 @RequestMapping("/getSlcExecution.service")
92 protected SlcExecution getSlcExecution(@RequestParam String uuid) {
93 SlcExecution slcExecution = slcExecutionDao.getSlcExecution(uuid);
94 initializeSEM();
95 slcExecutionManager.retrieveRealizedFlows(slcExecution);
96 return slcExecution;
97 }
98
99 @RequestMapping("/newSlcExecution.service")
100 protected ExecutionAnswer newSlcExecution(HttpServletRequest request,
101 Model model) throws Exception {
102
103 String agentId = request
104 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
105 Assert.notNull(agentId, "agent id");
106
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();
112 String line = null;
113 while (((line = reader.readLine()) != null)) {
114 buffer.append(line);
115 }
116 answer = buffer.toString();
117 }
118
119 if (log.isTraceEnabled())
120 log.debug("Received message:\n" + answer);
121
122 StringSource source = new StringSource(answer);
123 SlcExecution slcExecution = (SlcExecution) unmarshaller
124 .unmarshal(source);
125
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());
130
131 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
132 slcExecution.getSteps().add(
133 new SlcExecutionStep(SlcExecutionStep.TYPE_START,
134 "Process started from the Web UI"));
135
136 initializeSEM();
137 slcExecutionManager.storeRealizedFlows(slcExecution);
138 slcExecutionService.newExecution(slcExecution);
139 SlcAgent agent = agentFactory.getAgent(agentId);
140 agent.runSlcExecution(slcExecution);
141
142 return ExecutionAnswer.ok("Execution completed properly");
143 }
144
145 private void initializeSEM() {
146 slcExecutionManager = new SlcExecutionManager(agentFactory,
147 unmarshaller, marshaller, slcExecutionService,
148 attachmentsStorage);
149
150 }
151
152 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao) {
153 this.slcExecutionDao = slcExecutionDao;
154 }
155
156 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
157 this.slcExecutionService = slcExecutionService;
158 }
159
160 public void setUnmarshaller(Unmarshaller unmarshaller) {
161 this.unmarshaller = unmarshaller;
162 }
163
164 public void setMarshaller(Marshaller marshaller) {
165 this.marshaller = marshaller;
166 }
167
168 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
169 this.attachmentsStorage = attachmentsStorage;
170 }
171
172 public void setAgentFactory(SlcAgentFactory agentFactory) {
173 this.agentFactory = agentFactory;
174 }
175
176 }