aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/DatabaseHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/DatabaseHelper.java')
-rw-r--r--src/net/cbaines/suma/DatabaseHelper.java379
1 files changed, 195 insertions, 184 deletions
diff --git a/src/net/cbaines/suma/DatabaseHelper.java b/src/net/cbaines/suma/DatabaseHelper.java
index 4563d7c..13f4957 100644
--- a/src/net/cbaines/suma/DatabaseHelper.java
+++ b/src/net/cbaines/suma/DatabaseHelper.java
@@ -37,218 +37,229 @@ import com.j256.ormlite.table.TableUtils;
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
- private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/";
- private static final String DATABASE_NAME = "data.db";
-
- private static final int DATABASE_VERSION = 40;
-
- private static final String TAG = "DatabaseHelper";
-
- // the DAO object we use to access the SimpleData table
- private Dao<Building, String> buildingDao = null;
- private Dao<BusStop, String> busStopDao = null;
- private Dao<BusRoute, Integer> busRouteDao = null;
- private Dao<RouteStops, Integer> routeStopsDao = null;
- private Dao<Site, String> siteDao = null;
- private Dao<Bus, Integer> busDao = null;
-
- private Context context;
-
- public DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- Log.i(TAG, "Database Helper created");
- this.context = context;
- }
-
- @Override
- public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
- try {
- Log.i(DatabaseHelper.class.getName(), "onCreate");
- TableUtils.createTable(connectionSource, Building.class);
- TableUtils.createTable(connectionSource, BusStop.class);
- TableUtils.createTable(connectionSource, BusRoute.class);
- TableUtils.createTable(connectionSource, RouteStops.class);
- TableUtils.createTable(connectionSource, Site.class);
- TableUtils.createTable(connectionSource, Bus.class);
- } catch (SQLException e) {
- Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
- throw new RuntimeException(e);
- }
- }
+ private static final String DATABASE_PATH = "/data/data/net.cbaines.suma/databases/";
+ private static final String DATABASE_NAME = "data.db";
+
+ private static final int DATABASE_VERSION = 40;
+
+ private static final String TAG = "DatabaseHelper";
- @Override
- public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
- try {
- Log.i(DatabaseHelper.class.getName(), "onUpgrade");
- copyDataBase();
+ // the DAO object we use to access the SimpleData table
+ private Dao<Building, String> buildingDao = null;
+ private Dao<BusStop, String> busStopDao = null;
+ private Dao<BusRoute, Integer> busRouteDao = null;
+ private Dao<RouteStops, Integer> routeStopsDao = null;
+ private Dao<Site, String> siteDao = null;
+ private Dao<Bus, Integer> busDao = null;
- } catch (IOException e) {
- e.printStackTrace();
+ private Context context;
+
+ public DatabaseHelper(Context context) {
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
+ Log.i(TAG, "Database Helper created");
+ this.context = context;
}
- }
+ @Override
+ public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
+ try {
+ Log.i(DatabaseHelper.class.getName(), "onCreate");
+ TableUtils.createTable(connectionSource, Building.class);
+ TableUtils.createTable(connectionSource, BusStop.class);
+ TableUtils.createTable(connectionSource, BusRoute.class);
+ TableUtils.createTable(connectionSource, RouteStops.class);
+ TableUtils.createTable(connectionSource, Site.class);
+ TableUtils.createTable(connectionSource, Bus.class);
+ } catch (SQLException e) {
+ Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
+ try {
+ Log.i(DatabaseHelper.class.getName(), "onUpgrade");
+ copyDataBase();
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
- public Dao<Building, String> getBuildingDao() throws SQLException {
- if (buildingDao == null) {
- buildingDao = getDao(Building.class);
}
- return buildingDao;
- }
-
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
- public Dao<BusStop, String> getBusStopDao() throws SQLException {
- if (busStopDao == null) {
- busStopDao = getDao(BusStop.class);
+
+ /**
+ * Returns the Database Access Object (DAO) for our SimpleData class. It
+ * will create it or just give the cached value.
+ */
+ public Dao<Building, String> getBuildingDao() throws SQLException {
+ if (buildingDao == null) {
+ buildingDao = getDao(Building.class);
+ }
+ return buildingDao;
}
- return busStopDao;
- }
-
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
- public Dao<BusRoute, Integer> getBusRouteDao() throws SQLException {
- if (busRouteDao == null) {
- busRouteDao = getDao(BusRoute.class);
+
+ /**
+ * Returns the Database Access Object (DAO) for our SimpleData class. It
+ * will create it or just give the cached value.
+ */
+ public Dao<BusStop, String> getBusStopDao() throws SQLException {
+ if (busStopDao == null) {
+ busStopDao = getDao(BusStop.class);
+ }
+ return busStopDao;
}
- return busRouteDao;
- }
-
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
- public Dao<RouteStops, Integer> getRouteStopsDao() throws SQLException {
- if (routeStopsDao == null) {
- routeStopsDao = getDao(RouteStops.class);
+
+ /**
+ * Returns the Database Access Object (DAO) for our SimpleData class. It
+ * will create it or just give the cached value.
+ */
+ public Dao<BusRoute, Integer> getBusRouteDao() throws SQLException {
+ if (busRouteDao == null) {
+ busRouteDao = getDao(BusRoute.class);
+ }
+ return busRouteDao;
}
- return routeStopsDao;
- }
-
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
- public Dao<Site, String> getSiteDao() throws SQLException {
- if (siteDao == null) {
- siteDao = getDao(Site.class);
+
+ /**
+ * Returns the Database Access Object (DAO) for our SimpleData class. It
+ * will create it or just give the cached value.
+ */
+ public Dao<RouteStops, Integer> getRouteStopsDao() throws SQLException {
+ if (routeStopsDao == null) {
+ routeStopsDao = getDao(RouteStops.class);
+ }
+ return routeStopsDao;
}
- return siteDao;
- }
-
- /**
- * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
- */
- public Dao<Bus, Integer> getBusDao() throws SQLException {
- if (busDao == null) {
- busDao = getDao(Bus.class);
+
+ /**
+ * Returns the Database Access Object (DAO) for our SimpleData class. It
+ * will create it or just give the cached value.
+ */
+ public Dao<Site, String> getSiteDao() throws SQLException {
+ if (siteDao == null) {
+ siteDao = getDao(Site.class);
+ }
+ return siteDao;
}
- return busDao;
- }
-
- /**
- * Check if the database already exist to avoid re-copying the file each time you open the application.
- *
- * @return true if it exists, false if it doesn't
- */
- public boolean checkDataBase() {
- Log.i(TAG, "Check database");
-
- /*
- * SQLiteDatabase checkDB = null;
- *
- * try { String myPath = DATABASE_PATH + DATABASE_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch
- * (SQLiteException e) {
- *
- * // database does't exist yet.
- *
- * }
- *
- * if (checkDB != null) {
- *
- * checkDB.close();
- *
- * }
+
+ /**
+ * Returns the Database Access Object (DAO) for our SimpleData class. It
+ * will create it or just give the cached value.
+ */
+ public Dao<Bus, Integer> getBusDao() throws SQLException {
+ if (busDao == null) {
+ busDao = getDao(Bus.class);
+ }
+ return busDao;
+ }
+
+ /**
+ * Check if the database already exist to avoid re-copying the file each
+ * time you open the application.
*
- * Log.i(TAG, "Finished checking database"); return checkDB != null ? true : false;
+ * @return true if it exists, false if it doesn't
*/
+ public boolean checkDataBase() {
+ Log.i(TAG, "Check database");
+
+ /*
+ * SQLiteDatabase checkDB = null;
+ *
+ * try { String myPath = DATABASE_PATH + DATABASE_NAME; checkDB =
+ * SQLiteDatabase.openDatabase(myPath, null,
+ * SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) {
+ *
+ * // database does't exist yet.
+ *
+ * }
+ *
+ * if (checkDB != null) {
+ *
+ * checkDB.close();
+ *
+ * }
+ *
+ * Log.i(TAG, "Finished checking database"); return checkDB != null ?
+ * true : false;
+ */
+
+ File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
+ return dbFile.exists();
+ }
- File dbFile = new File(DATABASE_PATH + DATABASE_NAME);
- return dbFile.exists();
- }
+ /**
+ * Copies your database from your local assets-folder to the just created
+ * empty database in the system folder, from where it can be accessed and
+ * handled. This is done by transfering bytestream.
+ * */
+ public void copyDataBase() throws IOException {
+ Log.i(TAG, "Begining copy database");
- /**
- * Copies your database from your local assets-folder to the just created empty database in the system folder, from where it can be accessed and handled.
- * This is done by transfering bytestream.
- * */
- public void copyDataBase() throws IOException {
- Log.i(TAG, "Begining copy database");
+ InputStream myInput = context.getAssets().open(DATABASE_NAME);
- InputStream myInput = context.getAssets().open(DATABASE_NAME);
+ // Path to the just created empty db
+ String outFileName = DATABASE_PATH + DATABASE_NAME;
- // Path to the just created empty db
- String outFileName = DATABASE_PATH + DATABASE_NAME;
+ File database = new File(outFileName);
+ if (database.exists()) {
+ database.delete();
+ }
- File database = new File(outFileName);
- if (database.exists()) {
- database.delete();
- }
+ // Open the empty db as the output stream
+ OutputStream myOutput = new FileOutputStream(outFileName);
- // Open the empty db as the output stream
- OutputStream myOutput = new FileOutputStream(outFileName);
+ // transfer bytes from the inputfile to the outputfile
+ byte[] buffer = new byte[1024];
+ int length;
+ while ((length = myInput.read(buffer)) > 0) {
+ myOutput.write(buffer, 0, length);
+ }
- // transfer bytes from the inputfile to the outputfile
- byte[] buffer = new byte[1024];
- int length;
- while ((length = myInput.read(buffer)) > 0) {
- myOutput.write(buffer, 0, length);
- }
+ // Close the streams
+ myOutput.flush();
+ myOutput.close();
+ myInput.close();
- // Close the streams
- myOutput.flush();
- myOutput.close();
- myInput.close();
+ // getWritableDatabase().close();
- // getWritableDatabase().close();
+ Log.i(TAG, "Finished copying db");
- Log.i(TAG, "Finished copying db");
+ }
- }
+ /**
+ * Creates a empty database on the system and rewrites it with your own
+ * database.
+ * */
+ public void createDataBase() throws IOException {
- /**
- * Creates a empty database on the system and rewrites it with your own database.
- * */
- public void createDataBase() throws IOException {
+ boolean dbExist = checkDataBase();
- boolean dbExist = checkDataBase();
+ if (dbExist) {
+ // do nothing - database already exist
+ } else {
- if (dbExist) {
- // do nothing - database already exist
- } else {
+ try {
+ Log.i(TAG, "Copy database");
+ copyDataBase();
+ } catch (IOException e) {
+ throw new Error("Error copying database");
+ }
+ }
- try {
- Log.i(TAG, "Copy database");
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
}
- }
-
- /**
- * Close the database connections and clear any cached DAOs.
- */
- @Override
- public void close() {
- super.close();
- buildingDao = null;
- busStopDao = null;
- busRouteDao = null;
- routeStopsDao = null;
- siteDao = null;
- busDao = null;
- }
+ /**
+ * Close the database connections and clear any cached DAOs.
+ */
+ @Override
+ public void close() {
+ super.close();
+ buildingDao = null;
+ busStopDao = null;
+ busRouteDao = null;
+ routeStopsDao = null;
+ siteDao = null;
+ busDao = null;
+ }
} \ No newline at end of file