Subversion Repositories ES

Rev

Rev 12 | Rev 16 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12 Rev 13
1
/**
1
/**
2
 *
2
 *
3
 */
3
 */
4
package de.pointedears.converter.db;
4
package de.pointedears.converter.db;
5
5
6
import java.util.HashMap;
6
import java.util.HashMap;
7
import java.util.Map.Entry;
7
import java.util.Map.Entry;
8
8
9
import android.content.ContentValues;
9
import android.content.ContentValues;
10
import android.database.Cursor;
10
import android.database.Cursor;
11
import android.database.sqlite.SQLiteDatabase;
11
import android.database.sqlite.SQLiteDatabase;
12
import android.database.sqlite.SQLiteException;
12
import android.database.sqlite.SQLiteException;
13
import android.database.sqlite.SQLiteOpenHelper;
13
import android.database.sqlite.SQLiteOpenHelper;
14
import de.pointedears.converter.app.CurrenciesActivity;
14
import de.pointedears.converter.app.CurrenciesActivity;
15
15
16
/**
16
/**
17
 * @author pelinux
17
 * @author pelinux
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 = 2;
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_CURRENCY1 = "currency1"; //$NON-NLS-1$
27
  private static final String COLUMN_CURRENCY2 = "currency2"; //$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$
28
  private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
29
29
30
  private static HashMap<String, HashMap<String, Double>> currencyConversions =
30
  private static HashMap<String, HashMap<String, Double>> conversionRates =
31
    new HashMap<String, HashMap<String, Double>>();
31
    new HashMap<String, HashMap<String, Double>>();
32
  static
32
  static
33
  {
33
  {
34
    HashMap<String, Double> conversionFactors = new HashMap<String, Double>();
34
    HashMap<String, Double> conversionFactors = new HashMap<String, Double>();
35
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293);
35
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293);
36
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413);
36
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413);
37
    CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_CHF,
37
    CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_CHF,
38
      conversionFactors);
38
      conversionFactors);
39
39
40
    conversionFactors = new HashMap<String, Double>();
40
    conversionFactors = new HashMap<String, Double>();
41
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
41
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
42
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
42
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
43
    CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_EUR,
43
    CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_EUR,
44
      conversionFactors);
44
      conversionFactors);
45
45
46
    conversionFactors = new HashMap<String, Double>();
46
    conversionFactors = new HashMap<String, Double>();
47
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412);
47
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412);
48
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743);
48
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743);
49
    CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_USD,
49
    CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_USD,
50
      conversionFactors);
50
      conversionFactors);
51
  }
51
  }
52
  private final CurrenciesActivity context;
52
  private final CurrenciesActivity context;
53
53
54
  /**
54
  /**
55
   * @param context
55
   * @param context
56
   *          The Activity in which this wrapper is used
56
   *          The Activity in which this wrapper is used
57
   */
57
   */
58
  public CurrenciesDatabase(CurrenciesActivity context)
58
  public CurrenciesDatabase(CurrenciesActivity context)
59
  {
59
  {
60
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
60
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
61
          CurrenciesDatabase.DATABASE_VERSION);
61
          CurrenciesDatabase.DATABASE_VERSION);
62
    this.context = context;
62
    this.context = context;
63
    this.readConversionsFromDatabase();
63
    this.readConversionsFromDatabase();
64
  }
64
  }
65
65
66
  /*
66
  /*
67
   * (non-Javadoc)
67
   * (non-Javadoc)
68
   *
68
   *
69
   * @see
69
   * @see
70
   * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
70
   * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
71
   * .SQLiteDatabase)
71
   * .SQLiteDatabase)
72
   */
72
   */
73
  @SuppressWarnings("nls")
73
  @SuppressWarnings("nls")
74
  @Override
74
  @Override
75
  public void onCreate(SQLiteDatabase db)
75
  public void onCreate(SQLiteDatabase db)
