2 * Copyright (C) 2007-2012 Mathieu Baudier
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.
16 package org
.argeo
.slc
.web
.mvc
.controllers
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
22 import java
.util
.SortedSet
;
24 import javax
.servlet
.ServletOutputStream
;
25 import javax
.servlet
.http
.HttpServletRequest
;
26 import javax
.servlet
.http
.HttpServletResponse
;
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
;
48 public class ResultController
{
50 // private final static Log log =
51 // LogFactory.getLog(ServiceController.class);
54 protected final String FORCE_DOWNLOAD
= "Content-Type: application/force-download";
57 private TreeTestResultDao treeTestResultDao
;
58 private TreeTestResultCollectionDao treeTestResultCollectionDao
;
59 private TestManagerService testManagerService
;
60 private AttachmentsStorage attachmentsStorage
;
63 @RequestMapping("/getResult.service")
64 protected TreeTestResult
getResult(
65 @RequestParam(value
= "uuid", required
= false) String uuid
) {
67 TreeTestResult result
= treeTestResultDao
.getTestResult(uuid
);
69 throw new SlcException("No result found for uuid " + uuid
);
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");
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");
89 if (collectionId
== null)
90 throw new SlcException("A collection id must be specified");
92 && (attrPatterns
== null || attrNames
.length
!= attrPatterns
.length
))
93 throw new SlcException(
94 "There must be as many attrName as attrPatterns");
96 // Remove specified results
97 if (resultUuids
!= null)
98 for (String resultUuid
: resultUuids
)
99 testManagerService
.removeResultFromCollection(collectionId
,
102 if (attrNames
!= null) {
103 TreeTestResultCollection sourceCollection
= treeTestResultCollectionDao
104 .getTestResultCollection(collectionId
);
107 for (String attrName
: attrNames
) {
108 String attrPattern
= attrPatterns
[index
];// safe: checked above
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());
122 if (resultUuids
== null) {// no specs
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());
136 return ExecutionAnswer
.ok("Execution completed properly");
139 @RequestMapping("/listCollectionRefs.service")
140 protected ReferenceList
listCollectionRefs(HttpServletRequest request
,
141 HttpServletResponse response
) {
143 SortedSet
<TreeTestResultCollection
> results
= treeTestResultCollectionDao
146 ReferenceList referenceList
= new ReferenceList();
147 for (TreeTestResultCollection collection
: results
) {
148 referenceList
.getReferences().add(collection
.getId());
150 return referenceList
;
153 @RequestMapping("/listResultAttributes.service")
154 protected ObjectList
listResultAttributes(@RequestParam String id
,
157 List
<ResultAttributes
> resultAttributes
= treeTestResultCollectionDao
158 .listResultAttributes(id
);
159 return new ObjectList(resultAttributes
);
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]);
176 List
<TreeTestResult
> resultAttributes
= treeTestResultCollectionDao
177 .listResults(collectionId
, attributes
);
178 return new ObjectList(resultAttributes
);
181 @RequestMapping("/copyCollectionToCollection.service")
182 protected ExecutionAnswer
copyCollectionToCollection(
183 @RequestParam String sourceCollectionId
,
184 @RequestParam String targetCollectionId
, HttpServletRequest request
) {
186 String
[] attrNames
= request
.getParameterValues("attrName");
187 String
[] attrPatterns
= request
.getParameterValues("attrPattern");
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");
198 TreeTestResultCollection sourceCollection
= treeTestResultCollectionDao
199 .getTestResultCollection(sourceCollectionId
);
200 if (attrNames
!= null) {
202 for (String attrName
: attrNames
) {
203 String attrPattern
= attrPatterns
[index
];// safe: checked above
205 for (TreeTestResult treeTestResult
: sourceCollection
207 if (PatternMatchUtils
.simpleMatch(attrPattern
,
208 treeTestResult
.getAttributes().get(attrName
))) {
209 testManagerService
.addResultToCollection(
210 targetCollectionId
, treeTestResult
.getUuid());
218 for (TreeTestResult treeTestResult
: sourceCollection
.getResults()) {
219 testManagerService
.addResultToCollection(targetCollectionId
,
220 treeTestResult
.getUuid());
223 return ExecutionAnswer
.ok("Execution completed properly");
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())) {
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";
255 contentType
= contentType
+ ";name=\"" + name
+ "\"";
256 response
.setHeader("Content-Disposition", "attachment; filename=\""
259 response
.setHeader("Expires", "0");
260 response
.setHeader("Cache-Control", "no-cache, must-revalidate");
261 response
.setHeader("Pragma", "no-cache");
263 SimpleAttachment resourceDescriptor
= new SimpleAttachment();
264 resourceDescriptor
.setUuid(uuid
);
265 resourceDescriptor
.setContentType(contentType
);
267 response
.setContentType(contentType
);
268 ServletOutputStream outputStream
= response
.getOutputStream();
269 attachmentsStorage
.retrieveAttachment(resourceDescriptor
, outputStream
);
274 public void setTreeTestResultDao(TreeTestResultDao treeTestResultDao
) {
275 this.treeTestResultDao
= treeTestResultDao
;
278 public void setTestManagerService(TestManagerService testManagerService
) {
279 this.testManagerService
= testManagerService
;
282 public void setTreeTestResultCollectionDao(
283 TreeTestResultCollectionDao treeTestResultCollectionDao
) {
284 this.treeTestResultCollectionDao
= treeTestResultCollectionDao
;
287 public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage
) {
288 this.attachmentsStorage
= attachmentsStorage
;