Rev 12 | Go to most recent revision | Details | Compare with Previous | 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; | ||
| 12 | PointedEar | 10 | import android.database.Cursor; | 
| 11 | PointedEar | 11 | import android.database.sqlite.SQLiteDatabase; | 
| 12 | PointedEar | 12 | import android.database.sqlite.SQLiteException; | 
| 11 | PointedEar | 13 | import android.database.sqlite.SQLiteOpenHelper; | 
| 14 | import de.pointedears.converter.app.CurrenciesActivity; | ||
| 15 | |||
| 16 | /** | ||
| 17 |  * @author pelinux | ||
| 18 |  * | ||
| 19 |  */ | ||
| 20 | public class CurrenciesDatabase extends SQLiteOpenHelper | ||
| 21 | { | ||
| 22 | private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$ | ||
| 12 | PointedEar | 23 | private static final int DATABASE_VERSION = 2; | 
| 11 | PointedEar | 24 | |
| 12 | PointedEar | 25 | private static final String TABLE = "currency"; //$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$ | ||
| 28 | private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ | ||
| 11 | PointedEar | 29 | |
| 13 | PointedEar | 30 | private static HashMap<String, HashMap<String, Double>> conversionRates = | 
| 12 | PointedEar | 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); | ||
| 13 | PointedEar | 37 | CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_CHF, | 
| 12 | PointedEar | 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); | ||
| 13 | PointedEar | 43 | CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_EUR, | 
| 12 | PointedEar | 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); | ||
| 13 | PointedEar | 49 | CurrenciesDatabase.conversionRates.put(CurrenciesActivity.VALUE_USD, | 
| 12 | PointedEar | 50 | conversionFactors); | 
| 51 |   } | ||
| 11 | PointedEar | 52 | private final CurrenciesActivity context; | 
| 53 | |||
| 54 |   /** | ||
| 55 |    * @param context | ||
| 56 |    *          The Activity in which this wrapper is used | ||
| 57 |    */ | ||
| 58 | public CurrenciesDatabase(CurrenciesActivity context) | ||
| 59 |   { | ||
| 60 | super(context, CurrenciesDatabase.DATABASE_NAME, null, | ||
| 61 | CurrenciesDatabase.DATABASE_VERSION); | ||
| 62 | this.context = context; | ||
| 12 | PointedEar | 63 | this.readConversionsFromDatabase(); | 
| 11 | PointedEar | 64 |   } | 
| 65 | |||
| 66 |   /* | ||
| 67 |    * (non-Javadoc) | ||
| 68 |    * | ||
| 69 |    * @see | ||
| 70 |    * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite | ||
| 71 |    * .SQLiteDatabase) | ||
| 72 |    */ | ||
| 73 | @SuppressWarnings("nls") | ||
| 74 | @Override | ||
| 75 | public void onCreate(SQLiteDatabase db) | ||
| 76 |   { | ||
| 77 | db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE | ||
| 12 | PointedEar | 78 | + " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, " | 
| 11 | PointedEar | 79 | + CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, " | 
| 80 |       + CurrenciesDatabase.COLUMN_FACTOR | ||
| 12 | PointedEar | 81 |       + " NUMERIC" | 
| 82 |       + ", CONSTRAINT unique_currency_pair UNIQUE (" | ||
| 83 | + CurrenciesDatabase.COLUMN_CURRENCY1 + ", " | ||
| 84 | + CurrenciesDatabase.COLUMN_CURRENCY2 + "))"); | ||
| 11 | PointedEar | 85 | |
| 86 | HashMap<String, HashMap<String, Double>> currencyConversions = | ||
| 12 | PointedEar | 87 | this.getConversionRates(); | 
| 11 | PointedEar | 88 | for (String key : currencyConversions.keySet()) | 
| 89 |     { | ||
| 90 | for (Entry<String, Double> factorEntry : currencyConversions.get(key) | ||
| 91 | .entrySet()) | ||
| 92 |       { | ||
| 93 | ContentValues values = new ContentValues(); | ||
| 94 | values.put(CurrenciesDatabase.COLUMN_CURRENCY1, key); | ||
| 95 | values.put(CurrenciesDatabase.COLUMN_CURRENCY2, factorEntry.getKey()); | ||
| 96 | values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue()); | ||
| 97 | db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR, | ||
| 98 | values); | ||
| 99 |       } | ||
| 100 |     } | ||
| 101 |   } | ||
| 102 | |||
| 103 |   /* | ||
| 104 |    * (non-Javadoc) | ||
| 105 |    * | ||
| 106 |    * @see | ||
| 107 |    * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite | ||
| 108 |    * .SQLiteDatabase, int, int) | ||
| 109 |    */ | ||
| 110 | @SuppressWarnings("nls") | ||
| 111 | @Override | ||
| 112 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) | ||
| 113 |   { | ||
| 13 | PointedEar | 114 |     /* NOTE: We should migrate an existing database instead */ | 
| 11 | PointedEar | 115 | db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE); | 
| 116 | this.onCreate(db); | ||
| 117 |   } | ||
| 12 | PointedEar | 118 | |
| 119 |   /** | ||
| 120 |    * @return | ||
| 121 |    */ | ||
| 122 | public HashMap<String, HashMap<String, Double>> getConversionRates() | ||
| 123 |   { | ||
| 13 | PointedEar | 124 | return CurrenciesDatabase.conversionRates; | 
| 12 | PointedEar | 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 | |||
| 13 | PointedEar | 212 | CurrenciesDatabase.conversionRates = newCurrencyConversions; | 
| 12 | PointedEar | 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 |   } | ||
| 11 | PointedEar | 291 | } |