Search in sources :

Example 1 with AuditRecord

use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.

the class AuditFacade method revertLastLinkAuditOperation.

public LinkInstance revertLastLinkAuditOperation(final String linkTypeId, final String linkInstanceId, final String auditRecordId) {
    final LinkType linkType = linkTypeDao.getLinkType(linkTypeId);
    final LinkInstance linkInstance = LinkInstanceUtils.loadLinkInstanceWithData(linkInstanceDao, linkDataDao, linkInstanceId);
    permissionsChecker.checkEditLinkInstance(linkType, linkInstance);
    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(linkTypeId, ResourceType.LINK, linkInstanceId);
        if (auditRecord != null && auditRecord.getId().equals(auditRecordId) && auditRecord.getOldState() != null) {
            var keysToBeRemoved = new HashSet<>(auditRecord.getNewState().keySet());
            keysToBeRemoved.removeAll(auditRecord.getOldState().keySet());
            linkInstance.getData().putAll(auditRecord.getOldState());
            keysToBeRemoved.forEach(key -> linkInstance.getData().remove(key));
            dataDao.patchData(linkTypeId, linkInstanceId, auditRecord.getOldState());
            auditDao.deleteAuditRecord(auditRecordId);
        }
        linkInstance.setData(constraintManager.decodeDataTypes(linkType, linkInstance.getData()));
        return linkInstanceAdapter.mapLinkInstanceData(linkInstance);
    }
    throw new UnsupportedOperationException("No organization specified.");
}
Also used : ServiceLimits(io.lumeer.api.model.ServiceLimits) LinkInstance(io.lumeer.api.model.LinkInstance) RemoveLinkInstance(io.lumeer.engine.api.event.RemoveLinkInstance) UpdateLinkInstance(io.lumeer.engine.api.event.UpdateLinkInstance) AuditRecord(io.lumeer.api.model.AuditRecord) LinkType(io.lumeer.api.model.LinkType) UnsupportedOperationException(io.lumeer.core.exception.UnsupportedOperationException) HashSet(java.util.HashSet)

Example 2 with AuditRecord

use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.

the class AuditRecordCodec method decode.

@Override
public AuditRecord decode(final BsonReader bsonReader, final DecoderContext decoderContext) {
    final Document bson = codec.decode(bsonReader, decoderContext);
    final AuditRecord record = new AuditRecord();
    record.setId(bson.getObjectId(ID).toHexString());
    record.setParentId(bson.getString(AuditRecord.PARENT_ID));
    record.setResourceType(ResourceType.fromString(bson.getString(AuditRecord.RESOURCE_TYPE)));
    record.setResourceId(bson.getString(AuditRecord.RESOURCE_ID));
    record.setUser(bson.getString(AuditRecord.USER));
    record.setAutomation(bson.getString(AuditRecord.AUTOMATION));
    record.setUserName(bson.getString(AuditRecord.USER_NAME));
    record.setUserEmail(bson.getString(AuditRecord.USER_EMAIL));
    record.setViewId(bson.getString(AuditRecord.VIEW));
    final Date changeDate = bson.getDate(AuditRecord.CHANGE_DATE);
    record.setChangeDate(changeDate != null ? ZonedDateTime.ofInstant(changeDate.toInstant(), ZoneOffset.UTC) : null);
    final Document oldState = bson.get(AuditRecord.OLD_STATE, Document.class);
    record.setOldState(oldState != null ? MongoUtils.convertDocument(oldState) : new DataDocument());
    final Document newState = bson.get(AuditRecord.NEW_STATE, Document.class);
    record.setNewState(newState != null ? MongoUtils.convertDocument(newState) : new DataDocument());
    final AuditType type = Objects.requireNonNullElse(AuditType.fromString(bson.getString(AuditRecord.TYPE)), AuditType.Updated);
    record.setType(type);
    return record;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) AuditType(io.lumeer.api.model.AuditType) AuditRecord(io.lumeer.api.model.AuditRecord) Document(org.bson.Document) DataDocument(io.lumeer.engine.api.data.DataDocument) Date(java.util.Date)

