Merged API-STRIKES-BACK (5.2.0) to HEAD (5.2)

126200 jvonka: Node Associations - follow-on to r126104 to provide initial impl for both peer & child assocs
   - experimental -> pending detailed review & discussion (and tests)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@127569 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jamal Kaabi-Mofrad
2016-06-02 21:39:39 +00:00
parent f3b786ec57
commit 4e8f965a36
8 changed files with 370 additions and 93 deletions

View File

@@ -774,7 +774,17 @@
<bean class="org.alfresco.rest.api.nodes.NodeChildrenRelation">
<property name="nodes" ref="Nodes" />
</bean>
<bean class="org.alfresco.rest.api.nodes.NodeSecondaryChildrenRelation">
<property name="serviceRegistry" ref="ServiceRegistry"/>
<property name="nodes" ref="Nodes" />
</bean>
<bean class="org.alfresco.rest.api.nodes.NodeParentsRelation">
<property name="serviceRegistry" ref="ServiceRegistry"/>
<property name="nodes" ref="Nodes" />
</bean>
<bean class="org.alfresco.rest.api.nodes.NodeTargetsRelation">
<property name="serviceRegistry" ref="ServiceRegistry"/>
<property name="nodes" ref="Nodes" />

View File

@@ -238,6 +238,8 @@ public interface Nodes
String PARAM_INCLUDE_ISLINK = "isLink";
String PARAM_INCLUDE_ALLOWABLEOPERATIONS = "allowableOperations";
String PARAM_INCLUDE_ASSOCIATION = "association";
String PARAM_ISFOLDER = "isFolder";
String PARAM_ISFILE = "isFile";

View File

@@ -46,6 +46,7 @@ import org.alfresco.rest.antlr.WhereClauseParser;
import org.alfresco.rest.api.Activities;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.QuickShareLinks;
import org.alfresco.rest.api.model.AssocChild;
import org.alfresco.rest.api.model.ContentInfo;
import org.alfresco.rest.api.model.Document;
import org.alfresco.rest.api.model.Folder;
@@ -894,6 +895,31 @@ public class NodesImpl implements Nodes
node.setAllowableOperations((allowableOperations.size() > 0 )? allowableOperations : null);
}
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);
if (! 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;
}
}
}
AssocChild childAssoc = new AssocChild(
parentAssocRef.getTypeQName().toPrefixString(namespaceService),
parentAssocRef.isPrimary(),
parentAssocRef.getQName().toPrefixString(namespaceService));
node.setAssociation(childAssoc);
}
node.setNodeType(nodeTypeQName.toPrefixString(namespaceService));
node.setPath(pathInfo);

View File

@@ -0,0 +1,79 @@
/*
* Copyright (C) 2005-2016 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.rest.api.model;
/**
* @author janv
*/
public class AssocChild extends Assoc
{
private String childId;
private String prefixAssocChildQName;
private Boolean isPrimaryParent;
public AssocChild()
{
}
public AssocChild(String prefixAssocTypeQName, boolean isPrimaryParent, String prefixAssocChildQName)
{
super(prefixAssocTypeQName);
this.prefixAssocChildQName = prefixAssocChildQName;
this.isPrimaryParent = isPrimaryParent;
}
public AssocChild(String childId, String prefixAssocTypeQName, String prefixAssocNameQName)
{
super(prefixAssocTypeQName);
this.childId = childId;
this.prefixAssocChildQName = prefixAssocNameQName;
}
public String getChildQName()
{
return prefixAssocChildQName;
}
public void setChildQName(String prefixAssocChildQName)
{
this.prefixAssocChildQName = prefixAssocChildQName;
}
public Boolean getIsPrimaryParent()
{
return isPrimaryParent;
}
public void setIsPrimaryParent(Boolean isPrimaryParent)
{
this.isPrimaryParent = isPrimaryParent;
}
public String getChildId()
{
return childId;
}
public void setChildId(String childId)
{
this.childId = childId;
}
}

View File

