]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/controllers/ResultController.java
Move NameVersion
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / controllers / ResultController.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.controllers;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.SortedSet;
24
25 import javax.servlet.ServletOutputStream;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.apache.commons.io.FilenameUtils;
30 import org.argeo.slc.SlcException;
31 import org.argeo.slc.core.attachment.AttachmentsStorage;
32 import org.argeo.slc.core.attachment.SimpleAttachment;
33 import org.argeo.slc.core.test.tree.ResultAttributes;
34 import org.argeo.slc.core.test.tree.TreeTestResult;
35 import org.argeo.slc.core.test.tree.TreeTestResultCollection;
36 import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;
37 import org.argeo.slc.dao.test.tree.TreeTestResultDao;
38 import org.argeo.slc.msg.ExecutionAnswer;
39 import org.argeo.slc.msg.ObjectList;
40 import org.argeo.slc.msg.ReferenceList;
41 import org.argeo.slc.services.TestManagerService;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.ui.Model;
44 import org.springframework.util.PatternMatchUtils;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestParam;
47
48 @Controller
49 public class ResultController {
50
51 // private final static Log log =
52 // LogFactory.getLog(ServiceController.class);
53
54 // Constants
55 protected final String FORCE_DOWNLOAD = "Content-Type: application/force-download";
56
57 // IoC
58 private TreeTestResultDao treeTestResultDao;
59 private TreeTestResultCollectionDao treeTestResultCollectionDao;
60 private TestManagerService testManagerService;
61 private AttachmentsStorage attachmentsStorage;
62
63 // Business Methods
64 @RequestMapping("/getResult.service")
65 protected TreeTestResult getResult(
66 @RequestParam(value = "uuid", required = false) String uuid) {
67
68 TreeTestResult result = treeTestResultDao.getTestResult(uuid);
69 if (result == null)
70 throw new SlcException("No result found for uuid " + uuid);
71 return result;
72 }
73
74 @RequestMapping("/addResultToCollection.service")
75 protected ExecutionAnswer addResultToCollection(
76 @RequestParam String collectionId, @RequestParam String resultUuid) {
77 testManagerService.addResultToCollection(collectionId, resultUuid);
78 return ExecutionAnswer.ok("Execution completed properly");
79 }
80
81 @RequestMapping("/removeResultFromCollection.service")
82 protected ExecutionAnswer removeResultFromCollection(
83 HttpServletRequest request) {
84 String collectionId = request.getParameter("collectionId");
85 String[] resultUuids = request.getParameterValues("resultUuid");
86 String[] attrNames = request.getParameterValues("attrName");
87 String[] attrPatterns = request.getParameterValues("attrPattern");
88
89 // Checks
90 if (collectionId == null)
91 throw new SlcException("A collection id must be specified");
92 if (attrNames != null
93 && (attrPatterns == null || attrNames.length != attrPatterns.length))
94 throw new SlcException(
95 "There must be as many attrName as attrPatterns");
96
97 // Remove specified results
98 if (resultUuids != null)
99 for (String resultUuid : resultUuids)
100 testManagerService.removeResultFromCollection(collectionId,
101 resultUuid);
102
103 if (attrNames != null) {
104 TreeTestResultCollection sourceCollection = treeTestResultCollectionDao
105 .getTestResultCollection(collectionId);
106
107 int index = 0;
108 for (String attrName : attrNames) {
109 String attrPattern = attrPatterns[index];// safe: checked above
110
111 List<TreeTestResult> results = new ArrayList<TreeTestResult>(
112 sourceCollection.getResults());
113 for (TreeTestResult treeTestResult : results) {
114 if (PatternMatchUtils.simpleMatch(attrPattern,
115 treeTestResult.getAttributes().get(attrName))) {
116 testManagerService.removeResultFromCollection(
117 collectionId, treeTestResult.getUuid());
118 }
119 }
120 index++;
121 }
122 } else {
123 if (resultUuids == null) {// no specs
124 // remove all
125 // TODO: optimize
126 TreeTestResultCollection sourceCollection = treeTestResultCollectionDao
127 .getTestResultCollection(collectionId);
128 List<TreeTestResult> results = new ArrayList<TreeTestResult>(
129 sourceCollection.getResults());
130 for (TreeTestResult treeTestResult : results) {
131 testManagerService.removeResultFromCollection(collectionId,
132 treeTestResult.getUuid());
133 }
134
135 }
136 }
137 return ExecutionAnswer.ok("Execution completed properly");
138 }
139
140 @RequestMapping("/listCollectionRefs.service")
141 protected ReferenceList listCollectionRefs(HttpServletRequest request,
142 HttpServletResponse response) {
143
144 SortedSet<TreeTestResultCollection> results = treeTestResultCollectionDao
145 .listCollections();
146
147 ReferenceList referenceList = new ReferenceList();
148 for (TreeTestResultCollection collection : results) {
149 referenceList.getReferences().add(collection.getId());
150 }
151 return referenceList;
152 }
153
154 @RequestMapping("/listResultAttributes.service")
155 protected ObjectList listResultAttributes(@RequestParam String id,
156 Model model) {
157
158 List<ResultAttributes> resultAttributes = treeTestResultCollectionDao
159 .listResultAttributes(id);
160 return new ObjectList(resultAttributes);
161 }
162
163 @RequestMapping("/listResults.service")
164 @SuppressWarnings(value = { "unchecked" })
165 protected ObjectList listResults(
166 @RequestParam(value = "collectionId", required = false) String collectionId,
167 HttpServletRequest request) {
168 Map<String, String[]> parameterMap = request.getParameterMap();
169 Map<String, String> attributes = new HashMap<String, String>();
170 for (String parameter : parameterMap.keySet()) {
171 if (parameter.startsWith("attr.")) {
172 String key = parameter.substring("attr.".length());
173 attributes.put(key, parameterMap.get(parameter)[0]);
174 }
175 }
176
177 List<TreeTestResult> resultAttributes = treeTestResultCollectionDao
178 .listResults(collectionId, attributes);
179 return new ObjectList(resultAttributes);
180 }
181
182 @RequestMapping("/copyCollectionToCollection.service")
183 protected ExecutionAnswer copyCollectionToCollection(
184 @RequestParam String sourceCollectionId,
185 @RequestParam String targetCollectionId, HttpServletRequest request) {
186
187 String[] attrNames = request.getParameterValues("attrName");
188 String[] attrPatterns = request.getParameterValues("attrPattern");
189
190 // Checks
191 if (sourceCollectionId == null || targetCollectionId == null)
192 throw new SlcException(
193 "Source and target collection ids must be specified");
194 if (attrNames != null
195 && (attrPatterns == null || attrNames.length != attrPatterns.length))
196 throw new SlcException(
197 "There must be as many attrName as attrPatterns");
198
199 TreeTestResultCollection sourceCollection = treeTestResultCollectionDao
200 .getTestResultCollection(sourceCollectionId);
201 if (attrNames != null) {
202 int index = 0;
203 for (String attrName : attrNames) {
204 String attrPattern = attrPatterns[index];// safe: checked above
205
206 for (TreeTestResult treeTestResult : sourceCollection
207 .getResults()) {
208 if (PatternMatchUtils.simpleMatch(attrPattern,
209 treeTestResult.getAttributes().get(attrName))) {
210 testManagerService.addResultToCollection(
211 targetCollectionId, treeTestResult.getUuid());
212 }
213 }
214 index++;
215 }
216 } else {
217 // remove all
218 // TODO: optimize
219 for (TreeTestResult treeTestResult : sourceCollection.getResults()) {
220 testManagerService.addResultToCollection(targetCollectionId,
221 treeTestResult.getUuid());
222 }
223 }
224 return ExecutionAnswer.ok("Execution completed properly");
225 }
226
227 @RequestMapping("/getAttachment.service")
228 protected void getAttachment(@RequestParam String uuid,
229 @RequestParam String contentType, @RequestParam String name,
230 HttpServletResponse response) throws Exception {
231 if (contentType == null || "".equals(contentType.trim())) {
232 if (name != null) {
233 contentType = FORCE_DOWNLOAD;
234 String ext = FilenameUtils.getExtension(name);
235 // cf. http://en.wikipedia.org/wikServicei/Internet_media_type
236 if ("csv".equals(ext))
237 contentType = "text/csv";
238 else if ("pdf".equals(ext))
239 contentType = "application/pdf";
240 else if ("zip".equals(ext))
241 contentType = "application/zip";
242 else if ("html".equals(ext))
243 contentType = "application/html";
244 else if ("txt".equals(ext))
245 contentType = "text/plain";
246 else if ("doc".equals(ext) || "docx".equals(ext))
247 contentType = "application/msword";
248 else if ("xls".equals(ext) || "xlsx".equals(ext))
249 contentType = "application/vnd.ms-excel";
250 else if ("xml".equals(ext))
251 contentType = "text/xml";
252 }
253 }
254
255 if (name != null) {
256 contentType = contentType + ";name=\"" + name + "\"";
257 response.setHeader("Content-Disposition", "attachment; filename=\""
258 + name + "\"");
259 }
260 response.setHeader("Expires", "0");
261 response.setHeader("Cache-Control", "no-cache, must-revalidate");
262 response.setHeader("Pragma", "no-cache");
263
264 SimpleAttachment resourceDescriptor = new SimpleAttachment();
265 resourceDescriptor.setUuid(uuid);
266 resourceDescriptor.setContentType(contentType);
267
268 response.setContentType(contentType);
269 ServletOutputStream outputStream = response.getOutputStream();
270 attachmentsStorage.retrieveAttachment(resourceDescriptor, outputStream);
271 }
272
273 // IoC
274
275 public void setTreeTestResultDao(TreeTestResultDao treeTestResultDao) {
276 this.treeTestResultDao = treeTestResultDao;
277 }
278
279 public void setTestManagerService(TestManagerService testManagerService) {
280 this.testManagerService = testManagerService;
281 }
282
283 public void setTreeTestResultCollectionDao(
284 TreeTestResultCollectionDao treeTestResultCollectionDao) {
285 this.treeTestResultCollectionDao = treeTestResultCollectionDao;
286 }
287
288 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {
289 this.attachmentsStorage = attachmentsStorage;
290 }
291 }