Example 3 with AuditRecord

use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.

the class AuditFacade method decodeWithTitle.

private List<AuditRecord> decodeWithTitle(final LinkType linkType, final List<AuditRecord> auditRecords) {
    var linkIds = auditRecords.stream().map(AuditRecord::getResourceId).collect(toSet());
    var defaultAttribute = linkType.getAttributes().stream().findFirst().orElse(null);
    var defaultAttributeId = defaultAttribute != null ? defaultAttribute.getId() : "";
    var defaultConstraint = Utils.computeIfNotNull(defaultAttribute, Attribute::getConstraint);
    Map<String, Object> linkValues = new HashMap<>();
    Map<String, Object> defaultValues = Collections.emptyMap();
    if (!defaultAttributeId.isEmpty()) {
        defaultValues = linkDataDao.getData(linkType.getId(), linkIds, defaultAttributeId).stream().collect(toMap(DataDocument::getId, d -> d.get(defaultAttributeId)));
    }
    for (AuditRecord auditRecord : auditRecords) {
        decode(linkType, auditRecord);
        decodeTitle(auditRecord, defaultAttributeId, defaultConstraint, defaultValues, linkValues);
    }
    return auditRecords;
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) Attribute(io.lumeer.api.model.Attribute) HashMap(java.util.HashMap) AuditRecord(io.lumeer.api.model.AuditRecord)

Example 4 with AuditRecord

use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.

the class AuditFacade method getAuditRecordsForDocument.

public List<AuditRecord> getAuditRecordsForDocument(final String collectionId, final String documentId) {
    final Collection collection = collectionDao.getCollectionById(collectionId);
    final Document document = DocumentUtils.loadDocumentWithData(documentDao, dataDao, collection, documentId);
    permissionsChecker.checkEditDocument(collection, document);
    return auditAdapter.getAuditRecords(collectionId, ResourceType.DOCUMENT, documentId, getServiceLevel()).stream().peek(log -> decode(collection, log)).collect(toList());
}
Also used : CollectionUtil(io.lumeer.api.util.CollectionUtil) ZonedDateTime(java.time.ZonedDateTime) User(io.lumeer.api.model.User) LinkTypeDao(io.lumeer.storage.api.dao.LinkTypeDao) UpdateDefaultWorkspace(io.lumeer.engine.api.event.UpdateDefaultWorkspace) UnsupportedOperationException(io.lumeer.core.exception.UnsupportedOperationException) CreateLinkInstance(io.lumeer.engine.api.event.CreateLinkInstance) CollectionDao(io.lumeer.storage.api.dao.CollectionDao) ResourceType(io.lumeer.api.model.ResourceType) Resource(io.lumeer.api.model.common.Resource) Map(java.util.Map) Observes(javax.enterprise.event.Observes) ResourceCommentDao(io.lumeer.storage.api.dao.ResourceCommentDao) RequestDataKeeper(io.lumeer.core.auth.RequestDataKeeper) CollectionAdapter(io.lumeer.core.adapter.CollectionAdapter) ServiceLimits(io.lumeer.api.model.ServiceLimits) ConstraintManager(io.lumeer.core.constraint.ConstraintManager) RoleType(io.lumeer.api.model.RoleType) 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) Set(java.util.Set) Collectors(java.util.stream.Collectors) Constraint(io.lumeer.api.model.Constraint) LinkInstance(io.lumeer.api.model.LinkInstance) LinkType(io.lumeer.api.model.LinkType) DefaultConfigurationProducer(io.lumeer.core.facade.configuration.DefaultConfigurationProducer) List(java.util.List) AuditRecord(io.lumeer.api.model.AuditRecord) PusherClient(io.lumeer.core.util.PusherClient) PostConstruct(javax.annotation.PostConstruct) FavoriteItemDao(io.lumeer.storage.api.dao.FavoriteItemDao) Attribute(io.lumeer.api.model.Attribute) Utils(io.lumeer.core.util.Utils) LinkInstanceAdapter(io.lumeer.core.adapter.LinkInstanceAdapter) UserDao(io.lumeer.storage.api.dao.UserDao) AuditType(io.lumeer.api.model.AuditType) HashMap(java.util.HashMap) DocumentAdapter(io.lumeer.core.adapter.DocumentAdapter) Event(org.marvec.pusher.data.Event) LinkInstanceUtils(io.lumeer.core.util.LinkInstanceUtils) GroupDao(io.lumeer.storage.api.dao.GroupDao) HashSet(java.util.HashSet) Inject(javax.inject.Inject) DataDao(io.lumeer.storage.api.dao.DataDao) ViewDao(io.lumeer.storage.api.dao.ViewDao) Organization(io.lumeer.api.model.Organization) DocumentDao(io.lumeer.storage.api.dao.DocumentDao) ResourceAdapter(io.lumeer.core.adapter.ResourceAdapter) AuditAdapter(io.lumeer.core.adapter.AuditAdapter) LinkTypeAdapter(io.lumeer.core.adapter.LinkTypeAdapter) Payment(io.lumeer.api.model.Payment) DocumentUtils(io.lumeer.core.util.DocumentUtils) RemoveLinkInstance(io.lumeer.engine.api.event.RemoveLinkInstance) Project(io.lumeer.api.model.Project) UpdateLinkInstance(io.lumeer.engine.api.event.UpdateLinkInstance) LinkInstanceDao(io.lumeer.storage.api.dao.LinkInstanceDao) AuditDao(io.lumeer.storage.api.dao.AuditDao) PermissionAdapter(io.lumeer.core.adapter.PermissionAdapter) PusherAdapter(io.lumeer.core.adapter.PusherAdapter) RequestScoped(javax.enterprise.context.RequestScoped) LinkDataDao(io.lumeer.storage.api.dao.LinkDataDao) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) Collections(java.util.Collections) Collection(io.lumeer.api.model.Collection) Collection(io.lumeer.api.model.Collection) 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)

