Search in sources :

Example 1 with ChangesTracker

use of io.lumeer.core.task.executor.ChangesTracker in project engine by Lumeer.

the class FunctionTask method process.

@Override
public void process(final TaskExecutor taskExecutor, final ChangesTracker changesTracker) {
    if (daoContextSnapshot.getSelectedWorkspace() != null && daoContextSnapshot.getSelectedWorkspace().getOrganization().isPresent() && daoContextSnapshot.getSelectedWorkspace().getProject().isPresent()) {
        log.info(String.format("Running function task on %s/%s > Resource '%s', Attribute '%s'.", daoContextSnapshot.getSelectedWorkspace().getOrganization().get().getCode(), daoContextSnapshot.getSelectedWorkspace().getProject().get().getCode(), collection != null ? collection.getName() : linkType.getName(), attribute.getName()));
    }
    if (documents != null && collection != null) {
        getDocumentsWithData(collection, documents).forEach(document -> {
            originalDocuments.put(document.getId(), new Document(document));
            final FunctionTaskExecutor executor = new FunctionTaskExecutor(this, collection, document);
            changesTracker.merge(executor.execute(taskExecutor));
        });
    } else if (linkType != null && linkInstances != null) {
        getLinkInstancesWithData(linkType, linkInstances).forEach(linkInstance -> {
            originalLinkInstances.put(linkInstance.getId(), new LinkInstance(linkInstance));
            final FunctionTaskExecutor executor = new FunctionTaskExecutor(this, linkType, linkInstance);
            changesTracker.merge(executor.execute(taskExecutor));
        });
    }
    if (parent != null) {
        parent.process(taskExecutor, changesTracker);
    }
}
Also used : ChangesTracker(io.lumeer.core.task.executor.ChangesTracker) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) Set(java.util.Set) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) LinkInstance(io.lumeer.api.model.LinkInstance) Function(io.lumeer.api.model.function.Function) LinkType(io.lumeer.api.model.LinkType) FunctionTaskExecutor(io.lumeer.core.task.executor.FunctionTaskExecutor) HashSet(java.util.HashSet) DefaultConfigurationProducer(io.lumeer.core.facade.configuration.DefaultConfigurationProducer) List(java.util.List) Map(java.util.Map) Attribute(io.lumeer.api.model.Attribute) Collections(java.util.Collections) Collection(io.lumeer.api.model.Collection) ConstraintManager(io.lumeer.core.constraint.ConstraintManager) FunctionTaskExecutor(io.lumeer.core.task.executor.FunctionTaskExecutor) LinkInstance(io.lumeer.api.model.LinkInstance) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document)

Example 2 with ChangesTracker

use of io.lumeer.core.task.executor.ChangesTracker in project engine by Lumeer.

the class SingleStage method commitOperations.

