Subversion Repositories ES

Compare Revisions

Last modification

Ignore whitespace Rev 16 → Rev 17

/trunk/src/de/pointedears/converter/db/ConversionData.java
0,0 → 1,60
package de.pointedears.converter.db;
 
import java.util.Date;
 
/**
* Stores conversion data for a currency
*
* @author pelinux
*/
public class ConversionData
{
private double rate;
private Date updated;
 
/**
* @param rate
* 1 EUR equals this value in a currency
* @param updated
* Date that the rate was updated
*/
public ConversionData(Double rate, Date updated)
{
this.setRate(rate);
this.setUpdated(updated);
}
 
/**
* @return the rate
*/
public Double getRate()
{
return this.rate;
}
 
/**
* @param rate
* the rate to set
*/
public void setRate(Double rate)
{
this.rate = rate;
}
 
/**
* @return the updated
*/
public Date getUpdated()
{
return this.updated;
}
 
/**
* @param updated
* the updated to set
*/
public void setUpdated(Date updated)
{
this.updated = updated;
}
}
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: CurrenciesDatabase.java
===================================================================
--- CurrenciesDatabase.java (revision 16)
+++ CurrenciesDatabase.java (revision 17)
@@ -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,22 +75,62 @@
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)
{
- 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);
+ 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()
+ .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);
+ }
+ }
}
/*
@@ -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,18 +181,37 @@
.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);
+ String updatedStr = cursor.getString(updatedId);
+
+ Date updated = new Date(0);
+ try
+ {
+ if (updatedStr != null)
+ {
+ updated = this.iso8601format.parse(updatedStr);
+ }
+ }
+ catch (ParseException e)
+ {
+ Log.e(this.getClass().toString(),
+ "Parsing ISO8601 datetime failed: '" + updatedStr + "'", e);
+ }
+
+ newCurrencyConversions.put(currencyStr, new ConversionData(
+ factor, updated));
}
while (cursor.moveToNext());
@@ -146,72 +220,16 @@
}
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);
}
}
-
- /**
- * 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$
- }
- while (myCursor.moveToNext());
- }
- }
- catch (IllegalArgumentException e)
- {
- /* Could not retrieve column index */
- e.printStackTrace();
- }
- }
-
- dbConn.close();
- }
- catch (SQLiteException e1)
- {
- /* Could not open database */
- e1.printStackTrace();
- }
- }
}