Search in sources :

Example 11 with ClassMapping

use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.

the class AbstractColumnTemplate method delete.

@Override
public <T, K> void delete(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());
    ColumnDeleteQuery query = ColumnDeleteQuery.delete().from(classMapping.getName()).where(idField.getName()).eq(value).build();
    getManager().delete(query);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) ColumnDeleteQuery(jakarta.nosql.column.ColumnDeleteQuery)

Example 12 with ClassMapping

use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.

the class AbstractGraphConverter method getProperties.

@Override
public <T> List<Property<?>> getProperties(T entity) {
    Objects.requireNonNull(entity, "entity is required");
    ClassMapping mapping = getClassMappings().get(entity.getClass());
    List<FieldGraph> fields = mapping.getFields().stream().map(f -> to(f, entity)).filter(FieldGraph::isNotEmpty).collect(toList());
    return fields.stream().filter(FieldGraph::isNotId).flatMap(f -> f.toElements(this, getConverters()).stream()).collect(Collectors.toList());
}
Also used : Property(org.apache.tinkerpop.gremlin.structure.Property) Iterator(java.util.Iterator) AttributeConverter(jakarta.nosql.mapping.AttributeConverter) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Predicate(java.util.function.Predicate) Graph(org.apache.tinkerpop.gremlin.structure.Graph) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException) Converters(jakarta.nosql.mapping.Converters) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Value(jakarta.nosql.Value) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ClassMappings(org.eclipse.jnosql.mapping.reflection.ClassMappings) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) EMBEDDED(org.eclipse.jnosql.mapping.reflection.FieldType.EMBEDDED) Optional(java.util.Optional) Collections(java.util.Collections) Edge(org.apache.tinkerpop.gremlin.structure.Edge) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping)

Example 13 with ClassMapping

use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.

the class AbstractGraphConverter method toEntity.

private <T> T toEntity(Class<T> entityClass, List<Property> properties) {
    ClassMapping mapping = getClassMappings().get(entityClass);
    T instance = mapping.newInstance();
    return convertEntity(properties, mapping, instance);
}
Also used : ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping)

Example 14 with ClassMapping

use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.

the class AbstractGraphConverter method toEntity.

@Override
public <T> T toEntity(Vertex vertex) {
    requireNonNull(vertex, "vertex is required");
    ClassMapping mapping = getClassMappings().findByName(vertex.label());
    List<Property> properties = vertex.keys().stream().map(k -> DefaultProperty.of(k, vertex.value(k))).collect(toList());
    T entity = toEntity((Class<T>) mapping.getClassInstance(), properties);
    feedId(vertex, entity);
    return entity;
}
Also used : Property(org.apache.tinkerpop.gremlin.structure.Property) Iterator(java.util.Iterator) AttributeConverter(jakarta.nosql.mapping.AttributeConverter) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Predicate(java.util.function.Predicate) Graph(org.apache.tinkerpop.gremlin.structure.Graph) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException) Converters(jakarta.nosql.mapping.Converters) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Value(jakarta.nosql.Value) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ClassMappings(org.eclipse.jnosql.mapping.reflection.ClassMappings) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) EMBEDDED(org.eclipse.jnosql.mapping.reflection.FieldType.EMBEDDED) Optional(java.util.Optional) Collections(java.util.Collections) Edge(org.apache.tinkerpop.gremlin.structure.Edge) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Property(org.apache.tinkerpop.gremlin.structure.Property)

Example 15 with ClassMapping

use of org.eclipse.jnosql.mapping.reflection.ClassMapping in project jnosql-diana by eclipse.

the class AbstractGraphConverter method toVertex.

@Override
public <T> Vertex toVertex(T entity) {
    requireNonNull(entity, "entity is required");
    ClassMapping mapping = getClassMappings().get(entity.getClass());
    String label = mapping.getName();
    List<FieldGraph> fields = mapping.getFields().stream().map(f -> to(f, entity)).filter(FieldGraph::isNotEmpty).collect(toList());
    Optional<FieldGraph> id = fields.stream().filter(FieldGraph::isId).findFirst();
    final Function<Property, Vertex> findVertexOrCreateWithId = p -> {
        Iterator<Vertex> vertices = getGraph().vertices(p.value());
        return vertices.hasNext() ? vertices.next() : getGraph().addVertex(org.apache.tinkerpop.gremlin.structure.T.label, label, org.apache.tinkerpop.gremlin.structure.T.id, p.value());
    };
    Vertex vertex = id.map(i -> i.toElement(getConverters())).map(findVertexOrCreateWithId).orElseGet(() -> getGraph().addVertex(label));
    fields.stream().filter(FieldGraph::isNotId).flatMap(f -> f.toElements(this, getConverters()).stream()).forEach(p -> vertex.property(p.key(), p.value()));
    return vertex;
}
Also used : Property(org.apache.tinkerpop.gremlin.structure.Property) Iterator(java.util.Iterator) AttributeConverter(jakarta.nosql.mapping.AttributeConverter) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Predicate(java.util.function.Predicate) Graph(org.apache.tinkerpop.gremlin.structure.Graph) FieldMapping(org.eclipse.jnosql.mapping.reflection.FieldMapping) EntityNotFoundException(jakarta.nosql.mapping.EntityNotFoundException) Converters(jakarta.nosql.mapping.Converters) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Consumer(java.util.function.Consumer) Value(jakarta.nosql.Value) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ClassMappings(org.eclipse.jnosql.mapping.reflection.ClassMappings) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) EMBEDDED(org.eclipse.jnosql.mapping.reflection.FieldType.EMBEDDED) Optional(java.util.Optional) Collections(java.util.Collections) Edge(org.apache.tinkerpop.gremlin.structure.Edge) ClassMapping(org.eclipse.jnosql.mapping.reflection.ClassMapping) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Iterator(java.util.Iterator) Property(org.apache.tinkerpop.gremlin.structure.Property)

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