Search in sources :

Example 11 with AuditRecord

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);
    }
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Collection(io.lumeer.api.model.Collection) AuditRecord(io.lumeer.api.model.AuditRecord) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) RemoveDocument(io.lumeer.engine.api.event.RemoveDocument) CreateDocument(io.lumeer.engine.api.event.CreateDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) UnsupportedOperationException(io.lumeer.core.exception.UnsupportedOperationException) HashSet(java.util.HashSet)

Example 12 with AuditRecord

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);
    }
}
Also used : MongoException(com.mongodb.MongoException) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) AuditRecord(io.lumeer.api.model.AuditRecord) Document(org.bson.Document) ReturnDocument(com.mongodb.client.model.ReturnDocument) StorageException(io.lumeer.storage.api.exception.StorageException) Bson(org.bson.conversions.Bson)

Example 13 with AuditRecord

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.");
}
Also used : ServiceLimits(io.lumeer.api.model.ServiceLimits) Collection(io.lumeer.api.model.Collection) AuditRecord(io.lumeer.api.model.AuditRecord) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) RemoveDocument(io.lumeer.engine.api.event.RemoveDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) UnsupportedOperationException(io.lumeer.core.exception.UnsupportedOperationException) HashSet(java.util.HashSet)

Aggregations

AuditRecord (io.lumeer.api.model.AuditRecord)13 DataDocument (io.lumeer.engine.api.data.DataDocument)9 UnsupportedOperationException (io.lumeer.core.exception.UnsupportedOperationException)8 HashSet (java.util.HashSet)6 Collection (io.lumeer.api.model.Collection)5 Document (io.lumeer.api.model.Document)5 Attribute (io.lumeer.api.model.Attribute)4 LinkInstance (io.lumeer.api.model.LinkInstance)4 LinkType (io.lumeer.api.model.LinkType)4 ServiceLimits (io.lumeer.api.model.ServiceLimits)4 RemoveDocument (io.lumeer.engine.api.event.RemoveDocument)4 UpdateDocument (io.lumeer.engine.api.event.UpdateDocument)4 AuditType (io.lumeer.api.model.AuditType)3 Organization (io.lumeer.api.model.Organization)3 Payment (io.lumeer.api.model.Payment)3 Project (io.lumeer.api.model.Project)3 CreateDocument (io.lumeer.engine.api.event.CreateDocument)3 RemoveLinkInstance (io.lumeer.engine.api.event.RemoveLinkInstance)3 UpdateLinkInstance (io.lumeer.engine.api.event.UpdateLinkInstance)3 HashMap (java.util.HashMap)3