nodePair = getNodePairNotNull(nodeRef);
// Invoke policy behaviour
invokeBeforeUpdateNode(nodeRef);
// cm:name special handling
setPropertiesCommonWork(
nodePair,
Collections.singletonMap(qname, value));
// Add the property and all required defaults
boolean changed = addAspectsAndProperties(
nodePair, null,
null, null,
null, Collections.singletonMap(qname, value), false);
if (changed)
{
// Invoke policy behaviour
invokeOnUpdateNode(nodeRef);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
}
/**
* Ensures that all required properties are present on the node and copies the
* property values to the Node
.
*
* To remove a property, remove it from the map before calling this method.
* Null-valued properties are allowed.
*
* If any of the values are null, a marker object is put in to mimic nulls. They will be turned back into
* a real nulls when the properties are requested again.
*
* @see Node#getProperties()
*/
public void setProperties(NodeRef nodeRef, Map properties) throws InvalidNodeRefException
{
Pair nodePair = getNodePairNotNull(nodeRef);
// Invoke policy behaviours
invokeBeforeUpdateNode(nodeRef);
// SetProperties common tasks
setPropertiesCommonWork(nodePair, properties);
// Set properties and defaults, overwriting the existing properties
boolean changed = addAspectsAndProperties(nodePair, null, null, null, null, properties, true);
if (changed)
{
// Invoke policy behaviours
invokeOnUpdateNode(nodeRef);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
}
public void addProperties(NodeRef nodeRef, Map properties) throws InvalidNodeRefException
{
Pair nodePair = getNodePairNotNull(nodeRef);
// Invoke policy behaviours
invokeBeforeUpdateNode(nodeRef);
// cm:name special handling
setPropertiesCommonWork(nodePair, properties);
// Add properties and defaults
boolean changed = addAspectsAndProperties(nodePair, null, null, null, null, properties, false);
if (changed)
{
// Invoke policy behaviours
invokeOnUpdateNode(nodeRef);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
}
public void removeProperty(NodeRef nodeRef, QName qname) throws InvalidNodeRefException
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
// Invoke policy behaviours
invokeBeforeUpdateNode(nodeRef);
// Get the values before
Map propertiesBefore = getPropertiesImpl(nodePair);
// cm:name special handling
if (qname.equals(ContentModel.PROP_NAME))
{
String oldName = extractNameProperty(nodeDAO.getNodeProperties(nodeId));
String newName = null;
setChildNameUnique(nodePair, newName, oldName);
}
// Remove
nodeDAO.removeNodeProperties(nodeId, Collections.singleton(qname));
// Invoke policy behaviours
Map propertiesAfter = getPropertiesImpl(nodePair);
invokeOnUpdateNode(nodeRef);
invokeOnUpdateProperties(nodeRef, propertiesBefore, propertiesAfter);
// Index
nodeIndexer.indexUpdateNode(nodeRef);
}
public Collection getParents(NodeRef nodeRef) throws InvalidNodeRefException
{
List parentAssocs = getParentAssocs(
nodeRef,
RegexQNamePattern.MATCH_ALL,
RegexQNamePattern.MATCH_ALL);
// Copy into the set to avoid duplicates
Set parentNodeRefs = new HashSet(parentAssocs.size());
for (ChildAssociationRef parentAssoc : parentAssocs)
{
NodeRef parentNodeRef = parentAssoc.getParentRef();
parentNodeRefs.add(parentNodeRef);
}
// Done
return new ArrayList(parentNodeRefs);
}
/**
* Filters out any associations if their qname is not a match to the given pattern.
*/
public List getParentAssocs(
final NodeRef nodeRef,
final QNamePattern typeQNamePattern,
final QNamePattern qnamePattern)
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
final List results = new ArrayList(10);
// We have a callback handler to filter results
ChildAssocRefQueryCallback callback = new ChildAssocRefQueryCallback()
{
public boolean preLoadNodes()
{
return false;
}
public boolean handle(
Pair childAssocPair,
Pair parentNodePair,
Pair childNodePair)
{
if (!typeQNamePattern.isMatch(childAssocPair.getSecond().getTypeQName()))
{
return true;
}
if (!qnamePattern.isMatch(childAssocPair.getSecond().getQName()))
{
return true;
}
results.add(childAssocPair.getSecond());
return true;
}
public void done()
{
}
};
// Get the assocs pointing to it
QName typeQName = (typeQNamePattern instanceof QName) ? (QName) typeQNamePattern : null;
QName qname = (qnamePattern instanceof QName) ? (QName) qnamePattern : null;
nodeDAO.getParentAssocs(nodeId, typeQName, qname, null, callback);
// done
return results;
}
/**
* Filters out any associations if their qname is not a match to the given pattern.
*/
public List getChildAssocs(NodeRef nodeRef, final QNamePattern typeQNamePattern, final QNamePattern qnamePattern)
{
return getChildAssocs(nodeRef, typeQNamePattern, qnamePattern, true) ;
}
/**
* Filters out any associations if their qname is not a match to the given pattern.
*/
public List getChildAssocs(
NodeRef nodeRef,
final QNamePattern typeQNamePattern,
final QNamePattern qnamePattern,
final boolean preload)
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
final List results = new ArrayList(10);
// We have a callback handler to filter results
ChildAssocRefQueryCallback callback = new ChildAssocRefQueryCallback()
{
public boolean preLoadNodes()
{
return preload;
}
public boolean handle(
Pair childAssocPair,
Pair parentNodePair,
Pair childNodePair)
{
if (!typeQNamePattern.isMatch(childAssocPair.getSecond().getTypeQName()))
{
return true;
}
if (!qnamePattern.isMatch(childAssocPair.getSecond().getQName()))
{
return true;
}
results.add(childAssocPair.getSecond());
return true;
}
public void done()
{
}
};
// Get the assocs pointing to it
QName typeQName = (typeQNamePattern instanceof QName) ? (QName) typeQNamePattern : null;
QName qname = (qnamePattern instanceof QName) ? (QName) qnamePattern : null;
nodeDAO.getChildAssocs(nodeId, null, typeQName, qname, null, null, callback);
// sort the results
List orderedList = reorderChildAssocs(results);
// Done
return orderedList;
}
public List getChildAssocs(NodeRef nodeRef, Set childNodeTypeQNames)
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
final List results = new ArrayList(100);
NodeDAO.ChildAssocRefQueryCallback callback = new NodeDAO.ChildAssocRefQueryCallback()
{
public boolean handle(
Pair childAssocPair,
Pair parentNodePair,
Pair childNodePair)
{
results.add(childAssocPair.getSecond());
// More results
return true;
}
public boolean preLoadNodes()
{
return true;
}
public void done()
{
}
};
// Get all child associations with the specific qualified name
nodeDAO.getChildAssocsByChildTypes(nodeId, childNodeTypeQNames, callback);
// Sort the results
List orderedList = reorderChildAssocs(results);
// Done
return orderedList;
}
private List reorderChildAssocs(Collection childAssocRefs)
{
// shortcut if there are no assocs
if (childAssocRefs.size() == 0)
{
return Collections.emptyList();
}
// sort results
ArrayList orderedList = new ArrayList(childAssocRefs);
Collections.sort(orderedList);
// list of results
int nthSibling = 0;
Iterator iterator = orderedList.iterator();
while(iterator.hasNext())
{
ChildAssociationRef childAssocRef = iterator.next();
childAssocRef.setNthSibling(nthSibling);
nthSibling++;
}
// done
return orderedList;
}
public NodeRef getChildByName(NodeRef nodeRef, QName assocTypeQName, String childName)
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
Pair childAssocPair = nodeDAO.getChildAssoc(nodeId, assocTypeQName, childName);
if (childAssocPair != null)
{
return childAssocPair.getSecond().getChildRef();
}
else
{
return null;
}
}
public List getChildrenByName(NodeRef nodeRef, QName assocTypeQName, Collection childNames)
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
final List results = new ArrayList(100);
NodeDAO.ChildAssocRefQueryCallback callback = new NodeDAO.ChildAssocRefQueryCallback()
{
public boolean handle(
Pair childAssocPair,
Pair parentNodePair,
Pair childNodePair)
{
results.add(childAssocPair.getSecond());
// More results
return true;
}
public boolean preLoadNodes()
{
return true;
}
public void done()
{
}
};
// Get all child associations with the specific qualified name
nodeDAO.getChildAssocs(nodeId, assocTypeQName, childNames, callback);
// Sort the results
List orderedList = reorderChildAssocs(results);
// Done
return orderedList;
}
public ChildAssociationRef getPrimaryParent(NodeRef nodeRef) throws InvalidNodeRefException
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
// get the primary parent assoc
Pair assocPair = nodeDAO.getPrimaryParentAssoc(nodeId);
// done - the assoc may be null for a root node
ChildAssociationRef assocRef = null;
if (assocPair == null)
{
assocRef = new ChildAssociationRef(null, null, null, nodeRef);
}
else
{
assocRef = assocPair.getSecond();
}
return assocRef;
}
public AssociationRef createAssociation(NodeRef sourceRef, NodeRef targetRef, QName assocTypeQName)
throws InvalidNodeRefException, AssociationExistsException
{
Pair sourceNodePair = getNodePairNotNull(sourceRef);
long sourceNodeId = sourceNodePair.getFirst();
Pair targetNodePair = getNodePairNotNull(targetRef);
long targetNodeId = targetNodePair.getFirst();
// we are sure that the association doesn't exist - make it
Long assocId = nodeDAO.newNodeAssoc(sourceNodeId, targetNodeId, assocTypeQName);
AssociationRef assocRef = new AssociationRef(assocId, sourceRef, assocTypeQName, targetRef);
// Invoke policy behaviours
invokeOnCreateAssociation(assocRef);
// Add missing aspects
addAspectsAndPropertiesAssoc(sourceNodePair, assocTypeQName, null, null, null, null, false);
return assocRef;
}
public Collection getChildAssocsWithoutParentAssocsOfType(NodeRef parent, QName assocTypeQName)
{
// Get the parent node
Pair nodePair = getNodePairNotNull(parent);
Long parentNodeId = nodePair.getFirst();
final List results = new ArrayList(100);
NodeDAO.ChildAssocRefQueryCallback callback = new NodeDAO.ChildAssocRefQueryCallback()
{
public boolean handle(Pair childAssocPair, Pair parentNodePair,
Pair childNodePair)
{
results.add(childAssocPair.getSecond());
// More results
return true;
}
public boolean preLoadNodes()
{
return false;
}
public void done()
{
}
};
// Get the child associations that meet the criteria
nodeDAO.getChildAssocsWithoutParentAssocsOfType(parentNodeId, assocTypeQName, callback);
// done
return results;
}
/**
* Specific properties not supported by {@link #getChildAssocsByPropertyValue(NodeRef, QName, Serializable)}
*/
private static List getChildAssocsByPropertyValueBannedProps = new ArrayList();
static
{
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_NODE_DBID);
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_NODE_UUID);
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_NAME);
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_MODIFIED);
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_MODIFIER);
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_CREATED);
getChildAssocsByPropertyValueBannedProps.add(ContentModel.PROP_CREATOR);
}
@Override
public List getChildAssocsByPropertyValue(
NodeRef nodeRef,
QName propertyQName,
Serializable value)
{
// Get the node
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
// Check the QName is not one of the "special" system maintained ones.
if (getChildAssocsByPropertyValueBannedProps.contains(propertyQName))
{
throw new IllegalArgumentException(
"getChildAssocsByPropertyValue does not allow search of system maintained properties: " + propertyQName);
}
final List results = new ArrayList(10);
// We have a callback handler to filter results
ChildAssocRefQueryCallback callback = new ChildAssocRefQueryCallback()
{
public boolean preLoadNodes()
{
return false;
}
public boolean handle(
Pair childAssocPair,
Pair parentNodePair,
Pair childNodePair)
{
results.add(childAssocPair.getSecond());
return true;
}
public void done()
{
}
};
// Get the assocs pointing to it
nodeDAO.getChildAssocsByPropertyValue(nodeId, propertyQName, value, callback);
// sort the results
List orderedList = reorderChildAssocs(results);
// Done
return orderedList;
}
public void removeAssociation(NodeRef sourceRef, NodeRef targetRef, QName assocTypeQName)
throws InvalidNodeRefException
{
Pair sourceNodePair = getNodePairNotNull(sourceRef);
long sourceNodeId = sourceNodePair.getFirst();
Pair targetNodePair = getNodePairNotNull(targetRef);
long targetNodeId = targetNodePair.getFirst();
// delete it
int assocsDeleted = nodeDAO.removeNodeAssoc(sourceNodeId, targetNodeId, assocTypeQName);
if (assocsDeleted > 0)
{
AssociationRef assocRef = new AssociationRef(sourceRef, assocTypeQName, targetRef);
// Invoke policy behaviours
invokeOnDeleteAssociation(assocRef);
}
}
public AssociationRef getAssoc(Long id)
{
return nodeDAO.getNodeAssoc(id).getSecond();
}
public List getTargetAssocs(NodeRef sourceRef, QNamePattern qnamePattern)
{
Pair sourceNodePair = getNodePairNotNull(sourceRef);
long sourceNodeId = sourceNodePair.getFirst();
// get all assocs to target
Collection> assocPairs = nodeDAO.getTargetNodeAssocs(sourceNodeId);
List nodeAssocRefs = new ArrayList(assocPairs.size());
for (Pair assocPair : assocPairs)
{
AssociationRef assocRef = assocPair.getSecond();
// check qname pattern
if (!qnamePattern.isMatch(assocRef.getTypeQName()))
{
continue; // the assoc name doesn't match the pattern given
}
nodeAssocRefs.add(assocRef);
}
// done
return nodeAssocRefs;
}
public List getSourceAssocs(NodeRef targetRef, QNamePattern qnamePattern)
{
Pair targetNodePair = getNodePairNotNull(targetRef);
long targetNodeId = targetNodePair.getFirst();
// get all assocs to target
Collection> assocPairs = nodeDAO.getSourceNodeAssocs(targetNodeId);
List nodeAssocRefs = new ArrayList(assocPairs.size());
for (Pair assocPair : assocPairs)
{
AssociationRef assocRef = assocPair.getSecond();
// check qname pattern
if (!qnamePattern.isMatch(assocRef.getTypeQName()))
{
continue; // the assoc name doesn't match the pattern given
}
nodeAssocRefs.add(assocRef);
}
// done
return nodeAssocRefs;
}
/**
* @see #getPaths(NodeRef, boolean)
* @see #prependPaths(Node, Path, Collection, Stack, boolean)
*/
public Path getPath(NodeRef nodeRef) throws InvalidNodeRefException
{
List paths = getPaths(nodeRef, true); // checks primary path count
if (paths.size() == 1)
{
return paths.get(0); // we know there is only one
}
throw new RuntimeException("Primary path count not checked"); // checked by getPaths()
}
/**
* When searching for primaryOnly == true
, checks that there is exactly
* one path.
* @see #prependPaths(Node, Path, Collection, Stack, boolean)
*/
public List getPaths(NodeRef nodeRef, boolean primaryOnly) throws InvalidNodeRefException
{
// get the starting node
Pair nodePair = getNodePairNotNull(nodeRef);
return nodeDAO.getPaths(nodePair, primaryOnly);
}
private void archiveNode(NodeRef nodeRef, StoreRef archiveStoreRef)
{
Pair nodePair = getNodePairNotNull(nodeRef);
Long nodeId = nodePair.getFirst();
Pair primaryParentAssocPair = nodeDAO.getPrimaryParentAssoc(nodeId);
Set newAspects = new HashSet(5);
Map existingProperties = nodeDAO.getNodeProperties(nodeId);
Map newProperties = new HashMap(11);
// add the aspect
newAspects.add(ContentModel.ASPECT_ARCHIVED);
newProperties.put(ContentModel.PROP_ARCHIVED_BY, AuthenticationUtil.getFullyAuthenticatedUser());
newProperties.put(ContentModel.PROP_ARCHIVED_DATE, new Date());
newProperties.put(ContentModel.PROP_ARCHIVED_ORIGINAL_PARENT_ASSOC, primaryParentAssocPair.getSecond());
Serializable originalOwner = existingProperties.get(ContentModel.PROP_OWNER);
Serializable originalCreator = existingProperties.get(ContentModel.PROP_CREATOR);
if (originalOwner != null || originalCreator != null)
{
newProperties.put(
ContentModel.PROP_ARCHIVED_ORIGINAL_OWNER,
originalOwner != null ? originalOwner : originalCreator);
}
// change the node ownership
newAspects.add(ContentModel.ASPECT_OWNABLE);
newProperties.put(ContentModel.PROP_OWNER, AuthenticationUtil.getFullyAuthenticatedUser());
// Set the aspects and properties
nodeDAO.addNodeProperties(nodeId, newProperties);
nodeDAO.addNodeAspects(nodeId, newAspects);
// move the node
Pair archiveStoreRootNodePair = nodeDAO.getRootNode(archiveStoreRef);
moveNode(
nodeRef,
archiveStoreRootNodePair.getSecond(),
ContentModel.ASSOC_CHILDREN,
NodeArchiveService.QNAME_ARCHIVED_ITEM);
}
public NodeRef restoreNode(NodeRef archivedNodeRef, NodeRef destinationParentNodeRef, QName assocTypeQName, QName assocQName)
{
Pair archivedNodePair = getNodePairNotNull(archivedNodeRef);
Long archivedNodeId = archivedNodePair.getFirst();
Set existingAspects = nodeDAO.getNodeAspects(archivedNodeId);
Set newAspects = new HashSet(5);
Map existingProperties = nodeDAO.getNodeProperties(archivedNodeId);
Map newProperties = new HashMap(11);
// the node must be a top-level archive node
if (!existingAspects.contains(ContentModel.ASPECT_ARCHIVED))
{
throw new AlfrescoRuntimeException("The node to restore is not an archive node");
}
ChildAssociationRef originalPrimaryParentAssocRef = (ChildAssociationRef) existingProperties.get(
ContentModel.PROP_ARCHIVED_ORIGINAL_PARENT_ASSOC);
Serializable originalOwner = existingProperties.get(ContentModel.PROP_ARCHIVED_ORIGINAL_OWNER);
// remove the archived aspect
Set removePropertyQNames = new HashSet(11);
removePropertyQNames.add(ContentModel.PROP_ARCHIVED_ORIGINAL_PARENT_ASSOC);
removePropertyQNames.add(ContentModel.PROP_ARCHIVED_BY);
removePropertyQNames.add(ContentModel.PROP_ARCHIVED_DATE);
removePropertyQNames.add(ContentModel.PROP_ARCHIVED_ORIGINAL_OWNER);
nodeDAO.removeNodeProperties(archivedNodeId, removePropertyQNames);
nodeDAO.removeNodeAspects(archivedNodeId, Collections.singleton(ContentModel.ASPECT_ARCHIVED));
// restore the original ownership
if (originalOwner != null)
{
newAspects.add(ContentModel.ASPECT_OWNABLE);
newProperties.put(ContentModel.PROP_OWNER, originalOwner);
}
if (destinationParentNodeRef == null)
{
// we must restore to the original location
destinationParentNodeRef = originalPrimaryParentAssocRef.getParentRef();
}
// check the associations
if (assocTypeQName == null)
{
assocTypeQName = originalPrimaryParentAssocRef.getTypeQName();
}
if (assocQName == null)
{
assocQName = originalPrimaryParentAssocRef.getQName();
}
// move the node to the target parent, which may or may not be the original parent
ChildAssociationRef newChildAssocRef = moveNode(
archivedNodeRef,
destinationParentNodeRef,
assocTypeQName,
assocQName);
// the node reference has changed due to the store move
NodeRef restoredNodeRef = newChildAssocRef.getChildRef();
invokeOnRestoreNode(newChildAssocRef);
// done
if (logger.isDebugEnabled())
{
logger.debug("Restored node: \n" +
" original noderef: " + archivedNodeRef + "\n" +
" restored noderef: " + restoredNodeRef + "\n" +
" new parent: " + destinationParentNodeRef);
}
return restoredNodeRef;
}
/**
* Move Node
*
* Drops the old primary association and creates a new one
*/
public ChildAssociationRef moveNode(
NodeRef nodeToMoveRef,
NodeRef newParentRef,
QName assocTypeQName,
QName assocQName)
{
if (isDeletedNodeRef(newParentRef))
{
throw new InvalidNodeRefException("The parent node has been deleted", newParentRef);
}
Pair nodeToMovePair = getNodePairNotNull(nodeToMoveRef);
Pair parentNodePair = getNodePairNotNull(newParentRef);
Long nodeToMoveId = nodeToMovePair.getFirst();
QName nodeToMoveTypeQName = nodeDAO.getNodeType(nodeToMoveId);
NodeRef oldNodeToMoveRef = nodeToMovePair.getSecond();
Long parentNodeId = parentNodePair.getFirst();
NodeRef parentNodeRef = parentNodePair.getSecond();
StoreRef oldStoreRef = oldNodeToMoveRef.getStoreRef();
StoreRef newStoreRef = parentNodeRef.getStoreRef();
NodeRef newNodeToMoveRef = new NodeRef(newStoreRef, oldNodeToMoveRef.getId());
Pair