use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.
the class JsonCrudService method createEntity.
private UUID createEntity(Collection collection, ObjectNode input, User user) throws IOException, PermissionFetchingException {
List<TimProperty<?>> timProperties = jsonToEntityMapper.getDataProperties(collection, input);
Optional<Collection> baseCollection = mappings.getCollectionForType(collection.getAbstractType());
UUID id = timDbAccess.createEntity(collection, baseCollection, timProperties, user);
return id;
}
use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty 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;
}
use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty 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;
}
use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method createEntitySetsCreatedAndModified.
@Test
public void createEntitySetsCreatedAndModified() throws Exception {
TinkerPopGraphManager graphManager = newGraph().wrap();
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testthings").get();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
List<TimProperty<?>> properties = Lists.newArrayList();
String userId = "userId";
long timeStamp = Instant.now().toEpochMilli();
CreateEntity createEntity = withProperties(properties, userId, timeStamp);
instance.createEntity(collection, Optional.empty(), createEntity);
assertThat(graphManager.getGraph().traversal().V().has("tim_id", createEntity.getId().toString()).next().value("created"), sameJSONAs(String.format("{\"timeStamp\": %s,\"userId\": \"%s\"}", timeStamp, userId)));
assertThat(graphManager.getGraph().traversal().V().has("tim_id", createEntity.getId().toString()).next().value("modified"), sameJSONAs(String.format("{\"timeStamp\": %s,\"userId\": \"%s\"}", timeStamp, userId)));
}
use of nl.knaw.huygens.timbuctoo.core.dto.property.TimProperty in project timbuctoo by HuygensING.
the class TinkerPopOperationsTest method createEntitySetsTypesWithTheCollectionAndTheBaseCollection.
@Test
public void createEntitySetsTypesWithTheCollectionAndTheBaseCollection() throws Exception {
TinkerPopGraphManager graphManager = newGraph().wrap();
Vres vres = createConfiguration();
Collection collection = vres.getCollection("testthings").get();
TinkerPopOperations instance = forGraphWrapperAndMappings(graphManager, vres);
List<TimProperty<?>> properties = Lists.newArrayList();
CreateEntity createEntity = withProperties(properties);
instance.createEntity(collection, Optional.empty(), createEntity);
assertThat(graphManager.getGraph().traversal().V().has("tim_id", createEntity.getId().toString()).next().value("types"), allOf(containsString("testthing"), containsString("thing")));
}
Aggregations