]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/RemoveResultFromCollectionController.java
Add generic copy/remove to/from collection(s)
[gpl/argeo-slc.git] / runtime / org.argeo.slc.server / src / main / java / org / argeo / slc / web / mvc / result / RemoveResultFromCollectionController.java
index 8b655bd1baff62fdf756a371be2e1d322885a856..a9709dc234bf2e0e0734c47c8aa45835ac8e9fc2 100644 (file)
@@ -1,18 +1,29 @@
 package org.argeo.slc.web.mvc.result;\r
 \r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
 import javax.servlet.http.HttpServletRequest;\r
 import javax.servlet.http.HttpServletResponse;\r
 \r
+import org.argeo.slc.SlcException;\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.services.test.TestManagerService;\r
 import org.argeo.slc.web.mvc.AbstractServiceController;\r
+import org.springframework.util.PatternMatchUtils;\r
 import org.springframework.web.servlet.ModelAndView;\r
 \r
 public class RemoveResultFromCollectionController extends\r
                AbstractServiceController {\r
+       private final TreeTestResultCollectionDao testResultCollectionDao;\r
        private final TestManagerService testManagerService;\r
 \r
        public RemoveResultFromCollectionController(\r
+                       TreeTestResultCollectionDao testResultCollectionDao,\r
                        TestManagerService testManagerService) {\r
+               this.testResultCollectionDao = testResultCollectionDao;\r
                this.testManagerService = testManagerService;\r
        }\r
 \r
@@ -21,8 +32,58 @@ public class RemoveResultFromCollectionController extends
                        throws Exception {\r
 \r
                String collectionId = request.getParameter("collectionId");\r
-               String resultUuid = request.getParameter("resultUuid");\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 = testResultCollectionDao\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 = testResultCollectionDao\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
 \r
-               testManagerService.removeResultFromCollection(collectionId, resultUuid);\r
        }\r
 }\r