public ChangesTracker commitOperations() {
    if (operations.isEmpty()) {
        return new ChangesTracker();
    }
    @SuppressWarnings("rawtypes") final List<Operation> invalidOperations = operations.stream().filter(operation -> !operation.isComplete()).collect(toList());
    if (invalidOperations.size() > 0) {
        final StringBuilder sb = new StringBuilder();
        invalidOperations.forEach(operation -> sb.append("Invalid update request: ").append(operation.toString()).append("\n"));
        throw new IllegalArgumentException(sb.toString());
    }
    // first create all new documents
    final List<Document> createdDocuments = createDocuments(operations.stream().filter(operation -> operation instanceof DocumentCreationOperation && operation.isComplete()).map(operation -> (DocumentCreationOperation) operation).collect(toList()));
    final Map<String, List<Document>> toBeRemovedDocumentsByCollection = Utils.categorize(operations.stream().filter(operation -> operation instanceof DocumentRemovalOperation && operation.isComplete()).map(operation -> ((DocumentRemovalOperation) operation).getEntity()), Document::getCollectionId);
    // get data structures for efficient manipulation with the new documents
    final Map<String, List<Document>> documentsByCollection = DocumentUtils.getDocumentsByCollection(createdDocuments);
    final List<String> usedCollections = new ArrayList<>(documentsByCollection.keySet());
    usedCollections.addAll(toBeRemovedDocumentsByCollection.keySet());
    final Map<String, Collection> collectionsMap = task.getDaoContextSnapshot().getCollectionDao().getCollectionsByIds(usedCollections).stream().collect(Collectors.toMap(Collection::getId, Function.identity()));
    final Map<String, String> correlationIdsToIds = createdDocuments.stream().collect(Collectors.toMap(doc -> doc.createIfAbsentMetaData().getString(Document.META_CORRELATION_ID), Document::getId));
    // report new empty documents, later updates are sent separately
    changesTracker.addCreatedDocuments(createdDocuments);
    changesTracker.addCollections(collectionsMap.values().stream().filter(c -> documentsByCollection.containsKey(c.getId())).collect(toSet()));
    // map the newly create document IDs to all other changes so that we use the correct document in updates etc.
    operations.stream().filter(operation -> operation instanceof DocumentOperation).forEach(operation -> {
        final Document doc = (Document) operation.getEntity();
        if (StringUtils.isEmpty(doc.getId()) && StringUtils.isNotEmpty(doc.createIfAbsentMetaData().getString(Document.META_CORRELATION_ID))) {
            doc.setId(correlationIdsToIds.get(doc.getMetaData().getString(Document.META_CORRELATION_ID)));
        }
    });
    operations.stream().filter(operation -> operation instanceof LinkCreationOperation).forEach(operation -> {
        final LinkInstance link = ((LinkCreationOperation) operation).getEntity();
        if (StringUtils.isEmpty(link.getId()) && StringUtils.isNotEmpty(link.getTemplateId())) {
            link.setDocumentIds(List.of(correlationIdsToIds.containsKey(link.getDocumentIds().get(0)) ? correlationIdsToIds.get(link.getDocumentIds().get(0)) : link.getDocumentIds().get(0), correlationIdsToIds.containsKey(link.getDocumentIds().get(1)) ? correlationIdsToIds.get(link.getDocumentIds().get(1)) : link.getDocumentIds().get(1)));
        }
    });
    // commit document changes
    final List<Document> changedDocuments = commitDocumentOperations(operations.stream().filter(operation -> operation instanceof DocumentOperation && operation.isComplete()).map(operation -> (DocumentOperation) operation).collect(toList()), createdDocuments, collectionsMap);
    // remove documents
    final List<Document> removedDocuments = removeDocuments(operations.stream().filter(operation -> operation instanceof DocumentRemovalOperation).map(operation -> (DocumentRemovalOperation) operation).collect(toList()));
    changesTracker.addRemovedDocuments(removedDocuments);
    // remove created documents that were deleted later
    final Set<Document> unusedCreatedDocuments = new HashSet<>(changesTracker.getRemovedDocuments());
    unusedCreatedDocuments.retainAll(changesTracker.getCreatedDocuments());
    changesTracker.getCreatedDocuments().removeAll(unusedCreatedDocuments);
    // create new links
    final List<LinkCreationOperation> linkCreationOperations = operations.stream().filter(operation -> operation instanceof LinkCreationOperation && operation.isComplete()).map(operation -> (LinkCreationOperation) operation).collect(toList());
    final List<LinkInstance> createdLinks = createLinks(linkCreationOperations);
    final Map<String, List<LinkInstance>> linksByType = LinkTypeUtils.getLinksByType(createdLinks);
    final Map<String, LinkType> linkTypesMap = task.getDaoContextSnapshot().getLinkTypeDao().getLinkTypesByIds(linksByType.keySet()).stream().collect(Collectors.toMap(LinkType::getId, Function.identity()));
    final Map<String, String> linkCorrelationIdsToIds = createdLinks.stream().collect(Collectors.toMap(LinkInstance::getTemplateId, LinkInstance::getId));
    // report new empty links, later updates are sent separately
    changesTracker.addCreatedLinkInstances(createdLinks);
    changesTracker.addLinkTypes(linkTypesMap.values().stream().filter(c -> linksByType.containsKey(c.getId())).collect(toSet()));
    // map the newly create link IDs to all other changes so that we use the correct document in updates etc.
    operations.stream().filter(operation -> operation instanceof LinkOperation).forEach(operation -> {
        final LinkInstance link = (LinkInstance) operation.getEntity();
        if (StringUtils.isEmpty(link.getId()) && StringUtils.isNotEmpty(link.getTemplateId())) {
            link.setId(linkCorrelationIdsToIds.get(link.getTemplateId()));
        }
    });
    // commit link changes
    final List<LinkInstance> changedLinkInstances = commitLinkOperations(taskExecutor, operations.stream().filter(operation -> operation instanceof LinkOperation && operation.isComplete()).map(operation -> (LinkOperation) operation).collect(toList()), createdLinks, linkTypesMap);
    // report user messages, print, navigate, and send email requests for rules triggered via an Action button
    final String correlationId = task.getAppId() != null ? task.getAppId().getValue() : null;
    if (StringUtils.isNotEmpty(correlationId)) {
        final List<UserMessageRequest> userMessageRequests = operations.stream().filter(operation -> operation instanceof UserMessageOperation).map(operation -> ((UserMessageOperation) operation).getEntity()).collect(toList());
        changesTracker.addUserMessageRequests(userMessageRequests);
        List<GenericPrintRequest> printRequests = operations.stream().filter(operation -> operation instanceof PrintAttributeOperation).map(operation -> ((PrintAttributeOperation) operation).getEntity()).collect(toList());
        changesTracker.addPrintRequests(printRequests);
        printRequests = operations.stream().filter(operation -> operation instanceof PrintTextOperation).map(operation -> ((PrintTextOperation) operation).getEntity()).collect(toList());
        changesTracker.addPrintRequests(printRequests);
        final List<NavigationRequest> navigationRequests = operations.stream().filter(operation -> operation instanceof NavigationOperation).map(operation -> ((NavigationOperation) operation).getEntity()).collect(toList());
        changesTracker.addNavigationRequests(navigationRequests);
        final List<SendEmailRequest> sendEmailRequests = operations.stream().filter(operation -> operation instanceof SendEmailOperation).map(operation -> ((SendEmailOperation) operation).getEntity()).collect(toList());
        changesTracker.addSendEmailRequests(sendEmailRequests);
    }
    // propagate changes in existing documents and links that has been loaded prior to calling this rule
    task.propagateChanges(changedDocuments, changedLinkInstances);
    return changesTracker;
}
Also used : TaskExecutor(io.lumeer.core.task.TaskExecutor) PrintTextOperation(io.lumeer.core.task.executor.operation.PrintTextOperation) ZonedDateTime(java.time.ZonedDateTime) Operation(io.lumeer.core.task.executor.operation.Operation) StringUtils(org.apache.commons.lang3.StringUtils) CreateLinkInstance(io.lumeer.engine.api.event.CreateLinkInstance) UserMessageOperation(io.lumeer.core.task.executor.operation.UserMessageOperation) GenericPrintRequest(io.lumeer.core.task.executor.request.GenericPrintRequest) ResourceType(io.lumeer.api.model.ResourceType) Map(java.util.Map) FunctionTask(io.lumeer.core.task.FunctionTask) FunctionFacade(io.lumeer.core.facade.FunctionFacade) DocumentOperation(io.lumeer.core.task.executor.operation.DocumentOperation) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) CreateDocument(io.lumeer.engine.api.event.CreateDocument) Set(java.util.Set) Collectors(java.util.stream.Collectors) LinkInstance(io.lumeer.api.model.LinkInstance) PurposeChangeProcessor(io.lumeer.core.facade.detector.PurposeChangeProcessor) LinkType(io.lumeer.api.model.LinkType) PrintAttributeOperation(io.lumeer.core.task.executor.operation.PrintAttributeOperation) List(java.util.List) SendEmailOperation(io.lumeer.core.task.executor.operation.SendEmailOperation) Optional(java.util.Optional) Utils(io.lumeer.core.util.Utils) ChangesTracker(io.lumeer.core.task.executor.ChangesTracker) LinkOperation(io.lumeer.core.task.executor.operation.LinkOperation) DocumentCreationOperation(io.lumeer.core.task.executor.operation.DocumentCreationOperation) SendEmailRequest(io.lumeer.core.task.executor.request.SendEmailRequest) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NavigationOperation(io.lumeer.core.task.executor.operation.NavigationOperation) HashSet(java.util.HashSet) TaskProcessingFacade(io.lumeer.core.facade.TaskProcessingFacade) LinkCreationOperation(io.lumeer.core.task.executor.operation.LinkCreationOperation) DocumentRemovalOperation(io.lumeer.core.task.executor.operation.DocumentRemovalOperation) LinkTypeUtils(io.lumeer.core.util.LinkTypeUtils) CollectionPurposeType(io.lumeer.api.model.CollectionPurposeType) UserMessageRequest(io.lumeer.core.task.executor.request.UserMessageRequest) NavigationRequest(io.lumeer.core.task.executor.request.NavigationRequest) LinkTypeAdapter(io.lumeer.core.adapter.LinkTypeAdapter) DocumentUtils(io.lumeer.core.util.DocumentUtils) UpdateLinkInstance(io.lumeer.engine.api.event.UpdateLinkInstance) RuleTask(io.lumeer.core.task.RuleTask) OperationExecutor(io.lumeer.core.task.executor.operation.OperationExecutor) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) AutoLinkBatchTask(io.lumeer.core.task.AutoLinkBatchTask) Collection(io.lumeer.api.model.Collection) ArrayList(java.util.ArrayList) PrintAttributeOperation(io.lumeer.core.task.executor.operation.PrintAttributeOperation) PrintTextOperation(io.lumeer.core.task.executor.operation.PrintTextOperation) Operation(io.lumeer.core.task.executor.operation.Operation) UserMessageOperation(io.lumeer.core.task.executor.operation.UserMessageOperation) DocumentOperation(io.lumeer.core.task.executor.operation.DocumentOperation) PrintAttributeOperation(io.lumeer.core.task.executor.operation.PrintAttributeOperation) SendEmailOperation(io.lumeer.core.task.executor.operation.SendEmailOperation) LinkOperation(io.lumeer.core.task.executor.operation.LinkOperation) DocumentCreationOperation(io.lumeer.core.task.executor.operation.DocumentCreationOperation) NavigationOperation(io.lumeer.core.task.executor.operation.NavigationOperation) LinkCreationOperation(io.lumeer.core.task.executor.operation.LinkCreationOperation) DocumentRemovalOperation(io.lumeer.core.task.executor.operation.DocumentRemovalOperation) DataDocument(io.lumeer.engine.api.data.DataDocument) Document(io.lumeer.api.model.Document) CreateDocument(io.lumeer.engine.api.event.CreateDocument) UpdateDocument(io.lumeer.engine.api.event.UpdateDocument) DocumentCreationOperation(io.lumeer.core.task.executor.operation.DocumentCreationOperation) DocumentRemovalOperation(io.lumeer.core.task.executor.operation.DocumentRemovalOperation) SendEmailOperation(io.lumeer.core.task.executor.operation.SendEmailOperation) List(java.util.List) ArrayList(java.util.ArrayList) GenericPrintRequest(io.lumeer.core.task.executor.request.GenericPrintRequest) HashSet(java.util.HashSet) PrintTextOperation(io.lumeer.core.task.executor.operation.PrintTextOperation) SendEmailRequest(io.lumeer.core.task.executor.request.SendEmailRequest) DocumentOperation(io.lumeer.core.task.executor.operation.DocumentOperation) NavigationRequest(io.lumeer.core.task.executor.request.NavigationRequest) UserMessageOperation(io.lumeer.core.task.executor.operation.UserMessageOperation) CreateLinkInstance(io.lumeer.engine.api.event.CreateLinkInstance) LinkInstance(io.lumeer.api.model.LinkInstance) UpdateLinkInstance(io.lumeer.engine.api.event.UpdateLinkInstance) LinkOperation(io.lumeer.core.task.executor.operation.LinkOperation) LinkCreationOperation(io.lumeer.core.task.executor.operation.LinkCreationOperation) NavigationOperation(io.lumeer.core.task.executor.operation.NavigationOperation) ChangesTracker(io.lumeer.core.task.executor.ChangesTracker) Collection(io.lumeer.api.model.Collection) LinkType(io.lumeer.api.model.LinkType) UserMessageRequest(io.lumeer.core.task.executor.request.UserMessageRequest)

