use of org.springframework.social.InternalServerErrorException in project records-management by Alfresco.
the class FilePlanComponentsApiUtils method lookupAndValidateRelativePath.
/**
* Helper method that creates a relative path if it doesn't already exist and if relative path is not read only.
* If relative path is read only an exception will be thrown if the provided relative path does not exist.
* The relative path will be build with nodes of the type specified in nodesType
* If the relative path already exists the method validates if the last element is of type nodesType
* The method does not validate the type of parentNodeRef
*
* @param parentNodeRef the first node of the path
* @param relativePath a string representing the relative path in the format "Folder1/Folder2/Folder3"
* @param readOnlyRelativePath the flag that indicates if the relativePath should be created if doesn't exist or not
* @param nodesType the type of all the containers in the path
* @return the last element of the relative path
*/
public NodeRef lookupAndValidateRelativePath(final NodeRef parentNodeRef, String relativePath, boolean readOnlyRelativePath, QName nodesType) {
mandatory("parentNodeRef", parentNodeRef);
mandatory("nodesType", nodesType);
if (StringUtils.isBlank(relativePath)) {
return parentNodeRef;
}
List<String> pathElements = getPathElements(relativePath);
if (pathElements.isEmpty()) {
return parentNodeRef;
}
/*
* Get the latest existing path element
*/
NodeRef lastNodeRef = parentNodeRef;
int i = 0;
for (; i < pathElements.size(); i++) {
final String pathElement = pathElements.get(i);
final NodeRef contextParentNodeRef = lastNodeRef;
// Navigation should not check permissions
NodeRef child = authenticationUtil.runAsSystem(new RunAsWork<NodeRef>() {
@Override
public NodeRef doWork() throws Exception {
return nodeService.getChildByName(contextParentNodeRef, ContentModel.ASSOC_CONTAINS, pathElement);
}
});
if (child == null) {
break;
}
lastNodeRef = child;
}
if (i == pathElements.size()) {
QName nodeType = nodeService.getType(lastNodeRef);
if (!nodeType.equals(nodesType)) {
throw new InvalidArgumentException("The given id:'" + parentNodeRef.getId() + "' and the relative path '" + relativePath + "' reach a node type invalid for this endpoint." + " Expected nodeType is:" + nodesType.toString() + ". Actual nodeType is:" + nodeType);
}
return lastNodeRef;
} else {
if (!readOnlyRelativePath) {
pathElements = pathElements.subList(i, pathElements.size());
} else {
throw new NotFoundException("The entity with relativePath: " + relativePath + " was not found.");
}
}
/*
* Starting from the latest existing element create the rest of the elements
*/
if (nodesType.equals(RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER)) {
for (String pathElement : pathElements) {
lastNodeRef = fileFolderService.create(lastNodeRef, pathElement, RecordsManagementModel.TYPE_UNFILED_RECORD_FOLDER).getNodeRef();
}
} else if (nodesType.equals(RecordsManagementModel.TYPE_RECORD_CATEGORY)) {
for (String pathElement : pathElements) {
lastNodeRef = filePlanService.createRecordCategory(lastNodeRef, pathElement);
}
} else {
// Throw internal error as this method should not be called for other types
throw new InternalServerErrorException("Creating relative path of type '" + nodesType + "' not suported for this endpoint");
}
return lastNodeRef;
}
Aggregations