76
  {
76
  {
77
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
77
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
78
      + " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
78
      + " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
79
      + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
79
      + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
80
      + CurrenciesDatabase.COLUMN_FACTOR
80
      + CurrenciesDatabase.COLUMN_FACTOR
81
      + " NUMERIC"
81
      + " NUMERIC"
82
      + ", CONSTRAINT unique_currency_pair UNIQUE ("
82
      + ", CONSTRAINT unique_currency_pair UNIQUE ("
83
      + CurrenciesDatabase.COLUMN_CURRENCY1 + ", "
83
      + CurrenciesDatabase.COLUMN_CURRENCY1 + ", "
84
      + CurrenciesDatabase.COLUMN_CURRENCY2 + "))");
84
      + CurrenciesDatabase.COLUMN_CURRENCY2 + "))");
85
85
86
    HashMap<String, HashMap<String, Double>> currencyConversions =
86
    HashMap<String, HashMap<String, Double>> currencyConversions =
87
      this.getConversionRates();
87
      this.getConversionRates();
88
    for (String key : currencyConversions.keySet())
88
    for (String key : currencyConversions.keySet())
89
    {
89
    {
90
      for (Entry<String, Double> factorEntry : currencyConversions.get(key)
90
      for (Entry<String, Double> factorEntry : currencyConversions.get(key)
91
        .entrySet())
91
        .entrySet())
92
      {
92
      {
93
        ContentValues values = new ContentValues();
93
        ContentValues values = new ContentValues();
94
        values.put(CurrenciesDatabase.COLUMN_CURRENCY1, key);
94
        values.put(CurrenciesDatabase.COLUMN_CURRENCY1, key);
95
        values.put(CurrenciesDatabase.COLUMN_CURRENCY2, factorEntry.getKey());
95
        values.put(CurrenciesDatabase.COLUMN_CURRENCY2, factorEntry.getKey());
96
        values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue());
96
        values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue());
97
        db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR,
97
        db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR,
98
          values);
98
          values);
99
      }
99
      }
100
    }
100
    }
101
  }
101
  }
102
102
103
  /*
103
  /*
104
   * (non-Javadoc)
104
   * (non-Javadoc)
105
   *
105
   *
106
   * @see
106
   * @see
107
   * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
107
   * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
108
   * .SQLiteDatabase, int, int)
108
   * .SQLiteDatabase, int, int)
109
   */
109
   */
110
  @SuppressWarnings("nls")
110
  @SuppressWarnings("nls")
111
  @Override
111
  @Override
112
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
112
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
113
  {
113
  {
114
    /* NOTE: Should migrate database instead */
114
    /* NOTE: We should migrate an existing database instead */
115
    db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
115
    db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
116
    this.onCreate(db);
116
    this.onCreate(db);
117
  }
117
  }
118
118
119
  /**
119
  /**
120
   * @return
120
   * @return
121
   */
121
   */
122
  public HashMap<String, HashMap<String, Double>> getConversionRates()
122
  public HashMap<String, HashMap<String, Double>> getConversionRates()
123
  {
123
  {
124
    return CurrenciesDatabase.currencyConversions;
124
    return CurrenciesDatabase.conversionRates;
125
  }
125
  }
126
126
127
  /**
127
  /**
128
   * Reads currency conversions and updates the static currencyConversions field
128
   * Reads currency conversions and updates the static currencyConversions field
129
   * of this class
129
   * of this class
130
   */
130
   */
131
  public void readConversionsFromDatabase()
131
  public void readConversionsFromDatabase()
