Fixed `unlockAndPatch` method

This commit is contained in:
Francesco Mangiacrapa 2023-04-19 10:43:15 +02:00
parent 04a86e0280
commit c273a2db2c
1 changed files with 13 additions and 6 deletions

View File

@ -184,28 +184,35 @@ public class ProfiledMongoManager extends MongoManager implements MongoManagerI<
protected Project unlockAndPatch(Project proj) throws InvalidLockException, ProjectNotFoundException, protected Project unlockAndPatch(Project proj) throws InvalidLockException, ProjectNotFoundException,
JsonProcessingException, InvalidUserRoleException, UnauthorizedAccess { JsonProcessingException, InvalidUserRoleException, UnauthorizedAccess {
log.info("Unlocking for patching {} lock is {} ", proj.getId(), proj.getLock()); log.info("Unlocking for patching {} lock is {} ", proj.getId(), proj.getLock());
// find one and update // find one and update
Lock oldLock = proj.getLock(); Lock oldLock = proj.getLock();
proj.setLock(null);
Document filter = new Document(mongoIDFieldName(), asId(proj.getId())).append(Project.LOCK + "." + Lock.ID, Document filter = new Document(mongoIDFieldName(), asId(proj.getId())).append(Project.LOCK + "." + Lock.ID,
oldLock.getId()); oldLock.getId());
Document setUpdatedDocument = new Document("$set", new Document(Project.THE_DOCUMENT, proj.getTheDocument()));
log.info("Filter document is {} ", filter.toJson()); log.info("Filter document is {} ", filter.toJson());
log.info("$set is {} ", setUpdatedDocument);
Object obj = getCollection().findOneAndUpdate( Object obj = getCollection().findOneAndUpdate(
// filter by id and missing lock // filter by id and missing lock
filter, filter,
// update lock info // update lock info
asDocumentWithId(proj), new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); setUpdatedDocument,
new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER));
if (obj == null) { if (obj == null) {
// can-t unlock, check cause // can-t unlock, check cause
Project p = getByID(proj.getId());
throw new InvalidLockException( throw new InvalidLockException(
"Found lock for " + p.getId() + " is " + p.getLock() + ", expected is " + oldLock); "Found lock for " + proj.getId() + " is " + proj.getLock() + ", expected is " + oldLock);
} }
return Serialization.convert(obj, Project.class);
//I could use Serialization.convert(obj, Project.class), but to be sure I'm reading again the project by
proj = getByID(proj.getId());
proj = unlock(proj);
return proj;
} }
protected Project unlock(Project proj) protected Project unlock(Project proj)