io.realm.DynamicRealmObject Java Examples

The following examples show how to use io.realm.DynamicRealmObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RealmUtils.java    From quill with MIT License 6 votes vote down vote up
private static void changeFieldType(RealmObjectSchema objectSchema, String fieldName,
                             Class newType, @Nullable FieldAttribute attribute,
                             Action3<DynamicRealmObject, String, String> transformation) {
    String tempFieldName = fieldName + "_temp";
    if (attribute != null) {
        if (attribute == FieldAttribute.PRIMARY_KEY && objectSchema.hasPrimaryKey()) {
            // remove existing primary key
            objectSchema.removePrimaryKey();
        }
        objectSchema.addField(tempFieldName, newType, attribute);
    } else {
        objectSchema.addField(tempFieldName, newType);
    }
    objectSchema
            .transform(obj -> {
                transformation.call(obj, fieldName, tempFieldName);
            })
            .removeField(fieldName)
            .renameField(tempFieldName, fieldName);
}
 
Example #2
Source File: RealmBarDataSet.java    From MPAndroidChart-Realm with Apache License 2.0 6 votes vote down vote up
@Override
public BarEntry buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    if (dynamicObject.getFieldType(mYValuesField) == RealmFieldType.LIST) {

        RealmList<DynamicRealmObject> list = dynamicObject.getList(mYValuesField);
        float[] values = new float[list.size()];

        int i = 0;
        for (DynamicRealmObject o : list) {
            values[i] = o.getFloat(mStackValueFieldName);
            i++;
        }

        return new BarEntry(
                mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField), values);
    } else {
        float value = dynamicObject.getFloat(mYValuesField);
        return new BarEntry(mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField), value);
    }
}
 
Example #3
Source File: RealmBarDataSet.java    From NetKnight with Apache License 2.0 6 votes vote down vote up
@Override
public BarEntry buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    if (dynamicObject.getFieldType(mValuesField) == RealmFieldType.LIST) {

        RealmList<DynamicRealmObject> list = dynamicObject.getList(mValuesField);
        float[] values = new float[list.size()];

        int i = 0;
        for (DynamicRealmObject o : list) {
            values[i] = o.getFloat(mStackValueFieldName);
            i++;
        }

        return new BarEntry(values,
                mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
    } else {
        float value = dynamicObject.getFloat(mValuesField);
        return new BarEntry(value,
                mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
    }
}
 
Example #4
Source File: RealmBarDataSet.java    From JNChartDemo with Apache License 2.0 6 votes vote down vote up
@Override
public BarEntry buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    if (dynamicObject.getFieldType(mValuesField) == RealmFieldType.LIST) {

        RealmList<DynamicRealmObject> list = dynamicObject.getList(mValuesField);
        float[] values = new float[list.size()];

        int i = 0;
        for (DynamicRealmObject o : list) {
            values[i] = o.getFloat(mStackValueFieldName);
            i++;
        }

        return new BarEntry(values,
                mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
    } else {
        float value = dynamicObject.getFloat(mValuesField);
        return new BarEntry(value,
                mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
    }
}
 
Example #5
Source File: RawDataRealmMigration.java    From OpenLibre with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void apply(DynamicRealmObject obj) {
    // set timezone offset value
    long date = obj.getLong("date");
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
    cal.set(2017, Calendar.JANUARY, 30, 0, 0, 0);
    TimeZone timeZone; // the timezone during data aquisition was not saved before schema version 2, so we have to guess the correct value for old data
    if (date < cal.getTime().getTime()) { // before 30.01.2017 the only existing app installation was exclusively used in germany...
        timeZone = TimeZone.getTimeZone("Europe/Berlin");
    } else {
        timeZone = TimeZone.getDefault(); // after that, use the device's current timezone
    }
    int timezoneOffsetInMinutes = timeZone.getOffset(date) / 1000 / 60;
    obj.set("timezoneOffsetInMinutes", timezoneOffsetInMinutes);
}
 
Example #6
Source File: RealmBarDataSet.java    From Stayfit with Apache License 2.0 5 votes vote down vote up
@Override
public void build(RealmResults<T> results) {

    for (T realmObject : results) {

        DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

        try { // normal entry

            float value = dynamicObject.getFloat(mValuesField);
            mValues.add(new BarEntry(value, dynamicObject.getInt(mIndexField)));

        } catch (IllegalArgumentException e) { // stacked entry

            RealmList<DynamicRealmObject> list = dynamicObject.getList(mValuesField);
            float[] values = new float[list.size()];

            int i = 0;
            for (DynamicRealmObject o : list) {
                values[i] = o.getFloat(mStackValueFieldName);
                i++;
            }

            mValues.add(new BarEntry(values, dynamicObject.getInt(mIndexField)));
        }
    }

    calcStackSize();
}
 
Example #7
Source File: RealmBubbleDataSet.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
@Override
public BubbleEntry buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return new BubbleEntry(
            mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField),
            dynamicObject.getFloat(mValuesField),
            dynamicObject.getFloat(mSizeField));
}
 
