Added removeChildAssociation method to NodeService

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@4706 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2007-01-02 14:25:16 +00:00
parent f2c70b78e1
commit 61c2f1a614
8 changed files with 179 additions and 34 deletions

View File

@@ -816,6 +816,31 @@ public abstract class BaseNodeServiceTest extends BaseSpringTest
// expected
}
}
public void testRemoveSpecificChild() throws Exception
{
NodeRef parentRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("parent_child"),
ContentModel.TYPE_CONTAINER).getChildRef();
ChildAssociationRef pathARef = nodeService.createNode(
parentRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathA"),
ContentModel.TYPE_CONTAINER);
ChildAssociationRef pathBRef = nodeService.addChild(
parentRef,
pathARef.getChildRef(),
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName("pathB"));
// now remove the second association
boolean removed = nodeService.removeChildAssociation(pathBRef);
assertTrue("Association was not removed", removed);
removed = nodeService.removeChildAssociation(pathBRef);
assertFalse("Non-existent association was apparently removed", removed);
}
public void testRemoveChildByRef() throws Exception
{

View File

@@ -771,6 +771,23 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
// done
}
public boolean removeChildAssociation(ChildAssociationRef childAssocRef)
{
Node parentNode = getNodeNotNull(childAssocRef.getParentRef());
Node childNode = getNodeNotNull(childAssocRef.getChildRef());
QName typeQName = childAssocRef.getTypeQName();
QName qname = childAssocRef.getQName();
// Delete the association
invokeBeforeDeleteChildAssociation(childAssocRef);
boolean deleted = nodeDaoService.deleteChildAssoc(parentNode, childNode, typeQName, qname);
if (deleted)
{
invokeOnDeleteChildAssociation(childAssocRef);
}
// Done
return deleted;
}
/**
* Remove properties that should not be persisted as general properties. Where necessary, the
* properties are set on the node.

View File

@@ -189,6 +189,17 @@ public interface NodeDaoService
*/
public ChildAssoc getChildAssoc(Node parentNode, QName assocTypeQName, String childName);
/**
* Deletes an explicit child association.
*
* @return Returns <tt>true</tt> if the association was deleted, otherwise <tt>false</tt>
*/
public boolean deleteChildAssoc(
final Node parentNode,
final Node childNode,
final QName assocTypeQName,
final QName qname);
/**
* @param assoc the child association to remove
* @param cascade true if the assoc deletions must cascade to primary child nodes

View File

@@ -85,6 +85,7 @@ public class HibernateNodeDaoServiceImpl extends HibernateDaoSupport implements
private static final String UPDATE_SET_CHILD_ASSOC_NAME = "node.updateChildAssocName";
private static final String QUERY_GET_PRIMARY_CHILD_NODE_STATUSES = "node.GetPrimaryChildNodeStatuses";
private static final String QUERY_GET_CHILD_ASSOCS = "node.GetChildAssocs";
private static final String QUERY_GET_CHILD_ASSOCS_BY_ALL = "node.GetChildAssocsByAll";
private static final String QUERY_GET_CHILD_ASSOC_BY_TYPE_AND_NAME = "node.GetChildAssocByTypeAndName";
private static final String QUERY_GET_CHILD_ASSOC_REFS = "node.GetChildAssocRefs";
private static final String QUERY_GET_CHILD_ASSOC_REFS_BY_QNAME = "node.GetChildAssocRefsByQName";
@@ -758,33 +759,55 @@ public class HibernateNodeDaoServiceImpl extends HibernateDaoSupport implements
}
public ChildAssoc getChildAssoc(
Node parentNode,
Node childNode,
QName assocTypeQName,
QName qname)
final Node parentNode,
final Node childNode,
final QName assocTypeQName,
final QName qname)
{
ChildAssociationRef childAssocRef = new ChildAssociationRef(
assocTypeQName,
parentNode.getNodeRef(),
qname,
childNode.getNodeRef());
// get all the parent's child associations
Collection<ChildAssoc> assocs = getChildAssocs(parentNode);
// hunt down the desired assoc
for (ChildAssoc assoc : assocs)
HibernateCallback callback = new HibernateCallback()
{
// is it a match?
if (!assoc.getChildAssocRef().equals(childAssocRef)) // not a match
public Object doInHibernate(Session session)
{
continue;
Query query = session
.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_CHILD_ASSOCS_BY_ALL)
.setLong("parentId", parentNode.getId())
.setLong("childId", childNode.getId())
.setParameter("typeQName", assocTypeQName)
.setParameter("qname", qname);
return query.uniqueResult();
}
else
};
ChildAssoc childAssoc = (ChildAssoc) getHibernateTemplate().execute(callback);
return childAssoc;
}
@SuppressWarnings("unchecked")
public boolean deleteChildAssoc(
final Node parentNode,
final Node childNode,
final QName assocTypeQName,
final QName qname)
{
HibernateCallback callback = new HibernateCallback()
{
public Object doInHibernate(Session session)
{
return assoc;
Query query = session
.getNamedQuery(HibernateNodeDaoServiceImpl.QUERY_GET_CHILD_ASSOCS_BY_ALL)
.setLong("parentId", parentNode.getId())
.setLong("childId", childNode.getId())
.setParameter("typeQName", assocTypeQName)
.setParameter("qname", qname);
return query.list();
}
};
List<ChildAssoc> childAssocs = (List<ChildAssoc>) getHibernateTemplate().execute(callback);
// Remove each child association with full cascade
for (ChildAssoc assoc : childAssocs)
{
deleteChildAssoc(assoc, true);
}
// not found
return null;
return (childAssocs.size() > 0);
}
public ChildAssoc getChildAssoc(final Node parentNode, final QName assocTypeQName, final String childName)