use of io.openk9.entity.manager.model.payload.RelationRequest in project openk9 by smclab.
the class GetOrAddEntitiesConsumer method writeRelations.
public Mono<ResponseList> writeRelations(List<EntityContext> entityContext) {
return Mono.defer(() -> {
List<Statement> statementList = new ArrayList<>();
for (EntityContext context : entityContext) {
EntityRequest entityRequest = context.getEntityRequest();
List<RelationRequest> relations = entityRequest.getRelations();
if (relations == null || relations.isEmpty()) {
continue;
}
Entity currentEntity = context.getEntity();
List<Tuple2<String, Entity>> entityRelations = entityContext.stream().flatMap(entry -> {
for (RelationRequest relation : relations) {
if (entry.getEntityRequest().getTmpId() == relation.getTo()) {
return Stream.of(Tuples.of(relation.getName(), entry.getEntity()));
}
}
return Stream.empty();
}).collect(Collectors.toList());
Node currentEntityNode = Cypher.node(currentEntity.getType()).named("a");
List<Statement> currentStatementList = entityRelations.stream().map(t2 -> {
Entity entityRelation = t2.getT2();
Node entityRelationNode = Cypher.node(entityRelation.getType()).named("b");
return Cypher.match(currentEntityNode, entityRelationNode).where(Functions.id(currentEntityNode).eq(literalOf(currentEntity.getId())).and(Functions.id(entityRelationNode).eq(literalOf(entityRelation.getId())))).merge(currentEntityNode.relationshipTo(entityRelationNode, t2.getT1())).build();
}).collect(Collectors.toList());
statementList.addAll(currentStatementList);
}
List<Response> response = entityContext.stream().map(context -> Response.builder().entity(Entity.builder().name(context.getEntity().getName()).id(context.getEntity().getId()).tenantId(context.getEntity().getTenantId()).type(context.getEntity().getType()).build()).tmpId(context.getEntityRequest().getTmpId()).build()).collect(Collectors.toList());
if (statementList.size() > 1) {
return _graphClient.write(Cypher.unionAll(statementList.toArray(new Statement[0]))).then(Mono.just(ResponseList.of("", response)));
} else if (statementList.size() == 1) {
return _graphClient.write(statementList.get(0)).then(Mono.just(ResponseList.of("", response)));
} else {
return Mono.just(ResponseList.of("", response));
}
});
}
use of io.openk9.entity.manager.model.payload.RelationRequest in project openk9 by smclab.
the class EntityManagerBus method run.
@SneakyThrows
public void run() {
while (true) {
Payload request = _entityManagerQueue.take();
TransactionContext transactionContext = _hazelcastInstance.newTransactionContext();
transactionContext.beginTransaction();
try {
TransactionalMap<EntityKey, Entity> entityTransactionalMap = transactionContext.getMap("entityMap");
TransactionalMap<EntityRelationKey, EntityRelation> transactionalEntityRelationMap = transactionContext.getMap("entityRelationMap");
TransactionalMultiMap<DocumentKey, String> documentEntityMap = transactionContext.getMultiMap("documentEntityMap");
EntityManagerRequest payload = request.getPayload();
_loggerAggregator.emitLog("process ingestionId", payload.getIngestionId());
long tenantId = payload.getTenantId();
String ingestionId = payload.getIngestionId();
List<EntityRequest> entities = request.getEntities();
Map<EntityKey, Entity> localEntityMap = new HashMap<>(entities.size());
for (EntityRequest entityRequest : entities) {
String name = entityRequest.getName();
String type = entityRequest.getType();
String cacheId = Long.toString(_entityFlakeId.newId());
EntityKey key = EntityKey.of(tenantId, name, type, cacheId, ingestionId);
Entity entity = new Entity(null, cacheId, tenantId, name, type, null, ingestionId, false, true, entityRequest.getContext());
entityTransactionalMap.set(key, entity);
localEntityMap.put(key, entity);
for (EntityRequest entityRequest2 : entities) {
for (RelationRequest relation : entityRequest2.getRelations()) {
if (relation.getTo().equals(entityRequest.getTmpId())) {
relation.setTo(entity.getCacheId());
}
}
}
}
for (EntityRequest entity : entities) {
List<RelationRequest> relations = entity.getRelations();
if (relations == null || relations.isEmpty()) {
continue;
}
Collection<Entity> values = localEntityMap.values();
Entity current = values.stream().filter(e -> e.getName().equals(entity.getName()) && e.getType().equals(entity.getType())).findFirst().orElse(null);
if (current == null) {
continue;
}
for (RelationRequest relation : relations) {
String to = relation.getTo();
String name = relation.getName();
for (Entity value : values) {
if (value.getCacheId().equals(to)) {
long entityRelationId = _entityRelationFlakeId.newId();
EntityRelation entityRelation = new EntityRelation(entityRelationId, current.getCacheId(), ingestionId, name, value.getCacheId());
transactionalEntityRelationMap.set(EntityRelationKey.of(entityRelationId, current.getCacheId(), ingestionId), entityRelation);
}
}
}
}
if (!localEntityMap.isEmpty()) {
DocumentKey key = DocumentKey.of(payload.getDatasourceId(), payload.getContentId(), tenantId);
for (Entity value : localEntityMap.values()) {
documentEntityMap.put(key, value.getCacheId());
}
}
} catch (Exception e) {
_log.error(e.getMessage(), e);
transactionContext.rollbackTransaction();
} finally {
transactionContext.commitTransaction();
}
}
}
use of io.openk9.entity.manager.model.payload.RelationRequest in project openk9 by smclab.
the class GetOrAddEntitiesHttpHandler method writeRelations.
public Mono<ResponseList> writeRelations(List<EntityContext> entityContext) {
return Mono.defer(() -> {
List<Statement> statementList = new ArrayList<>();
for (EntityContext context : entityContext) {
EntityRequest entityRequest = context.getEntityRequest();
List<RelationRequest> relations = entityRequest.getRelations();
if (relations == null || relations.isEmpty()) {
continue;
}
Entity currentEntity = context.getEntity();
List<Tuple2<String, Entity>> entityRelations = entityContext.stream().flatMap(entry -> {
for (RelationRequest relation : relations) {
if (entry.getEntityRequest().getTmpId() == relation.getTo()) {
return Stream.of(Tuples.of(relation.getName(), entry.getEntity()));
}
}
return Stream.empty();
}).collect(Collectors.toList());
Node currentEntityNode = Cypher.node(currentEntity.getType()).named("a");
List<Statement> currentStatementList = entityRelations.stream().map(t2 -> {
Entity entityRelation = t2.getT2();
Node entityRelationNode = Cypher.node(entityRelation.getType()).named("b");
return Cypher.match(currentEntityNode, entityRelationNode).where(Functions.id(currentEntityNode).eq(literalOf(currentEntity.getId())).and(Functions.id(entityRelationNode).eq(literalOf(entityRelation.getId())))).merge(currentEntityNode.relationshipTo(entityRelationNode, t2.getT1())).build();
}).collect(Collectors.toList());
statementList.addAll(currentStatementList);
}
List<Response> response = entityContext.stream().map(context -> Response.builder().entity(Entity.builder().name(context.getEntity().getName()).id(context.getEntity().getId()).tenantId(context.getEntity().getTenantId()).type(context.getEntity().getType()).build()).tmpId(context.getEntityRequest().getTmpId()).build()).collect(Collectors.toList());
if (statementList.size() > 1) {
return _graphClient.write(Cypher.unionAll(statementList.toArray(new Statement[0]))).then(Mono.just(ResponseList.of("", response)));
} else if (statementList.size() == 1) {
return _graphClient.write(statementList.get(0)).then(Mono.just(ResponseList.of("", response)));
} else {
return Mono.just(ResponseList.of("", response));
}
});
}
Aggregations