From 44bed4a796ef6b51ba0c071e7b220e265d7991ae Mon Sep 17 00:00:00 2001 From: Ross Gale Date: Tue, 10 Apr 2018 17:11:26 +0100 Subject: [PATCH] RM-6137 Add record category identifier to search changes added --- .../rm-webscript-context.xml | 5 + .../script/slingshot/RMSearchGet.java | 23 +++ .../script/slingshot/RecordCategoryUtil.java | 86 +++++++++++ .../slingshot/RecordCategoryUtilUnitTest.java | 135 ++++++++++++++++++ 4 files changed, 249 insertions(+) create mode 100644 rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtil.java create mode 100644 rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtilUnitTest.java diff --git a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml index f1a6815869..3c3d0ccf20 100644 --- a/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml +++ b/rm-community/rm-community-repo/config/alfresco/module/org_alfresco_module_rm/rm-webscript-context.xml @@ -466,6 +466,11 @@ + + + + + personDataCache = null; + /** Utility class for record categories */ + private RecordCategoryUtil recordCategoryUtil; + /** * @param recordsManagementSearchService records management search service */ @@ -148,6 +151,14 @@ public class RMSearchGet extends DeclarativeWebScript this.permissionService = permissionService; } + /** + * @param recordCategoryUtil utility class for record categories + */ + public void setRecordCategoryUtil(RecordCategoryUtil recordCategoryUtil) + { + this.recordCategoryUtil = recordCategoryUtil; + } + /** * @param personService person service */ @@ -247,6 +258,7 @@ public class RMSearchGet extends DeclarativeWebScript private String createdBy; private Map nodeProperties; private Map properties; + private String recordCategoryId; public Item(NodeRef parent, NodeRef nodeRef) { @@ -340,6 +352,7 @@ public class RMSearchGet extends DeclarativeWebScript properties.put(prefixName, entry.getValue()); } } + properties.put("rma_recordCategoryIdentifier", recordCategoryUtil.getCategoryIdFromNodeId(nodeRef, false)); } private String getDisplayName(String userName) @@ -445,5 +458,15 @@ public class RMSearchGet extends DeclarativeWebScript { return properties; } + + public String getRecordCategoryId() + { + return recordCategoryId; + } + + public void setRecordCategoryId(String recordCategoryId) + { + this.recordCategoryId = recordCategoryId; + } } } diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtil.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtil.java new file mode 100644 index 0000000000..d70a952a87 --- /dev/null +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtil.java @@ -0,0 +1,86 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2018 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.script.slingshot; + +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.PROP_IDENTIFIER; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RECORD_CATEGORY; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RECORD_FOLDER; + +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.Path; +import org.alfresco.service.cmr.repository.Path.ChildAssocElement; + +/** + * Utility class for record categories + * @author Ross Gale + * @since 2.7 + */ +public class RecordCategoryUtil +{ + /** + * Node service + */ + private NodeService nodeService; + + /** + * Setter for the node service + * @param nodeService Node service + */ + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + } + + /** + * Return the record category id for a file plan element + * @param nodeRef the node reference of the file plan element + * @param includeFolders return an id for records, folders and categories + * @return Record category identifier + */ + public String getCategoryIdFromNodeId(NodeRef nodeRef, boolean includeFolders) + { + if(!includeFolders) + { + if (nodeService.getType(nodeRef).equals(TYPE_RECORD_FOLDER) || nodeService.getType(nodeRef).equals(TYPE_RECORD_CATEGORY)) + { + return null; + } + } + //Search for the first category from the end of the path to save time + Path path = nodeService.getPath(nodeRef); + for(int x = path.size()-1; x >= 0; x--) + { + NodeRef ref = ((ChildAssocElement) path.get(x)).getRef().getChildRef(); + if (nodeService.getType(ref).equals(TYPE_RECORD_CATEGORY)) + { + return nodeService.getProperty(ref, PROP_IDENTIFIER).toString(); + } + } + return null; + } +} diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtilUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtilUnitTest.java new file mode 100644 index 0000000000..d0c01e372b --- /dev/null +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RecordCategoryUtilUnitTest.java @@ -0,0 +1,135 @@ +/* + * #%L + * Alfresco Records Management Module + * %% + * Copyright (C) 2005 - 2018 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.script.slingshot; + +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.PROP_IDENTIFIER; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_NON_ELECTRONIC_DOCUMENT; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RECORD_CATEGORY; +import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RECORD_FOLDER; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.when; + +import org.alfresco.service.cmr.repository.ChildAssociationRef; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.NodeService; +import org.alfresco.service.cmr.repository.Path; +import org.alfresco.service.cmr.repository.Path.ChildAssocElement; +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** + * Tests for methods in the RecordsCategoryUtil class + * + * @author Ross Gale + * @since 2.7 + */ +public class RecordCategoryUtilUnitTest +{ + @Mock + private NodeService nodeService; + + @Mock + private ChildAssocElement element; + + @Mock + private ChildAssociationRef childAssociationRef; + + @InjectMocks + private RecordCategoryUtil recordCategoryUtil; + + private Path path; + + private NodeRef recordNodeRef; + + private NodeRef recordFolderNodeRef; + + private NodeRef categoryNodeRef; + + private NodeRef otherNodeRef; + + @Before + public void setUp() + { + MockitoAnnotations.initMocks(this); + recordNodeRef = new NodeRef("test://recordNode/"); + recordFolderNodeRef = new NodeRef("test://recordFolderNode/"); + categoryNodeRef = new NodeRef("test://categoryNode/"); + otherNodeRef = new NodeRef("test://otherNode/"); + path = new Path(); + path.append(element); + when(nodeService.getType(recordFolderNodeRef)).thenReturn(TYPE_RECORD_FOLDER); + when(nodeService.getType(recordNodeRef)).thenReturn(TYPE_NON_ELECTRONIC_DOCUMENT); + when(nodeService.getPath(recordNodeRef)).thenReturn(path); + when(nodeService.getPath(recordFolderNodeRef)).thenReturn(path); + when(element.getRef()).thenReturn(childAssociationRef); + when(childAssociationRef.getChildRef()).thenReturn(categoryNodeRef); + when(nodeService.getType(categoryNodeRef)).thenReturn(TYPE_RECORD_CATEGORY); + when(nodeService.getProperty(categoryNodeRef, PROP_IDENTIFIER)).thenReturn("RecordCategoryId"); + } + + /** + * Tests an id is returned from a valid node ref + */ + @Test + public void testGetIdFromNodeRef() + { + assertEquals("RecordCategoryId",recordCategoryUtil.getCategoryIdFromNodeId(recordNodeRef,false)); + } + + /** + * Tests an id can be returned for a non record with the correct option selected + */ + @Test + public void testGetIdFromNodeRefReturnsForNonRecordWhenOptionSelected() + { + assertEquals("RecordCategoryId", recordCategoryUtil.getCategoryIdFromNodeId(recordFolderNodeRef, true)); + } + + /** + * Tests no id is returned for a folder if option isn't selected + */ + @Test + public void testGetIdFromNodeRefReturnsNullForNonRecordWhenOptionSelected() + { + assertNull(recordCategoryUtil.getCategoryIdFromNodeId(recordFolderNodeRef,false)); + } + + /** + * Tests no id is returned when a categories isn't found on the path + */ + @Test + public void testGetIdFromNodeRefReturnsNullWithNoCategory() + { + when(nodeService.getPath(recordNodeRef)).thenReturn(new Path()); + assertNull(recordCategoryUtil.getCategoryIdFromNodeId(recordNodeRef, false)); + } +}