use of org.alfresco.service.cmr.repository.DuplicateChildNodeNameException in project alfresco-remote-api by Alfresco.
the class NodesImpl method updateNodeImpl.
protected NodeRef updateNodeImpl(String nodeId, Node nodeInfo, Parameters parameters) {
final NodeRef nodeRef = validateOrLookupNode(nodeId, null);
QName nodeTypeQName = getNodeType(nodeRef);
validateCmObject(nodeTypeQName);
Map<QName, Serializable> props = new HashMap<>(0);
if (nodeInfo.getProperties() != null) {
props = mapToNodeProperties(nodeInfo.getProperties());
}
String name = nodeInfo.getName();
if ((name != null) && (!name.isEmpty())) {
// update node name if needed - note: if the name is different than existing then this is equivalent of a rename (within parent folder)
props.put(ContentModel.PROP_NAME, name);
}
NodePermissions nodePerms = nodeInfo.getPermissions();
if (nodePerms != null) {
// Cannot set inherited permissions, only direct (locally set) permissions can be set
if ((nodePerms.getInherited() != null) && (nodePerms.getInherited().size() > 0)) {
throw new InvalidArgumentException("Cannot set *inherited* permissions on this node");
}
// Check inherit from parent value and if it's changed set the new value
if (nodePerms.getIsInheritanceEnabled() != null) {
if (nodePerms.getIsInheritanceEnabled() != permissionService.getInheritParentPermissions(nodeRef)) {
permissionService.setInheritParentPermissions(nodeRef, nodePerms.getIsInheritanceEnabled());
}
}
// set direct permissions
if ((nodePerms.getLocallySet() != null)) {
// list of all directly set permissions
Set<AccessPermission> directPerms = new HashSet<>(5);
for (AccessPermission accessPerm : permissionService.getAllSetPermissions(nodeRef)) {
if (accessPerm.isSetDirectly()) {
directPerms.add(accessPerm);
}
}
// check if same permission is sent more than once
if (hasDuplicatePermissions(nodePerms.getLocallySet())) {
throw new InvalidArgumentException("Duplicate node permissions, there is more than one permission with the same authority and name!");
}
for (NodePermissions.NodePermission nodePerm : nodePerms.getLocallySet()) {
String permName = nodePerm.getName();
String authorityId = nodePerm.getAuthorityId();
AccessStatus accessStatus = AccessStatus.ALLOWED;
if (nodePerm.getAccessStatus() != null) {
accessStatus = AccessStatus.valueOf(nodePerm.getAccessStatus());
}
if (authorityId == null || authorityId.isEmpty()) {
throw new InvalidArgumentException("Authority Id is expected.");
}
if (permName == null || permName.isEmpty()) {
throw new InvalidArgumentException("Permission name is expected.");
}
if (((!authorityId.equals(PermissionService.ALL_AUTHORITIES) && (!authorityService.authorityExists(authorityId))))) {
throw new InvalidArgumentException("Cannot set permissions on this node - unknown authority: " + authorityId);
}
AccessPermission existing = null;
boolean addPerm = true;
boolean updatePerm = false;
// If the permission already exists but with different access status it will be updated
for (AccessPermission accessPerm : directPerms) {
if (accessPerm.getAuthority().equals(authorityId) && accessPerm.getPermission().equals(permName)) {
existing = accessPerm;
addPerm = false;
if (accessPerm.getAccessStatus() != accessStatus) {
updatePerm = true;
}
break;
}
}
if (existing != null) {
// ignore existing permissions
directPerms.remove(existing);
}
if (addPerm || updatePerm) {
try {
permissionService.setPermission(nodeRef, authorityId, permName, (accessStatus == AccessStatus.ALLOWED));
} catch (UnsupportedOperationException e) {
throw new InvalidArgumentException("Cannot set permissions on this node - unknown access level: " + permName);
}
}
}
// remove any remaining direct perms
for (AccessPermission accessPerm : directPerms) {
permissionService.deletePermission(nodeRef, accessPerm.getAuthority(), accessPerm.getPermission());
}
}
}
String nodeType = nodeInfo.getNodeType();
if ((nodeType != null) && (!nodeType.isEmpty())) {
// update node type - ensure that we are performing a specialise (we do not support generalise)
QName destNodeTypeQName = createQName(nodeType);
if ((!destNodeTypeQName.equals(nodeTypeQName)) && isSubClass(destNodeTypeQName, nodeTypeQName) && (!isSubClass(destNodeTypeQName, ContentModel.TYPE_SYSTEM_FOLDER))) {
nodeService.setType(nodeRef, destNodeTypeQName);
} else {
throw new InvalidArgumentException("Failed to change (specialise) node type - from " + nodeTypeQName + " to " + destNodeTypeQName);
}
}
NodeRef parentNodeRef = nodeInfo.getParentId();
if (parentNodeRef != null) {
NodeRef currentParentNodeRef = getParentNodeRef(nodeRef);
if (currentParentNodeRef == null) {
// implies root (Company Home) hence return 403 here
throw new PermissionDeniedException();
}
if (!currentParentNodeRef.equals(parentNodeRef)) {
// moveOrCopy(nodeRef, parentNodeRef, name, false); // not currently supported - client should use explicit POST /move operation instead
throw new InvalidArgumentException("Cannot update parentId of " + nodeId + " via PUT /nodes/{nodeId}. Please use explicit POST /nodes/{nodeId}/move operation instead");
}
}
List<String> aspectNames = nodeInfo.getAspectNames();
updateCustomAspects(nodeRef, aspectNames, EXCLUDED_ASPECTS);
if (props.size() > 0) {
validatePropValues(props);
try {
// update node properties - note: null will unset the specified property
nodeService.addProperties(nodeRef, props);
} catch (DuplicateChildNodeNameException dcne) {
throw new ConstraintViolatedException(dcne.getMessage());
}
}
return nodeRef;
}
Aggregations