Merged DEV/WEBAPP-API (5.2.0) to 5.2.N (5.2.N)

131265 kroast: Merged 130934 from HEAD - SHA-1698

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@131488 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2016-10-17 15:47:06 +00:00
parent 5b079ab544
commit 829474d7e4
2 changed files with 184 additions and 64 deletions

View File

@@ -82,7 +82,6 @@ import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.lock.LockStatus;
import org.alfresco.service.cmr.model.FileExistsException;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
@@ -1870,6 +1869,64 @@ public class ScriptNode implements Scopeable, NamespacePrefixResolverProvider
return newInstance(fileInfo.getNodeRef(), this.services, this.scope);
}
/**
* Create a path of folder (cm:folder) nodes as a child of this node.
* <p>
* This method operates like a unix 'mkdir -p' no error if existing, make parent directories as needed.
* <p>
* Beware: Any unsaved property changes will be lost when this is called. To preserve property changes call {@link #save()} first.
*
* @param path Folder path to create - of the form "One/Two/Three". Leading and trailing slashes are not expected
* to be present in the supplied path.
*
* @return reference to the last child of the newly created folder node(s) or null if failed to create.
*/
public ScriptNode createFolderPath(String path)
{
ParameterCheck.mandatoryString("Folder path", path);
List<String> pathElements = Arrays.asList(path.split("/"));
NodeRef currentParentRef = this.nodeRef;
// just loop and create if necessary
for (final String element : pathElements)
{
final NodeRef contextNodeRef = currentParentRef;
// does it exist?
// Navigation should not check permissions
NodeRef nodeRef = AuthenticationUtil.runAs(new RunAsWork<NodeRef>()
{
@Override
public NodeRef doWork() throws Exception
{
return nodeService.getChildByName(contextNodeRef, ContentModel.ASSOC_CONTAINS, element);
}
}, AuthenticationUtil.getSystemUserName());
if (nodeRef == null)
{
// Checks for create permissions as the fileFolderService is a public service.
FileInfo createdFileInfo = services.getFileFolderService().create(
currentParentRef, element, ContentModel.TYPE_FOLDER);
currentParentRef = createdFileInfo.getNodeRef();
}
else if (!services.getDictionaryService().isSubClass(nodeService.getType(nodeRef), ContentModel.TYPE_FOLDER))
{
String parentName = (String) nodeService.getProperty(contextNodeRef, ContentModel.PROP_NAME);
throw new ScriptException("Name [" + element + "] already exists in the target parent: " + parentName);
}
else
{
// it exists
currentParentRef = nodeRef;
}
}
reset();
return newInstance(currentParentRef, this.services, this.scope);
}
/**
* Create a new Node of the specified type as a child of this node.
*

View File

@@ -30,6 +30,7 @@ package org.alfresco.repo.jscript;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.io.InputStream;
import java.util.ArrayList;
@@ -52,6 +53,7 @@ import org.alfresco.repo.tenant.TenantAdminService;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.version.VersionableAspect;
import org.alfresco.scripts.ScriptException;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.dictionary.PropertyDefinition;
@@ -624,6 +626,67 @@ public class ScriptNodeTest
NODE_SERVICE.removeProperty(newNode2, ContentModel.PROP_CONTENT);
}
@Test
public void testCreateFolderPath()
{
Repository repositoryHelper = (Repository) APP_CONTEXT_INIT.getApplicationContext().getBean("repositoryHelper");
NodeRef companyHome = repositoryHelper.getCompanyHome();
NodeRef folderNodeRef = testNodes.createNode(companyHome, "foldertest1", ContentModel.TYPE_FOLDER, AuthenticationUtil.getFullyAuthenticatedUser());
assertNotNull(folderNodeRef);
ScriptNode folderNode = new ScriptNode(folderNodeRef, SERVICE_REGISTRY);
// create a simple path of depth one - does not exist yet
assertNotNull(folderNode.createFolderPath("One"));
// create a simple path of depth one - does exist (which should be ignored and continue - createFolderPath() emulates 'mkdir -p' behaviour)
assertNotNull(folderNode.createFolderPath("One"));
// create depth path - none of which exists
assertNotNull(folderNode.createFolderPath("A/B"));
// create depth path - all of which exists
assertNotNull(folderNode.createFolderPath("A/B"));
// create depth path - some of which exists
assertNotNull(folderNode.createFolderPath("A/B/C"));
// test last child is returned as the result
NodeRef folderARef = NODE_SERVICE.getChildByName(folderNodeRef, ContentModel.ASSOC_CONTAINS, "A");
NodeRef folderBRef = NODE_SERVICE.getChildByName(folderARef, ContentModel.ASSOC_CONTAINS, "B");
assertEquals(folderBRef, folderNode.createFolderPath("A/B").getNodeRef());
// test case where folder should not should be created - under a content node
NodeRef contentNodeRef = testNodes.createNode(folderNodeRef, "CONTENT", ContentModel.TYPE_CONTENT, AuthenticationUtil.getFullyAuthenticatedUser());
assertNotNull(contentNodeRef);
try
{
folderNode.createFolderPath("CONTENT/A");
fail("Should not be able to create folder path when all nodes are not subtypes of cm:folder");
}
catch (ScriptException se1)
{
// expected
}
// test string edge cases
try
{
assertNotNull(folderNode.createFolderPath("/A/B"));
fail("Leading slash not expected");
}
catch (Throwable e1)
{
// expected
}
try
{
assertNotNull(folderNode.createFolderPath("A/B/"));
fail("Trailing slash not expected");
}
catch (Throwable e2)
{
// expected
}
}
private ScriptableObject getScope()
{
// Create a scope for the value conversion. This scope will be an empty scope exposing basic Object and Function, sufficient for value-conversion.