Example #8
Source File: RealmCandleDataSet.java    From NetKnight with Apache License 2.0 5 votes vote down vote up
public CandleEntry buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return new CandleEntry(
            mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField),
            dynamicObject.getFloat(mHighField),
            dynamicObject.getFloat(mLowField),
            dynamicObject.getFloat(mOpenField),
            dynamicObject.getFloat(mCloseField));
}
 
Example #9
Source File: RealmPieDataSet.java    From MPAndroidChart-Realm with Apache License 2.0 5 votes vote down vote up
@Override
public PieEntry buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    if (mLabelField == null) {
        return new PieEntry(dynamicObject.getFloat(mYValuesField));
    } else {
        return new PieEntry(dynamicObject.getFloat(mYValuesField), dynamicObject.getString(mLabelField));
    }
}
 
Example #10
Source File: RealmBubbleDataSet.java    From JNChartDemo with Apache License 2.0 5 votes vote down vote up
@Override
public BubbleEntry buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return new BubbleEntry(
            mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField),
            dynamicObject.getFloat(mValuesField),
            dynamicObject.getFloat(mSizeField));
}
 
Example #11
Source File: RealmCandleDataSet.java    From JNChartDemo with Apache License 2.0 5 votes vote down vote up
public CandleEntry buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return new CandleEntry(
            mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField),
            dynamicObject.getFloat(mHighField),
            dynamicObject.getFloat(mLowField),
            dynamicObject.getFloat(mOpenField),
            dynamicObject.getFloat(mCloseField));
}
 
Example #12
Source File: RealmCandleDataSet.java    From MPAndroidChart-Realm with Apache License 2.0 5 votes vote down vote up
@Override
public CandleEntry buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return new CandleEntry(
            mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField),
            dynamicObject.getFloat(mHighField),
            dynamicObject.getFloat(mLowField),
            dynamicObject.getFloat(mOpenField),
            dynamicObject.getFloat(mCloseField));
}
 
Example #13
Source File: RealmBubbleDataSet.java    From MPAndroidChart-Realm with Apache License 2.0 5 votes vote down vote up
@Override
public BubbleEntry buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return new BubbleEntry(
            mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField),
            dynamicObject.getFloat(mYValuesField),
            dynamicObject.getFloat(mSizeField));
}
 
Example #14
Source File: BlogMetadataDBMigration.java    From quill with MIT License 4 votes vote down vote up
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    RealmSchema schema = realm.getSchema();
    Crashlytics.log(Log.INFO, TAG, "MIGRATING DATABASE from v" + oldVersion + " to v" + newVersion);

    if (oldVersion == 0) {
        if (schema.get("Post").isNullable("slug")) {
            // get rid of null-valued slugs, if any exist
            RealmResults<DynamicRealmObject> postsWithNullSlug = realm
                    .where(Post.class.getSimpleName())
                    .isNull("slug")
                    .findAll();
            Crashlytics.log(Log.DEBUG, TAG, "CONVERTING " + postsWithNullSlug.size() + " SLUGS FROM NULL TO \"\"");
            for (DynamicRealmObject obj : postsWithNullSlug) {
                obj.setString("slug", "");
            }
            // finally, make the field required
            schema.get("Post").setNullable("slug", false);
        }

        schema.get("Post")
                .setNullable("html", true)
                .setNullable("image", true)
                .setNullable("createdAt", true)
                .setNullable("publishedAt", true)
                .setNullable("metaTitle", true)
                .setNullable("metaDescription", true);
        schema.get("User")
                .addIndex("id")
                .setNullable("image", true)
                .setNullable("bio", true);
        schema.get("Tag")
                .setNullable("slug", true)
                .setNullable("description", true)
                .setNullable("image", true)
                .setNullable("metaTitle", true)
                .setNullable("metaDescription", true)
                .setNullable("createdAt", true)
                .setNullable("updatedAt", true);
        schema.get("Setting")
                .addIndex("id");
        ++oldVersion;
    }

    if (oldVersion == 1) {
        // delete all etags, so the info can be fetched and stored
        // again, with role-based permissions enforced
        RealmResults<DynamicRealmObject> allEtags = realm
                .where(ETag.class.getSimpleName())
                .equalTo("type", ETag.TYPE_CURRENT_USER)
                .or()
                .equalTo("type", ETag.TYPE_ALL_POSTS)
                .findAll();
        Crashlytics.log(Log.DEBUG, TAG, "DELETING ALL ETAGS TO REFRESH DATA COMPLETELY");
        allEtags.deleteAllFromRealm();

        if (!schema.contains("Role")) {
            // create the Role table
            Crashlytics.log(Log.DEBUG, TAG, "CREATING ROLE TABLE");
            schema.create("Role")
                    .addField("id", Integer.class, FieldAttribute.PRIMARY_KEY)
                    .addField("uuid", String.class, FieldAttribute.REQUIRED)
                    .addField("name", String.class, FieldAttribute.REQUIRED)
                    .addField("description", String.class, FieldAttribute.REQUIRED);
        }

        if (!schema.get("User").hasField("roles")) {
            Crashlytics.log(Log.DEBUG, TAG, "ADDING ROLES FIELD TO USER TABLE");
            schema.get("User").addRealmListField("roles", schema.get("Role"));
        }
        ++oldVersion;
    }

    if (oldVersion == 2) {
        if (!schema.get("Post").hasField("conflictState")) {
            Crashlytics.log(Log.DEBUG, TAG, "ADDING CONFLICT STATE FIELD TO POST TABLE");
            schema.get("Post").addField("conflictState", String.class, FieldAttribute.REQUIRED);
        }
        ++oldVersion;
    }

    if (oldVersion == 3) {
        // Ghost 1.0 upgrade, drop all data
        Crashlytics.log(Log.WARN, TAG, "DROPPING ALL DATA");
        final SpectreApplication app = SpectreApplication.getInstance();
        app.setOldRealmSchemaVersion(3);

        // clear logged in state
        AppState.getInstance(app).setBoolean(AppState.Key.LOGGED_IN, false);
        UserPrefs.getInstance(app).clear(UserPrefs.Key.EMAIL);
        UserPrefs.getInstance(app).clear(UserPrefs.Key.PASSWORD);
        UserPrefs.getInstance(app).clear(UserPrefs.Key.PERMALINK_FORMAT);

        ++oldVersion;
    }

    // STARTING FROM V4, THE REALM THAT USED TO STORE THE BLOG DATA NOW
    // ONLY STORES THE *METADATA* FOR ALL CONNECTED BLOGS
}
 
