Search in sources :

Example 1 with DocumentMatcher

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

the class AutoLinkRuleTaskExecutor method addLinks.

private void addLinks(final TaskExecutor taskExecutor, final List<LinkInstance> links, final DocumentMatcher matcher) {
    final String thisCollection = matcher.getThisCollection().getId();
    if (thisCollection.equals(matcher.getThatCollection().getId())) {
        return;
    }
    final String thisDocumentId = matcher.getThisDocument().getId();
    final Set<String> alreadyLinkedDocumentIds = links.stream().map(link -> {
        final var res = new ArrayList<>(link.getDocumentIds());
        res.remove(thisDocumentId);
        return res.get(0);
    }).collect(toSet());
    final Tuple<List<Document>, List<LinkInstance>> newLinks = matcher.filterForCreation();
    final Set<String> targetDocuments = newLinks.getFirst().stream().map(Document::getId).filter(id -> !alreadyLinkedDocumentIds.contains(id)).collect(toSet());
    if (targetDocuments.size() > 0) {
        final List<LinkInstance> linkInstances = targetDocuments.stream().filter(documentId -> !documentId.equals(matcher.getNewDocument().getId())).map(documentId -> {
            var l = new LinkInstance(rule.getLinkType(), Arrays.asList(matcher.getNewDocument().getId(), documentId));
            l.setCreatedBy(ruleTask.getInitiator().getId());
            l.setCreationDate(ZonedDateTime.now());
            return l;
        }).collect(toList());
        if (!linkInstances.isEmpty()) {
            ruleTask.getDaoContextSnapshot().getLinkInstanceDao().createLinkInstances(linkInstances);
            changesTracker.updateLinkTypesMap(Map.of(matcher.getLinkType().getId(), matcher.getLinkType()));
            changesTracker.addCreatedLinkInstances(linkInstances);
            linkInstances.forEach(link -> {
                var decodedCreatedData = constraintManager.decodeDataTypes(matcher.getLinkType(), link.getData());
                auditAdapter.registerCreate(link.getLinkTypeId(), ResourceType.LINK, link.getId(), ruleTask.getInitiator(), ruleName, null, decodedCreatedData);
            });
            final FunctionFacade functionFacade = ruleTask.getFunctionFacade();
            linkInstances.forEach(linkInstance -> {
                taskExecutor.submitTask(functionFacade.createTaskForCreatedLinks(matcher.getLinkType(), Collections.singletonList(linkInstance)));
            });
        }
    }
}
Also used : Tuple(io.lumeer.core.util.Tuple) DocumentMatcher(io.lumeer.core.task.executor.matcher.DocumentMatcher) Arrays(java.util.Arrays) TaskExecutor(io.lumeer.core.task.TaskExecutor) ZonedDateTime(java.time.ZonedDateTime) Document(io.lumeer.api.model.Document) Set(java.util.Set) AutoLinkRule(io.lumeer.api.model.rule.AutoLinkRule) Logger(java.util.logging.Logger) AuditAdapter(io.lumeer.core.adapter.AuditAdapter) StringUtils(org.apache.commons.lang3.StringUtils) LinkInstance(io.lumeer.api.model.LinkInstance) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Collectors.toList(java.util.stream.Collectors.toList) DefaultConfigurationProducer(io.lumeer.core.facade.configuration.DefaultConfigurationProducer) List(java.util.List) ResourceType(io.lumeer.api.model.ResourceType) RuleTask(io.lumeer.core.task.RuleTask) Map(java.util.Map) Collections(java.util.Collections) Collectors.toSet(java.util.stream.Collectors.toSet) ConstraintManager(io.lumeer.core.constraint.ConstraintManager) FunctionFacade(io.lumeer.core.facade.FunctionFacade) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) LinkInstance(io.lumeer.api.model.LinkInstance) Document(io.lumeer.api.model.Document) FunctionFacade(io.lumeer.core.facade.FunctionFacade)

Example 2 with DocumentMatcher

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

the class AutoLinkRuleTaskExecutor method execute.

public ChangesTracker execute(final TaskExecutor taskExecutor) {
    try {
        final DocumentMatcher matcher = new DocumentMatcher(ruleTask.getDaoContextSnapshot(), ruleTask);
        if (matcher.initialize(rule)) {
            // both documents are set
            if (matcher.getOldDocument() != null && matcher.getNewDocument() != null) {
                // the attributes are different
                if ((matcher.getOldValue() != null && !matcher.getOldValue().equals(matcher.getNewValue())) || (matcher.getNewValue() != null && !matcher.getNewValue().equals(matcher.getOldValue()))) {
                    final List<LinkInstance> links = matcher.getAllLinkInstances();
                    List<LinkInstance> remainingLinks = List.of();
                    // it was not null before
                    if (matcher.getOldValue() != null && StringUtils.isNotEmpty(matcher.getOldValue().toString())) {
                        remainingLinks = removeLinks(taskExecutor, links, matcher);
                    }
                    // and it is not null either
                    if (matcher.getNewValue() != null && StringUtils.isNotEmpty(matcher.getNewValue().toString())) {
                        addLinks(taskExecutor, remainingLinks, matcher);
                    }
                }
            } else {
                // new document was created
                if (matcher.getNewDocument() != null && matcher.getNewDocument().getData().get(matcher.getThisAttribute().getId()) != null) {
                    addLinks(taskExecutor, List.of(), matcher);
                }
            }
        }
        return changesTracker;
    } catch (Exception ex) {
        log.log(Level.SEVERE, "Error executing auto-link rule: ", ex);
        return changesTracker;
    }
}
Also used : DocumentMatcher(io.lumeer.core.task.executor.matcher.DocumentMatcher) LinkInstance(io.lumeer.api.model.LinkInstance)

Aggregations

LinkInstance (io.lumeer.api.model.LinkInstance)2 DocumentMatcher (io.lumeer.core.task.executor.matcher.DocumentMatcher)2 Document (io.lumeer.api.model.Document)1 ResourceType (io.lumeer.api.model.ResourceType)1 AutoLinkRule (io.lumeer.api.model.rule.AutoLinkRule)1 AuditAdapter (io.lumeer.core.adapter.AuditAdapter)1 ConstraintManager (io.lumeer.core.constraint.ConstraintManager)1 FunctionFacade (io.lumeer.core.facade.FunctionFacade)1 DefaultConfigurationProducer (io.lumeer.core.facade.configuration.DefaultConfigurationProducer)1 RuleTask (io.lumeer.core.task.RuleTask)1 TaskExecutor (io.lumeer.core.task.TaskExecutor)1 Tuple (io.lumeer.core.util.Tuple)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Level (java.util.logging.Level)1