Search in sources :

Example 6 with IntegrityException

use of org.alfresco.repo.node.integrity.IntegrityException in project alfresco-remote-api by Alfresco.

the class NodeVersionsRelation method delete.

@Override
@WebApiDescription(title = "Delete version")
public void delete(String nodeId, String versionId, Parameters parameters) {
    Version v = findVersion(nodeId, versionId);
    // live (aka versioned) nodeRef
    NodeRef nodeRef = v.getVersionedNodeRef();
    if (sr.getPermissionService().hasPermission(nodeRef, PermissionService.DELETE) != AccessStatus.ALLOWED) {
        throw new PermissionDeniedException("Cannot delete version");
    }
    versionService.deleteVersion(nodeRef, v);
    Map<QName, Serializable> props = sr.getNodeService().getProperties(nodeRef);
    if (props.get(ContentModel.PROP_VERSION_LABEL) == null) {
        // note: alternatively, the client can remove the "cm:versionable" aspect (if permissions allow) to clear the version history and disable versioning
        throw new IntegrityException("Cannot delete last version (did you mean to disable versioning instead ?) [" + nodeId + "," + versionId + "]", null);
    /*
            if (props.get(ContentModel.PROP_VERSION_TYPE) != null)
            {
                // minor fix up to versionable aspect - ie. remove versionType
                behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
                behaviourFilter.disableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
                try
                {
                    sr.getNodeService().removeProperty(nodeRef, ContentModel.PROP_VERSION_TYPE);
                }
                finally
                {
                    behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_AUDITABLE);
                    behaviourFilter.enableBehaviour(nodeRef, ContentModel.ASPECT_VERSIONABLE);
                }
            }
            */
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) Version(org.alfresco.service.cmr.version.Version) QName(org.alfresco.service.namespace.QName) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) WebApiDescription(org.alfresco.rest.framework.WebApiDescription)

Example 7 with IntegrityException

use of org.alfresco.repo.node.integrity.IntegrityException in project alfresco-remote-api by Alfresco.

the class ExceptionResolverTests method testMatchException.

// 04180006 Authentication failed for Web Script org/alfresco/api/ResourceWebScript.get
@Test
public void testMatchException() {
    ErrorResponse response = assistant.resolveException(new ApiException(null));
    assertNotNull(response);
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InvalidArgumentException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new InvalidQueryException(null));
    // default to STATUS_BAD_REQUEST
    assertEquals(400, response.getStatusCode());
    response = assistant.resolveException(new NotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new EntityNotFoundException(null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new RelationshipResourceNotFoundException(null, null));
    // default to STATUS_NOT_FOUND
    assertEquals(404, response.getStatusCode());
    response = assistant.resolveException(new PermissionDeniedException(null));
    // default to STATUS_FORBIDDEN
    assertEquals(403, response.getStatusCode());
    response = assistant.resolveException(new UnsupportedResourceOperationException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new DeletedResourceException(null));
    // default to STATUS_METHOD_NOT_ALLOWED
    assertEquals(405, response.getStatusCode());
    response = assistant.resolveException(new ConstraintViolatedException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    response = assistant.resolveException(new StaleEntityException(null));
    // default to STATUS_CONFLICT
    assertEquals(409, response.getStatusCode());
    // Try a random exception
    response = assistant.resolveException(new FormNotFoundException(null));
    // default to INTERNAL_SERVER_ERROR
    assertEquals(500, response.getStatusCode());
    response = assistant.resolveException(new InsufficientStorageException(null));
    assertEquals(507, response.getStatusCode());
    response = assistant.resolveException(new IntegrityException(null));
    assertEquals(422, response.getStatusCode());
}
Also used : UnsupportedResourceOperationException(org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException) InsufficientStorageException(org.alfresco.rest.framework.core.exceptions.InsufficientStorageException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) DeletedResourceException(org.alfresco.rest.framework.core.exceptions.DeletedResourceException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) ErrorResponse(org.alfresco.rest.framework.core.exceptions.ErrorResponse) RelationshipResourceNotFoundException(org.alfresco.rest.framework.core.exceptions.RelationshipResourceNotFoundException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) FormNotFoundException(org.alfresco.repo.forms.FormNotFoundException) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) StaleEntityException(org.alfresco.rest.framework.core.exceptions.StaleEntityException) InvalidQueryException(org.alfresco.rest.framework.resource.parameters.where.InvalidQueryException) ApiException(org.alfresco.rest.framework.core.exceptions.ApiException) Test(org.junit.Test)

Example 8 with IntegrityException

