/** Move data from alert table into metadata table. */
  private void migrateAlarmsToMetadata() {
    Context context = ContextManager.getContext();

    if (!checkIfDatabaseExists(context, AlarmDatabase.NAME)) return;

    AlarmDatabase alarmsDatabase = new AlarmDatabase();
    DatabaseDao<TransitionalAlarm> dao =
        new DatabaseDao<TransitionalAlarm>(TransitionalAlarm.class, alarmsDatabase);

    TodorooCursor<TransitionalAlarm> cursor = dao.query(Query.select(TransitionalAlarm.PROPERTIES));
    try {
      if (cursor.getCount() == 0) return;

      Metadata metadata = new Metadata();
      metadata.setValue(Metadata.KEY, AlarmFields.METADATA_KEY);
      for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
        long task = cursor.get(TransitionalAlarm.TASK);
        long time = cursor.get(TransitionalAlarm.TIME);

        metadata.setValue(Metadata.TASK, task);
        metadata.setValue(AlarmFields.TIME, time);
        metadata.setValue(AlarmFields.TYPE, AlarmFields.TYPE_SINGLE);
        metadataDao.createNew(metadata);
        metadata.clearValue(Metadata.ID);
      }
    } finally {
      cursor.close();
      alarmsDatabase.close();
    }
  }
Exemple #2
0
    protected HashMap<Long, Long> cursorToMap(
        TodorooCursor<TYPE> cursor,
        DatabaseDao<?> dao,
        LongProperty remoteIdProperty,
        LongProperty localIdProperty) {
      try {
        HashMap<Long, Long> map = new HashMap<Long, Long>(cursor.getCount());
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
          long remoteId = cursor.get(remoteIdProperty);
          long localId = cursor.get(localIdProperty);

          if (map.containsKey(remoteId)) dao.delete(map.get(remoteId));
          map.put(remoteId, localId);
        }
        return map;
      } finally {
        cursor.close();
      }
    }
  /**
   * Helper that reads entries from legacy database and row-by-row creates new models and saves
   * them.
   *
   * @param context
   * @param legacyTable
   * @param propertyMap
   * @param model
   * @param dao
   */
  @SuppressWarnings("nls")
  private static final <TYPE extends AbstractModel> void upgradeTable(
      Context context,
      String legacyTable,
      HashMap<String, Property<?>> propertyMap,
      TYPE model,
      DatabaseDao<TYPE> dao) {

    if (!checkIfDatabaseExists(context, legacyTable)) return;

    SQLiteDatabase upgradeDb =
        new Astrid2UpgradeHelper(context, legacyTable, null, 1).getReadableDatabase();

    Cursor cursor = upgradeDb.rawQuery("SELECT * FROM " + legacyTable, null);
    UpgradeVisitorContainer<TYPE> container = new UpgradeVisitorContainer<TYPE>();
    container.cursor = cursor;
    container.model = model;
    ColumnUpgradeVisitor visitor = new ColumnUpgradeVisitor();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
      model.clear();
      for (Entry<String, Property<?>> entry : propertyMap.entrySet()) {
        container.columnIndex = cursor.getColumnIndex(entry.getKey());
        entry.getValue().accept(visitor, container);
      }

      // special tweak for adding upgrade notes to tasks
      if (container.upgradeNotes != null) {
        if (container.model.getValue(Task.NOTES).length() == 0)
          container.model.setValue(Task.NOTES, container.upgradeNotes.toString());
        else {
          container.model.setValue(
              Task.NOTES, container.model.getValue(Task.NOTES) + "\n\n" + container.upgradeNotes);
        }
        container.upgradeNotes = null;
      }
      dao.createNew(container.model);
    }
    cursor.close();

    upgradeDb.close();
  }