Search in sources :

Example 1 with ClassRepresentation

use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.

the class AbstractColumnTemplate method delete.

@Override
public <T, ID> void delete(Class<T> entityClass, ID id) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    ClassRepresentation classRepresentation = getClassRepresentations().get(entityClass);
    FieldRepresentation idField = classRepresentation.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classRepresentation, idField.getFieldName(), getConverters());
    ColumnDeleteQuery query = ColumnQueryBuilder.delete().from(classRepresentation.getName()).where(idField.getName()).eq(value).build();
    getManager().delete(query);
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation) ColumnDeleteQuery(org.jnosql.diana.api.column.ColumnDeleteQuery)

Example 2 with ClassRepresentation

use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.

the class AbstractColumnTemplate method find.

@Override
public <T, ID> Optional<T> find(Class<T> entityClass, ID id) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    ClassRepresentation classRepresentation = getClassRepresentations().get(entityClass);
    FieldRepresentation idField = classRepresentation.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classRepresentation, idField.getFieldName(), getConverters());
    ColumnQuery query = ColumnQueryBuilder.select().from(classRepresentation.getName()).where(idField.getName()).eq(value).build();
    return singleResult(query);
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) ColumnQuery(org.jnosql.diana.api.column.ColumnQuery) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation)

Example 3 with ClassRepresentation

use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.

the class AbstractColumnTemplateAsync method getDeleteQuery.

private <T, ID> ColumnDeleteQuery getDeleteQuery(Class<T> entityClass, ID id) {
    ClassRepresentation classRepresentation = getClassRepresentations().get(entityClass);
    FieldRepresentation idField = classRepresentation.getId().orElseThrow(() -> IdNotFoundException.newInstance(entityClass));
    Object value = ConverterUtil.getValue(id, classRepresentation, idField.getFieldName(), getConverters());
    return ColumnQueryBuilder.delete().from(classRepresentation.getName()).where(idField.getName()).eq(value).build();
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation)

Example 4 with ClassRepresentation

use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.

the class AbstractColumnEntityConverter method toColumn.

@Override
public ColumnEntity toColumn(Object entityInstance) {
    requireNonNull(entityInstance, "Object is required");
    ClassRepresentation representation = getClassRepresentations().get(entityInstance.getClass());
    ColumnEntity entity = ColumnEntity.of(representation.getName());
    representation.getFields().stream().map(f -> to(f, entityInstance)).filter(FieldValue::isNotEmpty).map(f -> f.toColumn(this, getConverters())).flatMap(List::stream).forEach(entity::add);
    return entity;
}
Also used : ColumnEntity(org.jnosql.diana.api.column.ColumnEntity) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation)

Example 5 with ClassRepresentation

use of org.jnosql.artemis.reflection.ClassRepresentation in project jnosql-artemis by eclipse.

the class ConverterUtilTest method shouldConvert.

@Test
public void shouldConvert() {
    ClassRepresentation representation = representations.get(Person.class);
    String value = "100";
    Object id = ConverterUtil.getValue(value, representation, "id", converters);
    assertEquals(100L, id);
}
Also used : ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation) Test(org.junit.jupiter.api.Test)

Aggregations

ClassRepresentation (org.jnosql.artemis.reflection.ClassRepresentation)29 FieldRepresentation (org.jnosql.artemis.reflection.FieldRepresentation)11 Test (org.junit.jupiter.api.Test)6 Field (java.lang.reflect.Field)2 Optional (java.util.Optional)2 Converters (org.jnosql.artemis.Converters)2 Money (org.jnosql.artemis.model.Money)2 Value (org.jnosql.diana.api.Value)2 ColumnQuery (org.jnosql.diana.api.column.ColumnQuery)2 DocumentQuery (org.jnosql.diana.api.document.DocumentQuery)2 ColumnDeleteQuery (org.jnosql.diana.api.column.ColumnDeleteQuery)1 ColumnEntity (org.jnosql.diana.api.column.ColumnEntity)1 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)1 DocumentEntity (org.jnosql.diana.api.document.DocumentEntity)1