use of org.alfresco.repo.node.integrity.IntegrityException in project records-management by Alfresco.

the class FilePlanComponentsApiUtils method createRMNode.

/**
 * Create an RM node
 *
 * @param parentNodeRef  the parent of the node
 * @param nodeInfo  the node infos to create
 * @param parameters  the object to get the parameters passed into the request
 * @return the new node
 */
public NodeRef createRMNode(NodeRef parentNodeRef, RMNode nodeInfo, Parameters parameters) {
    mandatory("parentNodeRef", parentNodeRef);
    mandatory("nodeInfo", nodeInfo);
    mandatory("parameters", parameters);
    String nodeName = nodeInfo.getName();
    String nodeType = nodeInfo.getNodeType();
    checkNotBlank(RMNode.PARAM_NAME, nodeName);
    checkNotBlank(RMNode.PARAM_NODE_TYPE, nodeType);
    // Create the node
    NodeRef newNodeRef = null;
    boolean autoRename = Boolean.valueOf(parameters.getParameter(RMNode.PARAM_AUTO_RENAME));
    try {
        QName typeQName = nodes.createQName(nodeType);
        // Existing file/folder name handling
        if (autoRename) {
            NodeRef existingNode = nodeService.getChildByName(parentNodeRef, ContentModel.ASSOC_CONTAINS, nodeName);
            if (existingNode != null) {
                // File already exists, find a unique name
                nodeName = findUniqueName(parentNodeRef, nodeName);
            }
        }
        newNodeRef = fileFolderService.create(parentNodeRef, nodeName, typeQName).getNodeRef();
        // Set the provided properties if any
        Map<QName, Serializable> qnameProperties = mapToNodeProperties(nodeInfo.getProperties());
        if (qnameProperties != null) {
            nodeService.addProperties(newNodeRef, qnameProperties);
        }
        // If electronic record create empty content
        if (!typeQName.equals(RecordsManagementModel.TYPE_NON_ELECTRONIC_DOCUMENT) && dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT)) {
            writeContent(newNodeRef, nodeName, new ByteArrayInputStream("".getBytes()), false);
        }
        // Add the provided aspects if any
        List<String> aspectNames = nodeInfo.getAspectNames();
        if (aspectNames != null) {
            nodes.addCustomAspects(newNodeRef, aspectNames, ApiNodesModelFactory.EXCLUDED_ASPECTS);
        }
    } catch (InvalidTypeException ex) {
        throw new InvalidArgumentException("The given type:'" + nodeType + "' is invalid '");
    } catch (DuplicateAttributeException ex) {
        // This exception can occur when setting a custom identifier that already exists
        throw new IntegrityException(ex.getMessage(), null);
    }
    return newNodeRef;
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(org.alfresco.service.namespace.QName) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) DuplicateAttributeException(org.alfresco.service.cmr.attributes.DuplicateAttributeException) InvalidTypeException(org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException)

Example 9 with IntegrityException

use of org.alfresco.repo.node.integrity.IntegrityException in project records-management by Alfresco.

the class RecordServiceImpl method validateForCompletion.

/**
 * Helper method to validate whether the node is in a state suitable for completion
 *
 * @param nodeRef node reference
 * @throws Exception if node not valid for completion
 */
