Search in sources :

Example 1 with InvalidTypeException

use of org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1 InvalidTypeException (org.alfresco.repo.model.filefolder.FileFolderServiceImpl.InvalidTypeException)1 IntegrityException (org.alfresco.repo.node.integrity.IntegrityException)1 InvalidArgumentException (org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)1 DuplicateAttributeException (org.alfresco.service.cmr.attributes.DuplicateAttributeException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 QName (org.alfresco.service.namespace.QName)1