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