/* * Southampton University Map App * Copyright (C) 2011 Christopher Baines * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package net.cbaines.suma; import java.sql.SQLException; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.table.TableUtils; public class DatabaseHelper extends OrmLiteSqliteOpenHelper implements Preferences { private static final String TAG = "DatabaseHelper"; // the DAO object we use to access the SimpleData table private Dao buildingDao = null; private Dao busStopDao = null; private Dao busRouteDao = null; private Dao routeStopsDao = null; private Dao siteDao = null; private Dao busDao = null; volatile boolean doneUpgrade = false; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION, R.raw.ormlite_config); Log.i(TAG, "Database Helper created"); } @Override public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { try { Log.i(TAG, "onCreate"); TableUtils.createTable(connectionSource, Building.class); TableUtils.createTable(connectionSource, BusStop.class); TableUtils.createTable(connectionSource, BusRoute.class); TableUtils.createTable(connectionSource, RouteStop.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) { Log.i(TAG, "onUpgrade"); } /** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value. */ public Dao 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 getBusStopDao() throws SQLException { if (busStopDao == null) { busStopDao = getDao(BusStop.class); } return busStopDao; } /** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value. */ public Dao getBusRouteDao() throws SQLException { if (busRouteDao == null) { busRouteDao = getDao(BusRoute.class); } return busRouteDao; } /** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value. */ public Dao getRouteStopsDao() throws SQLException { if (routeStopsDao == null) { routeStopsDao = getDao(RouteStop.class); } return routeStopsDao; } /** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value. */ public Dao getSiteDao() throws SQLException { if (siteDao == null) { siteDao = getDao(Site.class); } return siteDao; } /** * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value. */ public Dao 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. * * @return true if it exists, false if it doesn't */ /** * 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; } }