Search in sources :

Example 21 with Type

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.

the class BdbSchemaStore method onChangedSubject.

@Override
public void onChangedSubject(String subject, ChangeFetcher changeFetcher) {
    // Step 1: Get the types that where added, unchanged, removed
    List<Type> addedTypes = new ArrayList<>();
    List<Type> removedTypes = new ArrayList<>();
    List<Type> unchangedTypes = new ArrayList<>();
    try (Stream<CursorQuad> subjTypes = changeFetcher.getPredicates(subject, RDF_TYPE, OUT, true, true, true)) {
        subjTypes.forEach(type -> {
            boolean hadTypesBefore = false;
            final Type typeOfSubject = types.computeIfAbsent(type.getObject(), TYPE_MAKER);
            if (type.getChangeType() == ChangeType.ASSERTED) {
                typeOfSubject.registerSubject(1);
                addedTypes.add(typeOfSubject);
            } else if (type.getChangeType() == ChangeType.RETRACTED) {
                hadTypesBefore = true;
                typeOfSubject.registerSubject(-1);
                removedTypes.add(typeOfSubject);
            } else if (type.getChangeType() == ChangeType.UNCHANGED) {
                hadTypesBefore = true;
                unchangedTypes.add(typeOfSubject);
            }
            if (!hadTypesBefore) {
                try (Stream<CursorQuad> predicates = changeFetcher.getPredicates(subject, true, true, false)) {
                    boolean subjectIsNew = !predicates.findAny().isPresent();
                    if (!subjectIsNew) {
                        final Type unknown = types.computeIfAbsent(UNKNOWN, TYPE_MAKER);
                        removedTypes.add(unknown);
                        unknown.registerSubject(-1);
                    }
                }
            }
        });
    }
    if (addedTypes.isEmpty() && unchangedTypes.isEmpty()) {
        // subject currently has no types
        if (removedTypes.isEmpty()) {
            // subject had no types either
            try (Stream<CursorQuad> predicates = changeFetcher.getPredicates(subject, true, true, false)) {
                boolean subjectIsNew = !predicates.findAny().isPresent();
                if (subjectIsNew) {
                    final Type unknown = types.computeIfAbsent(UNKNOWN, TYPE_MAKER);
                    addedTypes.add(unknown);
                    unknown.registerSubject(1);
                } else {
                    unchangedTypes.add(types.computeIfAbsent(UNKNOWN, TYPE_MAKER));
                }
            }
        } else {
            // subject has become unknown
            final Type unknown = types.computeIfAbsent(UNKNOWN, TYPE_MAKER);
            addedTypes.add(unknown);
            unknown.registerSubject(1);
        }
    }
    // it it was asserted -> add it to the unchanged types and to the added types
    try (Stream<CursorQuad> predicates = changeFetcher.getPredicates(subject, true, true, true)) {
        String prevPred = "";
        Direction[] prevDir = new Direction[] { null };
        int retractedCount = 0;
        int assertedCount = 0;
        int unchangedCount = 0;
        // updatePredicateOccurrence)
        for (CursorQuad quad : (Iterable<CursorQuad>) predicates::iterator) {
            boolean predicateSameAsPrev = prevPred.equals(quad.getPredicate()) && prevDir[0] == quad.getDirection();
            if (!predicateSameAsPrev) {
                updatePredicateOccurrence(addedTypes, removedTypes, unchangedTypes, retractedCount, unchangedCount, assertedCount, prevPred, prevDir[0]);
                prevPred = quad.getPredicate();
                prevDir[0] = quad.getDirection();
                retractedCount = 0;
                assertedCount = 0;
                unchangedCount = 0;
            }
            if (quad.getChangeType() == ChangeType.RETRACTED) {
                retractedCount++;
            } else if (quad.getChangeType() == ChangeType.UNCHANGED) {
                unchangedCount++;
            } else if (quad.getChangeType() == ChangeType.ASSERTED) {
                assertedCount++;
            }
            if (quad.getDirection() != Direction.IN) {
                if (quad.getChangeType() == ChangeType.RETRACTED) {
                    for (Type type : unchangedTypes) {
                        updatePredicateType(type, quad, false, changeFetcher);
                    }
                    for (Type type : removedTypes) {
                        updatePredicateType(type, quad, false, changeFetcher);
                    }
                } else if (quad.getChangeType() == ChangeType.UNCHANGED) {
                    for (Type type : removedTypes) {
                        updatePredicateType(type, quad, false, changeFetcher);
                    }
                    for (Type type : addedTypes) {
                        updatePredicateType(type, quad, true, changeFetcher);
                    }
                } else if (quad.getChangeType() == ChangeType.ASSERTED) {
                    for (Type type : unchangedTypes) {
                        updatePredicateType(type, quad, true, changeFetcher);
                    }
                    for (Type type : addedTypes) {
                        updatePredicateType(type, quad, true, changeFetcher);
                    }
                }
            }
        }
        updatePredicateOccurrence(addedTypes, removedTypes, unchangedTypes, retractedCount, unchangedCount, assertedCount, prevPred, prevDir[0]);
    }
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) ChangeType(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.ChangeType) CursorQuad(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad) ArrayList(java.util.ArrayList) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)

