Search in sources :

Example 26 with Entry

use of com.faltenreich.diaguard.shared.data.database.entity.Entry in project Diaguard by Faltenreich.

the class CsvImport method importFromVersion1_1.

@SuppressWarnings("ParameterCanBeLocal")
private void importFromVersion1_1(CSVReader reader, String[] nextLine) throws Exception {
    Entry entry = null;
    while ((nextLine = reader.readNext()) != null) {
        String key = nextLine[0];
        if (key.equalsIgnoreCase(Entry.BACKUP_KEY)) {
            entry = new Entry();
            entry.setDate(DateTimeFormat.forPattern(Export.BACKUP_DATE_FORMAT).parseDateTime(nextLine[1]));
            String note = nextLine[2];
            entry.setNote(note != null && note.length() > 0 ? note : null);
            entry = EntryDao.getInstance().createOrUpdate(entry);
        } else if (key.equalsIgnoreCase(Measurement.BACKUP_KEY) && entry != null) {
            try {
                CategoryDeprecated categoryDeprecated = Helper.valueOf(CategoryDeprecated.class, nextLine[2]);
                Category category = categoryDeprecated.toUpdate();
                Measurement measurement = category.toClass().newInstance();
                measurement.setValues(FloatUtils.parseNumber(nextLine[1]));
                measurement.setEntry(entry);
                MeasurementDao.getInstance(category.toClass()).createOrUpdate(measurement);
            } catch (InstantiationException exception) {
                Log.e(TAG, exception.toString());
            } catch (IllegalAccessException exception) {
                Log.e(TAG, exception.toString());
            }
        }
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) CategoryDeprecated(com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated)

Example 27 with Entry

use of com.faltenreich.diaguard.shared.data.database.entity.Entry in project Diaguard by Faltenreich.

the class DatabaseHelper method upgradeToVersion19.

private <M extends Measurement> void upgradeToVersion19(SQLiteDatabase sqliteDatabase, ConnectionSource connectionSource) {
    List<Entry> entries = new ArrayList<>();
    String entryQuery = String.format("SELECT * FROM %s", DatabaseHelper.ENTRY);
    Cursor cursor = sqliteDatabase.rawQuery(entryQuery, null);
    if (cursor.moveToFirst()) {
        while (!cursor.isAfterLast()) {
            try {
                Entry entry = new Entry();
                entry.setId(Long.parseLong(cursor.getString(0)));
                entry.setDate(DateTimeFormat.forPattern(DATE_TIME_FORMAT_1_1).parseDateTime(cursor.getString(1)));
                entry.setNote(cursor.getString(2));
                entries.add(entry);
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
            cursor.moveToNext();
        }
    }
    cursor.close();
    HashMap<Entry, List<M>> entities = new HashMap<>();
    for (Entry entry : entries) {
        List<M> measurements = new ArrayList<>();
        String measurementQuery = String.format("SELECT * FROM %s WHERE %s = %d", DatabaseHelper.MEASUREMENT, ENTRY_ID, entry.getId());
        cursor = sqliteDatabase.rawQuery(measurementQuery, null);
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                try {
                    String categoryString = cursor.getString(2);
                    CategoryDeprecated categoryDeprecated = Helper.valueOf(CategoryDeprecated.class, categoryString);
                    Category category = categoryDeprecated.toUpdate();
                    M measurement = (M) category.toClass().newInstance();
                    float value = FloatUtils.parseNumber(cursor.getString(1));
                    float[] values = new float[measurement.getValues().length];
                    values[0] = value;
                    measurement.setValues(values);
                    measurements.add(measurement);
                } catch (Exception e) {
                    Log.e(TAG, e.getMessage());
                }
                cursor.moveToNext();
            }
        }
        entities.put(entry, measurements);
        cursor.close();
    }
    sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + ENTRY);
    sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + MEASUREMENT);
    sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + FOOD);
    sqliteDatabase.execSQL("DROP TABLE IF EXISTS " + FOOD_EATEN);
    onCreate(sqliteDatabase, connectionSource);
    for (Map.Entry<Entry, List<M>> mapEntry : entities.entrySet()) {
        Entry tempEntry = mapEntry.getKey();
        tempEntry.setId(-1);
        Entry entry = EntryDao.getInstance().createOrUpdate(tempEntry);
        for (Measurement measurement : mapEntry.getValue()) {
            measurement.setId(-1);
            measurement.setEntry(entry);
            MeasurementDao.getInstance(measurement.getClass()).createOrUpdate(measurement);
        }
    }
}
Also used : Measurement(com.faltenreich.diaguard.shared.data.database.entity.Measurement) Category(com.faltenreich.diaguard.shared.data.database.entity.Category) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CategoryDeprecated(com.faltenreich.diaguard.shared.data.database.entity.deprecated.CategoryDeprecated) Cursor(android.database.Cursor) SQLException(java.sql.SQLException) Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with Entry