132
  {
132
  {
133
    try
133
    try
134
    {
134
    {
135
      /* Get database connection, but upgrade database first if necessary! */
135
      /* Get database connection, but upgrade database first if necessary! */
136
      SQLiteDatabase dbConn = this.getWritableDatabase();
136
      SQLiteDatabase dbConn = this.getWritableDatabase();
137
137
138
      @SuppressWarnings("nls")
138
      @SuppressWarnings("nls")
139
      Cursor cursor =
139
      Cursor cursor =
140
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
140
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
141
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
141
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
142
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
142
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
143
143
144
      if (cursor != null)
144
      if (cursor != null)
145
      {
145
      {
146
        try
146
        try
147
        {
147
        {
148
          int currency1Id =
148
          int currency1Id =
149
              cursor
149
              cursor
150
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
150
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
151
          int currency2Id =
151
          int currency2Id =
152
              cursor
152
              cursor
153
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
153
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
154
          int factorId =
154
          int factorId =
155
              cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
155
              cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
156
156
157
          /* NOTE: Don't change the default values if the table is empty */
157
          /* NOTE: Don't change the default values if the table is empty */
158
          if (cursor.moveToFirst())
158
          if (cursor.moveToFirst())
159
          {
159
          {
160
            HashMap<String, HashMap<String, Double>> newCurrencyConversions =
160
            HashMap<String, HashMap<String, Double>> newCurrencyConversions =
161
              new HashMap<String, HashMap<String, Double>>();
161
              new HashMap<String, HashMap<String, Double>>();
162
            HashMap<String, Double> mapForCurrency = null;
162
            HashMap<String, Double> mapForCurrency = null;
163
            String lastCurrency1Str = null;
163
            String lastCurrency1Str = null;
164
            String currency1Str;
164
            String currency1Str;
165
165
166
            do
166
            do
167
            {
167
            {
168
              currency1Str = cursor.getString(currency1Id);
168
              currency1Str = cursor.getString(currency1Id);
169
              String currency2Str = cursor.getString(currency2Id);
169
              String currency2Str = cursor.getString(currency2Id);
170
              Double factor = cursor.getDouble(factorId);
170
              Double factor = cursor.getDouble(factorId);
171
171
172
              if (lastCurrency1Str == null
172
              if (lastCurrency1Str == null
173
                || !lastCurrency1Str.equals(currency1Str))
173
                || !lastCurrency1Str.equals(currency1Str))
174
              {
174
              {
175
                /*
175
                /*
176
                 * NOTE: Update outer map when we see a new currency;
176
                 * NOTE: Update outer map when we see a new currency;
177
                 * ORDER BY ensures we don't see a currency1 twice except
177
                 * ORDER BY ensures we don't see a currency1 twice except
178
                 * consecutively
178
                 * consecutively
179
                 */
179
                 */
180
                if (mapForCurrency != null)
180
                if (mapForCurrency != null)
181
                {
181
                {
182
                  newCurrencyConversions.put(lastCurrency1Str, mapForCurrency);
182
                  newCurrencyConversions.put(lastCurrency1Str, mapForCurrency);
183
                }
183
                }
184
184
185
                lastCurrency1Str = new String(currency1Str);
185
                lastCurrency1Str = new String(currency1Str);
186
186
187
                /* NOTE: New currency1: Reset inner map */
187
                /* NOTE: New currency1: Reset inner map */
188
                mapForCurrency = newCurrencyConversions.get(currency1Str);
188
                mapForCurrency = newCurrencyConversions.get(currency1Str);
189
              }
189
              }
190
190
191
              /* If we did not see this currency1 before */
191
              /* If we did not see this currency1 before */
192
              if (mapForCurrency == null)
192
              if (mapForCurrency == null)
193
              {
193
              {
194
                mapForCurrency = new HashMap<String, Double>();
194
                mapForCurrency = new HashMap<String, Double>();
195
              }
195
              }
196
196
197
              /*
197
              /*
198
               * NOTE: Update inner map after each table row; assignment to
198
               * NOTE: Update inner map after each table row; assignment to
199
               * mapForCurrency above ensures we are putting the factor
199
               * mapForCurrency above ensures we are putting the factor
200
               * into the correct map.
200
               * into the correct map.
201
               */
201
               */
202
              mapForCurrency.put(currency2Str, factor);
202
              mapForCurrency.put(currency2Str, factor);
203
            }
203
            }
204
            while (cursor.moveToNext());
204
            while (cursor.moveToNext());
205
205
206
            /*
206
            /*
207
             * NOTE: Update from last table row; cursor not empty, so we can
207
             * NOTE: Update from last table row; cursor not empty, so we can
208
             * skip the test for null
208
             * skip the test for null
209
             */
209
             */
210
            newCurrencyConversions.put(currency1Str, mapForCurrency);
210
            newCurrencyConversions.put(currency1Str, mapForCurrency);
211
211
212
            CurrenciesDatabase.currencyConversions = newCurrencyConversions;
212
            CurrenciesDatabase.conversionRates = newCurrencyConversions;
213
          }
213
          }
214
        }
214
        }
215
        catch (IllegalArgumentException e)
215
        catch (IllegalArgumentException e)
216
        {
216
        {
217
          /* Could not retrieve column index */
217
          /* Could not retrieve column index */
218
          e.printStackTrace();
218
          e.printStackTrace();
219
        }
219
        }
220
      }
220
      }
221
221
222
      dbConn.close();
222
      dbConn.close();
223
    }
223
    }
224
    catch (SQLiteException e1)
224
    catch (SQLiteException e1)
225
    {
225
    {
226
      /* Could not open database */
226
      /* Could not open database */
227
      e1.printStackTrace();
227
      e1.printStackTrace();
228
    }
228
    }
229
  }
229
  }