Example 5 with AuditRecord

use of io.lumeer.api.model.AuditRecord in project engine by Lumeer.

the class AuditFacade method revertLinkChange.

public void revertLinkChange(final AuditRecord requestedRecord) {
    final AuditRecord auditRecord = auditDao.findLatestAuditRecord(requestedRecord.getParentId(), ResourceType.LINK, 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 LinkType linkType = linkTypeDao.getLinkType(auditRecord.getParentId());
    final LinkInstance linkInstance = LinkInstanceUtils.loadLinkInstanceWithData(linkInstanceDao, linkDataDao, auditRecord.getResourceId());
    permissionsChecker.checkEditLinkInstance(linkType, linkInstance);
    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());
        linkInstance.getData().putAll(auditRecord.getOldState());
        keysToRemove.forEach(key -> linkInstance.getData().remove(key));
        DataDocument storedData = linkDataDao.updateData(auditRecord.getParentId(), auditRecord.getResourceId(), linkInstance.getData());
        checkAuditOnObjectRevert(auditRecord);
        linkInstance.setUpdateDate(ZonedDateTime.now());
        linkInstance.setUpdatedBy(getCurrentUserId());
        LinkInstance storedLinkInstance = linkInstanceDao.updateLinkInstance(linkInstance.getId(), linkInstance);
        storedLinkInstance.setData(storedData);
        linkTypeAdapter.updateLinkTypeMetadata(linkType, keysToAdd, keysToRemove);
        sendLinkNotification(linkType, storedLinkInstance, PusherFacade.UPDATE_EVENT_SUFFIX);
    }
}
Also used : DataDocument(io.lumeer.engine.api.data.DataDocument) AuditRecord(io.lumeer.api.model.AuditRecord) CreateLinkInstance(io.lumeer.engine.api.event.CreateLinkInstance) LinkInstance(io.lumeer.api.model.LinkInstance) RemoveLinkInstance(io.lumeer.engine.api.event.RemoveLinkInstance) UpdateLinkInstance(io.lumeer.engine.api.event.UpdateLinkInstance) LinkType(io.lumeer.api.model.LinkType) 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