2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.slc
.web
.mvc
.controllers
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.SortedSet
;
25 import javax
.servlet
.ServletOutputStream
;
26 import javax
.servlet
.http
.HttpServletRequest
;
27 import javax
.servlet
.http
.HttpServletResponse
;
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
;
49 public class ResultController
{
51 // private final static Log log =
52 // LogFactory.getLog(ServiceController.class);
55 public final static String KEY_ANSWER
= "__answer";
56 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
59 private TreeTestResultDao treeTestResultDao
;
60 private TreeTestResultCollectionDao treeTestResultCollectionDao
;
61 private TestManagerService testManagerService
;
62 private AttachmentsStorage attachmentsStorage
;
65 @RequestMapping("/getResult.service")
66 protected TreeTestResult
getResult(
67 @RequestParam(value
= "uuid", required
= false) String uuid
) {
69 TreeTestResult result
= treeTestResultDao
.getTestResult(uuid
);
71 throw new SlcException("No result found for uuid " + uuid
);
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");
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");
91 if (collectionId
== null)
92 throw new SlcException("A collection id must be specified");
94 && (attrPatterns
== null || attrNames
.length
!= attrPatterns
.length
))
95 throw new SlcException(
96 "There must be as many attrName as attrPatterns");
98 // Remove specified results
99 if (resultUuids
!= null)
100 for (String resultUuid
: resultUuids
)
101 testManagerService
.removeResultFromCollection(collectionId
,
104 if (attrNames
!= null) {
105 TreeTestResultCollection sourceCollection
= treeTestResultCollectionDao
106 .getTestResultCollection(collectionId
);
109 for (String attrName
: attrNames
) {
110 String attrPattern
= attrPatterns
[index
];// safe: checked above
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());
124 if (resultUuids
== null) {// no specs
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());
138 return ExecutionAnswer
.ok("Execution completed properly");
141 @RequestMapping("/listCollectionRefs.service")
142 protected ReferenceList
listCollectionRefs(HttpServletRequest request
,
143 HttpServletResponse response
) {
145 SortedSet
<TreeTestResultCollection
> results
= treeTestResultCollectionDao
148 ReferenceList referenceList
= new ReferenceList();
149 for (TreeTestResultCollection collection
: results
) {
150 referenceList
.getReferences().add(collection
.getId());
152 return referenceList
;
155 @RequestMapping("/listResultAttributes.service")
156 protected ObjectList
listResultAttributes(@RequestParam String id
,
159 List
<ResultAttributes
> resultAttributes
= treeTestResultCollectionDao
160 .listResultAttributes(id
);
161 return new ObjectList(resultAttributes
);
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]);
178 List
<TreeTestResult
> resultAttributes
= treeTestResultCollectionDao
179 .listResults(collectionId
, attributes
);
180 return new ObjectList(resultAttributes
);
183 @RequestMapping("/copyCollectionToCollection.service")
184 protected ExecutionAnswer
copyCollectionToCollection(
185 @RequestParam String sourceCollectionId
,
186 @RequestParam String targetCollectionId
, HttpServletRequest request
) {
188 String
[] attrNames
= request
.getParameterValues("attrName");
189 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
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");
200 TreeTestResultCollection sourceCollection
= treeTestResultCollectionDao
201 .getTestResultCollection(sourceCollectionId
);
202 if (attrNames
!= null) {
204 for (String attrName
: attrNames
) {
205 String attrPattern
= attrPatterns
[index
];// safe: checked above
207 for (TreeTestResult treeTestResult
: sourceCollection
209 if (PatternMatchUtils
.simpleMatch(attrPattern
,
210 treeTestResult
.getAttributes().get(attrName
))) {
211 testManagerService
.addResultToCollection(
212 targetCollectionId
, treeTestResult
.getUuid());
220 for (TreeTestResult treeTestResult
: sourceCollection
.getResults()) {
221 testManagerService
.addResultToCollection(targetCollectionId
,
222 treeTestResult
.getUuid());
225 return ExecutionAnswer
.ok("Execution completed properly");
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())) {
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";
257 contentType
= contentType
+ ";name=\"" + name
+ "\"";
258 response
.setHeader("Content-Disposition", "attachment; filename=\""
261 response
.setHeader("Expires", "0");
262 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
263 response
.setHeader("Pragma", "no-cache");
265 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
266 resourceDescriptor
.setUuid(uuid
);
267 resourceDescriptor
.setContentType(contentType
);
269 response
.setContentType(contentType
);
270 ServletOutputStream outputStream
= response
.getOutputStream();
271 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
276 public void setTreeTestResultDao(TreeTestResultDao treeTestResultDao
) {
277 this.treeTestResultDao
= treeTestResultDao
;
280 public void setTestManagerService(TestManagerService testManagerService
) {
281 this.testManagerService
= testManagerService
;
284 public void setTreeTestResultCollectionDao(
285 TreeTestResultCollectionDao treeTestResultCollectionDao
) {
286 this.treeTestResultCollectionDao
= treeTestResultCollectionDao
;
289 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
290 this.attachmentsStorage
= attachmentsStorage
;