Search in sources :

Example 1 with InternalServerErrorException

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;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) QName(org.alfresco.service.namespace.QName) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) InternalServerErrorException(org.springframework.social.InternalServerErrorException) ContentQuotaException(org.alfresco.service.cmr.usage.ContentQuotaException) InvalidTypeException(org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) DuplicateAttributeException(org.alfresco.service.cmr.attributes.DuplicateAttributeException) NodeLockedException(org.alfresco.service.cmr.lock.NodeLockedException) InternalServerErrorException(org.springframework.social.InternalServerErrorException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) IOException(java.io.IOException) ContentLimitViolationException(org.alfresco.repo.content.ContentLimitViolationException) RequestEntityTooLargeException(org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Aggregations

IOException (java.io.IOException)1 ContentLimitViolationException (org.alfresco.repo.content.ContentLimitViolationException)1 InvalidTypeException (org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException)1 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)1 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)1 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)1 InsufficientStorageException (org.alfresco.rest.framework.core.exceptions.InsufficientStorageException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)1 RequestEntityTooLargeException (org.alfresco.rest.framework.core.exceptions.RequestEntityTooLargeException)1 DuplicateAttributeException (org.alfresco.service.cmr.attributes.DuplicateAttributeException)1 NodeLockedException (org.alfresco.service.cmr.lock.NodeLockedException)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 DuplicateChildNodeNameException (org.alfresco.service.cmr.repository.DuplicateChildNodeNameException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 ContentQuotaException (org.alfresco.service.cmr.usage.ContentQuotaException)1 QName (org.alfresco.service.namespace.QName)1 InternalServerErrorException (org.springframework.social.InternalServerErrorException)1