use of io.openk9.entity.manager.model.payload.Response 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));
}
});
}
use of io.openk9.entity.manager.model.payload.Response in project openk9 by smclab.
the class ResourcesHttpHandler method _sendResource.
private Mono<Void> _sendResource(Tenant tenant, Datasource datasource, PluginDriverDTO pluginDriverDTO, long datasourceId, String documentId, String resourceId, HttpServerRequest httpRequest, HttpServerResponse httpResponse) {
return _queryParser.apply(QueryParser.Context.of(tenant, null, null, null, httpRequest, QueryParser.QueryCondition.DEFAULT)).flatMap(consumer -> _search.search(factory -> {
SearchRequest searchRequest = factory.createSearchRequestData(tenant.getTenantId(), pluginDriverDTO.getName());
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.idsQuery().addIds(documentId));
boolQueryBuilder.must(QueryBuilders.nestedQuery(_RESOURCES_BINARIES, QueryBuilders.matchQuery(_RESOURCES_BINARIES_ID, resourceId), ScoreMode.Max));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
consumer.accept(boolQueryBuilder);
searchSourceBuilder.query(boolQueryBuilder);
searchSourceBuilder.fetchSource(new String[] { _RESOURCES_BINARIES_DATA, _RESOURCES_BINARIES_CONTENT_TYPE }, null);
return searchRequest.source(searchSourceBuilder);
})).flatMap(response -> {
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
return httpResponse.sendNotFound();
} else if (hits.length > 1) {
_log.warn("found more than one resource (datasourceId: " + datasourceId + " resourceId: " + resourceId + " tenantId: " + tenant.getTenantId() + " documentId: " + documentId + ")");
}
SearchHit hit = hits[0];
String source = hit.getSourceAsString();
Resources resources = _jsonFactory.fromJson(source, Resources.class);
ResourcesPayload resourcesPayload = resources.getResources();
BinaryPayload binaryPayload = resourcesPayload.getBinaries().get(0);
String data = binaryPayload.getData();
String contentType = binaryPayload.getContentType();
_manageCache(httpRequest, httpResponse);
if (contentType != null && !contentType.isBlank()) {
httpResponse.header(_CONTENT_TYPE, contentType);
}
byte[] decode = Base64.getDecoder().decode(data);
return Mono.from(httpResponse.sendByteArray(Mono.just(decode)));
});
}
use of io.openk9.entity.manager.model.payload.Response in project openk9 by smclab.
the class BaseNerEnrichProcessor method _getEntityOrCreate.
private Mono<JsonNode> _getEntityOrCreate(ObjectNode jsonNode) {
return _entityManagerClient.getOrAddEntities(_jsonFactory.fromJsonNode(jsonNode, Request.class)).map(httpResponse -> {
List<Response> responseList = httpResponse.getResponse();
JsonNode entitiesJsonNode = jsonNode.get(entitiesField());
ArrayNode entitiesArrayNode = entitiesJsonNode.toArrayNode();
ArrayNode arrayNode = _jsonFactory.createArrayNode();
for (JsonNode node : entitiesArrayNode) {
Optional<Response> responseOptional = responseList.stream().filter(response -> node.get("tmpId").asLong() == response.getTmpId()).findFirst();
if (responseOptional.isPresent()) {
Entity entity = responseOptional.get().getEntity();
ObjectNode result = _jsonFactory.createObjectNode();
result.put("entityType", entity.getType());
result.put("id", entity.getId());
result.put("context", node.get("context"));
arrayNode.add(result);
}
}
return arrayNode;
});
}
Aggregations