MNT-20740 New db query

This commit is contained in:
Sara Aspery
2019-08-22 11:19:34 +01:00
parent e5bb53b686
commit ecbaa3b40c
6 changed files with 100 additions and 47 deletions

View File

@@ -8,6 +8,10 @@
<parameter property="idValue" jdbcType="BIGINT" javaType="java.lang.String"/>
</parameterMap>
<resultMap id="result_NodeIds" type="java.lang.Long">
<result property="node.id" column="node_id" jdbcType="BIGINT" javaType="java.lang.Long"/>
</resultMap>
<select id="select_CountRMIndentifier" parameterMap="parameter_CountRMIndentifier" resultType="java.lang.Integer">
select
count(*)
@@ -35,6 +39,21 @@
</select>
<!-- Get list of node ids which reference given content url -->
<select id="select_NodeIdsWhichReferenceContentUrl"
parameterType="ContentUrl"
resultMap="result_NodeIds">
select
p.node_id
from
alf_content_url cu
LEFT OUTER JOIN alf_content_data cd ON (cd.content_url_id = cu.id)
LEFT OUTER JOIN alf_node_properties p ON (p.long_value = cd.id)
WHERE
content_url_short = #{contentUrlShort} and
content_url_crc = #{contentUrlCrc}
</select>
</mapper>

View File

@@ -3,18 +3,8 @@
<configuration>
<typeAliases>
<!-- General -->
<typeAlias alias="Ids" type="org.alfresco.ibatis.IdsEntity"/>
<!-- Content -->
<typeAlias alias="Mimetype" type="org.alfresco.repo.domain.mimetype.MimetypeEntity"/>
<typeAlias alias="Encoding" type="org.alfresco.repo.domain.encoding.EncodingEntity"/>
<typeAlias alias="ContentUrl" type="org.alfresco.repo.domain.contentdata.ContentUrlEntity"/>
<typeAlias alias="ContentUrlUpdate" type="org.alfresco.repo.domain.contentdata.ContentUrlUpdateEntity"/>
<typeAlias alias="ContentData" type="org.alfresco.repo.domain.contentdata.ContentDataEntity"/>
<typeAlias alias="ContentUrlKey" type="org.alfresco.repo.domain.contentdata.ContentUrlKeyEntity"/>
<typeAlias alias="ContentUrlOrphanQuery" type="org.alfresco.repo.domain.contentdata.ContentUrlOrphanQuery"/>
<typeAlias alias="SymmetricKeyCount" type="org.alfresco.repo.domain.contentdata.SymmetricKeyCount"/>
</typeAliases>
<typeHandlers>
@@ -23,7 +13,6 @@
<mappers>
<mapper resource="alfresco/module/org_alfresco_module_rm/query/rm-common-SqlMap.xml"/>
<mapper resource="alfresco/ibatis/#resource.dialect#/content-common-SqlMap.xml"/>
</mappers>
</configuration>

View File

@@ -29,7 +29,6 @@ package org.alfresco.module.org_alfresco_module_rm.query;
import java.util.Set;
import org.alfresco.repo.domain.contentdata.ContentUrlEntity;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;
@@ -64,9 +63,8 @@ public interface RecordsManagementQueryDAO
public Set<String> getChildrenStringPropertyValues(NodeRef parent, QName property);
/**
* @param contentUrl the URL of the content url entity
* @return Return the entity or null if it doesn't exist or is still
* referenced by a content_data entity
* @param contentUrl the URL of the content url entity
* @return Return a set of UUIDs which reference the given node
*/
public ContentUrlEntity getContentUrlEntityUnreferenced(String contentUrl);
Set<String> getNodeRefsWhichReferenceContentUrl(String contentUrl);
}

View File

