+package org.argeo.slc.web.mvc.result;\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
+/**\r
+ * Copy from a collection to another based on Spring simple pattern matching.\r
+ * \r
+ * @see PatternMatchUtils\r
+ */\r
+public class CopyCollectionToCollectionController extends\r
+ AbstractServiceController {\r
+ private final TreeTestResultCollectionDao testResultCollectionDao;\r
+ private final TestManagerService testManagerService;\r
+\r
+ public CopyCollectionToCollectionController(\r
+ TreeTestResultCollectionDao testResultCollectionDao,\r
+ TestManagerService testManagerService) {\r
+ this.testResultCollectionDao = testResultCollectionDao;\r
+ this.testManagerService = testManagerService;\r
+ }\r
+\r
+ protected void handleServiceRequest(HttpServletRequest request,\r
+ HttpServletResponse response, ModelAndView modelAndView)\r
+ throws Exception {\r
+\r
+ String sourceCollectionId = request.getParameter("sourceCollectionId");\r
+ String targetCollectionId = request.getParameter("targetCollectionId");\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 = testResultCollectionDao\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
+ }\r
+}\r