@@ -0,0 +1,124 @@
/*
* Copyright (C) 2005-2016 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.rest.api.nodes;
import org.alfresco.rest.antlr.WhereClauseParser;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Assoc;
import org.alfresco.rest.api.model.AssocTarget;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.RelationshipResource;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.AssociationExistsException;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.QNamePattern;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author janv
*/
public class AbstractNodeRelation implements InitializingBean
{
public final static String PARAM_ASSOC_TYPE = "assocType";
private final static Set<String> WHERE_PARAMS =
new HashSet<>(Arrays.asList(new String[] {PARAM_ASSOC_TYPE}));
protected ServiceRegistry sr;
protected NodeService nodeService;
protected NamespaceService namespaceService;
protected Nodes nodes;
public void setNodes(Nodes nodes)
{
this.nodes = nodes;
}
public void setServiceRegistry(ServiceRegistry sr)
{
this.sr = sr;
}
@Override
public void afterPropertiesSet()
{
PropertyCheck.mandatory(this, "serviceRegistry", sr);
ParameterCheck.mandatory("nodes", this.nodes);
this.nodeService = sr.getNodeService();
this.namespaceService = sr.getNamespaceService();
}
protected QName getAssocType(String prefixAssocTypeStr, boolean mandatory)
{
if (mandatory && ((prefixAssocTypeStr == null) || prefixAssocTypeStr.isEmpty()))
{
throw new InvalidArgumentException("Missing "+PARAM_ASSOC_TYPE);
}
return QName.createQName(prefixAssocTypeStr, namespaceService);
}
protected QNamePattern getAssocTypeFromWhereElseAll(Parameters parameters)
{
QNamePattern assocTypeQNameParam = RegexQNamePattern.MATCH_ALL;
Query q = parameters.getQuery();
if (q != null)
{
MapBasedQueryWalker propertyWalker = new MapBasedQueryWalker(WHERE_PARAMS, null);
QueryHelper.walk(q, propertyWalker);
String assocTypeQNameStr = propertyWalker.getProperty(PARAM_ASSOC_TYPE, WhereClauseParser.EQUALS, String.class);
if (assocTypeQNameStr != null)
{
assocTypeQNameParam = getAssocType(assocTypeQNameStr, true);
}
}
return assocTypeQNameParam;
}
}

View File

@@ -0,0 +1,111 @@
/*
* Copyright (C) 2005-2016 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.rest.api.nodes;
import org.alfresco.rest.api.model.AssocChild;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.resource.RelationshipResource;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.QNamePattern;
import org.alfresco.service.namespace.RegexQNamePattern;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Node Parents
*
* List node's parent(s) - primary & also secondary, if any - based on (parent ->) child associations
*
* @author janv
*/
@RelationshipResource(name = "parents", entityResource = NodesEntityResource.class, title = "Node Parents")
public class NodeParentsRelation extends AbstractNodeRelation implements RelationshipResourceAction.Read<Node>
{
/**
* List parents
*
* @param childNodeId String id of child node
*/
@Override
@WebApiDescription(title = "Return a paged list of parent nodes based on child assocs")
public CollectionWithPagingInfo<Node> readAll(String childNodeId, Parameters parameters)
{
NodeRef childNodeRef = nodes.validateOrLookupNode(childNodeId, null);
QNamePattern assocTypeQNameParam = getAssocTypeFromWhereElseAll(parameters);
List<ChildAssociationRef> assocRefs = null;
if (assocTypeQNameParam.equals(RegexQNamePattern.MATCH_ALL))
{
assocRefs = nodeService.getParentAssocs(childNodeRef);
}
else
{
assocRefs = nodeService.getParentAssocs(childNodeRef, assocTypeQNameParam, RegexQNamePattern.MATCH_ALL);
}
Map<QName, String> qnameMap = new HashMap<>(3);
Map<String, UserInfo> mapUserInfo = new HashMap<>(10);
List<String> includeParam = parameters.getInclude();
List<Node> collection = new ArrayList<>(assocRefs.size());
for (ChildAssociationRef assocRef : assocRefs)
{
// minimal info by default (unless "include"d otherwise)
Node node = nodes.getFolderOrDocument(assocRef.getParentRef(), null, null, includeParam, mapUserInfo);
QName assocTypeQName = assocRef.getTypeQName();
QName assocChildQName = assocRef.getQName();
String assocType = qnameMap.get(assocTypeQName);
if (assocType == null)
{
assocType = assocTypeQName.toPrefixString(namespaceService);
qnameMap.put(assocTypeQName, assocType);
}
String childQNameStr = qnameMap.get(assocChildQName);
if (childQNameStr == null)
{
childQNameStr = assocChildQName.toPrefixString(namespaceService);
qnameMap.put(assocChildQName, childQNameStr);
}
node.setAssociation(new AssocChild(assocType, assocRef.isPrimary(), childQNameStr));
collection.add(node);
}
Paging paging = parameters.getPaging();
return CollectionWithPagingInfo.asPaged(paging, collection, false, collection.size());
}
}

