Search in sources :

Example 1 with JavaFieldType

use of com.amplifyframework.core.model.types.JavaFieldType in project amplify-android by aws-amplify.

the class SQLiteModelFieldTypeConverter method convertValueFromTarget.

@Override
public Object convertValueFromTarget(Model model, ModelField field) throws DataStoreException {
    Object fieldValue;
    if (model.getClass() == SerializedModel.class) {
        fieldValue = ((SerializedModel) model).getValue(field);
    } else {
        fieldValue = ModelHelper.getValue(model, field);
    }
    if (fieldValue == null) {
        return null;
    }
    final JavaFieldType javaFieldType = TypeConverter.getJavaFieldType(field);
    return convertRawValueToTarget(fieldValue, javaFieldType, gson);
}
Also used : JavaFieldType(com.amplifyframework.core.model.types.JavaFieldType)

Example 2 with JavaFieldType

use of com.amplifyframework.core.model.types.JavaFieldType in project amplify-android by aws-amplify.

the class SQLPredicate method addBinding.

private void addBinding(Object value) {
    final JavaFieldType fieldType = TypeConverter.getJavaFieldTypeFromValue(value);
    final Object sqlValue = SQLiteModelFieldTypeConverter.convertRawValueToTarget(value, fieldType, GsonFactory.instance());
    bindings.add(sqlValue);
}
Also used : JavaFieldType(com.amplifyframework.core.model.types.JavaFieldType)

Example 3 with JavaFieldType

use of com.amplifyframework.core.model.types.JavaFieldType in project amplify-android by aws-amplify.

the class SQLiteModelFieldTypeConverter method convertValueFromSource.

@Override
public Object convertValueFromSource(@NonNull Cursor cursor, @NonNull ModelField field) throws DataStoreException {
    final JavaFieldType javaFieldType = TypeConverter.getJavaFieldType(field);
    try {
        // Skip if there is no equivalent column for field in object
        final SQLiteColumn column = columns.get(field.getName());
        if (column == null) {
            LOGGER.verbose(String.format("Column with name %s does not exist", field.getName()));
            return null;
        }
        String columnName = column.getAliasedName();
        if (javaFieldType == JavaFieldType.MODEL) {
            int newInnerModelCount = 1;
            String fieldTargetType = field.getTargetType();
            if (cursorInnerModelCounts.containsKey(fieldTargetType)) {
                Integer currentInnerModelCount = cursorInnerModelCounts.get(fieldTargetType);
                newInnerModelCount += currentInnerModelCount == null ? 0 : currentInnerModelCount;
            }
            cursorInnerModelCounts.put(fieldTargetType, newInnerModelCount);
        }
        if (isInnerModel && cursorInnerModelCounts.containsKey(parentSchema.getName())) {
            Integer modelCount = cursorInnerModelCounts.get(parentSchema.getName());
            if (!Objects.equals(modelCount, 1)) {
                // More than 1 of the model the field belongs to is present in the cursor
                columnName += modelCount;
            }
        }
        final int columnIndex = cursor.getColumnIndexOrThrow(columnName);
        // This check is necessary, because primitive values will return 0 even when null
        if (cursor.isNull(columnIndex)) {
            return null;
        }
        final String valueAsString = cursor.getString(columnIndex);
        LOGGER.verbose(String.format("Attempt to convert value \"%s\" from field %s of type %s in model %s", valueAsString, field.getName(), field.getTargetType(), parentSchema.getName()));
        switch(javaFieldType) {
            case STRING:
                return cursor.getString(columnIndex);
            case MODEL:
                return convertModelAssociationToTarget(cursor, field);
            case ENUM:
                return convertEnumValueToTarget(valueAsString, field);
            case CUSTOM_TYPE:
                return convertCustomTypeToTarget(cursor, field, columnIndex);
            case INTEGER:
                return cursor.getInt(columnIndex);
            case BOOLEAN:
                return cursor.getInt(columnIndex) != 0;
            case FLOAT:
                return cursor.getFloat(columnIndex);
            case DOUBLE:
                return cursor.getDouble(columnIndex);
            case LONG:
                return cursor.getLong(columnIndex);
            case DATE:
                return new Temporal.Date(valueAsString);
            case DATE_TIME:
                return new Temporal.DateTime(valueAsString);
            case TIME:
                return new Temporal.Time(valueAsString);
            case TIMESTAMP:
                return new Temporal.Timestamp(cursor.getLong(columnIndex), TimeUnit.SECONDS);
            default:
                LOGGER.warn(String.format("Field of type %s is not supported. Fallback to null.", javaFieldType));
                return null;
        }
    } catch (Exception exception) {
        throw new DataStoreException(String.format("Error converting field \"%s\" from model \"%s\"", field.getName(), parentSchema.getName()), exception, AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION);
    }
}
Also used : DataStoreException(com.amplifyframework.datastore.DataStoreException) SQLiteColumn(com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteColumn) LocalTime(java.time.LocalTime) OffsetTime(java.time.OffsetTime) OffsetDateTime(java.time.OffsetDateTime) JavaFieldType(com.amplifyframework.core.model.types.JavaFieldType) OffsetDateTime(java.time.OffsetDateTime) AmplifyException(com.amplifyframework.AmplifyException) IOException(java.io.IOException) DataStoreException(com.amplifyframework.datastore.DataStoreException)

Aggregations

JavaFieldType (com.amplifyframework.core.model.types.JavaFieldType)3 AmplifyException (com.amplifyframework.AmplifyException)1 DataStoreException (com.amplifyframework.datastore.DataStoreException)1 SQLiteColumn (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteColumn)1 IOException (java.io.IOException)1 LocalTime (java.time.LocalTime)1 OffsetDateTime (java.time.OffsetDateTime)1 OffsetTime (java.time.OffsetTime)1