Search in sources :

Example 16 with ClassMapping

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)));
        }
    }
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) AttributeConverter(jakarta.nosql.mapping.AttributeConverter) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping)

Example 17 with ClassMapping

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);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Optional(java.util.Optional) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping)

Example 18 with ClassMapping

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);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex)

Example 19 with ClassMapping

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()));
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping)

Example 20 with ClassMapping

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));
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) ParamsBinder(org.eclipse.jnosql.mapping.util.ParamsBinder) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) Params(jakarta.nosql.Params) Method(java.lang.reflect.Method) Document(jakarta.nosql.document.Document) SelectQuery(jakarta.nosql.query.SelectQuery) DocumentQuery(jakarta.nosql.document.DocumentQuery) SelectQueryConverter(jakarta.nosql.document.SelectQueryConverter) SelectMethodProvider(org.eclipse.jnosql.communication.query.method.SelectMethodProvider) DocumentQueryParams(jakarta.nosql.document.DocumentQueryParams) List(java.util.List) DocumentCondition(jakarta.nosql.document.DocumentCondition) Test(org.junit.jupiter.api.Test)

Aggregations

ClassMapping (org.eclipse.jnosql.mapping.reflection.ClassMapping)60 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)31 Method (java.lang.reflect.Method)25 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)19 ValueSource (org.junit.jupiter.params.provider.ValueSource)19 FieldMapping (org.eclipse.jnosql.mapping.reflection.FieldMapping)12 List (java.util.List)10 Converters (jakarta.nosql.mapping.Converters)8 Edge (org.apache.tinkerpop.gremlin.structure.Edge)8 Graph (org.apache.tinkerpop.gremlin.structure.Graph)8 ClassMappings (org.eclipse.jnosql.mapping.reflection.ClassMappings)8 Collectors (java.util.stream.Collectors)7 Test (org.junit.jupiter.api.Test)7 Value (jakarta.nosql.Value)6 Optional (java.util.Optional)6 EntityNotFoundException (jakarta.nosql.mapping.EntityNotFoundException)5 SelectQuery (jakarta.nosql.query.SelectQuery)5 Iterator (java.util.Iterator)5 Objects.requireNonNull (java.util.Objects.requireNonNull)5 Function (java.util.function.Function)5