Subversion Repositories ES

Rev

Rev 13 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 13 Rev 16
Line 18... Line 18...
18
 *
18
 *
19
 */
19
 */
20
public class CurrenciesDatabase extends SQLiteOpenHelper
20
public class CurrenciesDatabase extends SQLiteOpenHelper
21
{
21
{
22
  private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
22
  private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
23
  private static final int DATABASE_VERSION = 2;
23
  private static final int DATABASE_VERSION = 3;
24
24
25
  private static final String TABLE = "currency"; //$NON-NLS-1$
25
  private static final String TABLE = "currency"; //$NON-NLS-1$
26
  private static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
26
  private static final String COLUMN_CURRENCY = "currency1"; //$NON-NLS-1$
27
  private static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
-
 
28
  private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
27
  private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
29
28
30
  private static HashMap<String, HashMap<String, Double>> conversionRates =
29
  private static HashMap<String, Double> conversionRates =
31
    new HashMap<String, HashMap<String, Double>>();
30
    new HashMap<String, Double>();
32
  static
31
  static
33
  {
32
  {
34
    HashMap<String, Double> conversionFactors = new HashMap<String, Double>();
33
    /* Default conversion rates from Euro (EUR) to other currencies */
35
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293);
-
 
36
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413);
-
 
37
    CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_CHF,
34
    CurrenciesDatabase.conversionRates
38
      conversionFactors);
-
 
39
-
 
40
    conversionFactors = new HashMap<String, Double>();
-
 
41
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
35
      .put(CurrenciesActivity.VALUE_CHF, 1.3013);
42
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
-
 
43
    CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_EUR,
36
    CurrenciesDatabase.conversionRates
44
      conversionFactors);
-
 
45
-
 
46
    conversionFactors = new HashMap<String, Double>();
-
 
47
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412);
-
 
48
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743);
37
      .put(CurrenciesActivity.VALUE_USD, 1.3521);
49
    CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_USD,
-
 
50
      conversionFactors);
-
 
51
  }
38
  }
52
  private final CurrenciesActivity context;
-
 
53
39
54
  /**
40
  /**
55
   * @param context
41
   * @param context
56
   *          The Activity in which this wrapper is used
42
   *          The Activity in which this wrapper is used
57
   */
43
   */
58
  public CurrenciesDatabase(CurrenciesActivity context)
44
  public CurrenciesDatabase(CurrenciesActivity context)
59
  {
45
  {
60
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
46
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
61
          CurrenciesDatabase.DATABASE_VERSION);
47
          CurrenciesDatabase.DATABASE_VERSION);
62
    this.context = context;
-
 
63
    this.readConversionsFromDatabase();
48
    this.readConversionsFromDatabase();
64
  }
49
  }
65
50
66
  /*
51
  /*
67
   * (non-Javadoc)
52
   * (non-Javadoc)
Line 73... Line 58...
73
  @SuppressWarnings("nls")
58
  @SuppressWarnings("nls")
74
  @Override
59
  @Override
75
  public void onCreate(SQLiteDatabase db)
60
  public void onCreate(SQLiteDatabase db)
76
  {
61
  {
77
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
62
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
78
      + " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
63
      + " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT, "
79
      + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
-
 
80
      + CurrenciesDatabase.COLUMN_FACTOR
64
      + CurrenciesDatabase.COLUMN_FACTOR
81
      + " NUMERIC"
65
      + " NUMERIC"
82
      + ", CONSTRAINT unique_currency_pair UNIQUE ("
66
      + ", CONSTRAINT unique_currency_pair UNIQUE ("
83
      + CurrenciesDatabase.COLUMN_CURRENCY1 + ", "
-
 
84
      + CurrenciesDatabase.COLUMN_CURRENCY2 + "))");
67
      + CurrenciesDatabase.COLUMN_CURRENCY + "))");
85
68
86
    HashMap<String, HashMap<String, Double>> currencyConversions =
69
    HashMap<String, Double> currencyConversions =
87
      this.getConversionRates();
70
      this.getConversionRates();
88
    for (String key : currencyConversions.keySet())
71
    for (Entry<String, Double> factorEntry : currencyConversions.entrySet())
89
    {
72
    {
90
      for (Entry<String, Double> factorEntry : currencyConversions.get(key)
-
 
91
        .entrySet())
-
 
92
      {
-
 
93
        ContentValues values = new ContentValues();
73
      ContentValues values = new ContentValues();
94
        values.put(CurrenciesDatabase.COLUMN_CURRENCY1, key);
-
 
95
        values.put(CurrenciesDatabase.COLUMN_CURRENCY2, factorEntry.getKey());
74
      values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey());
96
        values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue());
75
      values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue());
97
        db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR,
76
      db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR,
98
          values);
77
        values);
99
      }
-
 
100
    }
78
    }
101
  }
79
  }
102
80
103
  /*
81
  /*
104
   * (non-Javadoc)
82
   * (non-Javadoc)
Line 117... Line 95...
117
  }
95
  }
118
96
119
  /**
97
  /**
120
   * @return
98
   * @return
121
   */
