Subversion Repositories ES

Rev

Rev 12 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 PointedEar 1
/**
2
 *
3
 */
4
package de.pointedears.converter.db;
5
 
6
import java.util.HashMap;
7
import java.util.Map.Entry;
8
 
9
import android.content.ContentValues;
10
import android.database.sqlite.SQLiteDatabase;
11
import android.database.sqlite.SQLiteOpenHelper;
12
import de.pointedears.converter.app.CurrenciesActivity;
13
 
14
/**
15
 * @author pelinux
16
 *
17
 */
18
public class CurrenciesDatabase extends SQLiteOpenHelper
19
{
20
  private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
21
  private static final int DATABASE_VERSION = 1;
22
 
23
  public static final String TABLE = "currency"; //$NON-NLS-1$
24
  public static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
25
  public static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
26
  public static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
27
 
28
  private final CurrenciesActivity context;
29
 
30
  /**
31
   * @param context
32
   *          The Activity in which this wrapper is used
33
   */
34
  public CurrenciesDatabase(CurrenciesActivity context)
35
  {
36
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
37
          CurrenciesDatabase.DATABASE_VERSION);
38
    this.context = context;
39
  }
40
 
41
  /*
42
   * (non-Javadoc)
43
   *
44
   * @see
45
   * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
46
   * .SQLiteDatabase)
47
   */
48
  @SuppressWarnings("nls")
49
  @Override
50
  public void onCreate(SQLiteDatabase db)
51
  {
52
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
53
      + "(" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
54
      + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
55
      + CurrenciesDatabase.COLUMN_FACTOR
56
      + " NUMERIC)");
57
 
58
    HashMap<String, HashMap<String, Double>> currencyConversions =
59
      this.context.getCurrencyConversions();
60
    for (String key : currencyConversions.keySet())
61
    {
62
      for (Entry<String, Double> factorEntry : currencyConversions.get(key)
63
        .entrySet())
64
      {
65
        ContentValues values = new ContentValues();
66
        values.put(CurrenciesDatabase.COLUMN_CURRENCY1, key);
67
        values.put(CurrenciesDatabase.COLUMN_CURRENCY2, factorEntry.getKey());
68
        values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue());
69
        db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR,
70
          values);
71
      }
72
    }
73
  }
74
 
75
  /*
76
   * (non-Javadoc)
77
   *
78
   * @see
79
   * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
80
   * .SQLiteDatabase, int, int)
81
   */
82
  @SuppressWarnings("nls")
83
  @Override
84
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
85
  {
86
    /* NOTE: Should migrate database instead */
87
    db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
88
    this.onCreate(db);
89
  }
90
}