Example 22 with Type

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type 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 23 with Type

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.

the class ExtendSchemaMutation method get.

@Override
public Object get(DataFetchingEnvironment env) {
    DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
    MutationHelpers.checkAdminPermissions(env, dataSet.getMetadata());
    final SchemaStore generatedSchema = dataSet.getSchemaStore();
    Map<String, Type> customTypes = new HashMap<>();
    List<ExplicitType> customSchema;
    Map<String, List<ExplicitField>> newCustomSchema = new HashMap<>();
    try {
        String customSchemaString = OBJECT_MAPPER.writeValueAsString(env.getArgument("customSchema"));
        customSchema = OBJECT_MAPPER.readValue(customSchemaString, new TypeReference<List<ExplicitType>>() {
        });
    } catch (IOException e) {
        throw new RuntimeException("Could not parse the schema input");
    }
    for (ExplicitType explicitType : customSchema) {
        customTypes.put(explicitType.getCollectionId(), explicitType.convertToType());
        newCustomSchema.put(explicitType.getCollectionId(), explicitType.getFields());
    }
    Map<String, List<ExplicitField>> existingCustomSchema = dataSet.getCustomSchema();
    Map<String, Type> existingCustomSchemaTypes = new HashMap<>();
    for (Map.Entry<String, List<ExplicitField>> entry : existingCustomSchema.entrySet()) {
        ExplicitType tempExplicitType = new ExplicitType(entry.getKey(), entry.getValue());
        existingCustomSchemaTypes.put(entry.getKey(), tempExplicitType.convertToType());
    }
    MergeSchemas mergeSchemas = new MergeSchemas();
    customTypes = mergeSchemas.mergeSchema(existingCustomSchemaTypes, customTypes);
    MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
    Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(existingCustomSchema, newCustomSchema);
    try {
        dataSet.saveCustomSchema(mergedExplicitSchema);
    } catch (IOException e) {
        LOG.error("Saving the custom schema failed", e);
        throw new RuntimeException(e);
    }
    Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema.getStableTypes(), customTypes);
    for (Map.Entry<String, Type> customType : customTypes.entrySet()) {
        if (!mergedSchema.containsKey(customType.getKey())) {
            return ImmutableMap.of("message", "Schema extension was unsuccessful.");
        }
    }
    return ImmutableMap.of("message", "Schema extended successfully.");
}
Also used : MergeSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) SchemaStore(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaStore) HashMap(java.util.HashMap) MergeExplicitSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas) IOException(java.io.IOException) ExplicitType(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType) Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ImmutableMap(jersey.repackaged.com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ExplicitType(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType)

Example 24 with Type

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.

the class RdfUpload method upload.

@Consumes(MediaType.MULTIPART_FORM_DATA)
@POST
public Response upload(@FormDataParam("file") final InputStream rdfInputStream, @FormDataParam("file") final FormDataBodyPart body, @FormDataParam("fileMimeTypeOverride") final MediaType mimeTypeOverride, @FormDataParam("encoding") final String encoding, @FormDataParam("baseUri") final URI baseUri, @FormDataParam("defaultGraph") final URI defaultGraph, @HeaderParam("authorization") final String authHeader, @PathParam("userId") final String userId, @PathParam("dataSet") final String dataSetId, @QueryParam("forceCreation") boolean forceCreation, @QueryParam("async") final boolean async) throws ExecutionException, InterruptedException, LogStorageFailedException, DataStoreCreationException {
    final Either<Response, Response> result = authCheck.getOrCreate(authHeader, userId, dataSetId, forceCreation).flatMap(userAndDs -> authCheck.hasAdminAccess(userAndDs.getLeft(), userAndDs.getRight())).map((Tuple<User, DataSet> userDataSetTuple) -> {
        final MediaType mediaType = mimeTypeOverride == null ? body.getMediaType() : mimeTypeOverride;
        final DataSet dataSet = userDataSetTuple.getRight();
        ImportManager importManager = dataSet.getImportManager();
        if (mediaType == null || !importManager.isRdfTypeSupported(mediaType)) {
            return Response.status(Response.Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON_TYPE).entity("{\"error\": \"We do not support the mediatype '" + mediaType + "'. Make sure to add the correct " + "mediatype to the file parameter. In curl you'd use `-F \"file=@<filename>;type=<mediatype>\"`. In a " + "webbrowser you probably have no way of setting the correct mimetype. So you can use a special " + "parameter " + "to override it: `formData.append(\"fileMimeTypeOverride\", \"<mimetype>\");`\"}").build();
        }
        if (StringUtils.isBlank(encoding)) {
            return Response.status(Response.Status.BAD_REQUEST).entity("Please provide an 'encoding' parameter").build();
        }
        if (StringUtils.isBlank(body.getContentDisposition().getFileName())) {
            return Response.status(400).entity("filename cannot be empty.").build();
        }
        Future<ImportStatus> promise = null;
        try {
            promise = importManager.addLog(baseUri == null ? dataSet.getMetadata().getBaseUri() : baseUri.toString(), defaultGraph == null ? dataSet.getMetadata().getBaseUri() : defaultGraph.toString(), body.getContentDisposition().getFileName(), rdfInputStream, Optional.of(Charset.forName(encoding)), mediaType);
        } catch (LogStorageFailedException e) {
            return Response.serverError().build();
        }
        if (!async) {
            return handleImportManagerResult(promise);
        }
        return Response.accepted().build();
    });
    if (result.isLeft()) {
        return result.getLeft();
    } else {
        return result.get();
    }
}
Also used : Response(javax.ws.rs.core.Response) PathParam(javax.ws.rs.PathParam) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) Path(javax.ws.rs.Path) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) StringUtils(org.apache.commons.lang3.StringUtils) User(nl.knaw.huygens.timbuctoo.v5.security.dto.User) MediaType(javax.ws.rs.core.MediaType) Future(java.util.concurrent.Future) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Charset(java.nio.charset.Charset) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) HeaderParam(javax.ws.rs.HeaderParam) URI(java.net.URI) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) AuthCheck(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.auth.AuthCheck) Either(javaslang.control.Either) POST(javax.ws.rs.POST) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataStoreCreationException(nl.knaw.huygens.timbuctoo.v5.dataset.exceptions.DataStoreCreationException) ErrorResponseHelper.handleImportManagerResult(nl.knaw.huygens.timbuctoo.v5.dropwizard.endpoints.ErrorResponseHelper.handleImportManagerResult) ExecutionException(java.util.concurrent.ExecutionException) FormDataParam(org.glassfish.jersey.media.multipart.FormDataParam) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) InputStream(java.io.InputStream) ImportManager(nl.knaw.huygens.timbuctoo.v5.dataset.ImportManager) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) LogStorageFailedException(nl.knaw.huygens.timbuctoo.v5.filestorage.exceptions.LogStorageFailedException) ImportStatus(nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus) MediaType(javax.ws.rs.core.MediaType) Tuple(nl.knaw.huygens.timbuctoo.util.Tuple) Consumes(javax.ws.rs.Consumes) POST(javax.ws.rs.POST)

