From f920f588b519ba4dc9626d12f232caa92b810830 Mon Sep 17 00:00:00 2001 From: Ancuta Morarasu Date: Wed, 11 May 2016 11:14:09 +0000 Subject: [PATCH] Merged HEAD (5.2) to 5.2.N (5.2.1) 126414 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2) 121705 jkaabimofrad: RA-751, RA-752: Added "autoRename" query parameter support when creating a folder/empty file. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126760 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/rest/api/impl/NodesImpl.java | 14 ++++++++ .../alfresco/rest/api/tests/NodeApiTest.java | 32 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/source/java/org/alfresco/rest/api/impl/NodesImpl.java b/source/java/org/alfresco/rest/api/impl/NodesImpl.java index b8c78f0281..4c01c3ae4b 100644 --- a/source/java/org/alfresco/rest/api/impl/NodesImpl.java +++ b/source/java/org/alfresco/rest/api/impl/NodesImpl.java @@ -84,6 +84,7 @@ import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.PropertyDefinition; import org.alfresco.service.cmr.model.FileExistsException; import org.alfresco.service.cmr.model.FileFolderService; +import org.alfresco.service.cmr.model.FileFolderServiceType; import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.model.FileNotFoundException; import org.alfresco.service.cmr.quickshare.QuickShareService; @@ -139,6 +140,7 @@ public class NodesImpl implements Nodes } private final static String PARAM_RELATIVE_PATH = "relativePath"; + private final static String PARAM_AUTO_RENAME = "autoRename"; private final static String PARAM_SELECT_PROPERTIES = "properties"; private final static String PARAM_SELECT_PATH = "path"; @@ -988,6 +990,18 @@ public class NodesImpl implements Nodes props = mapToNodeProperties(nodeInfo.getProperties()); } + // Existing file/folder name handling + final boolean autoRename = Boolean.valueOf(parameters.getParameter(PARAM_AUTO_RENAME)); + if (autoRename && (isContent || FileFolderServiceType.FOLDER.equals(fileFolderService.getType(nodeTypeQName)))) + { + NodeRef existingNode = nodeService.getChildByName(parentNodeRef, ContentModel.ASSOC_CONTAINS, nodeName); + if (existingNode != null) + { + // File already exists, find a unique name + nodeName = findUniqueName(parentNodeRef, nodeName); + } + } + // Create the node NodeRef nodeRef = createNodeImpl(parentNodeRef, nodeName, nodeTypeQName, props); diff --git a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java index 1cf12cfded..80d1d143db 100644 --- a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java @@ -1233,6 +1233,19 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - duplicate name post(postUrl, user1, toJsonAsStringNonNull(f1), 409); + + // Create a folder with a duplicate name (f1), but set the autoRename to true + response = post(postUrl, user1, toJsonAsStringNonNull(f1), "?autoRename=true", 201); + documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class); + assertEquals("f1-1", documentResp.getName()); + + // Create a folder with a duplicate name (f1) again, but set the autoRename to true + response = post(postUrl, user1, toJsonAsStringNonNull(f1), "?autoRename=true", 201); + documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class); + assertEquals("f1-2", documentResp.getName()); + + // -ve test - create a folder with a duplicate name (f1), but set the autoRename to false + post(postUrl, user1, toJsonAsStringNonNull(f1), "?autoRename=false", 409); } /** @@ -1327,6 +1340,25 @@ public class NodeApiTest extends AbstractBaseApiTest // -ve test - duplicate name post(postUrl, user1, toJsonAsStringNonNull(d1), 409); + + // Create a file with a duplicate name (d1.txt), but set the autoRename to true + response = post(postUrl, user1, toJsonAsStringNonNull(d1), "?autoRename=true", 201); + documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class); + assertEquals("d1-1.txt", documentResp.getName()); + + // Create a file with a duplicate name (d1.txt) again, but set the autoRename to true + response = post(postUrl, user1, toJsonAsStringNonNull(d1), "?autoRename=true", 201); + documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class); + assertEquals("d1-2.txt", documentResp.getName()); + + // Create a file with a duplicate name (d1-2.txt) again, but set the autoRename to true + d1.setName("d1-2.txt"); + response = post(postUrl, user1, toJsonAsStringNonNull(d1), "?autoRename=true", 201); + documentResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class); + assertEquals("d1-2-1.txt", documentResp.getName()); + + // -ve test - create a file with a duplicate name (d1-2.txt), but set the autoRename to false + post(postUrl, user1, toJsonAsStringNonNull(d1), "?autoRename=false", 409); } /**