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