use of com.faltenreich.diaguard.shared.data.database.entity.Entry in project Diaguard by Faltenreich.

the class EntryEditFragment method removeCategory.

private void removeCategory(Category category) {
    int index = indexOf(category);
    if (index != -1) {
        Entry entry = viewModel.getEntry();
        measurementContainer.removeViewAt(index);
        int indexInCache = entry.indexInMeasurementCache(category);
        if (indexInCache != -1) {
            entry.getMeasurementCache().remove(indexInCache);
        }
        fabMenu.removeIgnore(category);
        fabMenu.restock();
    }
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry)

Example 29 with Entry

use of com.faltenreich.diaguard.shared.data.database.entity.Entry in project Diaguard by Faltenreich.

the class DashboardValueTask method getBloodSugarOfToday.

private List<BloodSugar> getBloodSugarOfToday() {
    List<BloodSugar> bloodSugars = new ArrayList<>();
    List<Entry> entriesWithBloodSugar = EntryDao.getInstance().getAllWithMeasurementFromToday(BloodSugar.class);
    if (entriesWithBloodSugar != null) {
        for (Entry entry : entriesWithBloodSugar) {
            BloodSugar bloodSugar = (BloodSugar) MeasurementDao.getInstance(BloodSugar.class).getMeasurement(entry);
            if (bloodSugar != null) {
                bloodSugars.add(bloodSugar);
            }
        }
    }
    return bloodSugars;
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) ArrayList(java.util.ArrayList) BloodSugar(com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)

Example 30 with Entry

use of com.faltenreich.diaguard.shared.data.database.entity.Entry in project Diaguard by Faltenreich.

the class HbA1cDashboardValue method getLatestEntryWithHbA1c.

@Nullable
private Entry getLatestEntryWithHbA1c() {
    Entry entry = EntryDao.getInstance().getLatestWithMeasurement(HbA1c.class);
    // Return entry if younger than one month
    boolean isUpToDate = entry != null && entry.getDate().isAfter(DateTime.now().minusMonths(1));
    return isUpToDate ? entry : null;
}
Also used : Entry(com.faltenreich.diaguard.shared.data.database.entity.Entry) Nullable(androidx.annotation.Nullable)

Aggregations

Entry (com.faltenreich.diaguard.shared.data.database.entity.Entry)32 Measurement (com.faltenreich.diaguard.shared.data.database.entity.Measurement)17 ArrayList (java.util.ArrayList)15 Category (com.faltenreich.diaguard.shared.data.database.entity.Category)13 EntryTag (com.faltenreich.diaguard.shared.data.database.entity.EntryTag)10 FoodEaten (com.faltenreich.diaguard.shared.data.database.entity.FoodEaten)9 Tag (com.faltenreich.diaguard.shared.data.database.entity.Tag)8 DateTime (org.joda.time.DateTime)8 BloodSugar (com.faltenreich.diaguard.shared.data.database.entity.BloodSugar)7 Meal (com.faltenreich.diaguard.shared.data.database.entity.Meal)7 List (java.util.List)7 Nullable (androidx.annotation.Nullable)5 EntryAddedEvent (com.faltenreich.diaguard.shared.event.data.EntryAddedEvent)5 View (android.view.View)4 ImageView (android.widget.ImageView)4 Food (com.faltenreich.diaguard.shared.data.database.entity.Food)4 EntryDeletedEvent (com.faltenreich.diaguard.shared.event.data.EntryDeletedEvent)4 ChipView (com.faltenreich.diaguard.shared.view.chip.ChipView)4 Context (android.content.Context)3 LayoutInflater (android.view.LayoutInflater)3