DM NodeService automatically adds aspects when properties or associations are added to a node

- AVM NodeService not covered.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14328 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2009-05-15 04:15:40 +00:00
parent 78d5c20554
commit d1d35999de
5 changed files with 194 additions and 38 deletions

View File

@@ -131,6 +131,7 @@ public abstract class BaseNodeServiceTest extends BaseSpringTest
public static final QName PROP_QNAME_NULL_VALUE = QName.createQName(NAMESPACE, "nullValue");
public static final QName PROP_QNAME_MULTI_VALUE = QName.createQName(NAMESPACE, "multiValue");
public static final QName PROP_QNAME_MULTI_ML_VALUE = QName.createQName(NAMESPACE, "multiMLValue");
public static final QName PROP_QNAME_MARKER_PROP = QName.createQName(NAMESPACE, "markerProp");
public static final QName PROP_QNAME_PROP1 = QName.createQName(NAMESPACE, "prop1");
public static final QName PROP_QNAME_PROP2 = QName.createQName(NAMESPACE, "prop2");
public static final QName ASSOC_TYPE_QNAME_TEST_CHILDREN = ContentModel.ASSOC_CHILDREN;
@@ -669,6 +670,73 @@ public abstract class BaseNodeServiceTest extends BaseSpringTest
propertiesAfter.size());
}
public void testAspectsAddedAutomatically() throws Exception
{
// Add the test:titled properties
Map<QName, Serializable> properties = new HashMap<QName, Serializable>(20);
fillProperties(BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED, properties);
// Create a regular base node
NodeRef nodeRef = nodeService.createNode(
rootNodeRef,
ASSOC_TYPE_QNAME_TEST_CHILDREN,
QName.createQName(BaseNodeServiceTest.NAMESPACE, "test-container"),
ContentModel.TYPE_CONTAINER,
properties).getChildRef();
// Ensure that the aspect was automatically added
assertTrue("Aspect not automatically added during 'createNode'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED));
// Remove the aspect and test using setProperties
nodeService.removeAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED);
properties = nodeService.getProperties(nodeRef);
assertFalse("test:titled properties not removed",
properties.containsKey(BaseNodeServiceTest.PROP_QNAME_TEST_TITLE));
assertFalse("test:titled properties not removed",
properties.containsKey(BaseNodeServiceTest.PROP_QNAME_TEST_DESCRIPTION));
properties.put(BaseNodeServiceTest.PROP_QNAME_TEST_DESCRIPTION, "A description");
nodeService.setProperties(nodeRef, properties);
assertTrue("Aspect not automatically added during 'setProperties'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED));
// Remove the aspect and test using addProperties
nodeService.removeAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED);
properties = new HashMap<QName, Serializable>(5);
properties.put(BaseNodeServiceTest.PROP_QNAME_TEST_DESCRIPTION, "A description");
nodeService.addProperties(nodeRef, properties);
assertTrue("Aspect not automatically added during 'addProperties'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED));
// Remove the aspect and test using setProperty
nodeService.removeAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED);
nodeService.setProperty(nodeRef, BaseNodeServiceTest.PROP_QNAME_TEST_DESCRIPTION, "A description");
assertTrue("Aspect not automatically added during 'setProperty'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_TITLED));
// Check that aspects with further mandatory aspects are added properly
nodeService.setProperty(nodeRef, BaseNodeServiceTest.PROP_QNAME_MARKER_PROP, "Marker value");
assertTrue("Aspect not automatically added during 'setProperty'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_MARKER));
assertTrue("Aspect not automatically added during 'setProperty' (second-level)",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_QNAME_TEST_MARKER2));
// Check that child association creation adds the aspect to the parent
NodeRef childNodeRef = nodeService.createNode(
nodeRef,
BaseNodeServiceTest.ASSOC_ASPECT_CHILD_ASSOC,
BaseNodeServiceTest.ASSOC_ASPECT_CHILD_ASSOC,
ContentModel.TYPE_CMOBJECT).getChildRef();
assertTrue("Aspect not automatically added by child association during 'createNode'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_WITH_ASSOCIATIONS));
nodeService.removeAspect(nodeRef, BaseNodeServiceTest.ASPECT_WITH_ASSOCIATIONS);
assertFalse("Child node should have been deleted", nodeService.exists(childNodeRef));
// Check that normal association creation adds the aspect to the source
nodeService.createAssociation(nodeRef, rootNodeRef, BaseNodeServiceTest.ASSOC_ASPECT_NORMAL_ASSOC);
assertTrue("Aspect not automatically added by child association during 'createAssociation'",
nodeService.hasAspect(nodeRef, BaseNodeServiceTest.ASPECT_WITH_ASSOCIATIONS));
}
public void testAspectRemoval() throws Exception
{
// Create a node to add the aspect to
@@ -1237,7 +1305,7 @@ public abstract class BaseNodeServiceTest extends BaseSpringTest
}
}
public void setAddProperties() throws Exception
public void testAddProperties() throws Exception
{
Map<QName, Serializable> properties = nodeService.getProperties(rootNodeRef);
// Add an aspect with a default value

View File

@@ -324,6 +324,11 @@
<aspect name="test:marker">
<title>Marker Aspect</title>
<properties>
<property name="test:markerProp">
<type>d:text</type>
</property>
</properties>
<mandatory-aspects>
<aspect>test:marker2</aspect>
</mandatory-aspects>

View File

@@ -77,6 +77,7 @@ import org.alfresco.util.EqualsHelper;
import org.alfresco.util.GUID;
import org.alfresco.util.Pair;
import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
@@ -329,11 +330,16 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
invokeOnCreateNode(childAssocRef);
invokeOnCreateChildAssociation(childAssocRef, true);
addIntrinsicProperties(childNodePair, propertiesAfter);
Map<QName, Serializable> propertiesBefore = PropertyMap.EMPTY_MAP;
invokeOnUpdateProperties(
childAssocRef.getChildRef(),
Collections.<QName, Serializable>emptyMap(),
propertiesBefore,
propertiesAfter);
// Add missing aspects
addMissingAspects(childNodePair, propertiesBefore, propertiesAfter);
addMissingAspects(parentNodePair, assocTypeQName);
// Index
nodeIndexer.indexCreateNode(childAssocRef);
@@ -853,6 +859,9 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
// Invoke policy behaviours
invokeOnCreateChildAssociation(childAssocRef, false);
// Add missing aspects
addMissingAspects(parentNodePair, assocTypeQName);
// Index
nodeIndexer.indexCreateChildAssociation(childAssocRef);
@@ -1081,6 +1090,96 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
// done
return nodeProperties;
}
/**
* Find any aspects that are missing for the node, given the properties before and after an update.
*/
private void addMissingAspects(
Pair<Long, NodeRef> nodePair,
Map<QName, Serializable> propertiesBefore,
Map<QName, Serializable> propertiesAfter)
{
Long nodeId = nodePair.getFirst();
NodeRef nodeRef = nodePair.getSecond();
Set<QName> aspectQNamesToAdd = new HashSet<QName>(5);
Set<QName> newProperties = new HashSet<QName>(propertiesAfter.keySet());
newProperties.removeAll(propertiesBefore.entrySet());
Set<QName> existingAspectsQNames = nodeDaoService.getNodeAspects(nodeId);
for (QName newPropertyQName : newProperties)
{
PropertyDefinition propDef = dictionaryService.getProperty(newPropertyQName);
if (propDef == null)
{
continue; // Ignore undefined properties
}
if (!propDef.getContainerClass().isAspect())
{
continue;
}
QName containerClassQName = propDef.getContainerClass().getName();
// Remove this aspect - it is there
if (existingAspectsQNames.contains(containerClassQName))
{
// Already there
continue;
}
aspectQNamesToAdd.add(containerClassQName);
}
// Add the aspects and any missing, default properties
if (aspectQNamesToAdd.size() > 0)
{
for (QName aspectQNameToAdd : aspectQNamesToAdd)
{
invokeBeforeAddAspect(nodeRef, aspectQNameToAdd);
}
nodeDaoService.addNodeAspects(nodeId, aspectQNamesToAdd);
// Add the aspects and then their appropriate default values.
for (QName aspectQNameToAdd : aspectQNamesToAdd)
{
addDefaultProperties(nodePair, propertiesAfter, aspectQNameToAdd);
addDefaultAspects(nodePair, aspectQNameToAdd);
}
for (QName aspectQNameToAdd : aspectQNamesToAdd)
{
invokeOnAddAspect(nodeRef, aspectQNameToAdd);
}
}
}
/**
* Find any aspects that are missing for the node, given the association type.
*/
private void addMissingAspects(
Pair<Long, NodeRef> nodePair,
QName assocTypeQName)
{
Long nodeId = nodePair.getFirst();
NodeRef nodeRef = nodePair.getSecond();
Set<QName> existingAspectsQNames = nodeDaoService.getNodeAspects(nodeId);
AssociationDefinition assocDef = dictionaryService.getAssociation(assocTypeQName);
if (assocDef == null)
{
return; // Ignore undefined properties
}
if (!assocDef.getSourceClass().isAspect())
{
return;
}
QName aspectQNameToAdd = assocDef.getSourceClass().getName();
// Remove this aspect - it is there
if (existingAspectsQNames.contains(aspectQNameToAdd))
{
// Already there
return;
}
// Add the aspects and any missing, default properties
invokeBeforeAddAspect(nodeRef, aspectQNameToAdd);
nodeDaoService.addNodeAspects(nodeId, Collections.singleton(aspectQNameToAdd));
// Add the aspects and then their appropriate default values.
addDefaultProperties(nodePair, aspectQNameToAdd);
addDefaultAspects(nodePair, aspectQNameToAdd);
invokeOnAddAspect(nodeRef, aspectQNameToAdd);
}
/**
* Gets the properties map, sets the value (null is allowed) and checks that the new set
@@ -1118,6 +1217,9 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
invokeOnUpdateNode(nodeRef);
invokeOnUpdateProperties(nodeRef, propertiesBefore, propertiesAfter);
// Add any missing aspects
addMissingAspects(nodePair, propertiesBefore, propertiesAfter);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
@@ -1180,6 +1282,9 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
invokeOnUpdateNode(nodeRef);
invokeOnUpdateProperties(nodeRef, propertiesBefore, propertiesAfter);
// Add any missing aspects
addMissingAspects(nodePair, propertiesBefore, propertiesAfter);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
@@ -1208,6 +1313,9 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
invokeOnUpdateNode(nodeRef);
invokeOnUpdateProperties(nodeRef, propertiesBefore, propertiesAfter);
// Add any missing aspects
addMissingAspects(nodePair, propertiesBefore, propertiesAfter);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
@@ -1263,42 +1371,6 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
nodeIndexer.indexUpdateNode(nodeRef);
}
// private Map<QName, PropertyValue> convertProperties(Map<QName, Serializable> properties) throws InvalidNodeRefException
// {
// Map<QName, PropertyValue> convertedProperties = new HashMap<QName, PropertyValue>(17);
//
// // check the property type and copy the values across
// for (QName propertyQName : properties.keySet())
// {
// PropertyDefinition propertyDef = dictionaryService.getProperty(propertyQName);
// Serializable value = properties.get(propertyQName);
// // get a persistable value
// PropertyValue propertyValue = makePropertyValue(propertyDef, value);
// convertedProperties.put(propertyQName, propertyValue);
// }
//
// // Return the converted properties
// return convertedProperties;
// }
//
// private Map<QName, Serializable> convertPropertyValues(Map<QName, PropertyValue> propertyValues) throws InvalidNodeRefException
// {
// Map<QName, Serializable> convertedProperties = new HashMap<QName, Serializable>(17);
//
// // check the property type and copy the values across
// for (Map.Entry<QName, PropertyValue> entry : propertyValues.entrySet())
// {
// QName propertyQName = entry.getKey();
// PropertyValue propertyValue = entry.getValue();
// PropertyDefinition propertyDef = dictionaryService.getProperty(propertyQName);
// Serializable property = makeSerializableValue(propertyDef, propertyValue);
// convertedProperties.put(propertyQName, property);
// }
//
// // Return the converted properties
// return convertedProperties;
// }
//
public Collection<NodeRef> getParents(NodeRef nodeRef) throws InvalidNodeRefException
{
// Get the node
@@ -1554,6 +1626,9 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl
// Invoke policy behaviours
invokeOnCreateAssociation(assocRef);
// Add missing aspects
addMissingAspects(sourceNodePair, assocTypeQName);
return assocRef;
}

View File

@@ -40,6 +40,7 @@ public final class StoreRef implements EntityRef, Serializable
public static final String PROTOCOL_WORKSPACE = "workspace";
public static final String PROTOCOL_ARCHIVE = "archive";
public static final String PROTOCOL_AVM = "avm";
public static final String PROTOCOL_TEST = "test";
public static final StoreRef STORE_REF_WORKSPACE_SPACESSTORE = new StoreRef(PROTOCOL_WORKSPACE, "SpacesStore");
public static final StoreRef STORE_REF_ARCHIVE_SPACESSTORE = new StoreRef(PROTOCOL_ARCHIVE, "SpacesStore");

View File

@@ -25,7 +25,9 @@
package org.alfresco.util;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.service.namespace.QName;
@@ -41,6 +43,11 @@ public class PropertyMap extends HashMap<QName, Serializable>
{
private static final long serialVersionUID = 8052326301073209645L;
/**
* A static empty map to us when having to deal with nulls
*/
public static final Map<QName, Serializable> EMPTY_MAP = Collections.<QName, Serializable>emptyMap();
/**
* @see HashMap#HashMap(int, float)
*/