diff --git a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-common-SqlMap.xml b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-common-SqlMap.xml
index e3f1c1a89d..d3b63f4467 100644
--- a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-common-SqlMap.xml
+++ b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-common-SqlMap.xml
@@ -19,5 +19,23 @@
prop.string_value = ?
-
+
+
+
+
\ No newline at end of file
diff --git a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-query-context.xml b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-query-context.xml
index 59f1687234..dd5949efb9 100644
--- a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-query-context.xml
+++ b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/query/rm-query-context.xml
@@ -19,6 +19,8 @@
+
+
diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/ChildrenWithPropertyValuesQueryParams.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/ChildrenWithPropertyValuesQueryParams.java
new file mode 100644
index 0000000000..b0faa993e4
--- /dev/null
+++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/ChildrenWithPropertyValuesQueryParams.java
@@ -0,0 +1,73 @@
+/*
+ * #%L
+ * Alfresco Records Management Module
+ * %%
+ * Copyright (C) 2005 - 2017 Alfresco Software Limited
+ * %%
+ * This file is part of the Alfresco software.
+ * -
+ * If the software was purchased under a paid Alfresco license, the terms of
+ * the paid license agreement will prevail. Otherwise, the software is
+ * provided under the following open source license terms:
+ * -
+ * Alfresco is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * -
+ * Alfresco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ * -
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Alfresco. If not, see .
+ * #L%
+ */
+package org.alfresco.module.org_alfresco_module_rm.query;
+
+import java.util.Collection;
+
+/**
+ * Select parameter for select_CountChildrenWithPropertyValues.
+ *
+ * @author Ana Manolache
+ * @since 2.6
+ */
+public class ChildrenWithPropertyValuesQueryParams
+{
+ private Long parentId;
+ private Long propertyQnameId;
+ private Collection propertyValues;
+
+ public Long getParentId()
+ {
+ return parentId;
+ }
+
+ public void setParentId(Long parentId)
+ {
+ this.parentId = parentId;
+ }
+
+ public Long getPropertyQnameId()
+ {
+ return propertyQnameId;
+ }
+
+ public void setPropertyQnameId(Long propertyQnameId)
+ {
+ this.propertyQnameId = propertyQnameId;
+ }
+
+ public Collection getPropertyValues()
+ {
+ return propertyValues;
+ }
+
+ public void setPropertyValues(Collection propertyValues)
+ {
+ this.propertyValues = propertyValues;
+ }
+}
+
diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAO.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAO.java
index fec9ad5353..afd031cd39 100644
--- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAO.java
+++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAO.java
@@ -27,6 +27,11 @@
package org.alfresco.module.org_alfresco_module_rm.query;
+import java.util.Collection;
+
+import org.alfresco.service.cmr.repository.NodeRef;
+import org.alfresco.service.namespace.QName;
+
/**
* Records management query DAO
*
@@ -46,4 +51,15 @@ public interface RecordsManagementQueryDAO
* @return int count
*/
int getCountRmaIdentifier(String identifierValue);
+
+ /**
+ * Returns whether a given node contains children with one of the given values for the given property
+ *
+ * @param parent the parent to evaluate
+ * @param property the QName of the property to evaluate
+ * @param propertyValues the list of values to look for
+ * @return true if there is at least one child with one of the values from the list set on the given property
+ * false otherwise
+ */
+ public boolean hasChildrenWithPropertyValues(NodeRef parent, QName property, Collection propertyValues);
}
diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAOImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAOImpl.java
index 82744e7815..f62712d6ab 100644
--- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAOImpl.java
+++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/query/RecordsManagementQueryDAOImpl.java
@@ -27,11 +27,16 @@
package org.alfresco.module.org_alfresco_module_rm.query;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
+import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.qname.QNameDAO;
+import org.alfresco.repo.tenant.TenantService;
+import org.alfresco.service.cmr.repository.InvalidNodeRefException;
+import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.mybatis.spring.SqlSessionTemplate;
@@ -45,12 +50,15 @@ import org.mybatis.spring.SqlSessionTemplate;
public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO, RecordsManagementModel
{
private static final String COUNT_IDENTIFIER = "alfresco.query.rm.select_CountRMIndentifier";
+ private static final String COUNT_CHILDREN_WITH_PROPERTY_VALUES = "select_CountChildrenWithPropertyValues";
/** SQL session template */
protected SqlSessionTemplate template;
/** QName DAO */
protected QNameDAO qnameDAO;
+ protected NodeDAO nodeDAO;
+ protected TenantService tenantService;
/**
* @param sqlSessionTemplate SQL session template
@@ -67,7 +75,17 @@ public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO,
{
this.qnameDAO = qnameDAO;
}
-
+
+ public void setNodeDAO(NodeDAO nodeDAO)
+ {
+ this.nodeDAO = nodeDAO;
+ }
+
+ public void setTenantService(TenantService tenantService)
+ {
+ this.tenantService = tenantService;
+ }
+
/**
* @see org.alfresco.module.org_alfresco_module_rm.query.RecordsManagementQueryDAO#getCountRmaIdentifier(java.lang.String)
*/
@@ -97,4 +115,39 @@ public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO,
return result;
}
+ @Override
+ public boolean hasChildrenWithPropertyValues(NodeRef parent, QName property, Collection propertyValues)
+ {
+ if(propertyValues.isEmpty())
+ {
+ return false;
+ }
+
+ ChildrenWithPropertyValuesQueryParams queryParams = new ChildrenWithPropertyValuesQueryParams();
+
+ // Set the parent node id
+ Pair nodePair = nodeDAO.getNodePair(tenantService.getName(parent));
+ if (nodePair == null)
+ {
+ throw new InvalidNodeRefException("The parent node does not exist.", parent);
+ }
+ Long parentNodeId = nodePair.getFirst();
+ queryParams.setParentId(parentNodeId);
+
+ // Set the property qname id
+ Pair pair = qnameDAO.getQName(property);
+ if (pair == null)
+ {
+ return false;
+ }
+ queryParams.setPropertyQnameId(pair.getFirst());
+
+
+ // Set the property values
+ queryParams.setPropertyValues(propertyValues);
+
+ // Perform the query
+ Long count = template.selectOne(COUNT_CHILDREN_WITH_PROPERTY_VALUES, queryParams);
+ return count > 0;
+ }
}
diff --git a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/legacy/service/RecordsManagementQueryDAOImplTest.java b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/legacy/service/RecordsManagementQueryDAOImplTest.java
index 5f29c12f7a..49a1fbe101 100644
--- a/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/legacy/service/RecordsManagementQueryDAOImplTest.java
+++ b/rm-community/rm-community-repo/test/java/org/alfresco/module/org_alfresco_module_rm/test/legacy/service/RecordsManagementQueryDAOImplTest.java
@@ -27,10 +27,17 @@
package org.alfresco.module.org_alfresco_module_rm.test.legacy.service;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
import org.alfresco.module.org_alfresco_module_rm.query.RecordsManagementQueryDAO;
import org.alfresco.module.org_alfresco_module_rm.record.RecordService;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMTestCase;
+import org.alfresco.service.cmr.repository.NodeRef;
+import org.alfresco.service.namespace.QName;
+import org.alfresco.util.GUID;
/**
* Records Management Query DAO
@@ -86,5 +93,248 @@ public class RecordsManagementQueryDAOImplTest extends BaseRMTestCase implements
});
}
-
+ /**
+ * Given a folder containing 3 files with the descriptions set
+ * When I check if the folder contains children having the description of file2 or file2
+ * Then the answer is positive
+ */
+ @org.junit.Test
+ public void testHasChildrenWithPropertyValues_someChildrenWithValues() throws Exception
+ {
+ doBehaviourDrivenTest(new BehaviourDrivenTest()
+ {
+ NodeRef parentFolder;
+ NodeRef file1;
+ NodeRef file2;
+ NodeRef file3;
+ String propValue1 = "descr1"; // set on file1
+ String propValue2 = "descr2"; // set on file2
+ String propValue3 = "descr3"; // set on file3
+ String propValue4 = "descr4"; // not set on any file
+ Boolean result;
+
+ @Override
+ public void given() throws Exception
+ {
+ setupCollaborationSiteTestDataImpl();
+ parentFolder = fileFolderService.create(documentLibrary, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file1 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file2 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file3 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+
+ nodeService.setProperty(file1, PROP_DESCRIPTION, propValue1);
+ nodeService.setProperty(file2, PROP_DESCRIPTION, propValue2);
+ nodeService.setProperty(file3, PROP_DESCRIPTION, propValue3);
+ }
+
+ @Override
+ public void when() throws Exception
+ {
+ result = queryDAO.hasChildrenWithPropertyValues(parentFolder, PROP_DESCRIPTION, Arrays.asList(propValue1, propValue2, propValue4));
+ }
+
+ @Override
+ public void then() throws Exception
+ {
+ assertTrue(result);
+ }
+
+ @Override
+ public void after() throws Exception
+ {
+ if (parentFolder != null && nodeService.exists(parentFolder))
+ {
+ nodeService.deleteNode(parentFolder);
+ }
+ }
+ });
+ }
+
+ /**
+ * Given a folder containing 3 files with the descriptions unset
+ * When I check if the folder contains children having certain descriptions
+ * Then the answer is negative
+ */
+ @org.junit.Test
+ public void testHasChildrenWithPropertyValues_propertyNotSetOnChildren() throws Exception
+ {
+ doBehaviourDrivenTest(new BehaviourDrivenTest()
+ {
+ NodeRef parentFolder;
+ NodeRef file1;
+ NodeRef file2;
+ NodeRef file3;
+ Boolean result;
+
+ @Override
+ public void given() throws Exception
+ {
+ setupCollaborationSiteTestDataImpl();
+ parentFolder = fileFolderService.create(documentLibrary, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file1 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file2 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file3 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ }
+
+ @Override
+ public void when() throws Exception
+ {
+ result = queryDAO.hasChildrenWithPropertyValues(parentFolder, PROP_DESCRIPTION, Arrays.asList("descr1", "descr2", "descr3"));
+ }
+
+ @Override
+ public void then() throws Exception
+ {
+ assertFalse(result);
+ }
+
+ @Override
+ public void after() throws Exception
+ {
+ if (parentFolder != null && nodeService.exists(parentFolder))
+ {
+ nodeService.deleteNode(parentFolder);
+ }
+ }
+ });
+ }
+
+ /**
+ * Given a folder with no children but the property set on itself
+ * When I check if the folder contains children having certain descriptions
+ * Then the answer is negative
+ */
+ @org.junit.Test
+ public void testHasChildrenWithPropertyValues_noChildren() throws Exception
+ {
+ doBehaviourDrivenTest(new BehaviourDrivenTest()
+ {
+ NodeRef folder;
+ String propValue = "descr";
+ Boolean result;
+
+ @Override
+ public void given() throws Exception
+ {
+ setupCollaborationSiteTestDataImpl();
+ folder = fileFolderService.create(documentLibrary, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ nodeService.setProperty(folder, PROP_DESCRIPTION, propValue);
+ }
+
+ @Override
+ public void when() throws Exception
+ {
+ result = queryDAO.hasChildrenWithPropertyValues(folder, PROP_DESCRIPTION, Arrays.asList("descr"));
+ }
+
+ @Override
+ public void then() throws Exception
+ {
+ assertFalse(result);
+ }
+
+ @Override
+ public void after() throws Exception
+ {
+ if (folder != null && nodeService.exists(folder))
+ {
+ nodeService.deleteNode(folder);
+ }
+ }
+ });
+ }
+
+ /**
+ * Given a folder with children and an unused property
+ * When I check if the folder contains children having the unused property
+ * Then the answer is negative
+ */
+ @org.junit.Test
+ public void testHasChildrenWithPropertyValues_propertyNotUsed() throws Exception
+ {
+ doBehaviourDrivenTest(new BehaviourDrivenTest()
+ {
+ NodeRef parentFolder;
+ QName property;
+ Boolean result;
+
+ @Override
+ public void given() throws Exception
+ {
+ setupCollaborationSiteTestDataImpl();
+ parentFolder = fileFolderService.create(documentLibrary, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ property = QName.createQName(URI, "customProp-" + GUID.generate());
+ }
+
+ @Override
+ public void when() throws Exception
+ {
+ result = queryDAO.hasChildrenWithPropertyValues(folder, property, Arrays.asList("descr"));
+ }
+
+ @Override
+ public void then() throws Exception
+ {
+ assertFalse(result);
+ }
+
+ @Override
+ public void after() throws Exception
+ {
+ if (folder != null && nodeService.exists(folder))
+ {
+ nodeService.deleteNode(folder);
+ }
+ }
+ });
+ }
+
+ /**
+ * Given any folder and any property
+ * When I pass an empty array to the hasChildrenWithPropertyValues method
+ * Then the answer is negative
+ */
+ @org.junit.Test
+ public void testHasChildrenWithPropertyValues_emptyArray() throws Exception
+ {
+ doBehaviourDrivenTest(new BehaviourDrivenTest()
+ {
+ NodeRef parentFolder;
+ NodeRef file1;
+ Boolean result;
+
+ @Override
+ public void given() throws Exception
+ {
+ setupCollaborationSiteTestDataImpl();
+ parentFolder = fileFolderService.create(documentLibrary, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+ file1 = fileFolderService.create(parentFolder, GUID.generate(), ContentModel.TYPE_FOLDER).getNodeRef();
+
+ nodeService.setProperty(file1, PROP_DESCRIPTION, "descr1");
+ }
+
+ @Override
+ public void when() throws Exception
+ {
+ result = queryDAO.hasChildrenWithPropertyValues(folder, PROP_DESCRIPTION, new ArrayList());
+ }
+
+ @Override
+ public void then() throws Exception
+ {
+ assertFalse(result);
+ }
+
+ @Override
+ public void after() throws Exception
+ {
+ if (folder != null && nodeService.exists(folder))
+ {
+ nodeService.deleteNode(folder);
+ }
+ }
+ });
+ }
}