Example 25 with Type

use of nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type in project timbuctoo by HuygensING.

the class MergeSchemas method mergeSchema.

public Map<String, Type> mergeSchema(Map<String, Type> generatedSchema, Map<String, Type> customSchema) {
    Map<String, Type> mergedSchema = generatedSchema;
    Collection<Predicate> mergedPredicates;
    for (Map.Entry<String, Type> entry : customSchema.entrySet()) {
        mergedPredicates = new HashSet<>();
        if (mergedSchema.get(entry.getKey()) != null) {
            for (Predicate customPredicate : entry.getValue().getPredicates()) {
                if (mergedSchema.get(entry.getKey()).getPredicate(customPredicate.getName(), customPredicate.getDirection()) != null) {
                    Predicate generatedPredicate = mergedSchema.get(entry.getKey()).getPredicate(customPredicate.getName(), customPredicate.getDirection());
                    Predicate mergedPredicate = generatedPredicate.merge(customPredicate);
                    mergedPredicate.setIsExplicit(true);
                    mergedPredicates.add(mergedPredicate);
                } else {
                    mergedPredicates.add(customPredicate);
                }
            }
            mergedSchema.get(entry.getKey()).setPredicates(mergedPredicates);
        } else {
            mergedSchema.put(entry.getKey(), entry.getValue());
        }
    }
    return mergedSchema;
}
Also used : Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) Map(java.util.Map) Predicate(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)

Aggregations

Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)17 Test (org.junit.Test)13 HashMap (java.util.HashMap)9 Map (java.util.Map)8 Predicate (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Predicate)8 MergeSchemas (nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas)8 IOException (java.io.IOException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 List (java.util.List)5 Optional (java.util.Optional)5 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)5 QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)5 Logger (org.slf4j.Logger)5 ArrayList (java.util.ArrayList)4 ImportStatus (nl.knaw.huygens.timbuctoo.v5.dataset.ImportStatus)4 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)4 Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)4 DocumentLoader (com.github.jsonldjava.core.DocumentLoader)3 Collectors (java.util.stream.Collectors)3 LoggerFactory (org.slf4j.LoggerFactory)3