use of org.alfresco.service.cmr.attributes.DuplicateAttributeException 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;
}
Aggregations