@@ -30,18 +30,24 @@ package org.alfresco.module.org_alfresco_module_rm.query;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel;
import org.alfresco.repo.domain.contentdata.ContentUrlEntity;
import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.tenant.TenantService;
import org.alfresco.repo.version.Version2Model;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybatis.spring.SqlSessionTemplate;
/**
@@ -54,7 +60,7 @@ public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO,
{
private static final String COUNT_IDENTIFIER = "alfresco.query.rm.select_CountRMIndentifier";
private static final String GET_CHILDREN_PROPERTY_VALUES = "select_GetStringPropertyValuesOfChildren";
private static final String SELECT_CONTENT_URL_BY_KEY_UNREFERENCED = "alfresco.content.select_ContentUrlByKeyUnreferenced";
private static final String SELECT_NODE_IDS_WHICH_REFERENCE_CONTENT_URL = "select_NodeIdsWhichReferenceContentUrl";
/** SQL session template */
protected SqlSessionTemplate template;
@@ -103,7 +109,7 @@ public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO,
if (pair != null)
{
// create query params
Map<String, Object> params = new HashMap<String, Object>(2);
Map<String, Object> params = new HashMap<>(2);
params.put("qnameId", pair.getFirst());
params.put("idValue", identifierValue);
@@ -147,7 +153,7 @@ public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO,
}
@Override
public ContentUrlEntity getContentUrlEntityUnreferenced(String contentUrl)
public Set<String> getNodeRefsWhichReferenceContentUrl(String contentUrl)
{
ContentUrlEntity contentUrlEntity = new ContentUrlEntity();
contentUrlEntity.setContentUrl(contentUrl);
@@ -155,8 +161,31 @@ public class RecordsManagementQueryDAOImpl implements RecordsManagementQueryDAO,
{
contentUrlEntity.setContentUrlShort(contentUrlEntity.getContentUrlShort().toLowerCase());
}
contentUrlEntity = (ContentUrlEntity) template.selectOne(SELECT_CONTENT_URL_BY_KEY_UNREFERENCED, contentUrlEntity);
return contentUrlEntity;
// Get all the node ids which reference the given content url
List<Long> nodeIds = template.selectList(SELECT_NODE_IDS_WHICH_REFERENCE_CONTENT_URL, contentUrlEntity);
// create a set of uuids which reference the content url
Set<String> nodesReferencingContentUrl = new HashSet<>();
for(Long nodeId : nodeIds)
{
String uuidToAdd;
// if the referencing node is a version2Store reference to the content url, add the uuid for the version 2 frozen node ref
NodeRef version2FrozenNodeRef = (NodeRef) nodeDAO.getNodeProperty(nodeId, Version2Model.PROP_QNAME_FROZEN_NODE_REF);
if (version2FrozenNodeRef != null)
{
uuidToAdd = version2FrozenNodeRef.getId();
}
// add the uuid for the node ref of the referencing node
else
{
uuidToAdd = (String) nodeDAO.getNodeProperty(nodeId, ContentModel.PROP_NODE_UUID);
}
nodesReferencingContentUrl.add(uuidToAdd);
}
return nodesReferencingContentUrl;
}
}

View File

