use of org.alfresco.service.cmr.lock.LockType in project alfresco-repository by Alfresco.
the class LockServiceImpl method checkForLock.
/**
* {@inheritDoc}
*/
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
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)) {
// 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)) {
// modifications are prevented
throw new NodeLockedException(nodeRef);
}
} catch (AspectMissingException exception) {
// Ignore since this indicates that the node does not have the lock aspect applied
}
}
}
}
use of org.alfresco.service.cmr.lock.LockType in project alfresco-repository by Alfresco.
the class LockServiceImpl method getLockState.
@Override
@Extend(traitAPI = LockServiceTrait.class, extensionAPI = LockServiceExtension.class)
public LockState getLockState(NodeRef nodeRef) {
// Check in-memory for ephemeral locks first.
nodeRef = tenantService.getName(nodeRef);
LockState lockState = lockStore.get(nodeRef);
if (lockState != null) {
String lockOwner = lockState.getOwner();
Date expiryDate = lockState.getExpires();
LockStatus status = LockUtils.lockStatus(lockOwner, lockOwner, expiryDate);
// in-memory ephemeral lock which is expired is irrelevant
if (status.equals(LockStatus.LOCK_EXPIRED)) {
lockState = null;
}
}
// whether it represents a real lock
if (lockState == null || !lockState.isLockInfo()) {
// No in-memory state, so get from the DB.
if (nodeService.exists(nodeRef) && nodeService.hasAspect(nodeRef, ContentModel.ASPECT_LOCKABLE)) {
String lockOwner = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_OWNER);
Date expiryDate = (Date) nodeService.getProperty(nodeRef, ContentModel.PROP_EXPIRY_DATE);
String lockTypeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_TYPE);
LockType lockType = lockTypeStr != null ? LockType.valueOf(lockTypeStr) : null;
String lifetimeStr = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_LIFETIME);
Lifetime lifetime = lifetimeStr != null ? Lifetime.valueOf(lifetimeStr) : null;
String additionalInfo = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_LOCK_ADDITIONAL_INFO);
// Mark lockstate as PERSISTENT as it was in the persistent storage!
lockState = LockState.createLock(nodeRef, lockType, lockOwner, expiryDate, lifetime, additionalInfo);
} else {
// There is no lock information
lockState = LockState.createUnlocked(nodeRef);
}
}
// Never return a null LockState
Assert.notNull(lockState);
return lockState;
}
use of org.alfresco.service.cmr.lock.LockType in project alfresco-repository by Alfresco.
the class LockServiceImplTest method testGetLockType.
/**
* Test getLockType (and isLocked/isLockedReadOnly)
*/
public void testGetLockType() {
TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
// Get the lock type (should be null since the object is not locked)
LockType lockType1 = this.lockService.getLockType(this.parentNode);
assertNull(lockType1);
// Lock the object for writing
this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
LockType lockType2 = this.lockService.getLockType(this.parentNode);
assertNotNull(lockType2);
assertEquals(LockType.WRITE_LOCK, lockType2);
assertTrue(lockService.isLocked(parentNode));
assertFalse(lockService.isLockedAndReadOnly(parentNode));
// Unlock the node
this.lockService.unlock(this.parentNode);
LockType lockType3 = this.lockService.getLockType(this.parentNode);
assertNull(lockType3);
assertFalse(lockService.isLocked(parentNode));
assertFalse(lockService.isLockedAndReadOnly(parentNode));
// Lock the object for read only
this.lockService.lock(this.parentNode, LockType.READ_ONLY_LOCK);
LockType lockType4 = this.lockService.getLockType(this.parentNode);
assertNotNull(lockType4);
assertEquals(LockType.READ_ONLY_LOCK, lockType4);
assertTrue(lockService.isLocked(parentNode));
assertTrue(lockService.isLockedAndReadOnly(parentNode));
// Lock the object for node lock
this.lockService.lock(this.parentNode, LockType.NODE_LOCK);
LockType lockType5 = this.lockService.getLockType(this.parentNode);
assertNotNull(lockType5);
assertEquals(LockType.NODE_LOCK, lockType5);
assertTrue(lockService.isLocked(parentNode));
assertTrue(lockService.isLockedAndReadOnly(parentNode));
// Unlock the node
this.lockService.unlock(this.parentNode);
LockType lockType6 = this.lockService.getLockType(this.parentNode);
assertNull(lockType6);
assertFalse(lockService.isLocked(parentNode));
assertFalse(lockService.isLockedAndReadOnly(parentNode));
// Test with no apect node
LockType lockType7 = this.lockService.getLockType(this.noAspectNode);
assertTrue("lock type is not null", lockType7 == null);
}
Aggregations