230
230
231
  /**
231
  /**
232
   * Tests the database access
232
   * Tests the database access
233
   */
233
   */
234
  public void testAccess()
234
  public void testAccess()
235
  {
235
  {
236
    try
236
    try
237
    {
237
    {
238
      SQLiteDatabase dbConn = this.getReadableDatabase();
238
      SQLiteDatabase dbConn = this.getReadableDatabase();
239
239
240
      @SuppressWarnings("nls")
240
      @SuppressWarnings("nls")
241
      Cursor myCursor =
241
      Cursor myCursor =
242
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
242
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
243
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
243
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
244
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
244
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
245
245
246
      @SuppressWarnings({ "unused", "nls" })
246
      @SuppressWarnings({ "unused", "nls" })
247
      String queryResult = "";
247
      String queryResult = "";
248
      if (myCursor != null)
248
      if (myCursor != null)
249
      {
249
      {
250
        try
250
        try
251
        {
251
        {
252
          int currency1Id =
252
          int currency1Id =
253
              myCursor
253
              myCursor
254
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
254
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
255
          int currency2Id =
255
          int currency2Id =
256
              myCursor
256
              myCursor
257
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
257
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
258
          int factorId =
258
          int factorId =
259
              myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
259
              myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
260
260
261
          if (myCursor.moveToFirst())
261
          if (myCursor.moveToFirst())
262
          {
262
          {
263
            do
263
            do
264
            {
264
            {
265
              String currency1Str = myCursor.getString(currency1Id);
265
              String currency1Str = myCursor.getString(currency1Id);
266
              String currency2Str = myCursor.getString(currency2Id);
266
              String currency2Str = myCursor.getString(currency2Id);
267
              Double factor = myCursor.getDouble(factorId);
267
              Double factor = myCursor.getDouble(factorId);
268
268
269
              /* DEBUG */
269
              /* DEBUG */
270
              queryResult +=
270
              queryResult +=
271
                  currency1Str + " --> " + currency2Str + ": " + factor + "\n";
271
                  currency1Str + " --> " + currency2Str + ": " + factor + "\n";
272
            }
272
            }
273
            while (myCursor.moveToNext());
273
            while (myCursor.moveToNext());
274
          }
274
          }
275
        }
275
        }
276
        catch (IllegalArgumentException e)
276
        catch (IllegalArgumentException e)
277
        {
277
        {
278
          /* Could not retrieve column index */
278
          /* Could not retrieve column index */
279
          e.printStackTrace();
279
          e.printStackTrace();
280
        }
280
        }
281
      }
281
      }
282
282
283
      dbConn.close();
283
      dbConn.close();
284
    }
284
    }
285
    catch (SQLiteException e1)
285
    catch (SQLiteException e1)
286
    {
286
    {
287
      /* Could not open database */
287
      /* Could not open database */
288
      e1.printStackTrace();
288
      e1.printStackTrace();
289
    }
289
    }
290
  }
290
  }
291
}
291
}
292
 
292