@@ -28,13 +28,14 @@ package org.alfresco.module.org_alfresco_module_rm.util;
import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.query.RecordsManagementQueryDAO;
import org.alfresco.repo.domain.contentdata.ContentUrlEntity;
import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.NodeRef;
import java.util.Set;
/**
* Utility class to duplicate the content of a node without triggering the audit or versioning behaviours
* @author Ross Gale
@@ -94,8 +95,9 @@ public class ContentBinDuplicationUtility
{
boolean hasAtLeastOneOtherReference = false;
String contentUrl = contentService.getReader(nodeRef, ContentModel.PROP_CONTENT).getContentUrl();
ContentUrlEntity contentUrlEntity = recordsManagementQueryDAO.getContentUrlEntityUnreferenced(contentUrl);
if (contentUrlEntity == null)
Set<String> referencesToContentNode = recordsManagementQueryDAO.getNodeRefsWhichReferenceContentUrl(contentUrl);
if (referencesToContentNode.size() > 1)
{
hasAtLeastOneOtherReference = true;
}

View File

@@ -29,7 +29,6 @@ package org.alfresco.module.org_alfresco_module_rm.util;
import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.query.RecordsManagementQueryDAO;
import org.alfresco.repo.domain.contentdata.ContentUrlEntity;
import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentService;
@@ -41,6 +40,10 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
@@ -54,6 +57,9 @@ import static org.mockito.Mockito.when;
*/
public class ContentBinDuplicationUtilityUnitTest
{
private final static NodeRef NODE_REF = new NodeRef("some://test/noderef");
private final static NodeRef NODE_REF2 = new NodeRef("some://test/anothernoderef");
private final static String CONTENT_URL = "someContentUrl";
@Mock
private ContentService contentService;
@@ -85,10 +91,9 @@ public class ContentBinDuplicationUtilityUnitTest
@Test
public void testContentUrlIsUpdated()
{
NodeRef nodeRef = new NodeRef("some://test/noderef");
when(contentService.getReader(nodeRef, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true)).thenReturn(contentWriter);
contentBinDuplicationUtility.duplicate(nodeRef);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getWriter(NODE_REF, ContentModel.PROP_CONTENT, true)).thenReturn(contentWriter);
contentBinDuplicationUtility.duplicate(NODE_REF);
verify(contentWriter, times(1)).putContent(contentReader);
checkBehaviours(1);
}
@@ -99,9 +104,8 @@ public class ContentBinDuplicationUtilityUnitTest
@Test
public void testDuplicationDoesntHappenWithNoContent()
{
NodeRef nodeRef = new NodeRef("some://test/noderef");
when(contentService.getReader(nodeRef, ContentModel.PROP_CONTENT)).thenReturn(null);
contentBinDuplicationUtility.duplicate(nodeRef);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(null);
contentBinDuplicationUtility.duplicate(NODE_REF);
verify(contentWriter, times(0)).putContent(contentReader);
checkBehaviours(1);
}
@@ -112,32 +116,44 @@ public class ContentBinDuplicationUtilityUnitTest
@Test
public void testHasAtLeastOneOtherReference()
{
NodeRef nodeRef = new NodeRef("some://test/noderef");
String contentUrl = "someContentUrl";
Set<String> multipleReferences = new HashSet<>();
Collections.addAll(multipleReferences, NODE_REF.getId(), NODE_REF2.getId());
when(contentService.getReader(nodeRef, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getReader(nodeRef, ContentModel.PROP_CONTENT).getContentUrl()).thenReturn(contentUrl);
when(recordsManagementQueryDAO.getContentUrlEntityUnreferenced(contentUrl)).thenReturn(null);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT).getContentUrl()).thenReturn(CONTENT_URL);
when(recordsManagementQueryDAO.getNodeRefsWhichReferenceContentUrl(CONTENT_URL)).thenReturn(multipleReferences);
boolean hasReference = contentBinDuplicationUtility.hasAtLeastOneOtherReference(nodeRef);
assertTrue(hasReference);
assertTrue(contentBinDuplicationUtility.hasAtLeastOneOtherReference(NODE_REF));
}
/**
* Test hasAtLeastOneOtherReference returns false when node has no other reference to it
* Test hasAtLeastOneOtherReference returns false when node has no other reference to it other than its own content ref
*/
@Test
public void testHasNoOtherReference()
{
NodeRef nodeRef = new NodeRef("some://test/noderef");
String contentUrl = "someContentUrl";
ContentUrlEntity contentUrlEntity = new ContentUrlEntity();
Set<String> singleReference = Collections.singleton(NODE_REF.getId());
when(contentService.getReader(nodeRef, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getReader(nodeRef, ContentModel.PROP_CONTENT).getContentUrl()).thenReturn(contentUrl);
when(recordsManagementQueryDAO.getContentUrlEntityUnreferenced(contentUrl)).thenReturn(contentUrlEntity);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT).getContentUrl()).thenReturn(CONTENT_URL);
when(recordsManagementQueryDAO.getNodeRefsWhichReferenceContentUrl(CONTENT_URL)).thenReturn(singleReference);
assertFalse(contentBinDuplicationUtility.hasAtLeastOneOtherReference(nodeRef));
assertFalse(contentBinDuplicationUtility.hasAtLeastOneOtherReference(NODE_REF));
}
/**
* Test hasAtLeastOneOtherReference returns false when node has no references to it at all
*/
@Test
public void testHasNoReferences()
{
Set<String> noReferences = Collections.<String> emptySet();
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT)).thenReturn(contentReader);
when(contentService.getReader(NODE_REF, ContentModel.PROP_CONTENT).getContentUrl()).thenReturn(CONTENT_URL);
when(recordsManagementQueryDAO.getNodeRefsWhichReferenceContentUrl(CONTENT_URL)).thenReturn(noReferences);
assertFalse(contentBinDuplicationUtility.hasAtLeastOneOtherReference(NODE_REF));
}
/**