Search in sources :

Example 1 with UnknownPropertyException

use of nl.knaw.huygens.timbuctoo.core.UnknownPropertyException in project timbuctoo by HuygensING.

the class JsonToEntityMapper method getDataProperties.

/**
 * Retrieve all the properties that contain client data.
 */
public List<TimProperty<?>> getDataProperties(Collection collection, ObjectNode input) throws IOException {
    JsonPropertyConverter converter = new JsonPropertyConverter(collection);
    Set<String> fieldNames = getDataFields(input);
    List<TimProperty<?>> properties = Lists.newArrayList();
    for (String fieldName : fieldNames) {
        try {
            TimProperty<?> property = converter.from(fieldName, input.get(fieldName));
            // This is the only place to filter the empty strings.
            if (!(property.getValue() instanceof String) || !StringUtils.isBlank((String) property.getValue())) {
                properties.add(property);
            }
        } catch (UnknownPropertyException e) {
            LOG.error("Property with name '{}' is unknown for collection '{}'.", fieldName, collection.getCollectionName());
            throw new IOException(String.format("Items of %s have no property %s", collection.getCollectionName(), fieldName));
        } catch (IOException e) {
            LOG.error("Property '{}' with value '{}' could not be converted", fieldName, input.get(fieldName));
            throw new IOException(String.format("Property '%s' could not be converted. %s", fieldName, e.getMessage()), e);
        }
    }
    return properties;
}
Also used : TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) IOException(java.io.IOException) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException)

Example 2 with UnknownPropertyException

use of nl.knaw.huygens.timbuctoo.core.UnknownPropertyException in project timbuctoo by HuygensING.

the class TinkerPopToEntityMapper method mapEntity.

