Rev 13 | 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$ |
||
| 16 | PointedEar | 23 | private static final int DATABASE_VERSION = 3; |
| 11 | PointedEar | 24 | |
| 12 | PointedEar | 25 | private static final String TABLE = "currency"; //$NON-NLS-1$ |
| 16 | PointedEar | 26 | private static final String COLUMN_CURRENCY = "currency1"; //$NON-NLS-1$ |
| 12 | PointedEar | 27 | private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
| 11 | PointedEar | 28 | |
| 16 | PointedEar | 29 | private static HashMap<String, Double> conversionRates = |
| 30 | new HashMap<String, Double>(); |
||
| 12 | PointedEar | 31 | static |
| 32 | { |
||
| 16 | PointedEar | 33 | /* Default conversion rates from Euro (EUR) to other currencies */ |
| 34 | CurrenciesDatabase.conversionRates |
||
| 35 | .put(CurrenciesActivity.VALUE_CHF, 1.3013); |
||
| 36 | CurrenciesDatabase.conversionRates |
||
| 37 | .put(CurrenciesActivity.VALUE_USD, 1.3521); |
||
| 12 | PointedEar | 38 | } |
| 11 | PointedEar | 39 | |
| 40 | /** |
||
| 41 | * @param context |
||
| 42 | * The Activity in which this wrapper is used |
||
| 43 | */ |
||
| 44 | public CurrenciesDatabase(CurrenciesActivity context) |
||
| 45 | { |
||
| 46 | super(context, CurrenciesDatabase.DATABASE_NAME, null, |
||
| 47 | CurrenciesDatabase.DATABASE_VERSION); |
||
| 12 | PointedEar | 48 | this.readConversionsFromDatabase(); |
| 11 | PointedEar | 49 | } |
| 50 | |||
| 51 | /* |
||
| 52 | * (non-Javadoc) |
||
| 53 | * |
||
| 54 | * @see |
||
| 55 | * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite |
||
| 56 | * .SQLiteDatabase) |
||
| 57 | */ |
||
| 58 | @SuppressWarnings("nls") |
||
| 59 | @Override |
||
| 60 | public void onCreate(SQLiteDatabase db) |
||
| 61 | { |
||
| 62 | db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE |
||
| 16 | PointedEar | 63 | + " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT, " |
| 11 | PointedEar | 64 | + CurrenciesDatabase.COLUMN_FACTOR |
| 12 | PointedEar | 65 | + " NUMERIC" |
| 66 | + ", CONSTRAINT unique_currency_pair UNIQUE (" |
||
| 16 | PointedEar | 67 | + CurrenciesDatabase.COLUMN_CURRENCY + "))"); |
| 11 | PointedEar | 68 | |
| 16 | PointedEar | 69 | HashMap<String, Double> currencyConversions = |
| 12 | PointedEar | 70 | this.getConversionRates(); |
| 16 | PointedEar | 71 | for (Entry<String, Double> factorEntry : currencyConversions.entrySet()) |
| 11 | PointedEar | 72 | { |
| 16 | PointedEar | 73 | ContentValues values = new ContentValues(); |
| 74 | values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey()); |
||
| 75 | values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue()); |
||
| 76 | db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR, |
||
| 77 | values); |
||
| 11 | PointedEar | 78 | } |
| 79 | } |
||
| 80 | |||
| 81 | /* |
||
| 82 | * (non-Javadoc) |
||
| 83 | * |
||
| 84 | * @see |
||
| 85 | * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite |
||
| 86 | * .SQLiteDatabase, int, int) |
||
| 87 | */ |
||
| 88 | @SuppressWarnings("nls") |
||
| 89 | @Override |
||
| 90 | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) |
||
| 91 | { |
||
| 13 | PointedEar | 92 | /* NOTE: We should migrate an existing database instead */ |
| 11 | PointedEar | 93 | db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE); |
| 94 | this.onCreate(db); |
||
| 95 | } |
||
| 12 | PointedEar | 96 | |
| 97 | /** |
||
| 98 | * @return |
||
| 99 | */ |
||
| 16 | PointedEar | 100 | public HashMap<String, Double> getConversionRates() |
| 12 | PointedEar | 101 | { |
| 13 | PointedEar | 102 | return CurrenciesDatabase.conversionRates; |
| 12 | PointedEar | 103 | } |
| 104 | |||
| 105 | /** |
||
| 106 | * Reads currency conversions and updates the static currencyConversions field |
||
| 107 | * of this class |
||
| 108 | */ |
||
| 109 | public void readConversionsFromDatabase() |
||
| 110 | { |
||
| 111 | try |
||
| 112 | { |
||
| 113 | /* Get database connection, but upgrade database first if necessary! */ |
||
| 114 | SQLiteDatabase dbConn = this.getWritableDatabase(); |
||
| 115 | |||
| 116 | Cursor cursor = |
||
| 117 | dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
||
| 16 | PointedEar | 118 | null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
| 12 | PointedEar | 119 | |
| 120 | if (cursor != null) |
||
| 121 | { |
||
| 122 | try |
||
| 123 | { |
||
| 124 | int currency1Id = |
||
| 125 | cursor |
||
| 16 | PointedEar | 126 | .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
| 12 | PointedEar | 127 | int factorId = |
| 128 | cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
||
| 129 | |||
| 130 | /* NOTE: Don't change the default values if the table is empty */ |
||
| 131 | if (cursor.moveToFirst()) |
||
| 132 | { |
||
| 16 | PointedEar | 133 | HashMap<String, Double> newCurrencyConversions = |
| 134 | new HashMap<String, Double>(); |
||
| 12 | PointedEar | 135 | |
| 136 | do |
||
| 137 | { |
||
| 16 | PointedEar | 138 | String currencyStr = cursor.getString(currency1Id); |
| 12 | PointedEar | 139 | Double factor = cursor.getDouble(factorId); |
| 16 | PointedEar | 140 | newCurrencyConversions.put(currencyStr, factor); |
| 12 | PointedEar | 141 | } |
| 142 | while (cursor.moveToNext()); |
||
| 143 | |||
| 13 | PointedEar | 144 | CurrenciesDatabase.conversionRates = newCurrencyConversions; |
| 12 | PointedEar | 145 | } |
| 146 | } |
||
| 147 | catch (IllegalArgumentException e) |
||
| 148 | { |
||
| 149 | /* Could not retrieve column index */ |
||
| 150 | e.printStackTrace(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | dbConn.close(); |
||
| 155 | } |
||
| 156 | catch (SQLiteException e1) |
||
| 157 | { |
||
| 158 | /* Could not open database */ |
||
| 159 | e1.printStackTrace(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Tests the database access |
||
| 165 | */ |
||
| 166 | public void testAccess() |
||
| 167 | { |
||
| 168 | try |
||
| 169 | { |
||
| 170 | SQLiteDatabase dbConn = this.getReadableDatabase(); |
||
| 171 | |||
| 172 | Cursor myCursor = |
||
| 173 | dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
||
| 16 | PointedEar | 174 | null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
| 12 | PointedEar | 175 | |
| 176 | @SuppressWarnings({ "unused", "nls" }) |
||
| 177 | String queryResult = ""; |
||
| 178 | if (myCursor != null) |
||
| 179 | { |
||
| 180 | try |
||
| 181 | { |
||
| 182 | int currency1Id = |
||
| 183 | myCursor |
||
| 16 | PointedEar | 184 | .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
| 12 | PointedEar | 185 | int factorId = |
| 186 | myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
||
| 187 | |||
| 188 | if (myCursor.moveToFirst()) |
||
| 189 | { |
||
| 190 | do |
||
| 191 | { |
||
| 16 | PointedEar | 192 | String currencyStr = myCursor.getString(currency1Id); |
| 12 | PointedEar | 193 | Double factor = myCursor.getDouble(factorId); |
| 194 | |||
| 195 | /* DEBUG */ |
||
| 196 | queryResult += |
||
| 16 | PointedEar | 197 | "EUR --> " + currencyStr + ": " + factor + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
| 12 | PointedEar | 198 | } |
| 199 | while (myCursor.moveToNext()); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | catch (IllegalArgumentException e) |
||
| 203 | { |
||
| 204 | /* Could not retrieve column index */ |
||
| 205 | e.printStackTrace(); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | dbConn.close(); |
||
| 210 | } |
||
| 211 | catch (SQLiteException e1) |
||
| 212 | { |
||
| 213 | /* Could not open database */ |
||
| 214 | e1.printStackTrace(); |
||
| 215 | } |
||
| 216 | } |
||
| 11 | PointedEar | 217 | } |