aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/DatabaseHelper.java
blob: 17ef72004a1f5bc981be4603cee7ccece38cfd86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
 * 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.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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 {

	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 = 41;

	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<RouteStop, 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, R.raw.ormlite_config);
		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, 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) {
		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);
		}
		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);
		}
		return busRouteDao;
	}

	/**
	 * Returns the Database Access Object (DAO) for our SimpleData class. It will create it or just give the cached value.
	 */
	public Dao<RouteStop, Integer> 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<Site, String> 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<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.
	 * 
	 * @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();
	}

	/**
	 * 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);

		// Path to the just created empty db
		String outFileName = DATABASE_PATH + DATABASE_NAME;

		File database = new File(outFileName);
		if (database.exists()) {
			database.delete();
		}

		// 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);
		}

		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();

		// getWritableDatabase().close();

		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 {

		boolean dbExist = checkDataBase();

		if (dbExist) {
			// do nothing - database already exist
		} else {

			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;
	}
}