Example 3 with ChangesTracker

use of io.lumeer.core.task.executor.ChangesTracker in project engine by Lumeer.

the class TaskExecutor method submitTask.

public void submitTask(final Task task) {
    executorService.submit(() -> {
        final ChangesTracker changesTracker = new ChangesTracker();
        task.process(this, changesTracker);
        task.processChanges(changesTracker);
    });
}
Also used : ChangesTracker(io.lumeer.core.task.executor.ChangesTracker)

Example 4 with ChangesTracker

use of io.lumeer.core.task.executor.ChangesTracker in project engine by Lumeer.

the class OperationExecutor method call.

@Override
public ChangesTracker call() {
    final Stage fileAttachmentsStage = new FileAttachmentsStage(this);
    final Stage singleStage = new SingleStage(this);
    final Stage viewsStage = new ViewsUpdatingStage(this);
    final Stage smtpEmailsStage = new SendSmtpEmailsStage(this);
    final Stage sequencesStage = new SequencesStage(this);
    final ChangesTracker changes = fileAttachmentsStage.call();
    changes.merge(singleStage.call());
    changes.merge(viewsStage.call());
    changes.merge(smtpEmailsStage.call());
    changes.merge(sequencesStage.call());
    return changes;
}
Also used : ChangesTracker(io.lumeer.core.task.executor.ChangesTracker) SequencesStage(io.lumeer.core.task.executor.operation.stage.SequencesStage) FileAttachmentsStage(io.lumeer.core.task.executor.operation.stage.FileAttachmentsStage) SequencesStage(io.lumeer.core.task.executor.operation.stage.SequencesStage) Stage(io.lumeer.core.task.executor.operation.stage.Stage) ViewsUpdatingStage(io.lumeer.core.task.executor.operation.stage.ViewsUpdatingStage) SendSmtpEmailsStage(io.lumeer.core.task.executor.operation.stage.SendSmtpEmailsStage) SingleStage(io.lumeer.core.task.executor.operation.stage.SingleStage) ViewsUpdatingStage(io.lumeer.core.task.executor.operation.stage.ViewsUpdatingStage) FileAttachmentsStage(io.lumeer.core.task.executor.operation.stage.FileAttachmentsStage) SingleStage(io.lumeer.core.task.executor.operation.stage.SingleStage) SendSmtpEmailsStage(io.lumeer.core.task.executor.operation.stage.SendSmtpEmailsStage)

Aggregations

ChangesTracker (io.lumeer.core.task.executor.ChangesTracker)4 Collection (io.lumeer.api.model.Collection)2 Document (io.lumeer.api.model.Document)2 LinkInstance (io.lumeer.api.model.LinkInstance)2 LinkType (io.lumeer.api.model.LinkType)2 DataDocument (io.lumeer.engine.api.data.DataDocument)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Attribute (io.lumeer.api.model.Attribute)1 CollectionPurposeType (io.lumeer.api.model.CollectionPurposeType)1 ResourceType (io.lumeer.api.model.ResourceType)1 Function (io.lumeer.api.model.function.Function)1 LinkTypeAdapter (io.lumeer.core.adapter.LinkTypeAdapter)1 ConstraintManager (io.lumeer.core.constraint.ConstraintManager)1 FunctionFacade (io.lumeer.core.facade.FunctionFacade)1 TaskProcessingFacade (io.lumeer.core.facade.TaskProcessingFacade)1