Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #1 from ron1/FEATURE-5.5.0-NXP-9260-cmis-locking
Browse files Browse the repository at this point in the history
NXP-9260 CMIS locking on checkout
  • Loading branch information
Florent Guillaume committed Jul 18, 2012
2 parents 8d1c944 + b2fe113 commit 937c882
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
Expand Up @@ -1688,6 +1688,7 @@ public void checkIn(String repositoryId, Holder<String> objectIdHolder,
try {
coreSession.saveDocument(doc);
DocumentRef ver = doc.checkIn(option, checkinComment);
doc.removeLock();
coreSession.save();
objectIdHolder.setValue(getIdFromDocumentRef(ver));
} catch (ClientException e) {
Expand Down Expand Up @@ -1717,6 +1718,7 @@ public String checkIn(String objectId, boolean major,
try {
coreSession.saveDocument(doc);
DocumentRef ver = doc.checkIn(option, checkinComment);
doc.removeLock();
coreSession.save();
return getIdFromDocumentRef(ver);
} catch (ClientException e) {
Expand Down Expand Up @@ -1762,6 +1764,11 @@ public String checkOut(String objectId) {
throw new CmisConstraintException("Already checked out: "
+ objectId);
}
if (pwc.isLocked()) {
throw new CmisInvalidArgumentException(
"Cannot check out since currently locked: " + objectId);
}
pwc.setLock();
pwc.checkOut();
coreSession.save();
return pwc.getId();
Expand Down Expand Up @@ -1792,6 +1799,7 @@ public void cancelCheckOut(String objectId) {
} else {
// restore and keep checked in
coreSession.restoreToVersion(docRef, verRef, true, true);
doc.removeLock();
}
coreSession.save();
} catch (ClientException e) {
Expand Down Expand Up @@ -1965,7 +1973,21 @@ public void deleteObject(String repositoryId, String objectId,
@Override
public void deleteObjectOrCancelCheckOut(String repositoryId,
String objectId, Boolean allVersions, ExtensionsData extension) {
deleteObject(repositoryId, objectId, allVersions, extension);
try {
DocumentModel doc = getDocumentModel(objectId);
DocumentRef docRef = doc.getRef();
// find last version
DocumentRef verRef = coreSession.getLastDocumentVersionRef(docRef);
// If doc has versions, is locked, and is checkedOut, then it was likely
// explicitly checkedOut so invoke cancelCheckOut not delete
if (verRef != null && doc.isLocked() && doc.isCheckedOut()) {
cancelCheckOut(repositoryId, objectId, extension);
} else {
deleteObject(repositoryId, objectId, allVersions, extension);
}
} catch (ClientException e) {
throw new CmisRuntimeException(e.toString(), e);
}
}

}
Expand Up @@ -44,11 +44,14 @@
import org.apache.chemistry.opencmis.commons.enums.RelationshipDirection;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException;
import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
import org.apache.commons.io.IOUtils;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.IdRef;
import org.nuxeo.ecm.core.api.Lock;
import org.nuxeo.ecm.core.api.security.SecurityConstants;
import org.nuxeo.ecm.core.opencmis.impl.client.NuxeoSession;
import org.nuxeo.ecm.core.opencmis.impl.server.NuxeoRepositories;
Expand Down Expand Up @@ -677,6 +680,80 @@ public void testVersioning() throws Exception {
checkValue(PropertyIds.CHECKIN_COMMENT, null, co);
}

public void testVersionBasedLocking() throws Exception {
CmisObject ob = session.getObjectByPath("/testfolder1/testfile1");

// implicitly checked out after create - unlocked
assertFalse(isDocumentLocked(ob));

((Document) ob).checkIn(true, null, null, "comment");

// checked in - unlocked
assertFalse(isDocumentLocked(ob));

CmisObject ci = session.getObject(ob);
ObjectId coid = ((Document) ci).checkOut();
session.clear(); // clear cache
CmisObject co = session.getObject(coid);

// explicitly checked out - locked
assertTrue(isDocumentLocked(co));

((Document) co).cancelCheckOut();
session.clear(); // clear cache
CmisObject cco = session.getObject(ob);

// cancelled check out - unlocked
assertFalse(isDocumentLocked(cco));

// cannot check out a locked document
lockDocument(cco);
try {
((Document) cco).checkOut();
fail("Cannot check out a locked document");
} catch (CmisInvalidArgumentException e) {
// ok
}
}

public void testDeleteObjectOrCancelCheckOut() throws Exception {
// test cancelCheckOut
CmisObject ob = session.getObjectByPath("/testfolder1/testfile1");

((Document) ob).checkIn(true, null, null, "comment");
((Document) ob).checkOut();

Map<String, Object> map = new HashMap<String, Object>();
map.put("dc:title", "new title");
map.put("dc:subjects", Arrays.asList("a", "b", "c"));
ob.updateProperties(map);

((Document) ob).cancelCheckOut();

session.clear();
ob = session.getObjectByPath("/testfolder1/testfile1");
assertFalse("new title".equals(ob.getPropertyValue("dc:title")));

//test deleteObject
ob = session.getObjectByPath("/testfolder1/testfile2");

map = new HashMap<String, Object>();
map.put("dc:title", "new title");
map.put("dc:subjects", Arrays.asList("a", "b", "c"));
ob.updateProperties(map);

((Document) ob).cancelCheckOut();

session.clear();
try {
ob = session.getObjectByPath("/testfolder1/testfile2");
fail("Document should be deleted");
} catch (CmisObjectNotFoundException e) {
// ok
}

}

public void testCheckInWithChanges() throws Exception {
CmisObject ob = session.getObjectByPath("/testfolder1/testfile1");

Expand Down Expand Up @@ -724,4 +801,12 @@ protected void checkValue(String prop, Object expected, CmisObject ob) {
}
}

private boolean isDocumentLocked(CmisObject ob) throws ClientException {
return getCoreSession().getDocument(new IdRef(ob.getId())).isLocked();
}

private Lock lockDocument(CmisObject ob) throws ClientException {
return getCoreSession().getDocument(new IdRef(ob.getId())).setLock();
}

}

0 comments on commit 937c882

Please sign in to comment.