use of org.alfresco.rest.api.model.AssocChild in project records-management by Alfresco.
the class ApiNodesModelFactory method mapAssociations.
/**
* Utility method that maps associations, applicable only for records
*
* @param rmNode
* @param info
* @param includeParam
*/
private void mapAssociations(RMNode rmNode, FileInfo info, List<String> includeParam) {
if (includeParam.contains(RMNode.PARAM_INCLUDE_ASSOCIATION)) {
NodeRef nodeRef = info.getNodeRef();
ChildAssociationRef parentAssocRef = nodeService.getPrimaryParent(nodeRef);
if ((parentAssocRef == null) || (parentAssocRef.getParentRef() == null) || (!parentAssocRef.getParentRef().equals(rmNode.getParentId()))) {
List<ChildAssociationRef> parentAssocRefs = nodeService.getParentAssocs(nodeRef);
for (ChildAssociationRef pAssocRef : parentAssocRefs) {
if (pAssocRef.getParentRef().equals(rmNode.getParentId())) {
// for now, assume same parent/child cannot appear more than once (due to unique name)
parentAssocRef = pAssocRef;
break;
}
}
}
if (parentAssocRef != null) {
QName assocTypeQName = parentAssocRef.getTypeQName();
if ((assocTypeQName != null) && (!EXCLUDED_NS.contains(assocTypeQName.getNamespaceURI()))) {
AssocChild childAssoc = new AssocChild(assocTypeQName.toPrefixString(namespaceService), parentAssocRef.isPrimary());
rmNode.setAssociation(childAssoc);
}
}
}
}
use of org.alfresco.rest.api.model.AssocChild in project alfresco-remote-api by Alfresco.
the class NodesImpl method getFolderOrDocument.
@Override
public Node getFolderOrDocument(final NodeRef nodeRef, NodeRef parentNodeRef, QName nodeTypeQName, List<String> includeParam, Map<String, UserInfo> mapUserInfo) {
if (mapUserInfo == null) {
mapUserInfo = new HashMap<>(2);
}
if (includeParam == null) {
includeParam = Collections.emptyList();
}
Node node;
Map<QName, Serializable> properties = nodeService.getProperties(nodeRef);
PathInfo pathInfo = null;
if (includeParam.contains(PARAM_INCLUDE_PATH)) {
ChildAssociationRef archivedParentAssoc = (ChildAssociationRef) properties.get(ContentModel.PROP_ARCHIVED_ORIGINAL_PARENT_ASSOC);
pathInfo = lookupPathInfo(nodeRef, archivedParentAssoc);
}
if (nodeTypeQName == null) {
nodeTypeQName = getNodeType(nodeRef);
}
if (parentNodeRef == null) {
parentNodeRef = getParentNodeRef(nodeRef);
}
Type type = getType(nodeTypeQName, nodeRef);
if (type == null) {
// not direct folder (or file) ...
// might be sub-type of cm:cmobject (or a cm:link pointing to cm:cmobject or possibly even another cm:link)
node = new Node(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
node.setIsFolder(false);
node.setIsFile(false);
} else if (type.equals(Type.DOCUMENT)) {
node = new Document(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
} else if (type.equals(Type.FOLDER)) {
node = new Folder(nodeRef, parentNodeRef, properties, mapUserInfo, sr);
} else {
throw new RuntimeException("Unexpected - should not reach here: " + type);
}
if (includeParam.size() > 0) {
node.setProperties(mapFromNodeProperties(properties, includeParam, mapUserInfo, EXCLUDED_NS, EXCLUDED_PROPS));
}
Set<QName> aspects = null;
if (includeParam.contains(PARAM_INCLUDE_ASPECTNAMES)) {
aspects = nodeService.getAspects(nodeRef);
node.setAspectNames(mapFromNodeAspects(aspects, EXCLUDED_NS, EXCLUDED_ASPECTS));
}
if (includeParam.contains(PARAM_INCLUDE_ISLINK)) {
boolean isLink = isSubClass(nodeTypeQName, ContentModel.TYPE_LINK);
node.setIsLink(isLink);
}
if (includeParam.contains(PARAM_INCLUDE_ISLOCKED)) {
boolean isLocked = isLocked(nodeRef, aspects);
node.setIsLocked(isLocked);
}
if (includeParam.contains(PARAM_INCLUDE_ISFAVORITE)) {
boolean isFavorite = isFavorite(nodeRef);
node.setIsFavorite(isFavorite);
}
if (includeParam.contains(PARAM_INCLUDE_ALLOWABLEOPERATIONS)) {
// note: refactor when requirements change
Map<String, String> mapPermsToOps = new HashMap<>(3);
mapPermsToOps.put(PermissionService.DELETE, OP_DELETE);
mapPermsToOps.put(PermissionService.ADD_CHILDREN, OP_CREATE);
mapPermsToOps.put(PermissionService.WRITE, OP_UPDATE);
mapPermsToOps.put(PermissionService.CHANGE_PERMISSIONS, OP_UPDATE_PERMISSIONS);
List<String> allowableOperations = new ArrayList<>(3);
for (Entry<String, String> kv : mapPermsToOps.entrySet()) {
String perm = kv.getKey();
String op = kv.getValue();
if (perm.equals(PermissionService.ADD_CHILDREN) && Type.DOCUMENT.equals(type)) {
// special case: do not return "create" (as an allowable op) for file/content types - note: 'type' can be null
continue;
} else if (perm.equals(PermissionService.DELETE) && (isSpecialNode(nodeRef, nodeTypeQName))) {
// special case: do not return "delete" (as an allowable op) for specific system nodes
continue;
} else if (permissionService.hasPermission(nodeRef, perm) == AccessStatus.ALLOWED) {
allowableOperations.add(op);
}
}
node.setAllowableOperations((allowableOperations.size() > 0) ? allowableOperations : null);
}
if (includeParam.contains(PARAM_INCLUDE_PERMISSIONS)) {
Boolean inherit = permissionService.getInheritParentPermissions(nodeRef);
List<NodePermissions.NodePermission> inheritedPerms = new ArrayList<>(5);
List<NodePermissions.NodePermission> setDirectlyPerms = new ArrayList<>(5);
Set<String> settablePerms = null;
boolean allowRetrievePermission = true;
try {
for (AccessPermission accessPerm : permissionService.getAllSetPermissions(nodeRef)) {
NodePermissions.NodePermission nodePerm = new NodePermissions.NodePermission(accessPerm.getAuthority(), accessPerm.getPermission(), accessPerm.getAccessStatus().toString());
if (accessPerm.isSetDirectly()) {
setDirectlyPerms.add(nodePerm);
} else {
inheritedPerms.add(nodePerm);
}
}
settablePerms = permissionService.getSettablePermissions(nodeRef);
} catch (AccessDeniedException ade) {
// ignore - ie. denied access to retrieve permissions, eg. non-admin on root (Company Home)
allowRetrievePermission = false;
}
// returned only node info that he's allowed to see
if (allowRetrievePermission) {
NodePermissions nodePerms = new NodePermissions(inherit, inheritedPerms, setDirectlyPerms, settablePerms);
node.setPermissions(nodePerms);
}
}
if (includeParam.contains(PARAM_INCLUDE_ASSOCIATION)) {
// Ugh ... can we optimise this and return the actual assoc directly (via FileFolderService/GetChildrenCQ) ?
ChildAssociationRef parentAssocRef = nodeService.getPrimaryParent(nodeRef);
// note: parentAssocRef.parentRef can be null for -root- node !
if ((parentAssocRef == null) || (parentAssocRef.getParentRef() == null) || (!parentAssocRef.getParentRef().equals(parentNodeRef))) {
List<ChildAssociationRef> parentAssocRefs = nodeService.getParentAssocs(nodeRef);
for (ChildAssociationRef pAssocRef : parentAssocRefs) {
if (pAssocRef.getParentRef().equals(parentNodeRef)) {
// for now, assume same parent/child cannot appear more than once (due to unique name)
parentAssocRef = pAssocRef;
break;
}
}
}
if (parentAssocRef != null) {
QName assocTypeQName = parentAssocRef.getTypeQName();
if ((assocTypeQName != null) && (!EXCLUDED_NS.contains(assocTypeQName.getNamespaceURI()))) {
AssocChild childAssoc = new AssocChild(assocTypeQName.toPrefixString(namespaceService), parentAssocRef.isPrimary());
node.setAssociation(childAssoc);
}
}
}
node.setNodeType(nodeTypeQName.toPrefixString(namespaceService));
node.setPath(pathInfo);
return node;
}
use of org.alfresco.rest.api.model.AssocChild in project alfresco-remote-api by Alfresco.
the class NodeAssociationsApiTest method testCreateNodeWithAssocs.
/**
* Test ability to create a node and optionally specify one or more associations (to other existing nodes) at time of create.
*
* @throws Exception
*/
@Test
public void testCreateNodeWithAssocs() throws Exception {
// as user 1
setRequestContext(user1);
String myFolderNodeId = getMyNodeId();
// create node with some assocs in a single call
// create folder
Node n = new Node();
n.setName("f1");
n.setNodeType(TYPE_CM_FOLDER);
n.setAspectNames(Arrays.asList(ASPECT_CM_PREFERENCES));
HttpResponse response = post(getNodeChildrenUrl(myFolderNodeId), toJsonAsStringNonNull(n), 201);
String f1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
// create content node
String o1Name = "o1";
n = new Node();
n.setName(o1Name);
n.setNodeType(TYPE_CM_CONTENT);
response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
String o1Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
String o2Name = "o2";
n = new Node();
n.setName(o2Name);
n.setNodeType(TYPE_CM_CONTENT);
response = post(getNodeChildrenUrl(f1Id), toJsonAsStringNonNull(n), 201);
String o2Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
// create folder node with some assocs
String f2Name = "f2";
n = new Node();
n.setName(f2Name);
n.setNodeType(TYPE_CM_FOLDER);
AssocChild secChild = new AssocChild(o1Id, ASSOC_TYPE_CM_CONTAINS);
n.setSecondaryChildren(Collections.singletonList(secChild));
AssocTarget tgt = new AssocTarget(o2Id, ASSOC_TYPE_CM_REFERENCES);
n.setTargets(Collections.singletonList(tgt));
response = post(getNodeChildrenUrl(myFolderNodeId), toJsonAsStringNonNull(n), 201);
String f2Id = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class).getId();
String f3Id = createFolder(myFolderNodeId, "f3").getId();
try {
Paging paging = getPaging(0, 100);
response = getAll(getNodeSecondaryChildrenUrl(f2Id), paging, null, 200);
List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(o1Id, nodes.get(0).getId());
response = getAll(getNodeTargetsUrl(f2Id), paging, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(o2Id, nodes.get(0).getId());
// TODO test model with mandatory aspect
// -ve test - minor: error code if creating a cyclic child assoc (REPO-475)
n = new Node();
n.setName("my-folder");
n.setNodeType(TYPE_CM_FOLDER);
AssocChild assocChild = new AssocChild(myFolderNodeId, "cm:contains");
n.setSecondaryChildren(Collections.singletonList(assocChild));
post(getNodeChildrenUrl(myFolderNodeId), RestApiUtil.toJsonAsStringNonNull(n), 400);
// -ve tests - missing targetId / childId or assocType
n = new Node();
n.setName("my-folder");
n.setNodeType(TYPE_CM_FOLDER);
assocChild = new AssocChild(null, ASSOC_TYPE_CM_CONTAINS);
n.setSecondaryChildren(Collections.singletonList(assocChild));
post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
n = new Node();
n.setName("my-folder");
n.setNodeType(TYPE_CM_FOLDER);
assocChild = new AssocChild(f2Id, null);
n.setSecondaryChildren(Collections.singletonList(assocChild));
post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
n = new Node();
n.setName("my-folder");
n.setNodeType(TYPE_CM_FOLDER);
tgt = new AssocTarget(null, ASSOC_TYPE_CM_REFERENCES);
n.setTargets(Collections.singletonList(tgt));
post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
n = new Node();
n.setName("my-folder");
n.setNodeType(TYPE_CM_FOLDER);
tgt = new AssocTarget(f2Id, null);
n.setTargets(Collections.singletonList(tgt));
post(getNodeChildrenUrl(f3Id), RestApiUtil.toJsonAsStringNonNull(n), 400);
} finally {
// some cleanup
setRequestContext(user1);
deleteNode(f1Id, true, 204);
deleteNode(f2Id, true, 204);
deleteNode(f3Id, true, 204);
}
}
use of org.alfresco.rest.api.model.AssocChild in project alfresco-remote-api by Alfresco.
the class NodeAssociationsApiTest method testDeleteAndRestoreNodeWithAssocs.
/**
* Test ability to delete a node with associations (to and from the node) and then restore it.
* Only the primary parent/child assoc(s) for the deleted node(s) is/are restored.
*
* @throws Exception
*/
@Test
public void testDeleteAndRestoreNodeWithAssocs() throws Exception {
// as user 1 ...
setRequestContext(user1);
String f1Id = null;
String f2Id = null;
String f3Id = null;
try {
String myFolderNodeId = getMyNodeId();
// create primary parent-child hierarchy
f1Id = createFolder(myFolderNodeId, "f1").getId();
String f1bId = createFolder(f1Id, "f1b").getId();
String f1cId = createFolder(f1bId, "f1c").getId();
String f1dId = createFolder(f1cId, "f1d").getId();
String c1eId = createTextFile(f1dId, "c1e", "some text content").getId();
f2Id = createFolder(myFolderNodeId, "f2").getId();
f3Id = createFolder(myFolderNodeId, "f3").getId();
HttpResponse response = getAll(getNodeParentsUrl(f1bId), null, null, 200);
List<Node> nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(f1Id, nodes.get(0).getId());
response = getAll(getNodeParentsUrl(f1dId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(f1cId, nodes.get(0).getId());
response = getAll(getNodeSourcesUrl(c1eId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// add some secondary parent/child assocs outside of the hierarchy
AssocChild secChild = new AssocChild(f1bId, ASSOC_TYPE_CM_CONTAINS);
post(getNodeSecondaryChildrenUrl(f2Id), toJsonAsStringNonNull(secChild), 201);
secChild = new AssocChild(f1bId, ASSOC_TYPE_CM_CONTAINS);
post(getNodeSecondaryChildrenUrl(f3Id), toJsonAsStringNonNull(secChild), 201);
secChild = new AssocChild(f1dId, ASSOC_TYPE_CM_CONTAINS);
post(getNodeSecondaryChildrenUrl(f2Id), toJsonAsStringNonNull(secChild), 201);
secChild = new AssocChild(f1dId, ASSOC_TYPE_CM_CONTAINS);
post(getNodeSecondaryChildrenUrl(f3Id), toJsonAsStringNonNull(secChild), 201);
// also add a secondary parent/child assoc within the hierarchy
secChild = new AssocChild(f1dId, ASSOC_TYPE_CM_CONTAINS);
post(getNodeSecondaryChildrenUrl(f1bId), toJsonAsStringNonNull(secChild), 201);
// add some peer assocs outside of the hierarchy
AssocTarget tgt = new AssocTarget(c1eId, ASSOC_TYPE_CM_REFERENCES);
post(getNodeTargetsUrl(f2Id), toJsonAsStringNonNull(tgt), 201);
tgt = new AssocTarget(c1eId, ASSOC_TYPE_CM_PARTS);
post(getNodeTargetsUrl(f3Id), toJsonAsStringNonNull(tgt), 201);
// also add a peer assoc within the hierarchy
tgt = new AssocTarget(c1eId, ASSOC_TYPE_CM_PARTS);
post(getNodeTargetsUrl(f1cId), toJsonAsStringNonNull(tgt), 201);
// double-check the secondary parent/child assocs
response = getAll(getNodeParentsUrl(f1bId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(3, nodes.size());
response = getAll(getNodeParentsUrl(f1dId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(4, nodes.size());
response = getAll(getNodeSecondaryChildrenUrl(f2Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(2, nodes.size());
List<String> nodeIds = Arrays.asList(new String[] { nodes.get(0).getId(), nodes.get(1).getId() });
assertTrue(nodeIds.contains(f1bId));
assertTrue(nodeIds.contains(f1dId));
response = getAll(getNodeSecondaryChildrenUrl(f3Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(2, nodes.size());
nodeIds = Arrays.asList(new String[] { nodes.get(0).getId(), nodes.get(1).getId() });
assertTrue(nodeIds.contains(f1bId));
assertTrue(nodeIds.contains(f1dId));
response = getAll(getNodeSecondaryChildrenUrl(f1bId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(f1dId, nodes.get(0).getId());
// double-check the peer assocs
response = getAll(getNodeSourcesUrl(c1eId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(3, nodes.size());
response = getAll(getNodeTargetsUrl(f2Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(c1eId, nodes.get(0).getId());
response = getAll(getNodeTargetsUrl(f3Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(c1eId, nodes.get(0).getId());
response = getAll(getNodeTargetsUrl(f1cId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(c1eId, nodes.get(0).getId());
// ... delete to trashcan/archive ...
deleteNode(f1bId);
getSingle(NodesEntityResource.class, f1bId, null, 404);
response = getAll(getNodeTargetsUrl(f2Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
response = getAll(getNodeTargetsUrl(f3Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// ... and then restore again ...
post(URL_DELETED_NODES + "/" + f1bId + "/restore", null, null, 200);
// check primary parent-child hierarchy is restored
// but not the secondary parents or peer assocs of the deleted nodes (outside or within the hierarchy)
response = getSingle(NodesEntityResource.class, f1bId, null, 200);
Node nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals(f1Id, nodeResp.getParentId());
response = getSingle(NodesEntityResource.class, f1cId, null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals(f1bId, nodeResp.getParentId());
response = getSingle(NodesEntityResource.class, f1dId, null, 200);
nodeResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Node.class);
assertEquals(f1cId, nodeResp.getParentId());
// secondary child assocs have not been restored
response = getAll(getNodeParentsUrl(f1bId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(f1Id, nodes.get(0).getId());
response = getAll(getNodeParentsUrl(f1cId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(f1bId, nodes.get(0).getId());
response = getAll(getNodeParentsUrl(f1dId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(1, nodes.size());
assertEquals(f1cId, nodes.get(0).getId());
response = getAll(getNodeSecondaryChildrenUrl(f2Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
response = getAll(getNodeSecondaryChildrenUrl(f3Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
// peer assocs have not been restored
response = getAll(getNodeSourcesUrl(c1eId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
response = getAll(getNodeTargetsUrl(f1cId), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
response = getAll(getNodeTargetsUrl(f2Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
response = getAll(getNodeTargetsUrl(f3Id), null, null, 200);
nodes = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), Node.class);
assertEquals(0, nodes.size());
} finally {
// some cleanup
setRequestContext(user1);
if (f1Id != null) {
deleteNode(f1Id, true, 204);
}
if (f2Id != null) {
deleteNode(f2Id, true, 204);
}
if (f3Id != null) {
deleteNode(f3Id, true, 204);
}
}
}
Aggregations