Search in sources :

Example 11 with ClassRepresentation

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

the class ConverterUtilTest method shouldNotConvert.

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

Example 12 with ClassRepresentation

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

the class AbstractKeyValueEntityConverter method toKeyValue.

@Override
public KeyValueEntity<?> toKeyValue(Object entityInstance) {
    requireNonNull(entityInstance, "Object is required");
    Class<?> clazz = entityInstance.getClass();
    ClassRepresentation representation = getClassRepresentations().get(clazz);
    FieldRepresentation key = getId(clazz, representation);
    Object value = getReflections().getValue(entityInstance, key.getNativeField());
    requireNonNull(value, String.format("The key field %s is required", key.getName()));
    return KeyValueEntity.of(value, entityInstance);
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation)

Example 13 with ClassRepresentation

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

the class ConverterUtilTest method shouldUseAttributeConvert.

@Test
public void shouldUseAttributeConvert() {
    ClassRepresentation representation = representations.get(Worker.class);
    Object value = new Money("BRL", BigDecimal.TEN);
    Object converted = ConverterUtil.getValue(value, representation, "salary", converters);
    assertEquals("BRL 10", converted);
}
Also used : Money(org.jnosql.artemis.model.Money) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation) Test(org.junit.jupiter.api.Test)

Example 14 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)

Example 15 with ClassRepresentation

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

the class ConverterUtil method getValue.

public static Object getValue(Object value, ClassRepresentation representation, String name, Converters converters) {
    Optional<FieldRepresentation> fieldOptional = representation.getFieldRepresentation(name);
    if (fieldOptional.isPresent()) {
        FieldRepresentation field = fieldOptional.get();
        Field nativeField = field.getNativeField();
        if (!nativeField.getType().equals(value.getClass())) {
            return field.getConverter().map(converters::get).map(a -> a.convertToDatabaseColumn(value)).orElseGet(() -> Value.of(value).get(nativeField.getType()));
        }
        return field.getConverter().map(converters::get).map(a -> a.convertToDatabaseColumn(value)).orElse(value);
    }
    return value;
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) Converters(org.jnosql.artemis.Converters) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation) FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) Value(org.jnosql.diana.api.Value) Optional(java.util.Optional) Field(java.lang.reflect.Field) Field(java.lang.reflect.Field)

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