use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.
the class AbstractGraphConverter method feedId.
private <T> void feedId(Vertex vertex, T entity) {
ClassMapping mapping = getClassMappings().get(entity.getClass());
Optional<FieldMapping> id = mapping.getId();
Object vertexId = vertex.id();
if (Objects.nonNull(vertexId) && id.isPresent()) {
FieldMapping fieldMapping = id.get();
if (fieldMapping.getConverter().isPresent()) {
AttributeConverter attributeConverter = getConverters().get(fieldMapping.getConverter().get());
Object attributeConverted = attributeConverter.convertToEntityAttribute(vertexId);
fieldMapping.write(entity, fieldMapping.getValue(Value.of(attributeConverted)));
} else {
fieldMapping.write(entity, fieldMapping.getValue(Value.of(vertexId)));
}
}
}
use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.
the class AbstractGraphTemplate method find.
@Override
public <T, K> Optional<T> find(Class<T> entityClass, K id) {
requireNonNull(entityClass, "entityClass is required");
requireNonNull(id, "id is required");
ClassMapping classMapping = getClassMappings().get(entityClass);
FieldMapping idField = classMapping.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
Object value = ConverterUtil.getValue(id, classMapping, idField.getFieldName(), getConverters());
final Optional<Vertex> vertex = getTraversal().V(value).hasLabel(classMapping.getName()).tryNext();
return (Optional<T>) vertex.map(getConverter()::toEntity);
}
use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.
the class AbstractGraphTemplate method delete.
@Override
public <T, K> void delete(Class<T> entityClass, K id) {
requireNonNull(entityClass, "entityClass is required");
requireNonNull(id, "id is required");
ClassMapping mapping = getClassMappings().get(entityClass);
getTraversal().V(id).hasLabel(mapping.getName()).toStream().forEach(Vertex::remove);
}
use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.
the class AbstractGraphTemplate method checkId.
private <T> void checkId(T entity) {
ClassMapping classMapping = getClassMappings().get(entity.getClass());
classMapping.getId().orElseThrow(() -> IdNotFoundException.newInstance(entity.getClass()));
}
use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.
the class ParamsBinderTest method shouldConvert2.
@Test
public void shouldConvert2() {
Method method = Stream.of(PersonRepository.class.getMethods()).filter(m -> m.getName().equals("findByAgeAndName")).findFirst().get();
ClassMapping classMapping = mappings.get(Person.class);
RepositoryDocumentObserverParser parser = new RepositoryDocumentObserverParser(classMapping);
paramsBinder = new ParamsBinder(classMapping, converters);
SelectMethodProvider selectMethodFactory = SelectMethodProvider.get();
SelectQuery selectQuery = selectMethodFactory.apply(method, classMapping.getName());
SelectQueryConverter converter = ServiceLoaderProvider.get(SelectQueryConverter.class);
DocumentQueryParams queryParams = converter.apply(selectQuery, parser);
Params params = queryParams.getParams();
paramsBinder.bind(params, new Object[] { 10L, "Ada" }, method);
DocumentQuery query = queryParams.getQuery();
DocumentCondition columnCondition = query.getCondition().get();
List<DocumentCondition> conditions = columnCondition.getDocument().get(new TypeReference<List<DocumentCondition>>() {
});
List<Object> values = conditions.stream().map(DocumentCondition::getDocument).map(Document::getValue).map(Value::get).collect(Collectors.toList());
assertEquals(10, values.get(0));
assertEquals("Ada", values.get(1));
}
Aggregations