Subversion Repositories ES

Rev

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

Rev 11 Rev 12
Line 5... Line 5...
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.sqlite.SQLiteDatabase;
11
import android.database.sqlite.SQLiteDatabase;
-
 
12
import android.database.sqlite.SQLiteException;
11
import android.database.sqlite.SQLiteOpenHelper;
13
import android.database.sqlite.SQLiteOpenHelper;
12
import de.pointedears.converter.app.CurrenciesActivity;
14
import de.pointedears.converter.app.CurrenciesActivity;
13
15
14
/**
16
/**
15
 * @author pelinux
17
 * @author pelinux
16
 *
18
 *
17
 */
19
 */
18
public class CurrenciesDatabase extends SQLiteOpenHelper
20
public class CurrenciesDatabase extends SQLiteOpenHelper
19
{
21
{
20
  private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
22
  private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$
21
  private static final int DATABASE_VERSION = 1;
23
  private static final int DATABASE_VERSION = 2;
22
24
23
  public static final String TABLE = "currency"; //$NON-NLS-1$
25
  private static final String TABLE = "currency"; //$NON-NLS-1$
24
  public static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
26
  private static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$
25
  public static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
27
  private static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$
26
  public static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
28
  private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$
27
29
-
 
30
  private static HashMap<String, HashMap<String, Double>> currencyConversions =
-
 
31
    new HashMap<String, HashMap<String, Double>>();
-
 
32
  static
-
 
33
  {
-
 
34
    HashMap<String, Double> conversionFactors = new HashMap<String, Double>();
-
 
35
    conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293);
-
 
36
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413);
-
 
37
    CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_CHF,
-
 
38
      conversionFactors);
-
 
39
-
 
40
    conversionFactors = new HashMap<String, Double>();
-
 
41
    conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077);
-
 
42
    conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468);
-
 
43
    CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_EUR,
-
 
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);
-
 
49
    CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_USD,
-
 
50
      conversionFactors);
-
 
51
  }
28
  private final CurrenciesActivity context;
52
  private final CurrenciesActivity context;
29
53
30
  /**
54
  /**
31
   * @param context
55
   * @param context
32
   *          The Activity in which this wrapper is used
56
   *          The Activity in which this wrapper is used
Line 34... Line 58...
34
  public CurrenciesDatabase(CurrenciesActivity context)
58
  public CurrenciesDatabase(CurrenciesActivity context)
35
  {
59
  {
36
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
60
    super(context, CurrenciesDatabase.DATABASE_NAME, null,
37
          CurrenciesDatabase.DATABASE_VERSION);
61
          CurrenciesDatabase.DATABASE_VERSION);
38
    this.context = context;
62
    this.context = context;
-
 
63
    this.readConversionsFromDatabase();
39
  }
64
  }
40
65
41
  /*
66
  /*
42
   * (non-Javadoc)
67
   * (non-Javadoc)
43
   *
68
   *
Line 48... Line 73...
48
  @SuppressWarnings("nls")
73
  @SuppressWarnings("nls")
49
  @Override
74
  @Override
50
  public void onCreate(SQLiteDatabase db)
75
  public void onCreate(SQLiteDatabase db)
51
  {
76
  {
52
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
77
    db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE
53
      + "(" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
78
      + " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, "
54
      + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
79
      + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, "
55
      + CurrenciesDatabase.COLUMN_FACTOR
80
      + CurrenciesDatabase.COLUMN_FACTOR
56
      + " NUMERIC)");
81
      + " NUMERIC"
-
 
82
      + ", CONSTRAINT unique_currency_pair UNIQUE ("
-
 
83
      + CurrenciesDatabase.COLUMN_CURRENCY1 + ", "
-
 
84
      + CurrenciesDatabase.COLUMN_CURRENCY2 + "))");
57
85
58
    HashMap<String, HashMap<String, Double>> currencyConversions =
86
    HashMap<String, HashMap<String, Double>> currencyConversions =
59
      this.context.getCurrencyConversions();
87
      this.getConversionRates();
60
    for (String key : currencyConversions.keySet())
88
    for (String key : currencyConversions.keySet())
61
    {
89
    {
62
      for (Entry<String, Double> factorEntry : currencyConversions.get(key)
90
      for (Entry<String, Double> factorEntry : currencyConversions.get(key)
63
        .entrySet())
91
        .entrySet())
64
      {
92
      {
Line 85... Line 113...
85
  {
113
  {
86
    /* NOTE: Should migrate database instead */
114
    /* NOTE: Should migrate database instead */
87
    db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
115
    db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE);
88
    this.onCreate(db);
116
    this.onCreate(db);
89
  }
117
  }
-
 
118
-
 
119
  /**
-
 
120
   * @return
-
 
121
   */
-
 
122
  public HashMap<String, HashMap<String, Double>> getConversionRates()
