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;
}
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;
}
}
Aggregations