Rev 16 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 16 | Rev 17 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | /**
|
1 | /**
|
2 | *
|
2 | *
|
3 | */
|
3 | */
|
4 | package de.pointedears.converter.db; |
4 | package de.pointedears.converter.db; |
5 | 5 | ||
- | 6 | import java.text.DateFormat; |
|
- | 7 | import java.text.ParseException; |
|
- | 8 | import java.text.SimpleDateFormat; |
|
- | 9 | import java.util.Date; |
|
6 | import java.util.HashMap; |
10 | import java.util.HashMap; |
7 | import java.util.Map.Entry; |
11 | import java.util.Map.Entry; |
8 | 12 | ||
9 | import android.content.ContentValues; |
13 | import android.content.ContentValues; |
- | 14 | import android.content.Context; |
|
10 | import android.database.Cursor; |
15 | import android.database.Cursor; |
11 | import android.database.sqlite.SQLiteDatabase; |
16 | import android.database.sqlite.SQLiteDatabase; |
12 | import android.database.sqlite.SQLiteException; |
17 | import android.database.sqlite.SQLiteException; |
13 | import android.database.sqlite.SQLiteOpenHelper; |
18 | import android.database.sqlite.SQLiteOpenHelper; |
- | 19 | import android.util.Log; |
|
14 | import de.pointedears.converter.app.CurrenciesActivity; |
20 | import de.pointedears.converter.app.CurrenciesActivity; |
15 | 21 | ||
16 | /**
|
22 | /**
|
17 | * @author pelinux
|
23 | * @author pelinux
|
18 | *
|
24 | *
|
19 | */
|
25 | */
|
20 | public class CurrenciesDatabase extends SQLiteOpenHelper |
26 | public class CurrenciesDatabase extends SQLiteOpenHelper |
21 | {
|
27 | {
|
- | 28 | ||
22 | private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$ |
29 | private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$ |
23 | private static final int DATABASE_VERSION = 3; |
30 | private static final int DATABASE_VERSION = 8; |
24 | 31 | ||
25 | private static final String TABLE = "currency"; //$NON-NLS-1$ |
32 | private static final String TABLE = "currency"; //$NON-NLS-1$ |
26 | private static final String COLUMN_CURRENCY = "currency1"; //$NON-NLS-1$ |
33 | private static final String COLUMN_CURRENCY = "currency1"; //$NON-NLS-1$ |
27 | private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
34 | private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
- | 35 | private static final String COLUMN_UPDATED = "updated"; //$NON-NLS-1$ |
|
28 | 36 | ||
29 | private static HashMap<String, Double> conversionRates = |
37 | private static HashMap<String, ConversionData> conversionRates = |
30 | new HashMap<String, Double>(); |
38 | new HashMap<String, ConversionData>(); |
31 | static
|
39 | static
|
32 | {
|
40 | {
|
33 | /* Default conversion rates from Euro (EUR) to other currencies */
|
41 | /* Default conversion rates from Euro (EUR) to other currencies */
|
- | 42 | Date epoch = new Date(0); |
|
34 | CurrenciesDatabase.conversionRates
|
43 | CurrenciesDatabase.conversionRates
|
35 | .put(CurrenciesActivity.VALUE_CHF, 1.3013); |
44 | .put(CurrenciesActivity.VALUE_CHF, |
- | 45 | new ConversionData(1.3013, epoch)); |
|
36 | CurrenciesDatabase.conversionRates
|
46 | CurrenciesDatabase.conversionRates
|
37 | .put(CurrenciesActivity.VALUE_USD, 1.3521); |
47 | .put(CurrenciesActivity.VALUE_USD, |
- | 48 | new ConversionData(1.3521, epoch)); |
|
38 | }
|
49 | }
|
39 | 50 | ||
- | 51 | private SQLiteDatabase database; |
|
- | 52 | private final DateFormat iso8601format = new SimpleDateFormat( |
|
- | 53 | "yyyy-MM-dd HH:mm:ss"); |
|
- | 54 | ||
40 | /**
|
55 | /**
|
41 | * @param context
|
56 | * @param context
|
42 | * The Activity in which this wrapper is used
|
57 | * The Activity in which this wrapper is used
|
43 | */
|
58 | */
|
44 | public CurrenciesDatabase(CurrenciesActivity context) |
59 | public CurrenciesDatabase(Context context) |
45 | {
|
60 | {
|
46 | super(context, CurrenciesDatabase.DATABASE_NAME, null, |
61 | super(context, CurrenciesDatabase.DATABASE_NAME, null, |
47 | CurrenciesDatabase.DATABASE_VERSION); |
62 | CurrenciesDatabase.DATABASE_VERSION); |
48 | this.readConversionsFromDatabase(); |
63 | this.readConversionsFromDatabase(); |
49 | }
|
64 | }
|
Line 58... | Line 73... | ||
58 | @SuppressWarnings("nls") |
73 | @SuppressWarnings("nls") |
59 | @Override |
74 | @Override |
60 | public void onCreate(SQLiteDatabase db) |
75 | public void onCreate(SQLiteDatabase db) |
61 | {
|
76 | {
|
62 | db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE |
77 | db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE |
63 | + " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT, " |
78 | + " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT" |
- | 79 | + ", " + CurrenciesDatabase.COLUMN_FACTOR + " NUMERIC" |
|
64 | + CurrenciesDatabase.COLUMN_FACTOR
|
80 | + ", " + CurrenciesDatabase.COLUMN_UPDATED |
65 | + " NUMERIC"
|
81 | + " TEXT"
|
66 | + ", CONSTRAINT unique_currency_pair UNIQUE ("
|
82 | + ", CONSTRAINT unique_currency_pair UNIQUE ("
|
67 | + CurrenciesDatabase.COLUMN_CURRENCY + "))"); |
83 | + CurrenciesDatabase.COLUMN_CURRENCY + ") ON CONFLICT REPLACE)"); |
- | 84 | ||
- | 85 | this.writeConversionsToDatabase(db); |
|
- | 86 | }
|
|
68 | 87 | ||
- | 88 | /**
|
|
- | 89 | * @param db
|
|
- | 90 | * The database; <code>null</code> uses the default database
|
|
- | 91 | */
|
|
- | 92 | public void writeConversionsToDatabase(SQLiteDatabase db) |
|
- | 93 | {
|
|
69 | HashMap<String, Double> currencyConversions = |
94 | HashMap<String, ConversionData> currencyConversions = |
70 | this.getConversionRates(); |
95 | this.getConversionRates(); |
- | 96 | ||
71 | for (Entry<String, Double> factorEntry : currencyConversions.entrySet()) |
97 | if (db == null) |
72 | {
|
98 | {
|
- | 99 | db = this.database; |
|
- | 100 | }
|
|
- | 101 | ||
- | 102 | if (!db.isOpen()) |
|
- | 103 | {
|
|
- | 104 | try
|
|
- | 105 | {
|
|
- | 106 | db = this.getWritableDatabase(); |
|
- | 107 | }
|
|
- | 108 | catch (SQLiteException e) |
|
- | 109 | {
|
|
- | 110 | Log.e(this.getClass().toString(), "Could not open database", e); |
|
- | 111 | throw e; |
|
- | 112 | }
|
|
- | 113 | }
|
|
- | 114 | ||
- | 115 | if (db.isOpen()) |
|
- | 116 | {
|
|
- | 117 | for (Entry<String, ConversionData> factorEntry : currencyConversions |
|
- | 118 | .entrySet()) |
|
- | 119 | {
|
|
73 | ContentValues values = new ContentValues(); |
120 | ContentValues values = new ContentValues(); |
74 | values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey()); |
121 | values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey()); |
75 | values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue()); |
122 | values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue() |
- | 123 | .getRate()); |
|
- | 124 | values.put(CurrenciesDatabase.COLUMN_UPDATED, |
|
- | 125 | this.iso8601format.format(factorEntry.getValue() |
|
- | 126 | .getUpdated())); |
|
- | 127 | ||
- | 128 | /* INSERT suffices here, thanks to ON CONFLICT REPLACE */
|
|
- | 129 | db.insert(CurrenciesDatabase.TABLE, |
|
76 | db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR, |
130 | CurrenciesDatabase.COLUMN_FACTOR,
|
77 | values); |
131 | values); |
- | 132 | }
|
|
78 | }
|
133 | }
|
79 | }
|
134 | }
|
80 | 135 | ||
81 | /*
|
136 | /*
|
82 | * (non-Javadoc)
|
137 | * (non-Javadoc)
|
Line 95... | Line 150... | ||
95 | }
|
150 | }
|
96 | 151 | ||
97 | /**
|
152 | /**
|
98 | * @return
|
153 | * @return
|
99 | */
|
154 | */
|
100 | public HashMap<String, Double> getConversionRates() |
155 | public HashMap<String, ConversionData> getConversionRates() |
101 | {
|
156 | {
|
102 | return CurrenciesDatabase.conversionRates; |
157 | return CurrenciesDatabase.conversionRates; |
103 | }
|
158 | }
|
104 | 159 | ||
105 | /**
|
160 | /**
|
Line 109... | Line 164... | ||
109 | public void readConversionsFromDatabase() |
164 | public void readConversionsFromDatabase() |
110 | {
|
165 | {
|
111 | try
|
166 | try
|
112 | {
|
167 | {
|
113 | /* Get database connection, but upgrade database first if necessary! */
|
168 | /* Get database connection, but upgrade database first if necessary! */
|
114 | SQLiteDatabase dbConn = this.getWritableDatabase(); |
169 | this.database = this.getWritableDatabase(); |
115 | 170 | ||
116 | Cursor cursor = |
171 | Cursor cursor = |
117 | dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
172 | this.database.query(true, CurrenciesDatabase.TABLE, null, null, null, |
118 | null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
173 | null, null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
119 | 174 | ||
120 | if (cursor != null) |
175 | if (cursor != null) |
121 | {
|
176 | {
|
122 | try
|
177 | try
|
123 | {
|
178 | {
|
124 | int currency1Id =
|
179 | int currency1Id =
|
125 | cursor |
180 | cursor |
126 | .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
181 | .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
127 | int factorId =
|
182 | int factorId =
|
128 | cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
183 | cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
- | 184 | int updatedId =
|
|
- | 185 | cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_UPDATED); |
|
129 | 186 | ||
130 | /* NOTE: Don't change the default values if the table is empty */
|
187 | /* NOTE: Don't change the default values if the table is empty */
|
131 | if (cursor.moveToFirst()) |
188 | if (cursor.moveToFirst()) |
132 | {
|
189 | {
|
133 | HashMap<String, Double> newCurrencyConversions = |
190 | HashMap<String, ConversionData> newCurrencyConversions = |
134 | new HashMap<String, Double>(); |
191 | new HashMap<String, ConversionData>(); |
135 | 192 | ||
136 | do
|
193 | do
|
137 | {
|
194 | {
|
138 | String currencyStr = cursor.getString(currency1Id); |
195 | String currencyStr = cursor.getString(currency1Id); |
139 | Double factor = cursor.getDouble(factorId); |
196 | Double factor = cursor.getDouble(factorId); |
- | 197 | String updatedStr = cursor.getString(updatedId); |
|
- | 198 | ||
- | 199 | Date updated = new Date(0); |
|
- | 200 | try
|
|
- | 201 | {
|
|
- | 202 | if (updatedStr != null) |
|
- | 203 | {
|
|
- | 204 | updated = this.iso8601format.parse(updatedStr); |
|
- | 205 | }
|
|
- | 206 | }
|
|
- | 207 | catch (ParseException e) |
|
- | 208 | {
|
|
- | 209 | Log.e(this.getClass().toString(), |
|
- | 210 | "Parsing ISO8601 datetime failed: '" + updatedStr + "'", e); |
|
- | 211 | }
|
|
- | 212 | ||
140 | newCurrencyConversions.put(currencyStr, factor); |
213 | newCurrencyConversions.put(currencyStr, new ConversionData( |
- | 214 | factor, updated)); |
|
141 | }
|
215 | }
|
142 | while (cursor.moveToNext()); |
216 | while (cursor.moveToNext()); |
143 | 217 | ||
144 | CurrenciesDatabase.conversionRates = newCurrencyConversions; |
218 | CurrenciesDatabase.conversionRates = newCurrencyConversions; |
145 | }
|
219 | }
|
146 | }
|
220 | }
|
147 | catch (IllegalArgumentException e) |
221 | catch (IllegalArgumentException e) |
148 | {
|
222 | {
|
149 | /* Could not retrieve column index */
|
223 | Log.e(this.getClass().toString(), "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, |
- | |
174 | null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
- | |
175 | - | ||
176 | @SuppressWarnings({ "unused", "nls" }) |
- | |
177 | String queryResult = ""; |
- | |
178 | if (myCursor != null) |
- | |
179 | {
|
- | |
180 | try
|
- | |
181 | {
|
- | |
182 | int currency1Id =
|
- | |
183 | myCursor |
- | |
184 | .getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
- | |
185 | int factorId =
|
- | |
186 | myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
- | |
187 | - | ||
188 | if (myCursor.moveToFirst()) |
- | |
189 | {
|
- | |
190 | do
|
224 | e); |
191 | {
|
- | |
192 | String currencyStr = myCursor.getString(currency1Id); |
- | |
193 | Double factor = myCursor.getDouble(factorId); |
- | |
194 | - | ||
195 | /* DEBUG */
|
- | |
196 | queryResult += |
- | |
197 | "EUR --> " + currencyStr + ": " + factor + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
- | |
198 | }
|
- | |
199 | while (myCursor.moveToNext()); |
- | |
200 | }
|
- | |
201 | }
|
- | |
202 | catch (IllegalArgumentException e) |
- | |
203 | {
|
- | |
204 | /* Could not retrieve column index */
|
- | |
205 | e.printStackTrace(); |
- | |
206 | }
|
225 | }
|
207 | }
|
226 | }
|
208 | 227 | ||
209 | dbConn.close(); |
228 | this.database.close(); |
210 | }
|
229 | }
|
211 | catch (SQLiteException e1) |
230 | catch (SQLiteException e1) |
212 | {
|
231 | {
|
213 | /* Could not open database */
|
232 | Log.e(this.getClass().toString(), "Could not open database", e1); |
214 | e1.printStackTrace(); |
- | |
215 | }
|
233 | }
|
216 | }
|
234 | }
|
217 | }
|
235 | }
|