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 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
58 private TreeTestResultDao treeTestResultDao
;
59 private TreeTestResultCollectionDao treeTestResultCollectionDao
;
60 private TestManagerService testManagerService
;
61 private AttachmentsStorage attachmentsStorage
;
64 @RequestMapping("/getResult.service")
65 protected TreeTestResult
getResult(
66 @RequestParam(value
= "uuid", required
= false) String uuid
) {
68 TreeTestResult result
= treeTestResultDao
.getTestResult(uuid
);
70 throw new SlcException("No result found for uuid " + uuid
);
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");
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");
90 if (collectionId
== null)
91 throw new SlcException("A collection id must be specified");
93 && (attrPatterns
== null || attrNames
.length
!= attrPatterns
.length
))
94 throw new SlcException(
95 "There must be as many attrName as attrPatterns");
97 // Remove specified results
98 if (resultUuids
!= null)
99 for (String resultUuid
: resultUuids
)
100 testManagerService
.removeResultFromCollection(collectionId
,
103 if (attrNames
!= null) {
104 TreeTestResultCollection sourceCollection
= treeTestResultCollectionDao
105 .getTestResultCollection(collectionId
);
108 for (String attrName
: attrNames
) {
109 String attrPattern
= attrPatterns
[index
];// safe: checked above
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());
123 if (resultUuids
== null) {// no specs
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());
137 return ExecutionAnswer
.ok("Execution completed properly");
140 @RequestMapping("/listCollectionRefs.service")
141 protected ReferenceList
listCollectionRefs(HttpServletRequest request
,
142 HttpServletResponse response
) {
144 SortedSet
<TreeTestResultCollection
> results
= treeTestResultCollectionDao
147 ReferenceList referenceList
= new ReferenceList();
148 for (TreeTestResultCollection collection
: results
) {
149 referenceList
.getReferences().add(collection
.getId());
151 return referenceList
;
154 @RequestMapping("/listResultAttributes.service")
155 protected ObjectList
listResultAttributes(@RequestParam String id
,
158 List
<ResultAttributes
> resultAttributes
= treeTestResultCollectionDao
159 .listResultAttributes(id
);
160 return new ObjectList(resultAttributes
);
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]);
177 List
<TreeTestResult
> resultAttributes
= treeTestResultCollectionDao
178 .listResults(collectionId
, attributes
);
179 return new ObjectList(resultAttributes
);
182 @RequestMapping("/copyCollectionToCollection.service")
183 protected ExecutionAnswer
copyCollectionToCollection(
184 @RequestParam String sourceCollectionId
,
185 @RequestParam String targetCollectionId
, HttpServletRequest request
) {
187 String
[] attrNames
= request
.getParameterValues("attrName");
188 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
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");
199 TreeTestResultCollection sourceCollection
= treeTestResultCollectionDao
200 .getTestResultCollection(sourceCollectionId
);
201 if (attrNames
!= null) {
203 for (String attrName
: attrNames
) {
204 String attrPattern
= attrPatterns
[index
];// safe: checked above
206 for (TreeTestResult treeTestResult
: sourceCollection
208 if (PatternMatchUtils
.simpleMatch(attrPattern
,
209 treeTestResult
.getAttributes().get(attrName
))) {
210 testManagerService
.addResultToCollection(
211 targetCollectionId
, treeTestResult
.getUuid());
219 for (TreeTestResult treeTestResult
: sourceCollection
.getResults()) {
220 testManagerService
.addResultToCollection(targetCollectionId
,
221 treeTestResult
.getUuid());
224 return ExecutionAnswer
.ok("Execution completed properly");
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())) {
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";
256 contentType
= contentType
+ ";name=\"" + name
+ "\"";
257 response
.setHeader("Content-Disposition", "attachment; filename=\""
260 response
.setHeader("Expires", "0");
261 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
262 response
.setHeader("Pragma", "no-cache");
264 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
265 resourceDescriptor
.setUuid(uuid
);
266 resourceDescriptor
.setContentType(contentType
);
268 response
.setContentType(contentType
);
269 ServletOutputStream outputStream
= response
.getOutputStream();
270 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
275 public void setTreeTestResultDao(TreeTestResultDao treeTestResultDao
) {
276 this.treeTestResultDao
= treeTestResultDao
;
279 public void setTestManagerService(TestManagerService testManagerService
) {
280 this.testManagerService
= testManagerService
;
283 public void setTreeTestResultCollectionDao(
284 TreeTestResultCollectionDao treeTestResultCollectionDao
) {
285 this.treeTestResultCollectionDao
= treeTestResultCollectionDao
;
288 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
289 this.attachmentsStorage
= attachmentsStorage
;