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