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]);
}
}
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;
}
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.");
}
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();
}
}
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;
}
Aggregations