From 6e971428e780adc781c3d8146168590dabec7ada Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Fri, 24 Feb 2012 20:47:11 +0000 Subject: Added a BuildingActivity, with a photo, and am begining to use rdf data :) --- src/net/cbaines/suma/DataManager.java | 1445 ++++++++++++++++++--------------- 1 file changed, 771 insertions(+), 674 deletions(-) (limited to 'src/net/cbaines/suma/DataManager.java') diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java index c6a3a36..56dac5c 100644 --- a/src/net/cbaines/suma/DataManager.java +++ b/src/net/cbaines/suma/DataManager.java @@ -23,6 +23,8 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; @@ -58,6 +60,10 @@ import org.xml.sax.XMLReader; import android.content.Context; import android.util.Log; +import com.hp.hpl.jena.query.Dataset; +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; +import com.hp.hpl.jena.tdb.TDBFactory; import com.j256.ormlite.android.apptools.OpenHelperManager; import com.j256.ormlite.dao.Dao; import com.j256.ormlite.stmt.PreparedQuery; @@ -66,823 +72,914 @@ import com.j256.ormlite.table.TableUtils; public class DataManager { - private final static String TAG = "DataManager"; + private final static String TAG = "DataManager"; - private final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/"; + private final static String busStopUrl = "http://data.southampton.ac.uk/bus-stop/"; - private static DatabaseHelper helper; - private static Dao busRouteDao; - private static Dao busDao; - private static Dao busStopDao; + private static DatabaseHelper helper; + private static Dao busRouteDao; + private static Dao busDao; + private static Dao busStopDao; - public static void loadBuildings(Context context) throws SQLException, IOException { - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - Dao buildingDao = helper.getBuildingDao(); + public static void loadBuildings(Context context) throws SQLException, IOException { + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + Dao buildingDao = helper.getBuildingDao(); - TableUtils.clearTable(helper.getConnectionSource(), Building.class); + TableUtils.clearTable(helper.getConnectionSource(), Building.class); - Log.i(TAG, "Begining loading buildings from csv"); + Log.i(TAG, "Begining loading buildings from csv"); - HashMap buildingPoints = new HashMap(); + HashMap buildingPoints = new HashMap(); - InputStream inputStream = context.getAssets().open("buildings_points.csv"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String strLine; - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + InputStream inputStream = context.getAssets().open("buildings_points.csv"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String strLine; + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); - GeoPoint point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[1]); - // Log.i(TAG, "Creating building with id " + dataBits[0] + " and " + point.getLatitudeE6() + " " + - // point.getLongitudeE6()); - buildingPoints.put(dataBits[0], point); - } + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); + GeoPoint point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[1]); + // Log.i(TAG, "Creating building with id " + dataBits[0] + + // " and " + point.getLatitudeE6() + " " + + // point.getLongitudeE6()); + buildingPoints.put(dataBits[0], point); + } + + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + // Log.i(TAG, "Number of building points " + buildingPoints.size()); + + inputStream = context.getAssets().open("building_estates.csv"); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); + + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + if (strLine.startsWith("\"")) { + String[] quoteBits = strLine.split("\""); + // Log.i(TAG, "QuoteBits " + quoteBits[0] + " | " + + // quoteBits[1]); + String[] dataBits = quoteBits[2].split(","); + // Log.i(TAG, "dataBits " + dataBits[0] + " | " + + // dataBits[1]); + + if (buildingPoints.get(dataBits[1]) == null) { + // Log.w(TAG, "Building " + dataBits[1] + + // " has a null point"); + continue; + } + + Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), + quoteBits[0]); + /* + * Polygon poly = buildingPolys.get(dataBits[1]); + * + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } + */ + + // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + // + " " + bdg.point + " " + + // bdg.residential + " " + bdg.outline); + + buildingDao.create(bdg); + + } else { + + String[] dataBits = strLine.split(","); + + if (buildingPoints.get(dataBits[1]) == null) { + // Log.w(TAG, "Building " + dataBits[1] + + // " has a null point"); + continue; + } + + Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), + dataBits[0]); + /* + * Polygon poly = buildingPolys.get(dataBits[1]); + * + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // + * Log.i(TAG, "Adding building " + key + " " + + * bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } + */ + + // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + // + " " + bdg.point + " " + + // bdg.residential + " " + bdg.outline); + + buildingDao.create(bdg); + + } + } + + bufferedReader.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + /* + * for (Iterator iter = buildingPoints.keySet().iterator(); + * iter.hasNext();) { String key = iter.next(); + * + * Building bdg = new Building(key, buildingPoints.get(key), false); + * Polygon poly = buildingPolys.get(key); + * + * if (poly != null) { bdg.outline = poly; // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, + * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + + * bdg.point.getLongitudeE6()); } + * + * buildingDao.create(bdg); } + */ - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); } - // Log.i(TAG, "Number of building points " + buildingPoints.size()); + public static void loadBuildingPhotos(Context context) { + // Direct way: Make a TDB-backed Jena model in the named directory. + String directory = "/data/data/net.cbaines.suma/databases"; + Model model = TDBFactory.createModel(directory); - inputStream = context.getAssets().open("building_estates.csv"); - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + Log.i(TAG, "Created the model"); - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + // write it to standard out + StringWriter sw = new StringWriter(); + model.write(new PrintWriter(sw)); + Log.v(TAG, sw.toString()); - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - if (strLine.startsWith("\"")) { - String[] quoteBits = strLine.split("\""); - // Log.i(TAG, "QuoteBits " + quoteBits[0] + " | " + quoteBits[1]); - String[] dataBits = quoteBits[2].split(","); - // Log.i(TAG, "dataBits " + dataBits[0] + " | " + dataBits[1]); + Log.i(TAG, "Finished the first output"); - if (buildingPoints.get(dataBits[1]) == null) { - // Log.w(TAG, "Building " + dataBits[1] + " has a null point"); - continue; - } + if (model.isEmpty()) { - Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), - quoteBits[0]); - /* - * Polygon poly = buildingPolys.get(dataBits[1]); - * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } - */ + Log.i(TAG, "Model is empty"); - // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " + - // bdg.residential + " " + bdg.outline); + // use the FileManager to find the input file + InputStream in; + try { - buildingDao.create(bdg); + String[] datasets = { "photos.rdf", "places.rdf" }; - } else { + for (String dataset : datasets) { + + in = context.getAssets().open(dataset); + + if (in == null) { + throw new IllegalArgumentException(dataset + " not found"); + } - String[] dataBits = strLine.split(","); + // read the RDF/XML file + model.read(in, null); - if (buildingPoints.get(dataBits[1]) == null) { - // Log.w(TAG, "Building " + dataBits[1] + " has a null point"); - continue; - } + Log.i(TAG, "Finished the read of " + dataset); - Building bdg = new Building(dataBits[1], buildingPoints.get(dataBits[1]), dataBits[3].equals("R"), - dataBits[0]); - /* - * Polygon poly = buildingPolys.get(dataBits[1]); - * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // - * Log.i(TAG, "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + - * bdg.point.getLongitudeE6()); } - */ + } - // Log.i(TAG, "Creating building " + bdg.id + " " + bdg.name + " " + bdg.point + " " + - // bdg.residential + " " + bdg.outline); + // write it to standard out + sw = new StringWriter(); + model.write(new PrintWriter(sw)); + Log.i(TAG, sw.toString()); - buildingDao.create(bdg); + Log.i(TAG, "Finished the second output"); + + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - } - bufferedReader.close(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + model.close(); - /* - * for (Iterator iter = buildingPoints.keySet().iterator(); iter.hasNext();) { String key = iter.next(); - * - * Building bdg = new Building(key, buildingPoints.get(key), false); Polygon poly = buildingPolys.get(key); - * - * if (poly != null) { bdg.outline = poly; // Log.i(TAG, "Adding building " + key + " " + - * bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6() + " " + poly); } else { // Log.i(TAG, - * "Adding building " + key + " " + bdg.point.getLatitudeE6() + " " + bdg.point.getLongitudeE6()); } - * - * buildingDao.create(bdg); } - */ - - } - - public static void loadBusData(Context context, boolean onlyUniLink) throws SQLException, IOException { - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - - Dao busStopDao = helper.getBusStopDao(); - Dao busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = helper.getRouteStopsDao(); - - TableUtils.clearTable(helper.getConnectionSource(), BusStop.class); - TableUtils.clearTable(helper.getConnectionSource(), BusRoute.class); - TableUtils.clearTable(helper.getConnectionSource(), RouteStops.class); - - Log.i(TAG, "Loading busstops from csv"); - - InputStream inputStream = context.getAssets().open("bus_stops.csv"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String strLine = ""; - - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " +def ); - - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); - - String[] quBitsLat = dataBits[3].substring(1, dataBits[3].length() - 1).split(" "); - String[] quBitsLng = dataBits[4].substring(1, dataBits[4].length() - 1).split(" "); - - // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + quBitsLat[0] + " last bit " + quBitsLat[1]); - double lat = Double.valueOf(quBitsLat[0]) - + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; - // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + quBitsLng[0] + " last bit " + quBitsLng[1]); - double lng = Double.valueOf(quBitsLng[0]) - + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; - GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); - // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + point.getLongitudeE6()); - - busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] - .replace("\"", ""), point)); - - } - - bufferedReader.close(); - } catch (IOException e) { - Log.e(TAG, "Line: " + strLine); - e.printStackTrace(); + Log.i(TAG, "Closed the model"); } - Log.i(TAG, "Finished loading busstops, now loading routes"); + public static void loadBusData(Context context, boolean onlyUniLink) throws SQLException, IOException { + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - inputStream = context.getAssets().open("routes.csv"); - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + Dao busStopDao = helper.getBusStopDao(); + Dao busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = helper.getRouteStopsDao(); - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + TableUtils.clearTable(helper.getConnectionSource(), BusStop.class); + TableUtils.clearTable(helper.getConnectionSource(), BusRoute.class); + TableUtils.clearTable(helper.getConnectionSource(), RouteStops.class); - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); + Log.i(TAG, "Loading busstops from csv"); - BusRoute route; + InputStream inputStream = context.getAssets().open("bus_stops.csv"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String strLine = ""; - boolean uniLink; - int id = Integer.parseInt(dataBits[0]); - if (id == 326 || id == 468 || id == 327 || id == 329 || id == 354) { - uniLink = true; - } else { - uniLink = false; - } + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " +def ); + + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); + + String[] quBitsLat = dataBits[3].substring(1, dataBits[3].length() - 1).split(" "); + String[] quBitsLng = dataBits[4].substring(1, dataBits[4].length() - 1).split(" "); + + // Log.i(TAG, "Whole " + dataBits[3] + " First bit " + + // quBitsLat[0] + " last bit " + quBitsLat[1]); + double lat = Double.valueOf(quBitsLat[0]) + + Double.valueOf(quBitsLat[1].substring(0, quBitsLat[1].length() - 1)) / 60d; + // Log.i(TAG, "Whole " + dataBits[4] + " First bit " + + // quBitsLng[0] + " last bit " + quBitsLng[1]); + double lng = Double.valueOf(quBitsLng[0]) + + Double.valueOf(quBitsLng[1].substring(0, quBitsLng[1].length() - 1)) / 60d; + GeoPoint point = new GeoPoint((int) (lat * 1e6), (int) (lng * -1e6)); + // Log.i(TAG, "Lat " + point.getLatitudeE6() + " lng " + + // point.getLongitudeE6()); + + busStopDao.create(new BusStop(dataBits[0].replace("\"", ""), dataBits[1].replace("\"", ""), dataBits[2] + .replace("\"", ""), point)); + + } - route = new BusRoute(id, dataBits[1], dataBits[2].replace("\"", ""), uniLink); - - if (id == 326) { // U1 - route.forwardDirection = "A"; - route.reverseDirection = "C"; - } else if (id == 329) { // U2 - route.forwardDirection = "B"; - route.reverseDirection = "C"; - } else if (id == 327) { // U6 - route.forwardDirection = "H"; - route.reverseDirection = "C"; + bufferedReader.close(); + } catch (IOException e) { + Log.e(TAG, "Line: " + strLine); + e.printStackTrace(); } - // Log.i(TAG, "Loaded route " + route.id + " " + route.code + " " + route.label); - busRouteDao.create(route); + Log.i(TAG, "Finished loading busstops, now loading routes"); - } + inputStream = context.getAssets().open("routes.csv"); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - bufferedReader.close(); - } catch (IOException e) { - Log.e(TAG, "Line: " + strLine); - e.printStackTrace(); - } + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); - Log.i(TAG, "Finished loading routes, now loading routestops"); + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); - inputStream = context.getAssets().open("routestops.csv"); - bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + BusRoute route; - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the definition " + def); + boolean uniLink; + int id = Integer.parseInt(dataBits[0]); + if (id == 326 || id == 468 || id == 327 || id == 329 || id == 354) { + uniLink = true; + } else { + uniLink = false; + } - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Data: " + strLine); - String[] dataBits = strLine.split(","); + route = new BusRoute(id, dataBits[1], dataBits[2].replace("\"", ""), uniLink); - BusStop busStop = busStopDao.queryForId(dataBits[2]); - if (busStop != null) { - // Log.i(TAG, "Found stop " + stop.id); - } else { - Log.w(TAG, "No stop found for " + dataBits[2]); - continue; - } + if (id == 326) { // U1 + route.forwardDirection = "A"; + route.reverseDirection = "C"; + } else if (id == 329) { // U2 + route.forwardDirection = "B"; + route.reverseDirection = "C"; + } else if (id == 327) { // U6 + route.forwardDirection = "H"; + route.reverseDirection = "C"; + } - BusRoute busRoute = busRouteDao.queryForId(Integer.parseInt(dataBits[0])); - if (busRoute != null) { - // Log.i(TAG, "Found route " + route.id); - } else { - Log.w(TAG, "No route found for " + dataBits[0]); - continue; - } + // Log.i(TAG, "Loaded route " + route.id + " " + route.code + + // " " + route.label); + busRouteDao.create(route); - int sequence = Integer.parseInt(dataBits[1]); - Log.i(TAG, "Creating RouteStop " + busStop.id + " " + busRoute.code + " " + sequence); - - routeStopsDao.create(new RouteStops(busStop, busRoute, sequence)); - - if (busRoute.id == 326) { // U1 - busStop.routes = (byte) (busStop.routes | 1); - busStop.uniLink = true; - } else if (busRoute.id == 468) { // U1N - busStop.routes = (byte) (busStop.routes | (1 << 1)); - busStop.uniLink = true; - } else if (busRoute.id == 329) { // U2 - busStop.routes = (byte) (busStop.routes | (1 << 2)); - busStop.uniLink = true; - } else if (busRoute.id == 327) { // U6 - busStop.routes = (byte) (busStop.routes | (1 << 3)); - busStop.uniLink = true; - } else if (busRoute.id == 354) { // U9 - busStop.routes = (byte) (busStop.routes | (1 << 4)); - busStop.uniLink = true; - } else { - busStop.routes = 0; + } + + bufferedReader.close(); + } catch (IOException e) { + Log.e(TAG, "Line: " + strLine); + e.printStackTrace(); } - // Log.v(TAG, "Stop routes " + busStop.routes); - busStopDao.update(busStop); + Log.i(TAG, "Finished loading routes, now loading routestops"); + + inputStream = context.getAssets().open("routestops.csv"); + bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the definition " + def); + + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Data: " + strLine); + String[] dataBits = strLine.split(","); + + BusStop busStop = busStopDao.queryForId(dataBits[2]); + if (busStop != null) { + // Log.i(TAG, "Found stop " + stop.id); + } else { + Log.w(TAG, "No stop found for " + dataBits[2]); + continue; + } + + BusRoute busRoute = busRouteDao.queryForId(Integer.parseInt(dataBits[0])); + if (busRoute != null) { + // Log.i(TAG, "Found route " + route.id); + } else { + Log.w(TAG, "No route found for " + dataBits[0]); + continue; + } + + int sequence = Integer.parseInt(dataBits[1]); + Log.i(TAG, "Creating RouteStop " + busStop.id + " " + busRoute.code + " " + sequence); + + routeStopsDao.create(new RouteStops(busStop, busRoute, sequence)); + + if (busRoute.id == 326) { // U1 + busStop.routes = (byte) (busStop.routes | 1); + busStop.uniLink = true; + } else if (busRoute.id == 468) { // U1N + busStop.routes = (byte) (busStop.routes | (1 << 1)); + busStop.uniLink = true; + } else if (busRoute.id == 329) { // U2 + busStop.routes = (byte) (busStop.routes | (1 << 2)); + busStop.uniLink = true; + } else if (busRoute.id == 327) { // U6 + busStop.routes = (byte) (busStop.routes | (1 << 3)); + busStop.uniLink = true; + } else if (busRoute.id == 354) { // U9 + busStop.routes = (byte) (busStop.routes | (1 << 4)); + busStop.uniLink = true; + } else { + busStop.routes = 0; + } + + // Log.v(TAG, "Stop routes " + busStop.routes); + busStopDao.update(busStop); - } + } - bufferedReader.close(); - } catch (IOException e) { - Log.e(TAG, "Line: " + strLine); - e.printStackTrace(); + bufferedReader.close(); + } catch (IOException e) { + Log.e(TAG, "Line: " + strLine); + e.printStackTrace(); + } + + /* + * for (Iterator busStopIter = busStopDao.iterator(); + * busStopIter.hasNext();) { BusStop stop = busStopIter.next(); // + * Log.i(TAG, "Looking at stop " + stop.id); + * + * + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.where().eq(columnName, value) + * + * DeleteBuilder deleteBuilder = + * busStopDao.deleteBuilder(); // only delete the rows where password is + * null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) + * accountDao.delete(deleteBuilder.prepare()); + * + * + * QueryBuilder routeStopsQueryBuilder = + * routeStopsDao.queryBuilder(); + * routeStopsQueryBuilder.setCountOf(true); + * routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, + * stop); + * + * PreparedQuery routeStopsPreparedQuery = + * routeStopsQueryBuilder.prepare(); List routeStops = + * routeStopsDao.query(routeStopsPreparedQuery); // long num = + * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, + * "Number is " + num); + * + * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if + * (routeStop.busRoute.uniLink) { stop.uniLink = true; } } + * busStopDao.update(stop); } + */ + + Log.i(TAG, "Finished loading bus data"); } - /* - * for (Iterator busStopIter = busStopDao.iterator(); busStopIter.hasNext();) { BusStop stop = - * busStopIter.next(); // Log.i(TAG, "Looking at stop " + stop.id); - * - * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.where().eq(columnName, value) - * - * DeleteBuilder deleteBuilder = busStopDao.deleteBuilder(); // only delete the rows where - * password is null deleteBuilder.where().in(RouteStops.STOP_ID_FIELD_NAME, objects) - * accountDao.delete(deleteBuilder.prepare()); - * - * - * QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - * routeStopsQueryBuilder.setCountOf(true); routeStopsQueryBuilder.where().eq(RouteStops.STOP_ID_FIELD_NAME, - * stop); - * - * PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); List - * routeStops = routeStopsDao.query(routeStopsPreparedQuery); // long num = - * routeStopsDao.query(routeStopsPreparedQuery).size(); // Log.i(TAG, "Number is " + num); - * - * stop.uniLink = false; for (RouteStops routeStop : routeStops) { if (routeStop.busRoute.uniLink) { - * stop.uniLink = true; } } busStopDao.update(stop); } - */ - - Log.i(TAG, "Finished loading bus data"); - } - - public static void loadSiteData(Context context) throws SQLException, IOException { - Log.i(TAG, "Begining loading site data"); - - DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - - TableUtils.clearTable(helper.getConnectionSource(), Site.class); - - Dao siteDao = helper.getSiteDao(); - - InputStream inputStream = context.getAssets().open("sites.csv"); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); - String strLine = null; - - try { - String def = bufferedReader.readLine(); - // Log.i(TAG, "Reading the site definition " + def); - - while ((strLine = bufferedReader.readLine()) != null) { - // Log.i(TAG, "Site Data: " + strLine); - String[] dataBits = strLine.split(","); - - GeoPoint point = null; - if (dataBits[2].length() > 1 && dataBits[3].length() > 1) { - point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[3]); - } else { - point = new GeoPoint(0, 0); - } + public static void loadSiteData(Context context) throws SQLException, IOException { + Log.i(TAG, "Begining loading site data"); - Polygon poly = Util.csPolygonToPolygon(strLine.split("\"")[1]); - // Log.i(TAG, "Polygon: " + poly); + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - siteDao.create(new Site(dataBits[0], dataBits[1], point, poly)); - } + TableUtils.clearTable(helper.getConnectionSource(), Site.class); - bufferedReader.close(); - } catch (Exception e) { - // TODO Auto-generated catch block - Log.e(TAG, "Site Line: " + strLine); - e.printStackTrace(); - } + Dao siteDao = helper.getSiteDao(); - Log.i(TAG, "Loaded sites from csv"); - } - - private static Stop getStop(Context context, JSONObject stopObj, Set routes, BusStop busStop) - throws SQLException, JSONException { - - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - Dao routeStopsDao = null; - if (routeStopsDao == null) - routeStopsDao = helper.getRouteStopsDao(); - if (busDao == null) - busDao = helper.getBusDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); - - String time = stopObj.getString("time"); - - GregorianCalendar calender = new GregorianCalendar(); - boolean live = true; - if (!time.equals("Due")) { - - // Log.v(TAG, "Time: " + time + " current time " + calender.getTime()); - - if (time.contains(":")) { - String[] minAndHour = time.split(":"); - calender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minAndHour[0])); - calender.set(Calendar.MINUTE, Integer.parseInt(minAndHour[1])); - live = false; - } else { - // Log.i(TAG, "Parsing " + time.substring(0, time.length() - 1) + " for min"); - calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1))); - } - - // Log.v(TAG, "Date: " + calender.getTime()); - } + InputStream inputStream = context.getAssets().open("sites.csv"); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + String strLine = null; - String name = stopObj.getString("name"); + try { + String def = bufferedReader.readLine(); + // Log.i(TAG, "Reading the site definition " + def); - BusRoute route = null; - String dir = ""; + while ((strLine = bufferedReader.readLine()) != null) { + // Log.i(TAG, "Site Data: " + strLine); + String[] dataBits = strLine.split(","); - for (BusRoute tempRoute : routes) { - if (name.contains("U")) { - if (name.equals("U1N")) { - if (tempRoute.code.equals(name)) { - route = tempRoute; - dir = null; - } - } else { - if (tempRoute == null) { - Log.e(TAG, "tempRoute == null"); - } - if (tempRoute.code == null) { - Log.e(TAG, "tempRoute.code == null"); - } - if (tempRoute.code.equals(name.substring(0, 2))) { - route = tempRoute; - if (route.forwardDirection.equals(name.substring(2))) { - dir = route.forwardDirection; - } else if (route.reverseDirection.equals(name.substring(2))) { - dir = route.reverseDirection; - } else { - Log.e(TAG, "Error detecting direction for " + name); - dir = null; - return null; + GeoPoint point = null; + if (dataBits[2].length() > 1 && dataBits[3].length() > 1) { + point = Util.csLatLongToGeoPoint(dataBits[2], dataBits[3]); + } else { + point = new GeoPoint(0, 0); + } + + Polygon poly = Util.csPolygonToPolygon(strLine.split("\"")[1]); + // Log.i(TAG, "Polygon: " + poly); + + siteDao.create(new Site(dataBits[0], dataBits[1], point, poly)); } - } - } - } else { - if (tempRoute.code.equals(name)) { - route = tempRoute; - dir = null; + + bufferedReader.close(); + } catch (Exception e) { + // TODO Auto-generated catch block + Log.e(TAG, "Site Line: " + strLine); + e.printStackTrace(); } - } - } - if (route == null) { - Log.e(TAG, "Route not found (route == null) " + name); - return null; + Log.i(TAG, "Loaded sites from csv"); } - if (dir != null && dir.equals("")) { - Log.e(TAG, "Error detecting direction for " + name); - return null; - } + private static Stop getStop(Context context, JSONObject stopObj, Set routes, BusStop busStop) + throws SQLException, JSONException { + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + Dao routeStopsDao = null; + if (routeStopsDao == null) + routeStopsDao = helper.getRouteStopsDao(); + if (busDao == null) + busDao = helper.getBusDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + String time = stopObj.getString("time"); + + GregorianCalendar calender = new GregorianCalendar(); + boolean live = true; + if (!time.equals("Due")) { + + // Log.v(TAG, "Time: " + time + " current time " + + // calender.getTime()); + + if (time.contains(":")) { + String[] minAndHour = time.split(":"); + calender.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minAndHour[0])); + calender.set(Calendar.MINUTE, Integer.parseInt(minAndHour[1])); + live = false; + } else { + // Log.i(TAG, "Parsing " + time.substring(0, time.length() - 1) + // + " for min"); + calender.add(Calendar.MINUTE, Integer.parseInt(time.substring(0, time.length() - 1))); + } - String destString = stopObj.getString("dest"); - BusStop destStop = null; - - if (destString.equals("Central Station")) { - destStop = busStopDao.queryForId("SN120520"); - } else if (destString.equals("Civic Centre")) { - destStop = busStopDao.queryForId("SN120527"); - } else if (destString.equals("City DG4")) { - destStop = busStopDao.queryForId("HAA13579"); - } else if (destString.equals("Airport")) { - destStop = busStopDao.queryForId("HA030184"); - } else if (destString.equals("City, Town Quay")) { - destStop = busStopDao.queryForId("SNA13766"); - } else if (destString.equals("City Centre")) { - destStop = busStopDao.queryForId("SNA13766"); - } else if (destString.equals("Dock Gate 4")) { - destStop = busStopDao.queryForId("MG1031"); - } else if (destString.equals("Eastleigh")) { - destStop = busStopDao.queryForId("HA030212"); - } else if (destString.equals("Crematorium")) { - destStop = busStopDao.queryForId("SN121009"); - } else if (destString.equals("General Hosp")) { - destStop = busStopDao.queryForId("SNA19482"); - } else if (destString.equals("Wessex Lane")) { - destStop = busStopDao.queryForId("SNA19780"); - } else { - Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code); - } + // Log.v(TAG, "Date: " + calender.getTime()); + } - if (destStop != null) { - - QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); - routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() - .eq(RouteStops.STOP_ID_FIELD_NAME, destStop.id); - PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); - - List routeStops = routeStopsDao.query(routeStopsPreparedQuery); - if (routeStops.size() > 0) { - Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); - } else { - Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " - + route.code); - } - } + String name = stopObj.getString("name"); + + BusRoute route = null; + String dir = ""; + + for (BusRoute tempRoute : routes) { + if (name.contains("U")) { + if (name.equals("U1N")) { + if (tempRoute.code.equals(name)) { + route = tempRoute; + dir = null; + } + } else { + if (tempRoute == null) { + Log.e(TAG, "tempRoute == null"); + } + if (tempRoute.code == null) { + Log.e(TAG, "tempRoute.code == null"); + } + if (tempRoute.code.equals(name.substring(0, 2))) { + route = tempRoute; + if (route.forwardDirection.equals(name.substring(2))) { + dir = route.forwardDirection; + } else if (route.reverseDirection.equals(name.substring(2))) { + dir = route.reverseDirection; + } else { + Log.e(TAG, "Error detecting direction for " + name); + dir = null; + return null; + } + } + } + } else { + if (tempRoute.code.equals(name)) { + route = tempRoute; + dir = null; + } + } + } - Date now = new Date(System.currentTimeMillis()); - - String busID = null; - Stop stop; - Bus bus; - if (stopObj.has("vehicle")) { - busID = stopObj.getString("vehicle"); - - QueryBuilder busQueryBuilder = busDao.queryBuilder(); - busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); - PreparedQuery busPreparedQuery = busQueryBuilder.prepare(); - - bus = busDao.queryForFirst(busPreparedQuery); - - if (bus == null) { - bus = new Bus(busID, route, dir); - bus.destination = destStop; - busDao.create(bus); - } else { - bus.destination = destStop; - bus.route = route; - bus.direction = dir; - busDao.update(bus); - } - - } else { - bus = new Bus(null, route, dir); - busDao.create(bus); - } + if (route == null) { + Log.e(TAG, "Route not found (route == null) " + name); + return null; + } - stop = new Stop(bus, busStop, calender.getTime(), now, live); + if (dir != null && dir.equals("")) { + Log.e(TAG, "Error detecting direction for " + name); + return null; + } + + String destString = stopObj.getString("dest"); + BusStop destStop = null; + + if (destString.equals("Central Station")) { + destStop = busStopDao.queryForId("SN120520"); + } else if (destString.equals("Civic Centre")) { + destStop = busStopDao.queryForId("SN120527"); + } else if (destString.equals("City DG4")) { + destStop = busStopDao.queryForId("HAA13579"); + } else if (destString.equals("Airport")) { + destStop = busStopDao.queryForId("HA030184"); + } else if (destString.equals("City, Town Quay")) { + destStop = busStopDao.queryForId("SNA13766"); + } else if (destString.equals("City Centre")) { + destStop = busStopDao.queryForId("SNA13766"); + } else if (destString.equals("Dock Gate 4")) { + destStop = busStopDao.queryForId("MG1031"); + } else if (destString.equals("Eastleigh")) { + destStop = busStopDao.queryForId("HA030212"); + } else if (destString.equals("Crematorium")) { + destStop = busStopDao.queryForId("SN121009"); + } else if (destString.equals("General Hosp")) { + destStop = busStopDao.queryForId("SNA19482"); + } else if (destString.equals("Wessex Lane")) { + destStop = busStopDao.queryForId("SNA19780"); + } else { + Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code); + } - return stop; - } + if (destStop != null) { - public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) - throws SQLException, ClientProtocolException, IOException, JSONException { + QueryBuilder routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, route.id).and() + .eq(RouteStops.STOP_ID_FIELD_NAME, destStop.id); + PreparedQuery routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); + + List routeStops = routeStopsDao.query(routeStopsPreparedQuery); + if (routeStops.size() > 0) { + Log.i(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); + } else { + Log.w(TAG, "Found " + routeStops.size() + " stops matching the destStop " + destStop + " on route " + + route.code); + } + } - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); + Date now = new Date(System.currentTimeMillis()); - Timetable timetable = new Timetable(); + String busID = null; + Stop stop; + Bus bus; + if (stopObj.has("vehicle")) { + busID = stopObj.getString("vehicle"); - String file = getFileFromServer(busStopUrl + busStop + ".json"); + QueryBuilder busQueryBuilder = busDao.queryBuilder(); + busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID); + PreparedQuery busPreparedQuery = busQueryBuilder.prepare(); - JSONObject data = new JSONObject(file); - JSONArray stopsArray = data.getJSONArray("stops"); - JSONObject routesObject = data.getJSONObject("routes"); + bus = busDao.queryForFirst(busPreparedQuery); - HashSet busRoutes = new HashSet(); - for (Iterator keyIter = routesObject.keys(); keyIter.hasNext();) { - String key = keyIter.next(); + if (bus == null) { + bus = new Bus(busID, route, dir); + bus.destination = destStop; + busDao.create(bus); + } else { + bus.destination = destStop; + bus.route = route; + bus.direction = dir; + busDao.update(bus); + } - Log.v(TAG, "Route Key: " + key); + } else { + bus = new Bus(null, route, dir); + busDao.create(bus); + } - BusRoute route = busRouteDao.queryForId(Integer.parseInt(key.substring(key.length() - 3, key.length()))); + stop = new Stop(bus, busStop, calender.getTime(), now, live); - if (route != null) { - busRoutes.add(route); - } else { - throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " - + key); - } + return stop; } - Log.i(TAG, "Number of entries " + data.length()); + public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) + throws SQLException, ClientProtocolException, IOException, JSONException { - Log.i(TAG, "Stops: " + data.getJSONArray("stops")); + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); + Timetable timetable = new Timetable(); - if (!keepNonUniLink && !stopObj.getString("name").startsWith("U")) { - Log.v(TAG, "Skipping non uni-link stop " + stopObj.getString("name")); - continue; - } + String file = getFileFromServer(busStopUrl + busStop + ".json"); - if (!keepUniLink && stopObj.getString("name").startsWith("U")) { - Log.v(TAG, "Skipping uni-link stop " + stopObj.getString("name")); - continue; - } + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); + JSONObject routesObject = data.getJSONObject("routes"); - BusStop busStopObj = busStopDao.queryForId(busStop); - if (busStopObj == null) { - Log.e(TAG, "BusStopObj == null"); - } + HashSet busRoutes = new HashSet(); + for (Iterator keyIter = routesObject.keys(); keyIter.hasNext();) { + String key = keyIter.next(); - Stop stop = getStop(context, stopObj, busRoutes, busStopObj); + Log.v(TAG, "Route Key: " + key); - if (stop == null) { - Log.w(TAG, "Null stop, skiping"); - continue; - } + BusRoute route = busRouteDao.queryForId(Integer.parseInt(key.substring(key.length() - 3, key.length()))); - Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " - + stop.arivalTime); + if (route != null) { + busRoutes.add(route); + } else { + throw new RuntimeException("Route not found " + key.substring(key.length() - 3, key.length()) + " " + + key); + } + } - timetable.add(stop); - } + Log.i(TAG, "Number of entries " + data.length()); - timetable.fetchTime = new Date(System.currentTimeMillis()); - return timetable; - } + Log.i(TAG, "Stops: " + data.getJSONArray("stops")); - public static Timetable getTimetable(Context context, Bus bus, BusStop startStop, int num) throws SQLException, - ClientProtocolException, IOException, JSONException { + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); + if (!keepNonUniLink && !stopObj.getString("name").startsWith("U")) { + Log.v(TAG, "Skipping non uni-link stop " + stopObj.getString("name")); + continue; + } - Timetable timetable = new Timetable(); + if (!keepUniLink && stopObj.getString("name").startsWith("U")) { + Log.v(TAG, "Skipping uni-link stop " + stopObj.getString("name")); + continue; + } - List busStops = new ArrayList(num); - busStops.add(startStop); + BusStop busStopObj = busStopDao.queryForId(busStop); + if (busStopObj == null) { + Log.e(TAG, "BusStopObj == null"); + } + + Stop stop = getStop(context, stopObj, busRoutes, busStopObj); - BusRoute route = bus.route; + if (stop == null) { + Log.w(TAG, "Null stop, skiping"); + continue; + } - for (int i = 0; i < num; i++) { - BusStop nextStop = route.moveInRoute(context, busStops.get(i), bus.direction, 1); + Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " + + stop.arivalTime); - if (nextStop != null) { - busStops.add(nextStop); - } else { - Log.e(TAG, "nextStop is null"); - } + timetable.add(stop); + } + + timetable.fetchTime = new Date(System.currentTimeMillis()); + return timetable; } - for (BusStop busStop : busStops) { + public static Timetable getTimetable(Context context, Bus bus, BusStop startStop, int num) throws SQLException, + ClientProtocolException, IOException, JSONException { + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + Timetable timetable = new Timetable(); + + List busStops = new ArrayList(num); + busStops.add(startStop); + + BusRoute route = bus.route; - String file = getFileFromServer(busStopUrl + busStop.id + ".json"); + for (int i = 0; i < num; i++) { + BusStop nextStop = route.moveInRoute(context, busStops.get(i), bus.direction, 1); - JSONObject data = new JSONObject(file); - JSONArray stopsArray = data.getJSONArray("stops"); + if (nextStop != null) { + busStops.add(nextStop); + } else { + Log.e(TAG, "nextStop is null"); + } + } + + for (BusStop busStop : busStops) { - HashSet busRoutes = new HashSet(); - busRoutes.add(bus.route); + String file = getFileFromServer(busStopUrl + busStop.id + ".json"); - Log.v(TAG, "Number of entries " + data.length()); + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); - Log.v(TAG, "Stops: " + data.getJSONArray("stops")); + HashSet busRoutes = new HashSet(); + busRoutes.add(bus.route); - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); + Log.v(TAG, "Number of entries " + data.length()); - if (stopObj.getString("vehicle").equals(bus.id)) { + Log.v(TAG, "Stops: " + data.getJSONArray("stops")); - Stop stop = getStop(context, stopObj, busRoutes, busStop); + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); - if (stop == null) { - Log.w(TAG, "Null stop, skiping"); - continue; - } + if (stopObj.getString("vehicle").equals(bus.id)) { - Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id - + " at " + stop.arivalTime); + Stop stop = getStop(context, stopObj, busRoutes, busStop); - timetable.add(stop); + if (stop == null) { + Log.w(TAG, "Null stop, skiping"); + continue; + } + Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + + " at " + stop.arivalTime); + + timetable.add(stop); + + } + } } - } - } - timetable.fetchTime = new Date(System.currentTimeMillis()); + timetable.fetchTime = new Date(System.currentTimeMillis()); - return timetable; - } + return timetable; + } - public static Stop getStop(Context context, Bus bus, BusStop busStop) throws SQLException, ClientProtocolException, - IOException, JSONException { + public static Stop getStop(Context context, Bus bus, BusStop busStop) throws SQLException, ClientProtocolException, + IOException, JSONException { - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); - String file = getFileFromServer(busStopUrl + busStop.id + ".json"); + String file = getFileFromServer(busStopUrl + busStop.id + ".json"); - JSONObject data = new JSONObject(file); - JSONArray stopsArray = data.getJSONArray("stops"); + JSONObject data = new JSONObject(file); + JSONArray stopsArray = data.getJSONArray("stops"); - HashSet busRoutes = new HashSet(); - busRouteDao.refresh(bus.route); - busRoutes.add(bus.route); + HashSet busRoutes = new HashSet(); + busRouteDao.refresh(bus.route); + busRoutes.add(bus.route); - Stop stop = null; + Stop stop = null; - // Log.v(TAG, "Number of entries " + data.length()); + // Log.v(TAG, "Number of entries " + data.length()); - // Log.v(TAG, "Stops: " + data.getJSONArray("stops")); + // Log.v(TAG, "Stops: " + data.getJSONArray("stops")); - for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { - JSONObject stopObj = stopsArray.getJSONObject(stopNum); + for (int stopNum = 0; stopNum < stopsArray.length(); stopNum++) { + JSONObject stopObj = stopsArray.getJSONObject(stopNum); - // Log.v(TAG, "stopObj: " + stopObj); - if (stopObj.has("vehicle") && stopObj.getString("vehicle").equals(bus.id)) { + // Log.v(TAG, "stopObj: " + stopObj); + if (stopObj.has("vehicle") && stopObj.getString("vehicle").equals(bus.id)) { - stop = getStop(context, stopObj, busRoutes, busStop); - break; + stop = getStop(context, stopObj, busRoutes, busStop); + break; - // Log.v(TAG, "Found stop for a unidentified " + stop.bus.toString() + " at " + stop.busStop.id + " at " - // + stop.arivalTime); - } + // Log.v(TAG, "Found stop for a unidentified " + + // stop.bus.toString() + " at " + stop.busStop.id + " at " + // + stop.arivalTime); + } + } + + return stop; } - return stop; - } + static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) { + PathOverlay data = null; - static PathOverlay getRoutePath(InputStream routeResource, int colour, ResourceProxy resProxy) { - PathOverlay data = null; + // sax stuff + try { + SAXParserFactory spf = SAXParserFactory.newInstance(); + SAXParser sp = spf.newSAXParser(); - // sax stuff - try { - SAXParserFactory spf = SAXParserFactory.newInstance(); - SAXParser sp = spf.newSAXParser(); + XMLReader xr = sp.getXMLReader(); - XMLReader xr = sp.getXMLReader(); + DataHandler dataHandler = new DataHandler(colour, resProxy); + xr.setContentHandler(dataHandler); - DataHandler dataHandler = new DataHandler(colour, resProxy); - xr.setContentHandler(dataHandler); + xr.parse(new InputSource(routeResource)); - xr.parse(new InputSource(routeResource)); + data = dataHandler.getData(); - data = dataHandler.getData(); + } catch (ParserConfigurationException pce) { + Log.e("SAX XML", "sax parse error", pce); + } catch (SAXException se) { + Log.e("SAX XML", "sax error", se); + } catch (IOException ioe) { + Log.e("SAX XML", "sax parse io error", ioe); + } - } catch (ParserConfigurationException pce) { - Log.e("SAX XML", "sax parse error", pce); - } catch (SAXException se) { - Log.e("SAX XML", "sax error", se); - } catch (IOException ioe) { - Log.e("SAX XML", "sax parse io error", ioe); + return data; } - return data; - } - - public static String getFileFromServer(String request) throws ClientProtocolException, IOException { - StringBuilder builder = new StringBuilder(); - HttpClient client = new DefaultHttpClient(); - HttpGet httpGet = new HttpGet(request); - Log.v("Util.getFileFromServer", "Request used: " + request); - - HttpResponse response = client.execute(httpGet); - StatusLine statusLine = response.getStatusLine(); - int statusCode = statusLine.getStatusCode(); - if (statusCode == 200) { - HttpEntity entity = response.getEntity(); - InputStream content = entity.getContent(); - BufferedReader reader = new BufferedReader(new InputStreamReader(content)); - String line; - while ((line = reader.readLine()) != null) { - builder.append(line); - } - } else { - Log.e("", "Failed to download file"); + public static String getFileFromServer(String request) throws ClientProtocolException, IOException { + StringBuilder builder = new StringBuilder(); + HttpClient client = new DefaultHttpClient(); + HttpGet httpGet = new HttpGet(request); + Log.v("Util.getFileFromServer", "Request used: " + request); + + HttpResponse response = client.execute(httpGet); + StatusLine statusLine = response.getStatusLine(); + int statusCode = statusLine.getStatusCode(); + if (statusCode == 200) { + HttpEntity entity = response.getEntity(); + InputStream content = entity.getContent(); + BufferedReader reader = new BufferedReader(new InputStreamReader(content)); + String line; + while ((line = reader.readLine()) != null) { + builder.append(line); + } + } else { + Log.e("", "Failed to download file"); + } + + return builder.toString(); } - return builder.toString(); - } - - public static void routeMovementTest(Context context) throws SQLException { - - Dao routeStopsDao = null; - - if (helper == null) - helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); - if (routeStopsDao == null) - routeStopsDao = helper.getRouteStopsDao(); - if (busRouteDao == null) - busRouteDao = helper.getBusRouteDao(); - if (busStopDao == null) - busStopDao = helper.getBusStopDao(); - - for (BusRoute busRoute : busRouteDao) { - if (!busRoute.code.startsWith("U") || busRoute.code.equals("U9")) { - continue; - } - List routeStops = routeStopsDao.queryForEq(RouteStops.ROUTE_ID_FIELD_NAME, busRoute.id); - - ArrayList directions = new ArrayList(); - if (busRoute.forwardDirection != null) { - directions.add(busRoute.forwardDirection); - } - if (busRoute.reverseDirection != null) { - directions.add(busRoute.reverseDirection); - } - if (directions.size() == 0) { - directions.add(null); - } - - BusStop startBusStop = null; - - for (int moveAmount = 1; moveAmount >= -1; moveAmount = moveAmount - 2) { - for (String direction : directions) { - for (int busStop = 0; busStop < routeStops.size() && busStop >= 0; busStop = busStop + moveAmount) { - // if (routeStops.get(busStop).stop.equals(startBusStop)) - // continue; - startBusStop = routeStops.get(busStop).busStop; - busStopDao.refresh(startBusStop); - - BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount); - - BusStop nextStop; - if (busStop == routeStops.size() - 1) { - nextStop = routeStops.get(0).busStop; - } else { - nextStop = routeStops.get(busStop + 1).busStop; + public static void routeMovementTest(Context context) throws SQLException { + + Dao routeStopsDao = null; + + if (helper == null) + helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + if (routeStopsDao == null) + routeStopsDao = helper.getRouteStopsDao(); + if (busRouteDao == null) + busRouteDao = helper.getBusRouteDao(); + if (busStopDao == null) + busStopDao = helper.getBusStopDao(); + + for (BusRoute busRoute : busRouteDao) { + if (!busRoute.code.startsWith("U") || busRoute.code.equals("U9")) { + continue; } - busStopDao.refresh(nextStop); + List routeStops = routeStopsDao.queryForEq(RouteStops.ROUTE_ID_FIELD_NAME, busRoute.id); - if (!nextStop.equals(predictedNextStop) && !startBusStop.equals(nextStop)) { - Log.e(TAG, "predicted: " + predictedNextStop + " next: " + nextStop); - Log.e(TAG, startBusStop.id + " " + nextStop.id); - return; + ArrayList directions = new ArrayList(); + if (busRoute.forwardDirection != null) { + directions.add(busRoute.forwardDirection); + } + if (busRoute.reverseDirection != null) { + directions.add(busRoute.reverseDirection); + } + if (directions.size() == 0) { + directions.add(null); + } + + BusStop startBusStop = null; + + for (int moveAmount = 1; moveAmount >= -1; moveAmount = moveAmount - 2) { + for (String direction : directions) { + for (int busStop = 0; busStop < routeStops.size() && busStop >= 0; busStop = busStop + moveAmount) { + // if + // (routeStops.get(busStop).stop.equals(startBusStop)) + // continue; + startBusStop = routeStops.get(busStop).busStop; + busStopDao.refresh(startBusStop); + + BusStop predictedNextStop = busRoute.moveInRoute(context, startBusStop, direction, moveAmount); + + BusStop nextStop; + if (busStop == routeStops.size() - 1) { + nextStop = routeStops.get(0).busStop; + } else { + nextStop = routeStops.get(busStop + 1).busStop; + } + busStopDao.refresh(nextStop); + + if (!nextStop.equals(predictedNextStop) && !startBusStop.equals(nextStop)) { + Log.e(TAG, "predicted: " + predictedNextStop + " next: " + nextStop); + Log.e(TAG, startBusStop.id + " " + nextStop.id); + return; + } + } + } } - } } - } - } - } + } } -- cgit v1.2.3