]> git.argeo.org Git - gpl/argeo-slc.git/blob - ExecutionServiceController.java
de928675b245b4e44f98508ba993e7ce8765eeba
[gpl/argeo-slc.git] / ExecutionServiceController.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.slc.web.mvc.process;
18
19 import java.io.BufferedReader;
20 import java.util.Comparator;
21 import java.util.List;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24 import java.util.UUID;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.slc.core.attachment.AttachmentsStorage;
31 import org.argeo.slc.dao.process.SlcExecutionDao;
32 import org.argeo.slc.execution.ExecutionModuleDescriptor;
33 import org.argeo.slc.msg.ExecutionAnswer;
34 import org.argeo.slc.msg.MsgConstants;
35 import org.argeo.slc.msg.ObjectList;
36 import org.argeo.slc.process.SlcExecution;
37 import org.argeo.slc.process.SlcExecutionStep;
38 import org.argeo.slc.runtime.SlcAgent;
39 import org.argeo.slc.runtime.SlcAgentFactory;
40 import org.argeo.slc.services.SlcExecutionService;
41 import org.springframework.oxm.Marshaller;
42 import org.springframework.oxm.Unmarshaller;
43 import org.springframework.stereotype.Controller;
44 import org.springframework.ui.Model;
45 import org.springframework.util.Assert;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.xml.transform.StringSource;
49
50 @Controller
51 public class ExecutionServiceController {
52
53 public final static String KEY_ANSWER = "__answer";
54 private final static Log log = LogFactory
55 .getLog(ExecutionServiceController.class);
56
57 private SlcExecutionDao slcExecutionDao;
58 private SlcAgentFactory agentFactory;
59 private Unmarshaller unmarshaller;
60 private Marshaller marshaller;
61 private SlcExecutionService slcExecutionService;
62 private AttachmentsStorage attachmentsStorage;
63
64 private SlcExecutionManager slcExecutionManager;
65
66 @RequestMapping("/listSlcExecutions.service")
67 protected ObjectList listSlcExecutions() {
68 List<SlcExecution> list = slcExecutionDao.listSlcExecutions();
69 return new ObjectList(list);
70 }
71
72 @RequestMapping("/getExecutionDescriptor.service")
73 protected ExecutionModuleDescriptor getExecutionDescriptor(
74 @RequestParam String agentId, @RequestParam String moduleName,
75 @RequestParam String version) {
76
77 SlcAgent slcAgent = agentFactory.getAgent(agentId);
78
79 ExecutionModuleDescriptor md = slcAgent.getExecutionModuleDescriptor(
80 moduleName, version);
81 return md;
82 }
83
84 @RequestMapping("/listModulesDescriptors.service")
85 protected ObjectList listModulesDescriptors(@RequestParam String agentId) {
86 // TODO: use centralized agentId property (from MsgConstants)?
87 SlcAgent slcAgent = agentFactory.getAgent(agentId);
88
89 List<ExecutionModuleDescriptor> descriptors = slcAgent
90 .listExecutionModuleDescriptors();
91 SortedSet<ExecutionModuleDescriptor> set = new TreeSet<ExecutionModuleDescriptor>(
92 new Comparator<ExecutionModuleDescriptor>() {
93
94 public int compare(ExecutionModuleDescriptor md1,
95 ExecutionModuleDescriptor md2) {
96 String str1 = md1.getLabel() != null ? md1.getLabel()
97 : md1.getName();
98 String str2 = md2.getLabel() != null ? md2.getLabel()
99 : md2.getName();
100 return str1.compareTo(str2);
101 }
102 });
103 set.addAll(descriptors);
104 return new ObjectList(set);
105 }
106
107 @RequestMapping("/getSlcExecution.service")
108 protected SlcExecution getSlcExecution(@RequestParam String uuid) {
109 SlcExecution slcExecution = slcExecutionDao.getSlcExecution(uuid);
110 initializeSEM();
111 slcExecutionManager.retrieveRealizedFlows(slcExecution);
112 return slcExecution;
113 }
114
115 @RequestMapping("/newSlcExecution.service")
116 protected ExecutionAnswer newSlcExecution(HttpServletRequest request,
117 Model model) throws Exception {
118
119 String agentId = request
120 .getParameter(MsgConstants.PROPERTY_SLC_AGENT_ID);
121 Assert.notNull(agentId, "agent id");
122
123 String answer = request.getParameter("body");
124 if (answer == null) {
125 // lets read the message body instead
126 BufferedReader reader = request.getReader();
127 StringBuffer buffer = new StringBuffer();
128 String line = null;
129 while (((line = reader.readLine()) != null)) {
130 buffer.append(line);
131 }
132 answer = buffer.toString();
133 }
134
135 if (log.isTraceEnabled())
136 log.debug("Received message:\n" + answer);
137
138 StringSource source = new StringSource(answer);
139 SlcExecution slcExecution = (SlcExecution) unmarshaller
140 .unmarshal(source);
141
142 // Workaround for https://www.argeo.org/bugzilla/show_bug.cgi?id=86
143 if (slcExecution.getUuid() == null
144 || slcExecution.getUuid().length() < 8)
145 slcExecution.setUuid(UUID.randomUUID().toString());
146
147 slcExecution.setStatus(SlcExecution.STATUS_SCHEDULED);
148 slcExecution.getSteps().add(
149 new SlcExecutionStep(SlcExecutionStep.TYPE_START,
150 "Process started from the Web UI"));
151
152 initializeSEM();
153 slcExecutionManager.storeRealizedFlows(slcExecution);
154 slcExecutionService.newExecution(slcExecution);
155 SlcAgent agent = agentFactory.getAgent(agentId);
156 agent.runSlcExecution(slcExecution);
157
158 return ExecutionAnswer.ok("Execution completed properly");
159 }
160
161 private void initializeSEM() {
162 slcExecutionManager = new SlcExecutionManager(agentFactory,
163 unmarshaller, marshaller, slcExecutionService,
164 attachmentsStorage);
165
166 }
167
168 public void setSlcExecutionDao(SlcExecutionDao slcExecutionDao) {
169 this.slcExecutionDao = slcExecutionDao;
170 }
171
172 public void setSlcExecutionService(SlcExecutionService slcExecutionService) {
173 this.slcExecutionService = slcExecutionService;
174 }
175
176 public void setUnmarshaller(Unmarshaller unmarshaller) {
177 this.unmarshaller = unmarshaller;
178 }
179
180 public void setMarshaller(Marshaller marshaller) {
181 this.marshaller = marshaller;
182 }
183
184 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
185 this.attachmentsStorage = attachmentsStorage;
186 }
187
188 public void setAgentFactory(SlcAgentFactory agentFactory) {
189 this.agentFactory = agentFactory;
190 }
191
192 }