-
 
123
  {
-
 
124
    return CurrenciesDatabase.currencyConversions;
-
 
125
  }
-
 
126
-
 
127
  /**
-
 
128
   * Reads currency conversions and updates the static currencyConversions field
-
 
129
   * of this class
-
 
130
   */
-
 
131
  public void readConversionsFromDatabase()
-
 
132
  {
-
 
133
    try
-
 
134
    {
-
 
135
      /* Get database connection, but upgrade database first if necessary! */
-
 
136
      SQLiteDatabase dbConn = this.getWritableDatabase();
-
 
137
-
 
138
      @SuppressWarnings("nls")
-
 
139
      Cursor cursor =
-
 
140
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
-
 
141
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
-
 
142
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
-
 
143
-
 
144
      if (cursor != null)
-
 
145
      {
-
 
146
        try
-
 
147
        {
-
 
148
          int currency1Id =
-
 
149
              cursor
-
 
150
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
-
 
151
          int currency2Id =
-
 
152
              cursor
-
 
153
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
-
 
154
          int factorId =
-
 
155
              cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
-
 
156
-
 
157
          /* NOTE: Don't change the default values if the table is empty */
-
 
158
          if (cursor.moveToFirst())
-
 
159
          {
-
 
160
            HashMap<String, HashMap<String, Double>> newCurrencyConversions =
-
 
161
              new HashMap<String, HashMap<String, Double>>();
-
 
162
            HashMap<String, Double> mapForCurrency = null;
-
 
163
            String lastCurrency1Str = null;
-
 
164
            String currency1Str;
-
 
165
-
 
166
            do
-
 
167
            {
-
 
168
              currency1Str = cursor.getString(currency1Id);
-
 
169
              String currency2Str = cursor.getString(currency2Id);
-
 
170
              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);
-
 
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
            }
-
 
204
            while (cursor.moveToNext());
-
 
205
-
 
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.currencyConversions = newCurrencyConversions;
-
 
213
          }
-
 
214
        }
-
 
215
        catch (IllegalArgumentException e)
-
 
216
        {
-
 
217
          /* Could not retrieve column index */
-
 
218
          e.printStackTrace();
-
 
219
        }
-
 
220
      }
-
 
221
-
 
222
      dbConn.close();
-
 
223
    }
-
 
224
    catch (SQLiteException e1)
-
 
225
    {
-
 
226
      /* Could not open database */
-
 
227
      e1.printStackTrace();
-
 
228
    }
-
 
229
  }
-
 
230
-
 
231
  /**
-
 
232
   * Tests the database access
-
 
233
   */
-
 
234
  public void testAccess()
-
 
235
  {
-
 
236
    try
-
 
237
    {
-
 
238
      SQLiteDatabase dbConn = this.getReadableDatabase();
-
 
239
-
 
240
      @SuppressWarnings("nls")
-
 
241
      Cursor myCursor =
-
 
242
          dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null,
-
 
243
            null, CurrenciesDatabase.COLUMN_CURRENCY1 + ","
-
 
244
              + CurrenciesDatabase.COLUMN_CURRENCY2, null);
-
 
245
-
 
246
      @SuppressWarnings({ "unused", "nls" })
-
 
247
      String queryResult = "";
-
 
248
      if (myCursor != null)
-
 
249
      {
-
 
250
        try
-
 
251
        {
-
 
252
          int currency1Id =
-
 
253
              myCursor
-
 
254
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1);
-
 
255
          int currency2Id =
-
 
256
              myCursor
-
 
257
                .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2);
-
 
258
          int factorId =
-
 
259
              myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR);
-
 
260
-
 
261
          if (myCursor.moveToFirst())
-
 
262
          {
-
 
263
            do
-
 
264
            {
-
 
265
              String currency1Str = myCursor.getString(currency1Id);
-
 
266
              String currency2Str = myCursor.getString(currency2Id);
-
 
267
              Double factor = myCursor.getDouble(factorId);
-
 
268
-
 
269
              /* DEBUG */
-
 
270
              queryResult +=
-
 
271
                  currency1Str + " --> " + currency2Str + ": " + factor + "\n";
-
 
272
            }
-
 
273
            while (myCursor.moveToNext());
-
 
274
          }
-
 
275
        }
-
 
276
        catch (IllegalArgumentException e)
-
 
277
        {
-
 
278
          /* Could not retrieve column index */
-
 
279
          e.printStackTrace();
-
 
280
        }
-
 
281
      }
-
 
282
-
 
283
      dbConn.close();
-
 
284
    }
-
 
285
    catch (SQLiteException e1)
-
 
286
    {
-
 
287
      /* Could not open database */
-
 
288
      e1.printStackTrace();
-
 
289
    }
-
 
290
  }
90
}
291
}