Search in sources :

Example 11 with JsonNode

use of io.openk9.json.api.JsonNode 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;
    });
}
Also used : Response(io.openk9.entity.manager.model.payload.Response) EnrichItem(io.openk9.model.EnrichItem) PluginDriverDTO(io.openk9.plugin.driver.manager.model.PluginDriverDTO) Response(io.openk9.entity.manager.model.payload.Response) JsonNode(io.openk9.json.api.JsonNode) Mono(reactor.core.publisher.Mono) Entity(io.openk9.entity.manager.model.Entity) ArrayNode(io.openk9.json.api.ArrayNode) JsonFactory(io.openk9.json.api.JsonFactory) DatasourceContext(io.openk9.model.DatasourceContext) List(java.util.List) EntityManagerClient(io.openk9.entity.manager.client.api.EntityManagerClient) Map(java.util.Map) Optional(java.util.Optional) Request(io.openk9.entity.manager.model.payload.Request) ObjectNode(io.openk9.json.api.ObjectNode) HttpClient(io.openk9.http.client.HttpClient) Constants(io.openk9.core.api.constant.Constants) Entity(io.openk9.entity.manager.model.Entity) ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode) ArrayNode(io.openk9.json.api.ArrayNode)

Example 12 with JsonNode

use of io.openk9.json.api.JsonNode in project openk9 by smclab.

the class BaseNerEnrichProcessor method process.

@Override
public Mono<ObjectNode> process(ObjectNode objectNode, DatasourceContext context, EnrichItem enrichItem, PluginDriverDTO pluginDriverName) {
    return Mono.defer(() -> {
        JsonNode datasourceConfiguration = _jsonFactory.fromJsonToJsonNode(enrichItem.getJsonConfig());
        if (!datasourceConfiguration.isObject()) {
            return Mono.error(new RuntimeException("jsonConfig must be an instance of ObjectNode " + datasourceConfiguration.toString()));
        }
        ObjectNode request = prepareRequestRawContent(objectNode, datasourceConfiguration.toObjectNode(), context, pluginDriverName);
        return Mono.from(_httpClient.request(getMethod(), getPath(), request.toString(), getHeaders())).retry(5).map(_jsonFactory::fromJsonToJsonNode).map(JsonNode::toObjectNode).map(jsonNodes -> {
            jsonNodes.put(Constants.TENANT_ID, context.getTenant().getTenantId());
            jsonNodes.put(Constants.DATASOURCE_ID, context.getDatasource().getDatasourceId());
            jsonNodes.put(Constants.CONTENT_ID, objectNode.get(Constants.CONTENT_ID));
            jsonNodes.put(Constants.RAW_CONTENT, objectNode.get(Constants.RAW_CONTENT));
            jsonNodes.put(Constants.INGESTION_ID, objectNode.get(Constants.INGESTION_ID));
            return jsonNodes;
        }).flatMap(this::_getEntityOrCreate).map(entities -> objectNode.set(entitiesField(), entities));
    });
}
Also used : ObjectNode(io.openk9.json.api.ObjectNode) JsonNode(io.openk9.json.api.JsonNode)

Aggregations

JsonNode (io.openk9.json.api.JsonNode)12 ObjectNode (io.openk9.json.api.ObjectNode)8 ArrayNode (io.openk9.json.api.ArrayNode)7 JsonFactory (io.openk9.json.api.JsonFactory)3 Map (java.util.Map)3 Mono (reactor.core.publisher.Mono)3 Entity (io.openk9.entity.manager.model.Entity)2 Request (io.openk9.entity.manager.model.payload.Request)2 Response (io.openk9.entity.manager.model.payload.Response)2 HttpClient (io.openk9.http.client.HttpClient)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Activate (org.osgi.service.component.annotations.Activate)2 Constants (io.openk9.core.api.constant.Constants)1 EntityManagerClient (io.openk9.entity.manager.client.api.EntityManagerClient)1 EntityRequest (io.openk9.entity.manager.model.payload.EntityRequest)1 RelationRequest (io.openk9.entity.manager.model.payload.RelationRequest)1 ResponseList (io.openk9.entity.manager.model.payload.ResponseList)1