org.hermit.android.net
Class WebBasedData

java.lang.Object
  extended by java.util.Observable
      extended by org.hermit.android.net.WebBasedData
All Implemented Interfaces:
WebFetcher.Listener

public class WebBasedData
extends java.util.Observable
implements WebFetcher.Listener

This class implements a web-based source of data, which is cached in a local database. The data is assumed to be timestamped records.


Constructor Summary
WebBasedData(java.lang.String name, java.lang.String base, java.lang.String suff, long interval, boolean ldate, java.lang.String[] fields)
          Create a web-based data source.
 
Method Summary
 Cursor allRecords()
          Query for all records we have stored.
 Cursor allRecordsSince(long date)
          Query for all records we have stored timstamped AFTER a given date.
 void createTable(SQLiteDatabase db)
          Create our table in the database.
 java.lang.String getName()
          Get the name of this source.
 ContentValues lastRecord()
          Query for the newest record we have stored.
 void onWebData(java.net.URL url, java.lang.Object obj, long fileDate)
          This method is invoked when a data record is retrieved from the URL we were invoked on.
 void onWebDone()
          This method is invoked when the given URL has been fully fetched.
 void onWebError(java.lang.String msg)
          Handle an error while fetching web data.
protected  void process(ContentValues rec)
          Optional processing step -- this is called for each record we read.
 void setDatabase(SQLiteDatabase db)
          Set the database we use for storing our data.
 void update(long now)
          Check to see whether we need to update our stored data.
 void upgradeTable(SQLiteDatabase db, int oldV, int newV)
          Upgrade or table in the database to a new version.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebBasedData

public WebBasedData(java.lang.String name,
                    java.lang.String base,
                    java.lang.String suff,
                    long interval,
                    boolean ldate,
                    java.lang.String[] fields)
Create a web-based data source.

Parameters:
name - The name of this source, and its database table.
base - The base URL for this source; this is the whole URL if urlSuff is null.
suff - The URL suffix. If not null, the URL is urlBase + date + urlSuff, where date is in the format "20091231".
interval - Interval in ms between records in the source.
ldate - If true, the source uses long-format dates.
fields - Names for the fields in the source.
Method Detail

createTable

public void createTable(SQLiteDatabase db)
Create our table in the database.

Parameters:
db - The database.

upgradeTable

public void upgradeTable(SQLiteDatabase db,
                         int oldV,
                         int newV)
Upgrade or table in the database to a new version.

Parameters:
db - The database.
oldV - Version we're upgrading from.
newV - Version we're upgrading to.

setDatabase

public void setDatabase(SQLiteDatabase db)
Set the database we use for storing our data.

Parameters:
db - The database. Will be null if the database is being closed.

getName

public java.lang.String getName()
Get the name of this source.

Returns:
The name of this source.

allRecords

public Cursor allRecords()
Query for all records we have stored. It is the caller's responsibility to call close() on the cursor when done.

Returns:
A Cursor over all the records, sorted in ascending date order. The Cursor is positioned before the first record.

allRecordsSince

public Cursor allRecordsSince(long date)
Query for all records we have stored timstamped AFTER a given date. It is the caller's responsibility to call close() on the cursor when done.

Parameters:
date - Cut-off date -- only return records newer than this date (not equal).
Returns:
A Cursor over all the records, sorted in ascending date order. The Cursor is positioned before the first record.

lastRecord

public ContentValues lastRecord()
Query for the newest record we have stored.

Returns:
The contents of the last record we have. Returns null if there are no records.

update

public void update(long now)
Check to see whether we need to update our stored data. If so, kick off a web fetch. The observers will be notified for any new data that we load.

Parameters:
now - The current time in millis.

process

protected void process(ContentValues rec)
Optional processing step -- this is called for each record we read. It can be used to add synthesized values.

Parameters:
rec - The record to process.

onWebData

public void onWebData(java.net.URL url,
                      java.lang.Object obj,
                      long fileDate)
This method is invoked when a data record is retrieved from the URL we were invoked on.

Specified by:
onWebData in interface WebFetcher.Listener
Parameters:
url - The URL of the source being loaded.
obj - The object that was loaded; the type depends on the fetcher class used.
fileDate - The last modified time of the source file, as reported by the server, in ms UTC.

onWebDone

public void onWebDone()
This method is invoked when the given URL has been fully fetched.

Specified by:
onWebDone in interface WebFetcher.Listener

onWebError

public void onWebError(java.lang.String msg)
Handle an error while fetching web data.

Specified by:
onWebError in interface WebFetcher.Listener
Parameters:
msg - The error message.