3,14 → 3,20 |
*/ |
package de.pointedears.converter.db; |
|
import java.text.DateFormat; |
import java.text.ParseException; |
import java.text.SimpleDateFormat; |
import java.util.Date; |
import java.util.HashMap; |
import java.util.Map.Entry; |
|
import android.content.ContentValues; |
import android.content.Context; |
import android.database.Cursor; |
import android.database.sqlite.SQLiteDatabase; |
import android.database.sqlite.SQLiteException; |
import android.database.sqlite.SQLiteOpenHelper; |
import android.util.Log; |
import de.pointedears.converter.app.CurrenciesActivity; |
|
/** |
19,29 → 25,38 |
*/ |
public class CurrenciesDatabase extends SQLiteOpenHelper |
{ |
|
private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$ |
private static final int DATABASE_VERSION = 3; |
private static final int DATABASE_VERSION = 8; |
|
private static final String TABLE = "currency"; //$NON-NLS-1$ |
private static final String COLUMN_CURRENCY = "currency1"; //$NON-NLS-1$ |
private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
private static final String COLUMN_UPDATED = "updated"; //$NON-NLS-1$ |
|
private static HashMap<String, Double> conversionRates = |
new HashMap<String, Double>(); |
private static HashMap<String, ConversionData> conversionRates = |
new HashMap<String, ConversionData>(); |
static |
{ |
/* Default conversion rates from Euro (EUR) to other currencies */ |
Date epoch = new Date(0); |
CurrenciesDatabase.conversionRates |
.put(CurrenciesActivity.VALUE_CHF, 1.3013); |
.put(CurrenciesActivity.VALUE_CHF, |
new ConversionData(1.3013, epoch)); |
CurrenciesDatabase.conversionRates |
.put(CurrenciesActivity.VALUE_USD, 1.3521); |
.put(CurrenciesActivity.VALUE_USD, |
new ConversionData(1.3521, epoch)); |
} |
|
private SQLiteDatabase database; |
private final DateFormat iso8601format = new SimpleDateFormat( |
"yyyy-MM-dd HH:mm:ss"); |
|
/** |
* @param context |
* The Activity in which this wrapper is used |
*/ |
public CurrenciesDatabase(CurrenciesActivity context) |
public CurrenciesDatabase(Context context) |
{ |
super(context, CurrenciesDatabase.DATABASE_NAME, null, |
CurrenciesDatabase.DATABASE_VERSION); |
60,23 → 75,63 |
public void onCreate(SQLiteDatabase db) |
{ |
db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE |
+ " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT, " |
+ CurrenciesDatabase.COLUMN_FACTOR |
+ " NUMERIC" |
+ " (" + CurrenciesDatabase.COLUMN_CURRENCY + " TEXT" |
+ ", " + CurrenciesDatabase.COLUMN_FACTOR + " NUMERIC" |
+ ", " + CurrenciesDatabase.COLUMN_UPDATED |
+ " TEXT" |
+ ", CONSTRAINT unique_currency_pair UNIQUE (" |
+ CurrenciesDatabase.COLUMN_CURRENCY + "))"); |
+ CurrenciesDatabase.COLUMN_CURRENCY + ") ON CONFLICT REPLACE)"); |
|
HashMap<String, Double> currencyConversions = |
this.writeConversionsToDatabase(db); |
} |
|
/** |
* @param db |
* The database; <code>null</code> uses the default database |
*/ |
public void writeConversionsToDatabase(SQLiteDatabase db) |
{ |
HashMap<String, ConversionData> currencyConversions = |
this.getConversionRates(); |
for (Entry<String, Double> factorEntry : currencyConversions.entrySet()) |
|
if (db == null) |
{ |
db = this.database; |
} |
|
if (!db.isOpen()) |
{ |
try |
{ |
db = this.getWritableDatabase(); |
} |
catch (SQLiteException e) |
{ |
Log.e(this.getClass().toString(), "Could not open database", e); |
throw e; |
} |
} |
|
if (db.isOpen()) |
{ |
for (Entry<String, ConversionData> factorEntry : currencyConversions |
.entrySet()) |
{ |
ContentValues values = new ContentValues(); |
values.put(CurrenciesDatabase.COLUMN_CURRENCY, factorEntry.getKey()); |
values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue()); |
db.insert(CurrenciesDatabase.TABLE, CurrenciesDatabase.COLUMN_FACTOR, |
values.put(CurrenciesDatabase.COLUMN_FACTOR, factorEntry.getValue() |
.getRate()); |
values.put(CurrenciesDatabase.COLUMN_UPDATED, |
this.iso8601format.format(factorEntry.getValue() |
.getUpdated())); |
|
/* INSERT suffices here, thanks to ON CONFLICT REPLACE */ |
db.insert(CurrenciesDatabase.TABLE, |
CurrenciesDatabase.COLUMN_FACTOR, |
values); |
} |
} |
} |
|
/* |
* (non-Javadoc) |
97,7 → 152,7 |
/** |
* @return |
*/ |
public HashMap<String, Double> getConversionRates() |
public HashMap<String, ConversionData> getConversionRates() |
{ |
return CurrenciesDatabase.conversionRates; |
} |
111,11 → 166,11 |
try |
{ |
/* Get database connection, but upgrade database first if necessary! */ |
SQLiteDatabase dbConn = this.getWritableDatabase(); |
this.database = this.getWritableDatabase(); |
|
Cursor cursor = |
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
this.database.query(true, CurrenciesDatabase.TABLE, null, null, null, |
null, null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
|
if (cursor != null) |
{ |
126,92 → 181,55 |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
int factorId = |
cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
int updatedId = |
cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_UPDATED); |
|
/* NOTE: Don't change the default values if the table is empty */ |
if (cursor.moveToFirst()) |
{ |
HashMap<String, Double> newCurrencyConversions = |
new HashMap<String, Double>(); |
HashMap<String, ConversionData> newCurrencyConversions = |
new HashMap<String, ConversionData>(); |
|
do |
{ |
String currencyStr = cursor.getString(currency1Id); |
Double factor = cursor.getDouble(factorId); |
newCurrencyConversions.put(currencyStr, factor); |
} |
while (cursor.moveToNext()); |
String updatedStr = cursor.getString(updatedId); |
|
CurrenciesDatabase.conversionRates = newCurrencyConversions; |
Date updated = new Date(0); |
try |
{ |
if (updatedStr != null) |
{ |
updated = this.iso8601format.parse(updatedStr); |
} |
} |
catch (IllegalArgumentException e) |
catch (ParseException e) |
{ |
/* Could not retrieve column index */ |
e.printStackTrace(); |
Log.e(this.getClass().toString(), |
"Parsing ISO8601 datetime failed: '" + updatedStr + "'", e); |
} |
} |
|
dbConn.close(); |
newCurrencyConversions.put(currencyStr, new ConversionData( |
factor, updated)); |
} |
catch (SQLiteException e1) |
{ |
/* Could not open database */ |
e1.printStackTrace(); |
} |
} |
while (cursor.moveToNext()); |
|
/** |
* Tests the database access |
*/ |
public void testAccess() |
{ |
try |
{ |
SQLiteDatabase dbConn = this.getReadableDatabase(); |
|
Cursor myCursor = |
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
null, CurrenciesDatabase.COLUMN_CURRENCY, null); |
|
@SuppressWarnings({ "unused", "nls" }) |
String queryResult = ""; |
if (myCursor != null) |
{ |
try |
{ |
int currency1Id = |
myCursor |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY); |
int factorId = |
myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
|
if (myCursor.moveToFirst()) |
{ |
do |
{ |
String currencyStr = myCursor.getString(currency1Id); |
Double factor = myCursor.getDouble(factorId); |
|
/* DEBUG */ |
queryResult += |
"EUR --> " + currencyStr + ": " + factor + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
CurrenciesDatabase.conversionRates = newCurrencyConversions; |
} |
while (myCursor.moveToNext()); |
} |
} |
catch (IllegalArgumentException e) |
{ |
/* Could not retrieve column index */ |
e.printStackTrace(); |
Log.e(this.getClass().toString(), "Could not retrieve column index", |
e); |
} |
} |
|
dbConn.close(); |
this.database.close(); |
} |
catch (SQLiteException e1) |
{ |
/* Could not open database */ |
e1.printStackTrace(); |
Log.e(this.getClass().toString(), "Could not open database", e1); |
} |
} |
} |