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)));
});
}
}
}
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;
}
}
Aggregations