Search in sources :

Example 6 with FieldRepresentation

use of org.jnosql.artemis.reflection.FieldRepresentation 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 7 with FieldRepresentation

use of org.jnosql.artemis.reflection.FieldRepresentation 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)

Example 8 with FieldRepresentation

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

the class AbstractKeyValueEntityConverter method toEntity.

@Override
public <T> T toEntity(Class<T> entityClass, KeyValueEntity<?> entity) {
    Value value = entity.getValue();
    T t = value.get(entityClass);
    if (Objects.isNull(t)) {
        return null;
    }
    FieldRepresentation key = getId(entityClass, getClassRepresentations().get(entityClass));
    Object keyValue = getReflections().getValue(t, key.getNativeField());
    if (Objects.isNull(keyValue) || !keyValue.equals(entity.getKey())) {
        getReflections().setValue(t, key.getNativeField(), entity.getKey());
    }
    return t;
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) Value(org.jnosql.diana.api.Value)

Example 9 with FieldRepresentation

use of org.jnosql.artemis.reflection.FieldRepresentation 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)

Example 10 with FieldRepresentation

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

the class AbstractColumnTemplateAsync method find.

@Override
public <T, ID> void find(Class<T> entityClass, ID id, Consumer<Optional<T>> callBack) {
    requireNonNull(entityClass, "entityClass is required");
    requireNonNull(id, "id is required");
    requireNonNull(callBack, "callBack 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();
    singleResult(query, callBack);
}
Also used : FieldRepresentation(org.jnosql.artemis.reflection.FieldRepresentation) ColumnQuery(org.jnosql.diana.api.column.ColumnQuery) ClassRepresentation(org.jnosql.artemis.reflection.ClassRepresentation)

Aggregations

FieldRepresentation (org.jnosql.artemis.reflection.FieldRepresentation)12 ClassRepresentation (org.jnosql.artemis.reflection.ClassRepresentation)11 Value (org.jnosql.diana.api.Value)3 Field (java.lang.reflect.Field)2 Optional (java.util.Optional)2 Converters (org.jnosql.artemis.Converters)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 DocumentDeleteQuery (org.jnosql.diana.api.document.DocumentDeleteQuery)1