Example #15
Source File: RealmRadarDataSet.java    From MPAndroidChart-Realm with Apache License 2.0 4 votes vote down vote up
@Override
public RadarEntry buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);
    return new RadarEntry(dynamicObject.getFloat(mYValuesField));
}
 
Example #16
Source File: RealmBaseDataSet.java    From MPAndroidChart-Realm with Apache License 2.0 4 votes vote down vote up
public S buildEntryFromResultObject(T realmObject, float x) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return (S) new Entry(mXValuesField == null ? x : dynamicObject.getFloat(mXValuesField), dynamicObject.getFloat(mYValuesField));
}
 
Example #17
Source File: RealmBaseDataSet.java    From JNChartDemo with Apache License 2.0 4 votes vote down vote up
public S buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return (S)new Entry(dynamicObject.getFloat(mValuesField),
            mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
}
 
Example #18
Source File: RealmBaseDataSet.java    From NetKnight with Apache License 2.0 4 votes vote down vote up
public S buildEntryFromResultObject(T realmObject, int xIndex) {
    DynamicRealmObject dynamicObject = new DynamicRealmObject(realmObject);

    return (S)new Entry(dynamicObject.getFloat(mValuesField),
            mIndexField == null ? xIndex : dynamicObject.getInt(mIndexField));
}
 
Example #19
Source File: RealmUtils.java    From JNChartDemo with Apache License 2.0 3 votes vote down vote up
/**
 * Transforms the given Realm-ResultSet into a String array by using the provided xValuesField.
 *
 * @param result
 * @param xValuesField
 * @return
 */
public static List<String> toXVals(RealmResults<? extends RealmObject> result, String xValuesField) {

    List<String> xVals = new ArrayList<String>();

    for (RealmObject object : result) {

        DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
        xVals.add(dynamicObject.getString(xValuesField));
    }

    return xVals;
}
 
Example #20
Source File: RealmUtils.java    From NetKnight with Apache License 2.0 3 votes vote down vote up
/**
 * Transforms the given Realm-ResultSet into a String array by using the provided xValuesField.
 *
 * @param result
 * @param xValuesField
 * @return
 */
public static List<String> toXVals(RealmResults<? extends RealmObject> result, String xValuesField) {

    List<String> xVals = new ArrayList<String>();

    for (RealmObject object : result) {

        DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
        xVals.add(dynamicObject.getString(xValuesField));
    }

    return xVals;
}
 
Example #21
Source File: RealmUtils.java    From Stayfit with Apache License 2.0 3 votes vote down vote up
/**
 * Transforms the given Realm-ResultSet into a String array by using the provided xValuesField.
 *
 * @param result
 * @param xValuesField
 * @return
 */
public static List<String> toXVals(RealmResults<? extends RealmObject> result, String xValuesField) {

    List<String> xVals = new ArrayList<>();

    for (RealmObject object : result) {

        DynamicRealmObject dynamicObject = new DynamicRealmObject(object);
        xVals.add(dynamicObject.getString(xValuesField));
    }

    return xVals;
}