public ReadEntity mapEntity(GraphTraversal<Vertex, Vertex> entityT, boolean withRelations) {
    final List<TimProperty<?>> properties = Lists.newArrayList();
    TinkerPopPropertyConverter dbPropertyConverter = new TinkerPopPropertyConverter(collection);
    String entityTypeName = collection.getEntityTypeName();
    GraphTraversal[] propertyGetters = collection.getReadableProperties().entrySet().stream().map(prop -> prop.getValue().traversalRaw().sideEffect(x -> x.get().onSuccess(value -> {
        try {
            properties.add(dbPropertyConverter.from(prop.getKey(), value));
        } catch (UnknownPropertyException e) {
            LOG.error("Unknown property", e);
        } catch (IOException e) {
            LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
        }
    }).onFailure(e -> {
        if (e.getCause() instanceof IOException) {
            LOG.error(databaseInvariant, "Property '" + prop.getKey() + "' is not encoded correctly", e.getCause());
        } else {
            LOG.error("Something went wrong while reading the property '" + prop.getKey() + "'.", e.getCause());
        }
    }))).toArray(GraphTraversal[]::new);
    entityT.asAdmin().clone().union(propertyGetters).forEachRemaining(x -> {
    // Force side effects to happen
    });
    ReadEntityImpl entity = new ReadEntityImpl();
    entity.setProperties(properties);
    Vertex entityVertex = entityT.asAdmin().clone().next();
    // TODO make use conversion for the types
    entity.setRev(getProp(entityVertex, "rev", Integer.class).orElse(-1));
    entity.setDeleted(getProp(entityVertex, "deleted", Boolean.class).orElse(false));
    entity.setPid(getProp(entityVertex, "pid", String.class).orElse(null));
    URI rdfUri = getProp(entityVertex, RDF_URI_PROP, String.class).map(x -> {
        try {
            return new URI(x);
        } catch (URISyntaxException e) {
            return null;
        }
    }).orElse(null);
    entity.setRdfUri(rdfUri);
    Property<String[]> rdfAlternativesProp = entityVertex.property(RDF_SYNONYM_PROP);
    if (rdfAlternativesProp.isPresent()) {
        try {
            entity.setRdfAlternatives(Lists.newArrayList(rdfAlternativesProp.value()));
        } catch (Exception e) {
            LOG.error(databaseInvariant, "Error while reading rdfAlternatives", e);
        }
    }
    Optional<String> typesOptional = getProp(entityVertex, "types", String.class);
    if (typesOptional.isPresent()) {
        try {
            List<String> types = new ObjectMapper().readValue(typesOptional.get(), new TypeReference<List<String>>() {
            });
            entity.setTypes(types);
        } catch (Exception e) {
            LOG.error(databaseInvariant, "Error while generating variation refs", e);
            entity.setTypes(Lists.newArrayList(entityTypeName));
        }
    } else {
        entity.setTypes(Lists.newArrayList(entityTypeName));
    }
    Optional<String> modifiedStringOptional = getProp(entityVertex, "modified", String.class);
    if (modifiedStringOptional.isPresent()) {
        try {
            entity.setModified(new ObjectMapper().readValue(modifiedStringOptional.get(), Change.class));
        } catch (IOException e) {
            LOG.error(databaseInvariant, "Change cannot be converted", e);
            entity.setModified(new Change());
        }
    } else {
        entity.setModified(new Change());
    }
    Optional<String> createdStringOptional = getProp(entityVertex, "created", String.class);
    if (createdStringOptional.isPresent()) {
        try {
            entity.setCreated(new ObjectMapper().readValue(createdStringOptional.get(), Change.class));
        } catch (IOException e) {
            LOG.error(databaseInvariant, "Change cannot be converted", e);
            entity.setCreated(new Change());
        }
    } else {
        entity.setCreated(new Change());
    }
    entity.setDisplayName(DisplayNameHelper.getDisplayname(traversalSource, entityVertex, collection).orElse(""));
    entity.setId(getIdOrDefault(entityVertex));
    if (withRelations) {
        entity.setRelations(getRelations(entityVertex, traversalSource, collection));
    }
    customEntityProperties.execute(entity, entityVertex);
    return entity;
}
Also used : StreamIterator.stream(nl.knaw.huygens.timbuctoo.util.StreamIterator.stream) LabelP(org.apache.tinkerpop.gremlin.neo4j.process.traversal.LabelP) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) RDF_SYNONYM_PROP(nl.knaw.huygens.timbuctoo.rdf.Database.RDF_SYNONYM_PROP) Collection(nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection) CustomEntityProperties(nl.knaw.huygens.timbuctoo.database.tinkerpop.CustomEntityProperties) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) Lists(com.google.common.collect.Lists) Map(java.util.Map) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) P(org.apache.tinkerpop.gremlin.process.traversal.P) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Logmarkers.databaseInvariant(nl.knaw.huygens.timbuctoo.logging.Logmarkers.databaseInvariant) Property(org.apache.tinkerpop.gremlin.structure.Property) RelationRef(nl.knaw.huygens.timbuctoo.core.dto.RelationRef) Logger(org.slf4j.Logger) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CustomRelationProperties(nl.knaw.huygens.timbuctoo.database.tinkerpop.CustomRelationProperties) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) IOException(java.io.IOException) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) UUID(java.util.UUID) T(org.apache.tinkerpop.gremlin.structure.T) GraphReadUtils.getEntityTypesOrDefault(nl.knaw.huygens.timbuctoo.model.GraphReadUtils.getEntityTypesOrDefault) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Vres(nl.knaw.huygens.timbuctoo.model.vre.Vres) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) ReadEntity(nl.knaw.huygens.timbuctoo.core.dto.ReadEntity) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) EmptyGraph(org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph) RDF_URI_PROP(nl.knaw.huygens.timbuctoo.rdf.Database.RDF_URI_PROP) DisplayNameHelper(nl.knaw.huygens.timbuctoo.core.dto.DisplayNameHelper) Vre(nl.knaw.huygens.timbuctoo.model.vre.Vre) Optional(java.util.Optional) GraphReadUtils.getProp(nl.knaw.huygens.timbuctoo.model.GraphReadUtils.getProp) Change(nl.knaw.huygens.timbuctoo.model.Change) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Change(nl.knaw.huygens.timbuctoo.model.Change) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnknownPropertyException(nl.knaw.huygens.timbuctoo.core.UnknownPropertyException) ReadEntityImpl(nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) TimProperty(nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

IOException (java.io.IOException)2 UnknownPropertyException (nl.knaw.huygens.timbuctoo.core.UnknownPropertyException)2 TimProperty (nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Lists (com.google.common.collect.Lists)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Collectors.toList (java.util.stream.Collectors.toList)1 DisplayNameHelper (nl.knaw.huygens.timbuctoo.core.dto.DisplayNameHelper)1 ReadEntity (nl.knaw.huygens.timbuctoo.core.dto.ReadEntity)1 ReadEntityImpl (nl.knaw.huygens.timbuctoo.core.dto.ReadEntityImpl)1 RelationRef (nl.knaw.huygens.timbuctoo.core.dto.RelationRef)1 Collection (nl.knaw.huygens.timbuctoo.core.dto.dataset.Collection)1 CustomEntityProperties (nl.knaw.huygens.timbuctoo.database.tinkerpop.CustomEntityProperties)1 CustomRelationProperties (nl.knaw.huygens.timbuctoo.database.tinkerpop.CustomRelationProperties)1