nodeRefs) throws UnableToReleaseLockException
{
for (NodeRef nodeRef : nodeRefs)
{
unlock(nodeRef);
}
}
/**
* @see org.alfresco.service.cmr.lock.LockService#getLockStatus(NodeRef)
*/
public LockStatus getLockStatus(NodeRef nodeRef)
{
nodeRef = tenantService.getName(nodeRef);
return getLockStatus(nodeRef, getUserName());
}
/**
* Gets the lock status for a node and a user name
*
* @param nodeRef the node reference
* @param userName the user name
* @return the lock status
*/
public LockStatus getLockStatus(NodeRef nodeRef, String userName)
{
LockStatus result = LockStatus.NO_LOCK;
if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE) == true)
{
// Get the current lock owner
String currentUserRef = (String) this.nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_OWNER);
if (currentUserRef != null)
{
Date expiryDate = (Date)this.nodeService.getProperty(nodeRef, ContentModel.PROP_EXPIRY_DATE);
if (expiryDate != null && expiryDate.before(new Date()) == true)
{
// Indicate that the lock has expired
result = LockStatus.LOCK_EXPIRED;
}
else
{
if (currentUserRef.equals(userName) == true)
{
result = LockStatus.LOCK_OWNER;
}
else
{
result = LockStatus.LOCKED;
}
}
}
}
return result;
}
/**
* @see LockService#getLockType(NodeRef)
*/
public LockType getLockType(NodeRef nodeRef)
{
LockType result = null;
if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE) == true)
{
String lockTypeString = (String) this.nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_TYPE);
if (lockTypeString != null)
{
result = LockType.valueOf(lockTypeString);
}
}
return result;
}
/**
* Checks for the lock aspect. Adds if missing.
*
* @param nodeRef
* the node reference
*/
private void checkForLockApsect(NodeRef nodeRef)
{
if (this.nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE) == false)
{
this.nodeService.addAspect(nodeRef, ContentModel.ASPECT_LOCKABLE, null);
}
}
/**
* {@inheritDoc}
*/
public void checkForLock(NodeRef nodeRef) throws NodeLockedException
{
String userName = getUserName();
nodeRef = tenantService.getName(nodeRef);
// Ensure we have found a node reference
if (nodeRef != null && userName != null)
{
String effectiveUserName = AuthenticationUtil.getRunAsUser();
// Check to see if should just ignore this node - note: special MT System due to AuditableAspect
if (! (ignore(nodeRef) || tenantService.getBaseNameUser(effectiveUserName).equals(AuthenticationUtil.getSystemUserName())))
{
try
{
// Get the current lock status on the node ref
LockStatus currentLockStatus = getLockStatus(nodeRef, userName);
LockType lockType = getLockType(nodeRef);
if (LockType.WRITE_LOCK.equals(lockType) == true &&
LockStatus.LOCKED.equals(currentLockStatus) == true)
{
// Lock is of type Write Lock and the node is locked by another owner.
throw new NodeLockedException(nodeRef);
}
else if (LockType.READ_ONLY_LOCK.equals(lockType) == true &&
(LockStatus.LOCKED.equals(currentLockStatus) == true || LockStatus.LOCK_OWNER.equals(currentLockStatus) == true))
{
// Error since there is a read only lock on this object and all
// modifications are prevented
throw new NodeLockedException(nodeRef);
}
else if (LockType.NODE_LOCK.equals(lockType) == true &&
(LockStatus.LOCKED.equals(currentLockStatus) == true || LockStatus.LOCK_OWNER.equals(currentLockStatus) == true))
{
// Error since there is a read only lock on this object and all
// modifications are prevented
throw new NodeLockedException(nodeRef);
}
}
catch (AspectMissingException exception)
{
// Ignore since this indicates that the node does not have the lock aspect applied
}
}
}
}
/**
* Ensures that the parent is not locked.
*
* @see #checkForLock(NodeRef)
*/
public void onCreateChildAssociation(ChildAssociationRef childAssocRef, boolean isNewNode)
{
LockType lockType = getLockType(childAssocRef.getParentRef());
if(lockType != null)
{
switch (lockType)
{
case WRITE_LOCK:
case READ_ONLY_LOCK:
checkForLock(childAssocRef.getParentRef());
break;
case NODE_LOCK:
// don't check for lock
}
}
}
/**
* Ensures that node is not locked.
*
* @see #checkForLock(NodeRef)
*/
public void beforeUpdateNode(NodeRef nodeRef)
{
checkForLock(nodeRef);
}
/**
* Ensures that node is not locked.
*
* @see #checkForLock(NodeRef)
*/
public void beforeDeleteNode(NodeRef nodeRef)
{
checkForLock(nodeRef);
}
/**
* @return Returns {@link DoNothingCopyBehaviourCallback}
*/
public CopyBehaviourCallback getCopyCallback(QName classRef, CopyDetails copyDetails)
{
return DoNothingCopyBehaviourCallback.getInstance();
}
/**
* OnCreateVersion behaviour for the lock aspect
*
* Ensures that the property values of the lock aspect are not 'frozen' in
* the version store.
*/
public void onCreateVersion(
QName classRef,
NodeRef versionableNode,
Map versionProperties,
PolicyScope nodeDetails)
{
// Add the lock aspect, but do not version the property values
nodeDetails.addAspect(ContentModel.ASPECT_LOCKABLE);
}
/**
* Get the current user reference
*
* @return the current user reference
*/
private String getUserName()
{
return this.authenticationService.getCurrentUserName();
}
/**
* @see org.alfresco.service.cmr.lock.LockService#getLocks()
*/
public List getLocks(StoreRef storeRef)
{
return getLocks(
storeRef,
"ASPECT:\"" + ContentModel.ASPECT_LOCKABLE.toString() +
"\" +@\\{http\\://www.alfresco.org/model/content/1.0\\}" + ContentModel.PROP_LOCK_OWNER.getLocalName() + ":\"" + getUserName() + "\"");
}
/**
* Get the locks given a store and query string.
*
* @param storeRef the store reference
* @param query the query string
* @return the locked nodes
*/
private List getLocks(StoreRef storeRef, String query)
{
List result = new ArrayList();
ResultSet resultSet = null;
try
{
resultSet = this.searchService.query(
storeRef,
SearchService.LANGUAGE_LUCENE,
query);
result = resultSet.getNodeRefs();
}
finally
{
if (resultSet != null)
{
resultSet.close();
}
}
return result;
}
/**
* @see org.alfresco.service.cmr.lock.LockService#getLocks(org.alfresco.service.cmr.lock.LockType)
*/
public List getLocks(StoreRef storeRef, LockType lockType)
{
return getLocks(
storeRef,
"ASPECT:\"" + ContentModel.ASPECT_LOCKABLE.toString() +
"\" +@\\{http\\://www.alfresco.org/model/content/1.0\\}" + ContentModel.PROP_LOCK_OWNER.getLocalName() + ":\"" + getUserName() + "\"" +
" +@\\{http\\://www.alfresco.org/model/content/1.0\\}" + ContentModel.PROP_LOCK_TYPE.getLocalName() + ":\"" + lockType.toString() + "\"");
}
@Override
public void onMoveNode(ChildAssociationRef oldChildAssocRef, ChildAssociationRef newChildAssocRef)
{
NodeRef nodeRef = oldChildAssocRef.getChildRef();
checkForLock(nodeRef);
}
@Override
public void suspendLocks()
{
getBehaviourFilter().disableBehaviour(ContentModel.ASPECT_LOCKABLE);
}
@Override
public void enableLocks()
{
getBehaviourFilter().enableBehaviour(ContentModel.ASPECT_LOCKABLE);
}
public void setBehaviourFilter(BehaviourFilter behaviourFilter)
{
this.behaviourFilter = behaviourFilter;
}
public BehaviourFilter getBehaviourFilter()
{
return behaviourFilter;
}
}