Search in sources :

Example 1 with JsonBuilder

use of nl.knaw.huygens.timbuctoo.util.JsonBuilder in project timbuctoo by HuygensING.

the class EntityToJsonMapper method mapEntity.

public ObjectNode mapEntity(Collection collection, ReadEntity entity, boolean withRelations, ExtraEntityMappingOptions extraEntityMappingOptions, ExtraRelationMappingOptions relationMappingOptions) {
    final ObjectNode mappedEntity = JsonNodeFactory.instance.objectNode();
    String id = entity.getId().toString();
    mappedEntity.set("@type", jsn(collection.getEntityTypeName()));
    mappedEntity.set("_id", jsn(id));
    mappedEntity.set("^rev", jsn(entity.getRev()));
    mappedEntity.set("^deleted", jsn(entity.getDeleted()));
    mappedEntity.set("^pid", jsn(entity.getPid()));
    if (entity.getRdfUri() != null) {
        mappedEntity.set("^rdfUri", jsn(entity.getRdfUri().toString()));
    }
    mappedEntity.set("^rdfAlternatives", jsnA(entity.getRdfAlternatives().stream().map(JsonBuilder::jsn)));
    JsonNode variationRefs = jsnA(entity.getTypes().stream().map(type -> {
        ObjectNode variationRef = jsnO();
        variationRef.set("id", jsn(id));
        variationRef.set("type", jsn(type));
        return variationRef;
    }));
    mappedEntity.set("@variationRefs", variationRefs);
    Change modified = entity.getModified();
    mappedEntity.set("^modified", mapChange(modified));
    Change created = entity.getCreated();
    mappedEntity.set("^created", mapChange(created));
    // translate TimProperties to Json
    JsonPropertyConverter jsonPropertyConverter = new JsonPropertyConverter(collection);
    entity.getProperties().forEach(prop -> {
        try {
            Tuple<String, JsonNode> convertedProperty = prop.convert(jsonPropertyConverter);
            mappedEntity.set(convertedProperty.getLeft(), convertedProperty.getRight());
        } catch (IOException e) {
            LOG.error(databaseInvariant, propConversionErrorMessage(id, prop));
            LOG.error("Exception message: {}", e.getMessage());
            LOG.debug("Stack trace", e);
        }
    });
    if (!Strings.isNullOrEmpty(entity.getDisplayName())) {
        mappedEntity.set("@displayName", jsn(entity.getDisplayName()));
    }
    extraEntityMappingOptions.execute(entity, mappedEntity);
    if (withRelations) {
        mappedEntity.set("@relationCount", jsn(entity.getRelations().size()));
        mappedEntity.set("@relations", mapRelations(entity.getRelations(), relationMappingOptions));
    }
    return mappedEntity;
}
Also used : JsonBuilder(nl.knaw.huygens.timbuctoo.util.JsonBuilder) UserValidator(nl.knaw.huygens.timbuctoo.v5.security.UserValidator) JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) JsonBuilder(nl.knaw.huygens.timbuctoo.util.JsonBuilder) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) UrlGenerator(nl.knaw.huygens.timbuctoo.crud.UrlGenerator) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Strings(com.google.common.base.Strings) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) Logmarkers.databaseInvariant(nl.knaw.huygens.timbuctoo.logging.Logmarkers.databaseInvariant) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) Logger(org.slf4j.Logger) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) UUID(java.util.UUID) ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) List(java.util.List) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Change(nl.knaw.huygens.timbuctoo.model.Change) Tuple.tuple(nl.knaw.huygens.timbuctoo.util.Tuple.tuple) UserValidationException(nl.knaw.huygens.timbuctoo.v5.security.exceptions.UserValidationException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonNode(com.fasterxml.jackson.databind.JsonNode) Change(nl.knaw.huygens.timbuctoo.model.Change) IOException(java.io.IOException)

Example 2 with JsonBuilder

use of nl.knaw.huygens.timbuctoo.util.JsonBuilder in project timbuctoo by HuygensING.

the class JsonMetadata method getForCollection.

private JsonNode getForCollection(Collection collection, List<Vertex> relations, boolean withCollectionInfo) {
    ArrayNode result = jsnA();
    collection.getWriteableProperties().forEach((name, prop) -> {
        ObjectNode desc = jsnO("name", jsn(name), "type", jsn(prop.getTypeId()));
        prop.getOptions().ifPresent(options -> desc.set("options", jsnA(options.stream().map(JsonBuilder::jsn))));
        prop.getParts().ifPresent(parts -> desc.set("options", jsnA(parts.stream().map(JsonBuilder::jsn))));
        result.add(desc);
    });
    // FIXME add check to vres that certifies that the defined derived relations exist in the database
    String abstractType = collection.getAbstractType();
    Vre vre = collection.getVre();
    Map<String, String> keywordTypes = vre.getKeywordTypes();
    String relationCollectionName = vre.getImplementerOf("relation").map(Collection::getCollectionName).orElse(null);
    if (relationCollectionName == null) {
        LOG.warn(Logmarkers.databaseInvariant, "Collection {} seems to have no relationCollections", collection.getCollectionName());
    }
    relations.stream().filter(v -> getProp(v, "relationtype_sourceTypeName", String.class).orElse("").equals(abstractType)).forEach(v -> {
        String timId = getProp(v, "tim_id", String.class).orElse("<unknown>");
        Optional<String> regularName = getProp(v, "relationtype_regularName", String.class);
        Optional<String> inverseName = getProp(v, "relationtype_inverseName", String.class);
        Optional<String> abstractTargetType = getProp(v, "relationtype_targetTypeName", String.class);
        Optional<String> targetType = abstractTargetType.flatMap(typeName -> vre.getImplementerOf(typeName).map(Collection::getCollectionName));
        if (regularName.isPresent() && inverseName.isPresent() && targetType.isPresent()) {
            // special support for keywords:
            URI quickSearchUrl;
            if (abstractTargetType.orElse("").equals("keyword")) {
                quickSearchUrl = Autocomplete.makeUrl(targetType.get(), Optional.empty(), Optional.ofNullable(keywordTypes.get(regularName.get())));
            } else {
                quickSearchUrl = Autocomplete.makeUrl(targetType.get());
            }
            boolean isSymmetric = getProp(v, "relationtype_symmetric", Boolean.class).orElse(false);
            result.add(jsnO("name", jsn(regularName.get()), "type", jsn("relation"), "quicksearch", jsn(quickSearchUrl.toString()), "relation", jsnO(// for search
            "direction", isSymmetric ? jsn("BOTH") : jsn("OUT"), "outName", jsn(regularName.get()), "inName", jsn(inverseName.get()), "targetCollection", jsn(targetType.get()), // for CRUD
            "relationCollection", jsn(relationCollectionName), "relationTypeId", jsn(timId))));
        } else {
            if (!regularName.isPresent() || !inverseName.isPresent() || !abstractTargetType.isPresent()) {
                LOG.error(Logmarkers.databaseInvariant, "RelationType should have a relationtype_regularName, relationtype_inverseName and " + "relationtype_targetTypeName, but one of those is missing for " + v.id());
            }
        }
    });
    relations.stream().filter(v -> {
        final String targetType = getProp(v, "relationtype_targetTypeName", String.class).orElse("");
        final boolean isSymmetric = getProp(v, "relationtype_symmetric", Boolean.class).orElse(false);
        if (isSymmetric) {
            final String sourceType = getProp(v, "relationtype_sourceTypeName", String.class).orElse("");
            return targetType.equals(abstractType) && !sourceType.equals(targetType);
        } else {
            return targetType.equals(abstractType);
        }
    }).forEach(v -> {
        String timId = getProp(v, "tim_id", String.class).orElse("<unknown>");
        Optional<String> regularName = getProp(v, "relationtype_regularName", String.class);
        Optional<String> inverseName = getProp(v, "relationtype_inverseName", String.class);
        Optional<String> abstractTargetType = getProp(v, "relationtype_sourceTypeName", String.class);
        Optional<String> targetType = abstractTargetType.flatMap(typeName -> vre.getImplementerOf(typeName).map(Collection::getCollectionName));
        if (regularName.isPresent() && inverseName.isPresent() && targetType.isPresent()) {
            // special support for keywords:
            URI quickSearchUrl;
            if (abstractTargetType.orElse("").equals("keyword")) {
                quickSearchUrl = Autocomplete.makeUrl(targetType.get(), Optional.empty(), Optional.ofNullable(keywordTypes.get(regularName.get())));
            } else {
                quickSearchUrl = Autocomplete.makeUrl(targetType.get());
            }
            result.add(jsnO("name", jsn(inverseName.get()), "type", jsn("relation"), "quicksearch", jsn(quickSearchUrl.toString()), "relation", jsnO(// for search
            "direction", jsn("IN"), "outName", jsn(regularName.get()), "inName", jsn(inverseName.get()), "targetCollection", jsn(targetType.get()), // for CRUD
            "relationCollection", jsn(relationCollectionName), "relationTypeId", jsn(timId))));
        } else {
            if (!regularName.isPresent() || !inverseName.isPresent() || !abstractTargetType.isPresent()) {
                LOG.error(Logmarkers.databaseInvariant, "RelationType should have a relationtype_regularName, relationtype_inverseName and " + "relationtype_targetTypeName, but one of those is missing for " + v.id());
            }
        }
    });
    if (withCollectionInfo) {
        return jsnO("collectionName", jsn(collection.getCollectionName()), "collectionLabel", jsn(collection.getCollectionLabel()), "description", jsn(collection.getDescription()), "unknown", jsn(collection.isUnknown()), "relationCollection", jsn(collection.isRelationCollection()), "archetypeName", jsn(collection.getAbstractType()), "properties", result);
    } else {
        return result;
    }
}
Also used : JsonBuilder.jsnO(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO) Logger(org.slf4j.Logger) Autocomplete(nl.knaw.huygens.timbuctoo.server.endpoints.v2.domain.Autocomplete) JsonBuilder(nl.knaw.huygens.timbuctoo.util.JsonBuilder) LabelP(org.apache.tinkerpop.gremlin.neo4j.process.traversal.LabelP) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Logmarkers(nl.knaw.huygens.timbuctoo.logging.Logmarkers) Vres(nl.knaw.huygens.timbuctoo.model.vre.Vres) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) List(java.util.List) GraphWrapper(nl.knaw.huygens.timbuctoo.server.GraphWrapper) Stream(java.util.stream.Stream) Map(java.util.Map) JsonBuilder.jsnA(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) Optional(java.util.Optional) GraphReadUtils.getProp(nl.knaw.huygens.timbuctoo.model.GraphReadUtils.getProp) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) JsonBuilder.jsn(nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) URI(java.net.URI)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 List (java.util.List)2 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)2 JsonBuilder (nl.knaw.huygens.timbuctoo.util.JsonBuilder)2 JsonBuilder.jsn (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsn)2 JsonBuilder.jsnA (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnA)2 JsonBuilder.jsnO (nl.knaw.huygens.timbuctoo.util.JsonBuilder.jsnO)2 Logger (org.slf4j.Logger)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 Strings (com.google.common.base.Strings)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Collectors.groupingBy (java.util.stream.Collectors.groupingBy)1 Stream (java.util.stream.Stream)1