+++ /dev/null
-/*\r
- * Copyright (C) 2007-2012 Mathieu Baudier\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-package org.argeo.slc.web.mvc.controllers;\r
-\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.SortedSet;\r
-\r
-import javax.servlet.ServletOutputStream;\r
-import javax.servlet.http.HttpServletRequest;\r
-import javax.servlet.http.HttpServletResponse;\r
-\r
-import org.apache.commons.io.FilenameUtils;\r
-import org.argeo.slc.SlcException;\r
-import org.argeo.slc.core.attachment.AttachmentsStorage;\r
-import org.argeo.slc.core.attachment.SimpleAttachment;\r
-import org.argeo.slc.core.test.tree.ResultAttributes;\r
-import org.argeo.slc.core.test.tree.TreeTestResult;\r
-import org.argeo.slc.core.test.tree.TreeTestResultCollection;\r
-import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao;\r
-import org.argeo.slc.dao.test.tree.TreeTestResultDao;\r
-import org.argeo.slc.msg.ExecutionAnswer;\r
-import org.argeo.slc.msg.ObjectList;\r
-import org.argeo.slc.msg.ReferenceList;\r
-import org.argeo.slc.services.TestManagerService;\r
-import org.springframework.stereotype.Controller;\r
-import org.springframework.ui.Model;\r
-import org.springframework.util.PatternMatchUtils;\r
-import org.springframework.web.bind.annotation.RequestMapping;\r
-import org.springframework.web.bind.annotation.RequestParam;\r
-\r
-@Controller\r
-public class ResultController {\r
-\r
- // private final static Log log =\r
- // LogFactory.getLog(ServiceController.class);\r
-\r
- // Constants\r
- protected final String FORCE_DOWNLOAD = "Content-Type: application/force-download";\r
-\r
- // IoC\r
- private TreeTestResultDao treeTestResultDao;\r
- private TreeTestResultCollectionDao treeTestResultCollectionDao;\r
- private TestManagerService testManagerService;\r
- private AttachmentsStorage attachmentsStorage;\r
-\r
- // Business Methods\r
- @RequestMapping("/getResult.service")\r
- protected TreeTestResult getResult(\r
- @RequestParam(value = "uuid", required = false) String uuid) {\r
-\r
- TreeTestResult result = treeTestResultDao.getTestResult(uuid);\r
- if (result == null)\r
- throw new SlcException("No result found for uuid " + uuid);\r
- return result;\r
- }\r
-\r
- @RequestMapping("/addResultToCollection.service")\r
- protected ExecutionAnswer addResultToCollection(\r
- @RequestParam String collectionId, @RequestParam String resultUuid) {\r
- testManagerService.addResultToCollection(collectionId, resultUuid);\r
- return ExecutionAnswer.ok("Execution completed properly");\r
- }\r
-\r
- @RequestMapping("/removeResultFromCollection.service")\r
- protected ExecutionAnswer removeResultFromCollection(\r
- HttpServletRequest request) {\r
- String collectionId = request.getParameter("collectionId");\r
- String[] resultUuids = request.getParameterValues("resultUuid");\r
- String[] attrNames = request.getParameterValues("attrName");\r
- String[] attrPatterns = request.getParameterValues("attrPattern");\r
-\r
- // Checks\r
- if (collectionId == null)\r
- throw new SlcException("A collection id must be specified");\r
- if (attrNames != null\r
- && (attrPatterns == null || attrNames.length != attrPatterns.length))\r
- throw new SlcException(\r
- "There must be as many attrName as attrPatterns");\r
-\r
- // Remove specified results\r
- if (resultUuids != null)\r
- for (String resultUuid : resultUuids)\r
- testManagerService.removeResultFromCollection(collectionId,\r
- resultUuid);\r
-\r
- if (attrNames != null) {\r
- TreeTestResultCollection sourceCollection = treeTestResultCollectionDao\r
- .getTestResultCollection(collectionId);\r
-\r
- int index = 0;\r
- for (String attrName : attrNames) {\r
- String attrPattern = attrPatterns[index];// safe: checked above\r
-\r
- List<TreeTestResult> results = new ArrayList<TreeTestResult>(\r
- sourceCollection.getResults());\r
- for (TreeTestResult treeTestResult : results) {\r
- if (PatternMatchUtils.simpleMatch(attrPattern,\r
- treeTestResult.getAttributes().get(attrName))) {\r
- testManagerService.removeResultFromCollection(\r
- collectionId, treeTestResult.getUuid());\r
- }\r
- }\r
- index++;\r
- }\r
- } else {\r
- if (resultUuids == null) {// no specs\r
- // remove all\r
- // TODO: optimize\r
- TreeTestResultCollection sourceCollection = treeTestResultCollectionDao\r
- .getTestResultCollection(collectionId);\r
- List<TreeTestResult> results = new ArrayList<TreeTestResult>(\r
- sourceCollection.getResults());\r
- for (TreeTestResult treeTestResult : results) {\r
- testManagerService.removeResultFromCollection(collectionId,\r
- treeTestResult.getUuid());\r
- }\r
-\r
- }\r
- }\r
- return ExecutionAnswer.ok("Execution completed properly");\r
- }\r
-\r
- @RequestMapping("/listCollectionRefs.service")\r
- protected ReferenceList listCollectionRefs(HttpServletRequest request,\r
- HttpServletResponse response) {\r
-\r
- SortedSet<TreeTestResultCollection> results = treeTestResultCollectionDao\r
- .listCollections();\r
-\r
- ReferenceList referenceList = new ReferenceList();\r
- for (TreeTestResultCollection collection : results) {\r
- referenceList.getReferences().add(collection.getId());\r
- }\r
- return referenceList;\r
- }\r
-\r
- @RequestMapping("/listResultAttributes.service")\r
- protected ObjectList listResultAttributes(@RequestParam String id,\r
- Model model) {\r
-\r
- List<ResultAttributes> resultAttributes = treeTestResultCollectionDao\r
- .listResultAttributes(id);\r
- return new ObjectList(resultAttributes);\r
- }\r
-\r
- @RequestMapping("/listResults.service")\r
- @SuppressWarnings(value = { "unchecked" })\r
- protected ObjectList listResults(\r
- @RequestParam(value = "collectionId", required = false) String collectionId,\r
- HttpServletRequest request) {\r
- Map<String, String[]> parameterMap = request.getParameterMap();\r
- Map<String, String> attributes = new HashMap<String, String>();\r
- for (String parameter : parameterMap.keySet()) {\r
- if (parameter.startsWith("attr.")) {\r
- String key = parameter.substring("attr.".length());\r
- attributes.put(key, parameterMap.get(parameter)[0]);\r
- }\r
- }\r
-\r
- List<TreeTestResult> resultAttributes = treeTestResultCollectionDao\r
- .listResults(collectionId, attributes);\r
- return new ObjectList(resultAttributes);\r
- }\r
-\r
- @RequestMapping("/copyCollectionToCollection.service")\r
- protected ExecutionAnswer copyCollectionToCollection(\r
- @RequestParam String sourceCollectionId,\r
- @RequestParam String targetCollectionId, HttpServletRequest request) {\r
-\r
- String[] attrNames = request.getParameterValues("attrName");\r
- String[] attrPatterns = request.getParameterValues("attrPattern");\r
-\r
- // Checks\r
- if (sourceCollectionId == null || targetCollectionId == null)\r
- throw new SlcException(\r
- "Source and target collection ids must be specified");\r
- if (attrNames != null\r
- && (attrPatterns == null || attrNames.length != attrPatterns.length))\r
- throw new SlcException(\r
- "There must be as many attrName as attrPatterns");\r
-\r
- TreeTestResultCollection sourceCollection = treeTestResultCollectionDao\r
- .getTestResultCollection(sourceCollectionId);\r
- if (attrNames != null) {\r
- int index = 0;\r
- for (String attrName : attrNames) {\r
- String attrPattern = attrPatterns[index];// safe: checked above\r
-\r
- for (TreeTestResult treeTestResult : sourceCollection\r
- .getResults()) {\r
- if (PatternMatchUtils.simpleMatch(attrPattern,\r
- treeTestResult.getAttributes().get(attrName))) {\r
- testManagerService.addResultToCollection(\r
- targetCollectionId, treeTestResult.getUuid());\r
- }\r
- }\r
- index++;\r
- }\r
- } else {\r
- // remove all\r
- // TODO: optimize\r
- for (TreeTestResult treeTestResult : sourceCollection.getResults()) {\r
- testManagerService.addResultToCollection(targetCollectionId,\r
- treeTestResult.getUuid());\r
- }\r
- }\r
- return ExecutionAnswer.ok("Execution completed properly");\r
- }\r
-\r
- @RequestMapping("/getAttachment.service")\r
- protected void getAttachment(@RequestParam String uuid,\r
- @RequestParam String contentType, @RequestParam String name,\r
- HttpServletResponse response) throws Exception {\r
- if (contentType == null || "".equals(contentType.trim())) {\r
- if (name != null) {\r
- contentType = FORCE_DOWNLOAD;\r
- String ext = FilenameUtils.getExtension(name);\r
- // cf. http://en.wikipedia.org/wikServicei/Internet_media_type\r
- if ("csv".equals(ext))\r
- contentType = "text/csv";\r
- else if ("pdf".equals(ext))\r
- contentType = "application/pdf";\r
- else if ("zip".equals(ext))\r
- contentType = "application/zip";\r
- else if ("html".equals(ext))\r
- contentType = "application/html";\r
- else if ("txt".equals(ext))\r
- contentType = "text/plain";\r
- else if ("doc".equals(ext) || "docx".equals(ext))\r
- contentType = "application/msword";\r
- else if ("xls".equals(ext) || "xlsx".equals(ext))\r
- contentType = "application/vnd.ms-excel";\r
- else if ("xml".equals(ext))\r
- contentType = "text/xml";\r
- }\r
- }\r
-\r
- if (name != null) {\r
- contentType = contentType + ";name=\"" + name + "\"";\r
- response.setHeader("Content-Disposition", "attachment; filename=\""\r
- + name + "\"");\r
- }\r
- response.setHeader("Expires", "0");\r
- response.setHeader("Cache-Control", "no-cache, must-revalidate");\r
- response.setHeader("Pragma", "no-cache");\r
-\r
- SimpleAttachment resourceDescriptor = new SimpleAttachment();\r
- resourceDescriptor.setUuid(uuid);\r
- resourceDescriptor.setContentType(contentType);\r
-\r
- response.setContentType(contentType);\r
- ServletOutputStream outputStream = response.getOutputStream();\r
- attachmentsStorage.retrieveAttachment(resourceDescriptor, outputStream);\r
- }\r
-\r
- // IoC\r
-\r
- public void setTreeTestResultDao(TreeTestResultDao treeTestResultDao) {\r
- this.treeTestResultDao = treeTestResultDao;\r
- }\r
-\r
- public void setTestManagerService(TestManagerService testManagerService) {\r
- this.testManagerService = testManagerService;\r
- }\r
-\r
- public void setTreeTestResultCollectionDao(\r
- TreeTestResultCollectionDao treeTestResultCollectionDao) {\r
- this.treeTestResultCollectionDao = treeTestResultCollectionDao;\r
- }\r
-\r
- public void setAttachmentsStorage(AttachmentsStorage attachmentsStorage) {\r
- this.attachmentsStorage = attachmentsStorage;\r
- }\r
-}\r