private void validateForCompletion(NodeRef nodeRef) {
    if (!nodeService.exists(nodeRef)) {
        LOGGER.warn(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, nodeRef.toString()));
        throw new IntegrityException("The record does not exist.", null);
    }
    if (!isRecord(nodeRef)) {
        LOGGER.warn(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, nodeRef.toString()));
        throw new IntegrityException("The node is not a record.", null);
    }
    if (freezeService.isFrozen(nodeRef)) {
        LOGGER.warn(I18NUtil.getMessage(MSG_UNDECLARED_ONLY_RECORDS, nodeRef.toString()));
        throw new IntegrityException("The record is frozen.", null);
    }
    if (isDeclared(nodeRef)) {
        throw new IntegrityException("The record is already completed.", null);
    }
    // if the record is newly created make sure the record identifier is set before completing the record
    Set<NodeRef> newRecords = transactionalResourceHelper.getSet(RecordServiceImpl.KEY_NEW_RECORDS);
    if (newRecords.contains(nodeRef)) {
        generateRecordIdentifier(nodeService, identifierService, nodeRef);
    }
    // Validate that all mandatory properties, if any, are present
    List<String> missingProperties = new ArrayList<>(5);
    // Aspect not already defined - check mandatory properties then add
    if (checkMandatoryPropertiesEnabled) {
        Map<QName, Serializable> nodeRefProps = nodeService.getProperties(nodeRef);
        QName nodeRefType = nodeService.getType(nodeRef);
        // check for missing mandatory metadata from type definitions
        TypeDefinition typeDef = dictionaryService.getType(nodeRefType);
        checkDefinitionMandatoryPropsSet(typeDef, nodeRefProps, missingProperties);
        // check for missing mandatory metadata from aspect definitions
        Set<QName> aspects = nodeService.getAspects(nodeRef);
        for (QName aspect : aspects) {
            AspectDefinition aspectDef = dictionaryService.getAspect(aspect);
            checkDefinitionMandatoryPropsSet(aspectDef, nodeRefProps, missingProperties);
        }
        // check for missing mandatory metadata from custom aspect definitions
        QName customAspect = getCustomAspectImpl(nodeRefType);
        AspectDefinition aspectDef = dictionaryService.getAspect(customAspect);
        checkDefinitionMandatoryPropsSet(aspectDef, nodeRefProps, missingProperties);
        if (!missingProperties.isEmpty()) {
            LOGGER.debug(buildMissingPropertiesErrorString(missingProperties));
            throw new RecordMissingMetadataException("The record has missing mandatory properties.");
        }
    }
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) AspectDefinition(org.alfresco.service.cmr.dictionary.AspectDefinition) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef)

Example 10 with IntegrityException

use of org.alfresco.repo.node.integrity.IntegrityException in project records-management by Alfresco.

the class RecordFolderType method onCreateChildAssociation.

/**
 * @see org.alfresco.repo.node.NodeServicePolicies.OnCreateChildAssociationPolicy#onCreateChildAssociation(org.alfresco.service.cmr.repository.ChildAssociationRef, boolean)
 */
@Override
@Behaviour(kind = BehaviourKind.ASSOCIATION, notificationFrequency = NotificationFrequency.FIRST_EVENT)
public void onCreateChildAssociation(ChildAssociationRef childAssocRef, boolean bNew) {
    NodeRef nodeRef = childAssocRef.getChildRef();
    if (nodeService.exists(nodeRef)) {
        boolean notFolderOrRmFolderSubType = !instanceOf(nodeRef, ContentModel.TYPE_FOLDER) || instanceOf(nodeRef, RecordsManagementModel.TYPE_RECORDS_MANAGEMENT_CONTAINER) || instanceOf(nodeRef, RecordsManagementModel.TYPE_RECORD_FOLDER) || instanceOf(nodeRef, RecordsManagementModel.TYPE_TRANSFER);
        if (!instanceOf(nodeRef, ContentModel.TYPE_CONTENT) && notFolderOrRmFolderSubType) {
            throw new IntegrityException(I18NUtil.getMessage(MSG_CANNOT_CREATE_RECORD_FOLDER_CHILD, nodeService.getType(nodeRef)), null);
        }
        // ensure nothing is being added to a closed record folder
        NodeRef recordFolder = childAssocRef.getParentRef();
        Boolean isClosed = (Boolean) nodeService.getProperty(recordFolder, PROP_IS_CLOSED);
        if (isClosed != null && isClosed) {
            throw new IntegrityException(I18NUtil.getMessage(MSG_CANNOT_CREATE_CHILDREN_IN_CLOSED_RECORD_FOLDER), null);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) IntegrityException(org.alfresco.repo.node.integrity.IntegrityException) Behaviour(org.alfresco.repo.policy.annotation.Behaviour)

Aggregations

IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)10 NodeRef (org.alfresco.service.cmr.repository.NodeRef)9 PermissionDeniedException (org.alfresco.rest.framework.core.exceptions.PermissionDeniedException)4 QName (org.alfresco.service.namespace.QName)4 Serializable (java.io.Serializable)3 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)3 Behaviour (org.alfresco.repo.policy.annotation.Behaviour)2 Operation (org.alfresco.rest.framework.Operation)2 ApiException (org.alfresco.rest.framework.core.exceptions.ApiException)2 ConstraintViolatedException (org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException)2 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)2 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)2 NotFoundException (org.alfresco.rest.framework.core.exceptions.NotFoundException)2 FileInfo (org.alfresco.service.cmr.model.FileInfo)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 RecordMissingMetadataException (org.alfresco.module.org_alfresco_module_rm.record.RecordMissingMetadataException)1 FormNotFoundException (org.alfresco.repo.forms.FormNotFoundException)1 InvalidTypeException (org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException)1 RestoreNodeReport (org.alfresco.repo.node.archive.RestoreNodeReport)1