use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.
the class AuditFacade method revertDocumentChange.
private void revertDocumentChange(final AuditRecord requestedRecord) {
final AuditRecord auditRecord = auditDao.findLatestAuditRecord(requestedRecord.getParentId(), ResourceType.DOCUMENT, requestedRecord.getResourceId(), AuditType.Updated);
if (auditRecord == null || !auditRecord.getId().equals(requestedRecord.getId())) {
throw new UnsupportedOperationException("Cannot revert audit record that is not the last.");
}
final Collection collection = collectionDao.getCollectionById(auditRecord.getParentId());
final Document document = DocumentUtils.loadDocumentWithData(documentDao, dataDao, collection, auditRecord.getResourceId());
permissionsChecker.checkEditDocument(collection, document);
if (auditRecord.getOldState() != null) {
var keysToAdd = new HashSet<>(auditRecord.getOldState().keySet());
keysToAdd.removeAll(auditRecord.getNewState().keySet());
var keysToRemove = new HashSet<>(auditRecord.getNewState().keySet());
keysToRemove.removeAll(auditRecord.getOldState().keySet());
document.getData().putAll(auditRecord.getOldState());
keysToRemove.forEach(key -> document.getData().remove(key));
DataDocument storedData = dataDao.updateData(auditRecord.getParentId(), auditRecord.getResourceId(), document.getData());
checkAuditOnObjectRevert(auditRecord);
document.setUpdatedBy(getCurrentUserId());
document.setUpdateDate(ZonedDateTime.now());
Document storedDocument = documentDao.updateDocument(document.getId(), document);
storedDocument.setData(storedData);
collectionAdapter.updateCollectionMetadata(collection, keysToAdd, keysToRemove);
sendDocumentPushNotifications(collection, storedDocument, PusherFacade.UPDATE_EVENT_SUFFIX);
}
}
use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.
the class MongoAuditRecordDao method updateAuditRecord.
@Override
public AuditRecord updateAuditRecord(final AuditRecord record) {
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER);
try {
Bson update = new Document("$set", record);
final AuditRecord updatedRecord = databaseCollection().findOneAndUpdate(idFilter(record.getId()), update, options);
if (updatedRecord == null) {
throw new StorageException("Audit log record '" + record.getId() + "' has not been updated.");
}
return updatedRecord;
} catch (MongoException ex) {
throw new StorageException("Cannot update audit log record: " + record, ex);
}
}
use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.
the class AuditFacade method revertLastDocumentAuditOperation.
public Document revertLastDocumentAuditOperation(final String collectionId, final String documentId, final String auditRecordId) {
final Collection collection = collectionDao.getCollectionById(collectionId);
final Document document = DocumentUtils.loadDocumentWithData(documentDao, dataDao, collection, documentId);
permissionsChecker.checkEditDocument(collection, document);
if (workspaceKeeper.getOrganization().isPresent()) {
final ServiceLimits limits = paymentFacade.getCurrentServiceLimits(workspaceKeeper.getOrganization().get());
if (limits.getServiceLevel().equals(Payment.ServiceLevel.FREE)) {
throw new UnsupportedOperationException("Reverting audit log entries is not available on the free plan.");
}
final AuditRecord auditRecord = auditDao.findLatestAuditRecord(collectionId, ResourceType.DOCUMENT, documentId);
if (auditRecord != null && auditRecord.getId().equals(auditRecordId) && auditRecord.getOldState() != null) {
var keysToBeRemoved = new HashSet<>(auditRecord.getNewState().keySet());
keysToBeRemoved.removeAll(auditRecord.getOldState().keySet());
document.getData().putAll(auditRecord.getOldState());
keysToBeRemoved.forEach(key -> document.getData().remove(key));
dataDao.patchData(collectionId, documentId, auditRecord.getOldState());
auditDao.deleteAuditRecord(auditRecordId);
}
document.setData(constraintManager.decodeDataTypes(collection, document.getData()));
return documentAdapter.mapDocumentData(document, getCurrentUserId(), workspaceKeeper.getProjectId());
}
throw new UnsupportedOperationException("No organization specified.");
}
Aggregations