99
   */
122
  public HashMap<String, HashMap<String, Double>> getConversionRates()
100
  public HashMap<String, Double> getConversionRates()
123
  {
101
  {
124
    return CurrenciesDatabase.conversionRates;
102
    return CurrenciesDatabase.conversionRates;
125
  }
103
  }
126
104
127
  /**
105
  /**
Line 133... Line 111...
133
    try
111
    try
134
    {
112
    {
135
      /* Get database connection, but upgrade database first if necessary! */
113
      /* Get database connection, but upgrade database first if necessary! */
136
      SQLiteDatabase dbConn = this.getWritableDatabase();
114
      SQLiteDatabase dbConn = this.getWritableDatabase();
137
115
138
      @SuppressWarnings("nls")
-
 
139
      Cursor cursor =
116
      Cursor cursor =
140
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
117
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
141
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
-
 
142
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
118
            null, CurrenciesDatabase.COLUMN_CURRENCY, null);
143
119
144
      if (cursor != null)
120
      if (cursor != null)
145
      {
121
      {
146
        try
122
        try
147
        {
123
        {
148
          int currency1Id =
124
          int currency1Id =
149
              cursor
125
              cursor
150
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
126
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY);
151
          int currency2Id =
-
 
152
              cursor
-
 
153
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
-
 
154
          int factorId =
127
          int factorId =
155
              cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
128
              cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
156
129
157
          /* NOTE: Don't change the default values if the table is empty */
130
          /* NOTE: Don't change the default values if the table is empty */
158
          if (cursor.moveToFirst())
131
          if (cursor.moveToFirst())
159
          {
132
          {
160
            HashMap<String, HashMap<String, Double>> newCurrencyConversions =
133
            HashMap<String, Double> newCurrencyConversions =
161
              new HashMap<String, HashMap<String, Double>>();
134
              new HashMap<String, Double>();
162
            HashMap<String, Double> mapForCurrency = null;
-
 
163
            String lastCurrency1Str = null;
-
 
164
            String currency1Str;
-
 
165
135
166
            do
136
            do
167
            {
137
            {
168
              currency1Str = cursor.getString(currency1Id);
-
 
169
              String currency2Str = cursor.getString(currency2Id);
138
              String currencyStr = cursor.getString(currency1Id);
170
              Double factor = cursor.getDouble(factorId);
139
              Double factor = cursor.getDouble(factorId);
171
-
 
172
              if (lastCurrency1Str == null
-
 
173
                || !lastCurrency1Str.equals(currency1Str))
-
 
174
              {
-
 
175
                /*
-
 
176
                 * NOTE: Update outer map when we see a new currency;
-
 
177
                 * ORDER BY ensures we don't see a currency1 twice except
-
 
178
                 * consecutively
-
 
179
                 */
-
 
180
                if (mapForCurrency != null)
-
 
181
                {
-
 
182
                  newCurrencyConversions.put(lastCurrency1Str, mapForCurrency);
140
              newCurrencyConversions.put(currencyStr, factor);
183
                }
-
 
184
-
 
185
                lastCurrency1Str = new String(currency1Str);
-
 
186
-
 
187
                /* NOTE: New currency1: Reset inner map */
-
 
188
                mapForCurrency = newCurrencyConversions.get(currency1Str);
-
 
189
              }
-
 
190
-
 
191
              /* If we did not see this currency1 before */
-
 
192
              if (mapForCurrency == null)
-
 
193
              {
-
 
194
                mapForCurrency = new HashMap<String, Double>();
-
 
195
              }
-
 
196
-
 
197
              /*
-
 
198
               * NOTE: Update inner map after each table row; assignment to
-
 
199
               * mapForCurrency above ensures we are putting the factor
-
 
200
               * into the correct map.
-
 
201
               */
-
 
202
              mapForCurrency.put(currency2Str, factor);
-
 
203
            }
141
            }
204
            while (cursor.moveToNext());
142
            while (cursor.moveToNext());
205
143
206
            /*
-
 
207
             * NOTE: Update from last table row; cursor not empty, so we can
-
 
208
             * skip the test for null
-
 
209
             */
-
 
210
            newCurrencyConversions.put(currency1Str, mapForCurrency);
-
 
211
-
 
212
            CurrenciesDatabase.conversionRates = newCurrencyConversions;
144
            CurrenciesDatabase.conversionRates = newCurrencyConversions;
213
          }
145
          }
214
        }
146
        }
215
        catch (IllegalArgumentException e)
147
        catch (IllegalArgumentException e)
216
        {
148
        {
Line 235... Line 167...
235
  {
167
  {
236
    try
168
    try
237
    {
169
    {
238
      SQLiteDatabase dbConn = this.getReadableDatabase();
170
      SQLiteDatabase dbConn = this.getReadableDatabase();
239
171
240
      @SuppressWarnings("nls")
-
 
241
      Cursor myCursor =
172
      Cursor myCursor =
242
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
173
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
243
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
-
 
244
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
174
            null, CurrenciesDatabase.COLUMN_CURRENCY, null);
245
175
246
      @SuppressWarnings({ "unused", "nls" })
176
      @SuppressWarnings({ "unused", "nls" })
247
      String queryResult = "";
177
      String queryResult = "";
248
      if (myCursor != null)
178
      if (myCursor != null)
249
      {
179
      {
250
        try
180
        try
251
        {
181
        {
252
          int currency1Id =
182
          int currency1Id =
253
              myCursor
183
              myCursor
254
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
184
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY);
255
          int currency2Id =
-
 
256
              myCursor
-
 
257
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
-
 
258
          int factorId =
185
          int factorId =
259
              myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
186
              myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
260
187
261
          if (myCursor.moveToFirst())
188
          if (myCursor.moveToFirst())
262
          {
189
          {
263
            do
190
            do
264
            {
191
            {
265
              String currency1Str = myCursor.getString(currency1Id);
192
              String currencyStr = myCursor.getString(currency1Id);
266
              String currency2Str = myCursor.getString(currency2Id);
-
 
267
              Double factor = myCursor.getDouble(factorId);
193
              Double factor = myCursor.getDouble(factorId);
268
194
269
              /* DEBUG */
195
              /* DEBUG */
270
              queryResult +=
196
              queryResult +=
271
                  currency1Str + " --> " + currency2Str + ": " + factor + "\n";
197
                  "EUR --> " + currencyStr + ": " + factor + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
272
            }
198
            }
273
            while (myCursor.moveToNext());
199
            while (myCursor.moveToNext());
274
          }
200
          }
275
        }
201
        }
276
        catch (IllegalArgumentException e)
202
        catch (IllegalArgumentException e)