View File

@@ -20,6 +20,7 @@ package org.alfresco.rest.api.nodes;
import org.activiti.engine.history.HistoricActivityInstance;
import org.alfresco.repo.web.scripts.admin.NodeBrowserPost;
import org.alfresco.rest.antlr.WhereClauseParser;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Assoc;
import org.alfresco.rest.api.model.Comment;
@@ -35,7 +36,10 @@ import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResou
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.resource.parameters.where.Query;
import org.alfresco.rest.framework.resource.parameters.where.QueryHelper;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.rest.workflow.api.impl.MapBasedQueryWalker;
import org.alfresco.rest.workflow.api.model.Activity;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.AssociationRef;
@@ -45,6 +49,7 @@ import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.QNamePattern;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
@@ -63,33 +68,8 @@ import java.util.Map;
* @author janv
*/
@RelationshipResource(name = "sources", entityResource = NodesEntityResource.class, title = "Node Sources")
public class NodeSourcesRelation implements RelationshipResourceAction.Read<Node>, InitializingBean
public class NodeSourcesRelation extends AbstractNodeRelation implements RelationshipResourceAction.Read<Node>
{
private ServiceRegistry sr;
private NodeService nodeService;
private NamespaceService namespaceService;
private Nodes nodes;
public void setNodes(Nodes nodes)
{
this.nodes = nodes;
}
public void setServiceRegistry(ServiceRegistry sr)
{
this.sr = sr;
}
@Override
public void afterPropertiesSet()
{
PropertyCheck.mandatory(this, "serviceRegistry", sr);
ParameterCheck.mandatory("nodes", this.nodes);
this.nodeService = sr.getNodeService();
this.namespaceService = sr.getNamespaceService();
}
/**
* List sources
*
@@ -101,8 +81,9 @@ public class NodeSourcesRelation implements RelationshipResourceAction.Read<Node
{
NodeRef targetNodeRef = nodes.validateOrLookupNode(targetNodeId, null);
// TODO option to filter by assocType ... ?
List<AssociationRef> assocRefs = nodeService.getSourceAssocs(targetNodeRef, RegexQNamePattern.MATCH_ALL);
QNamePattern assocTypeQNameParam = getAssocTypeFromWhereElseAll(parameters);
List<AssociationRef> assocRefs = nodeService.getSourceAssocs(targetNodeRef, assocTypeQNameParam);
Map<QName, String> qnameMap = new HashMap<>(3);

View File

@@ -18,54 +18,30 @@
*/
package org.alfresco.rest.api.nodes;
import org.activiti.engine.history.HistoricActivityInstance;
import org.alfresco.repo.web.scripts.admin.NodeBrowserPost;
import org.alfresco.rest.api.Nodes;
import org.alfresco.rest.api.model.Assoc;
import org.alfresco.rest.api.model.AssocTarget;
import org.alfresco.rest.api.model.Comment;
import org.alfresco.rest.api.model.Node;
import org.alfresco.rest.api.model.UserInfo;
import org.alfresco.rest.framework.WebApiDescription;
import org.alfresco.rest.framework.WebApiParam;
import org.alfresco.rest.framework.WebApiParameters;
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
import org.alfresco.rest.framework.resource.RelationshipResource;
import org.alfresco.rest.framework.resource.actions.interfaces.MultiPartRelationshipResourceAction;
import org.alfresco.rest.framework.resource.actions.interfaces.RelationshipResourceAction;
import org.alfresco.rest.framework.resource.parameters.CollectionWithPagingInfo;
import org.alfresco.rest.framework.resource.parameters.Paging;
import org.alfresco.rest.framework.resource.parameters.Parameters;
import org.alfresco.rest.framework.webscripts.WithResponse;
import org.alfresco.rest.workflow.api.model.Activity;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.AssociationExistsException;
import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSetRow;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.QNamePattern;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyCheck;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.extensions.webscripts.servlet.FormData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import static org.alfresco.repo.publishing.PublishingModel.ASSOC_LAST_PUBLISHING_EVENT;
import static org.alfresco.util.collections.CollectionUtils.isEmpty;
/**
* Node Targets
*
@@ -74,36 +50,11 @@ import static org.alfresco.util.collections.CollectionUtils.isEmpty;
* @author janv
*/
@RelationshipResource(name = "targets", entityResource = NodesEntityResource.class, title = "Node Targets")
public class NodeTargetsRelation implements
public class NodeTargetsRelation extends AbstractNodeRelation implements
RelationshipResourceAction.Read<Node>,
RelationshipResourceAction.Create<AssocTarget>,
RelationshipResourceAction.Delete, InitializingBean
RelationshipResourceAction.Delete
{
private ServiceRegistry sr;
private NodeService nodeService;
private NamespaceService namespaceService;
private Nodes nodes;
public void setNodes(Nodes nodes)
{
this.nodes = nodes;
}
public void setServiceRegistry(ServiceRegistry sr)
{
this.sr = sr;
}
@Override
public void afterPropertiesSet()
{
PropertyCheck.mandatory(this, "serviceRegistry", sr);
ParameterCheck.mandatory("nodes", this.nodes);
this.nodeService = sr.getNodeService();
this.namespaceService = sr.getNamespaceService();
}
/**
* List targets
*
@@ -115,8 +66,9 @@ public class NodeTargetsRelation implements
{
NodeRef sourceNodeRef = nodes.validateOrLookupNode(sourceNodeId, null);
// TODO option to filter by assocType ... ?
List<AssociationRef> assocRefs = nodeService.getTargetAssocs(sourceNodeRef, RegexQNamePattern.MATCH_ALL);
QNamePattern assocTypeQNameParam = getAssocTypeFromWhereElseAll(parameters);
List<AssociationRef> assocRefs = nodeService.getTargetAssocs(sourceNodeRef, assocTypeQNameParam);
Map<QName, String> qnameMap = new HashMap<>(3);
@@ -124,7 +76,7 @@ public class NodeTargetsRelation implements
List<String> includeParam = parameters.getInclude();
List<Node> collection = new ArrayList<Node>(assocRefs.size());
List<Node> collection = new ArrayList<>(assocRefs.size());
for (AssociationRef assocRef : assocRefs)
{
// minimal info by default (unless "include"d otherwise)
@@ -156,17 +108,10 @@ public class NodeTargetsRelation implements
for (AssocTarget assoc : entity)
{
String assocTypeStr = assoc.getAssocType();
if ((assocTypeStr == null) || assocTypeStr.isEmpty())
{
throw new InvalidArgumentException("Missing assocType");
}
QName assocTypeQName = QName.createQName(assocTypeStr, namespaceService);
QName assocTypeQName = getAssocType(assoc.getAssocType(), true);
try
{
// TODO consider x-store refs ?
NodeRef tgtNodeRef = nodes.validateNode(assoc.getTargetId());
nodeService.createAssociation(srcNodeRef, tgtNodeRef, assocTypeQName);
}
@@ -184,11 +129,10 @@ public class NodeTargetsRelation implements
@WebApiDescription(title = "Remove node assoc(s)")
public void delete(String sourceNodeId, String targetNodeId, Parameters parameters)
{
// TODO consider x-store refs ?
NodeRef srcNodeRef = nodes.validateNode(sourceNodeId);
NodeRef tgtNodeRef = nodes.validateNode(targetNodeId);
String assocTypeStr = parameters.getParameter("assocType");
String assocTypeStr = parameters.getParameter(PARAM_ASSOC_TYPE);
if ((assocTypeStr != null) && (! assocTypeStr.isEmpty()))
{
QName assocTypeQName = QName.createQName(assocTypeStr, namespaceService);