7,7 → 7,9 |
import java.util.Map.Entry; |
|
import android.content.ContentValues; |
import android.database.Cursor; |
import android.database.sqlite.SQLiteDatabase; |
import android.database.sqlite.SQLiteException; |
import android.database.sqlite.SQLiteOpenHelper; |
import de.pointedears.converter.app.CurrenciesActivity; |
|
18,13 → 20,35 |
public class CurrenciesDatabase extends SQLiteOpenHelper |
{ |
private static final String DATABASE_NAME = "currency.db"; //$NON-NLS-1$ |
private static final int DATABASE_VERSION = 1; |
private static final int DATABASE_VERSION = 2; |
|
public static final String TABLE = "currency"; //$NON-NLS-1$ |
public static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$ |
public static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$ |
public static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
private static final String TABLE = "currency"; //$NON-NLS-1$ |
private static final String COLUMN_CURRENCY1 = "currency1"; //$NON-NLS-1$ |
private static final String COLUMN_CURRENCY2 = "currency2"; //$NON-NLS-1$ |
private static final String COLUMN_FACTOR = "factor"; //$NON-NLS-1$ |
|
private static HashMap<String, HashMap<String, Double>> currencyConversions = |
new HashMap<String, HashMap<String, Double>>(); |
static |
{ |
HashMap<String, Double> conversionFactors = new HashMap<String, Double>(); |
conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.767842293); |
conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.03413); |
CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_CHF, |
conversionFactors); |
|
conversionFactors = new HashMap<String, Double>(); |
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 1.30235077); |
conversionFactors.put(CurrenciesActivity.VALUE_USD, 1.3468); |
CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_EUR, |
conversionFactors); |
|
conversionFactors = new HashMap<String, Double>(); |
conversionFactors.put(CurrenciesActivity.VALUE_CHF, 0.966996412); |
conversionFactors.put(CurrenciesActivity.VALUE_EUR, 0.742500743); |
CurrenciesDatabase.currencyConversions.put(CurrenciesActivity.VALUE_USD, |
conversionFactors); |
} |
private final CurrenciesActivity context; |
|
/** |
36,6 → 60,7 |
super(context, CurrenciesDatabase.DATABASE_NAME, null, |
CurrenciesDatabase.DATABASE_VERSION); |
this.context = context; |
this.readConversionsFromDatabase(); |
} |
|
/* |
50,13 → 75,16 |
public void onCreate(SQLiteDatabase db) |
{ |
db.execSQL("CREATE TABLE IF NOT EXISTS " + CurrenciesDatabase.TABLE |
+ "(" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, " |
+ " (" + CurrenciesDatabase.COLUMN_CURRENCY1 + " TEXT, " |
+ CurrenciesDatabase.COLUMN_CURRENCY2 + " TEXT, " |
+ CurrenciesDatabase.COLUMN_FACTOR |
+ " NUMERIC)"); |
+ " NUMERIC" |
+ ", CONSTRAINT unique_currency_pair UNIQUE (" |
+ CurrenciesDatabase.COLUMN_CURRENCY1 + ", " |
+ CurrenciesDatabase.COLUMN_CURRENCY2 + "))"); |
|
HashMap<String, HashMap<String, Double>> currencyConversions = |
this.context.getCurrencyConversions(); |
this.getConversionRates(); |
for (String key : currencyConversions.keySet()) |
{ |
for (Entry<String, Double> factorEntry : currencyConversions.get(key) |
87,4 → 115,177 |
db.execSQL("DROP TABLE IF EXISTS " + CurrenciesDatabase.TABLE); |
this.onCreate(db); |
} |
|
/** |
* @return |
*/ |
public HashMap<String, HashMap<String, Double>> getConversionRates() |
{ |
return CurrenciesDatabase.currencyConversions; |
} |
|
/** |
* Reads currency conversions and updates the static currencyConversions field |
* of this class |
*/ |
public void readConversionsFromDatabase() |
{ |
try |
{ |
/* Get database connection, but upgrade database first if necessary! */ |
SQLiteDatabase dbConn = this.getWritableDatabase(); |
|
@SuppressWarnings("nls") |
Cursor cursor = |
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
null, CurrenciesDatabase.COLUMN_CURRENCY1 + "," |
+ CurrenciesDatabase.COLUMN_CURRENCY2, null); |
|
if (cursor != null) |
{ |
try |
{ |
int currency1Id = |
cursor |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1); |
int currency2Id = |
cursor |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2); |
int factorId = |
cursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
|
/* NOTE: Don't change the default values if the table is empty */ |
if (cursor.moveToFirst()) |
{ |
HashMap<String, HashMap<String, Double>> newCurrencyConversions = |
new HashMap<String, HashMap<String, Double>>(); |
HashMap<String, Double> mapForCurrency = null; |
String lastCurrency1Str = null; |
String currency1Str; |
|
do |
{ |
currency1Str = cursor.getString(currency1Id); |
String currency2Str = cursor.getString(currency2Id); |
Double factor = cursor.getDouble(factorId); |
|
if (lastCurrency1Str == null |
|| !lastCurrency1Str.equals(currency1Str)) |
{ |
/* |
* NOTE: Update outer map when we see a new currency; |
* ORDER BY ensures we don't see a currency1 twice except |
* consecutively |
*/ |
if (mapForCurrency != null) |
{ |
newCurrencyConversions.put(lastCurrency1Str, mapForCurrency); |
} |
|
lastCurrency1Str = new String(currency1Str); |
|
/* NOTE: New currency1: Reset inner map */ |
mapForCurrency = newCurrencyConversions.get(currency1Str); |
} |
|
/* If we did not see this currency1 before */ |
if (mapForCurrency == null) |
{ |
mapForCurrency = new HashMap<String, Double>(); |
} |
|
/* |
* NOTE: Update inner map after each table row; assignment to |
* mapForCurrency above ensures we are putting the factor |
* into the correct map. |
*/ |
mapForCurrency.put(currency2Str, factor); |
} |
while (cursor.moveToNext()); |
|
/* |
* NOTE: Update from last table row; cursor not empty, so we can |
* skip the test for null |
*/ |
newCurrencyConversions.put(currency1Str, mapForCurrency); |
|
CurrenciesDatabase.currencyConversions = newCurrencyConversions; |
} |
} |
catch (IllegalArgumentException e) |
{ |
/* Could not retrieve column index */ |
e.printStackTrace(); |
} |
} |
|
dbConn.close(); |
} |
catch (SQLiteException e1) |
{ |
/* Could not open database */ |
e1.printStackTrace(); |
} |
} |
|
/** |
* Tests the database access |
*/ |
public void testAccess() |
{ |
try |
{ |
SQLiteDatabase dbConn = this.getReadableDatabase(); |
|
@SuppressWarnings("nls") |
Cursor myCursor = |
dbConn.query(true, CurrenciesDatabase.TABLE, null, null, null, null, |
null, CurrenciesDatabase.COLUMN_CURRENCY1 + "," |
+ CurrenciesDatabase.COLUMN_CURRENCY2, null); |
|
@SuppressWarnings({ "unused", "nls" }) |
String queryResult = ""; |
if (myCursor != null) |
{ |
try |
{ |
int currency1Id = |
myCursor |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY1); |
int currency2Id = |
myCursor |
.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_CURRENCY2); |
int factorId = |
myCursor.getColumnIndexOrThrow(CurrenciesDatabase.COLUMN_FACTOR); |
|
if (myCursor.moveToFirst()) |
{ |
do |
{ |
String currency1Str = myCursor.getString(currency1Id); |
String currency2Str = myCursor.getString(currency2Id); |
Double factor = myCursor.getDouble(factorId); |
|
/* DEBUG */ |
queryResult += |
currency1Str + " --> " + currency2Str + ": " + factor + "\n"; |
} |
while (myCursor.moveToNext()); |
} |
} |
catch (IllegalArgumentException e) |
{ |
/* Could not retrieve column index */ |
e.printStackTrace(); |
} |
} |
|
dbConn.close(); |
} |
catch (SQLiteException e1) |
{ |
/* Could not open database */ |
e1.printStackTrace(); |
} |
} |
} |