From: Mathieu Baudier Date: Tue, 27 Jan 2009 11:39:10 +0000 (+0000) Subject: Add generic copy/remove to/from collection(s) X-Git-Tag: argeo-slc-2.1.7~2163 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=b57f27c766e4ce01990cff3398b9aaf8454ed5d9;p=gpl%2Fargeo-slc.git Add generic copy/remove to/from collection(s) git-svn-id: https://svn.argeo.org/slc/trunk@2135 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/CopyCollectionToCollectionController.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/CopyCollectionToCollectionController.java new file mode 100644 index 000000000..1841a3fcc --- /dev/null +++ b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/CopyCollectionToCollectionController.java @@ -0,0 +1,76 @@ +package org.argeo.slc.web.mvc.result; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.argeo.slc.SlcException; +import org.argeo.slc.core.test.tree.TreeTestResult; +import org.argeo.slc.core.test.tree.TreeTestResultCollection; +import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao; +import org.argeo.slc.services.test.TestManagerService; +import org.argeo.slc.web.mvc.AbstractServiceController; +import org.springframework.util.PatternMatchUtils; +import org.springframework.web.servlet.ModelAndView; + +/** + * Copy from a collection to another based on Spring simple pattern matching. + * + * @see PatternMatchUtils + */ +public class CopyCollectionToCollectionController extends + AbstractServiceController { + private final TreeTestResultCollectionDao testResultCollectionDao; + private final TestManagerService testManagerService; + + public CopyCollectionToCollectionController( + TreeTestResultCollectionDao testResultCollectionDao, + TestManagerService testManagerService) { + this.testResultCollectionDao = testResultCollectionDao; + this.testManagerService = testManagerService; + } + + protected void handleServiceRequest(HttpServletRequest request, + HttpServletResponse response, ModelAndView modelAndView) + throws Exception { + + String sourceCollectionId = request.getParameter("sourceCollectionId"); + String targetCollectionId = request.getParameter("targetCollectionId"); + String[] attrNames = request.getParameterValues("attrName"); + String[] attrPatterns = request.getParameterValues("attrPattern"); + + // Checks + if (sourceCollectionId == null || targetCollectionId == null) + throw new SlcException( + "Source and target collection ids must be specified"); + if (attrNames != null + && (attrPatterns == null || attrNames.length != attrPatterns.length)) + throw new SlcException( + "There must be as many attrName as attrPatterns"); + + TreeTestResultCollection sourceCollection = testResultCollectionDao + .getTestResultCollection(sourceCollectionId); + if (attrNames != null) { + int index = 0; + for (String attrName : attrNames) { + String attrPattern = attrPatterns[index];// safe: checked above + + for (TreeTestResult treeTestResult : sourceCollection + .getResults()) { + if (PatternMatchUtils.simpleMatch(attrPattern, + treeTestResult.getAttributes().get(attrName))) { + testManagerService.addResultToCollection( + targetCollectionId, treeTestResult.getUuid()); + } + } + index++; + } + } else { + // remove all + // TODO: optimize + for (TreeTestResult treeTestResult : sourceCollection.getResults()) { + testManagerService.addResultToCollection(targetCollectionId, + treeTestResult.getUuid()); + } + } + } +} diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/RemoveResultFromCollectionController.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/RemoveResultFromCollectionController.java index 8b655bd1b..3f4f52e48 100644 --- a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/RemoveResultFromCollectionController.java +++ b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/web/mvc/result/RemoveResultFromCollectionController.java @@ -3,16 +3,24 @@ package org.argeo.slc.web.mvc.result; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.argeo.slc.SlcException; +import org.argeo.slc.core.test.tree.TreeTestResult; +import org.argeo.slc.core.test.tree.TreeTestResultCollection; +import org.argeo.slc.dao.test.tree.TreeTestResultCollectionDao; import org.argeo.slc.services.test.TestManagerService; import org.argeo.slc.web.mvc.AbstractServiceController; +import org.springframework.util.PatternMatchUtils; import org.springframework.web.servlet.ModelAndView; public class RemoveResultFromCollectionController extends AbstractServiceController { + private final TreeTestResultCollectionDao testResultCollectionDao; private final TestManagerService testManagerService; public RemoveResultFromCollectionController( + TreeTestResultCollectionDao testResultCollectionDao, TestManagerService testManagerService) { + this.testResultCollectionDao = testResultCollectionDao; this.testManagerService = testManagerService; } @@ -21,8 +29,56 @@ public class RemoveResultFromCollectionController extends throws Exception { String collectionId = request.getParameter("collectionId"); - String resultUuid = request.getParameter("resultUuid"); + String[] resultUuids = request.getParameterValues("resultUuid"); + String[] attrNames = request.getParameterValues("attrName"); + String[] attrPatterns = request.getParameterValues("attrPattern"); + + // Checks + if (collectionId == null) + throw new SlcException("A collection id must be specified"); + if (attrNames != null + && (attrPatterns == null || attrNames.length != attrPatterns.length)) + throw new SlcException( + "There must be as many attrName as attrPatterns"); + + // Remove specified results + if (resultUuids != null) + for (String resultUuid : resultUuids) + testManagerService.removeResultFromCollection(collectionId, + resultUuid); + + if (attrNames != null) { + TreeTestResultCollection sourceCollection = testResultCollectionDao + .getTestResultCollection(collectionId); + + int index = 0; + for (String attrName : attrNames) { + String attrPattern = attrPatterns[index];// safe: checked above + + for (TreeTestResult treeTestResult : sourceCollection + .getResults()) { + if (PatternMatchUtils.simpleMatch(attrPattern, + treeTestResult.getAttributes().get(attrName))) { + testManagerService.removeResultFromCollection( + collectionId, treeTestResult.getUuid()); + } + } + index++; + } + } else { + if (resultUuids == null) {// no specs + // remove all + // TODO: optimize + TreeTestResultCollection sourceCollection = testResultCollectionDao + .getTestResultCollection(collectionId); + for (TreeTestResult treeTestResult : sourceCollection + .getResults()) { + testManagerService.removeResultFromCollection(collectionId, + treeTestResult.getUuid()); + } + + } + } - testManagerService.removeResultFromCollection(collectionId, resultUuid); } } diff --git a/runtime/org.argeo.slc.server/src/main/resources/org/argeo/slc/server/spring/slc-service-servlet.xml b/runtime/org.argeo.slc.server/src/main/resources/org/argeo/slc/server/spring/slc-service-servlet.xml index 3f5f2ae42..c245ecabd 100644 --- a/runtime/org.argeo.slc.server/src/main/resources/org/argeo/slc/server/spring/slc-service-servlet.xml +++ b/runtime/org.argeo.slc.server/src/main/resources/org/argeo/slc/server/spring/slc-service-servlet.xml @@ -16,6 +16,7 @@ + @@ -33,6 +34,12 @@ + + + + +