aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/DataManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/DataManager.java')
-rw-r--r--src/net/cbaines/suma/DataManager.java214
1 files changed, 102 insertions, 112 deletions
diff --git a/src/net/cbaines/suma/DataManager.java b/src/net/cbaines/suma/DataManager.java
index aad8b86..f642d56 100644
--- a/src/net/cbaines/suma/DataManager.java
+++ b/src/net/cbaines/suma/DataManager.java
@@ -424,7 +424,7 @@ public class DataManager {
Log.i(TAG, "Loaded sites from csv");
}
- private static Stop getStop(Context context, JSONObject stopObj, BusStop busStop) throws SQLException {
+ private static Stop getStop(Context context, JSONObject stopObj, BusStop busStop) throws SQLException, JSONException {
if (helper == null)
helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
@@ -435,145 +435,135 @@ public class DataManager {
if (busStopDao == null)
busStopDao = helper.getBusStopDao();
- try {
- String time = stopObj.getString("time");
-
- GregorianCalendar calender = new GregorianCalendar();
- boolean live = true;
- if (!time.equals("Due")) {
+ String time = stopObj.getString("time");
- Log.v(TAG, "Time: " + time + " current time " + calender.getTime());
+ GregorianCalendar calender = new GregorianCalendar();
+ boolean live = true;
+ if (!time.equals("Due")) {
- 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, "Time: " + time + " current time " + calender.getTime());
- Log.v(TAG, "Date: " + 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 name = stopObj.getString("name");
+ Log.v(TAG, "Date: " + calender.getTime());
+ }
- BusRoute route;
- String dir = null;
+ String name = stopObj.getString("name");
- if (name.contains("U")) {
- if (name.equals("U1N")) {
- route = busRoutes.queryForId(468);
- } else if (name.startsWith("U9")) {
- route = busRoutes.queryForId(354);
- } else {
- if (name.startsWith("U1")) {
- route = busRoutes.queryForId(326);
- } else if (name.startsWith("U2")) {
- route = busRoutes.queryForId(329);
- } else if (name.startsWith("U6")) {
- route = busRoutes.queryForId(327);
- } else {
- Log.e(TAG, "Error finding Uni-Link route " + name);
- return null;
- }
+ BusRoute route;
+ String dir = null;
- 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);
- return null;
- }
+ if (name.contains("U")) {
+ if (name.equals("U1N")) {
+ route = busRoutes.queryForId(468);
+ } else if (name.startsWith("U9")) {
+ route = busRoutes.queryForId(354);
+ } else {
+ if (name.startsWith("U1")) {
+ route = busRoutes.queryForId(326);
+ } else if (name.startsWith("U2")) {
+ route = busRoutes.queryForId(329);
+ } else if (name.startsWith("U6")) {
+ route = busRoutes.queryForId(327);
+ } else {
+ throw new RuntimeException("Error finding Uni-Link route " + name);
}
- } else {
- Log.v(TAG, "Route not Uni-Link");
- List<BusRoute> routes = (List<BusRoute>) busRoutes.queryForEq(BusRoute.CODE_FIELD_NAME, name);
- if (routes.size() != 1) {
- Log.e(TAG, "Found more than one non Uni-Link route?");
- for (BusRoute routeT : routes) {
- Log.i(TAG, "Route: " + routeT);
- }
- return null;
+ if (route.forwardDirection.equals(name.substring(2))) {
+ dir = route.forwardDirection;
+ } else if (route.reverseDirection.equals(name.substring(2))) {
+ dir = route.reverseDirection;
} else {
- route = routes.get(0);
- if (route == null) {
- Log.e(TAG, "Could not find database entry for " + name);
- return null;
- }
+ throw new RuntimeException("Error detecting direction for " + name);
}
}
- String destString = stopObj.getString("dest");
- BusStop destStop;
-
- if (destString.equals("Central Station")) {
- destStop = busStopDao.queryForId("SNA19709");
- } else if (destString.equals("Civic Centre")) {
- destStop = busStopDao.queryForId("SN120527");
- } else if (destString.equals("City DG4")) {
- destStop = busStopDao.queryForId("HAA13579");
- } else if (destString.equals("Central Station")) {
- destStop = busStopDao.queryForId("SN120520");
- } else if (destString.equals("Airport")) {
- destStop = busStopDao.queryForId("HA030184");
- } else if (destString.equals("City, Town Quay")) {
- 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 {
+ Log.v(TAG, "Route not Uni-Link");
+ List<BusRoute> routes = (List<BusRoute>) busRoutes.queryForEq(BusRoute.CODE_FIELD_NAME, name);
+ if (routes.size() != 1) {
+ Log.e(TAG, "Found more than one non Uni-Link route?");
+ for (BusRoute routeT : routes) {
+ Log.i(TAG, "Route: " + routeT);
+ }
+ throw new RuntimeException("Found more than one non Uni-Link route?");
} else {
- Log.e(TAG, "Unknown end dest " + destString + " for route " + route.code);
- return null;
+ route = routes.get(0);
+ if (route == null) {
+ throw new RuntimeException("Could not find database entry for " + name);
+ }
}
+ }
- Date now = new Date(System.currentTimeMillis());
+ String destString = stopObj.getString("dest");
+ BusStop destStop;
+
+ if (destString.equals("Central Station")) {
+ destStop = busStopDao.queryForId("SNA19709");
+ } else if (destString.equals("Civic Centre")) {
+ destStop = busStopDao.queryForId("SN120527");
+ } else if (destString.equals("City DG4")) {
+ destStop = busStopDao.queryForId("HAA13579");
+ } else if (destString.equals("Central Station")) {
+ destStop = busStopDao.queryForId("SN120520");
+ } 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 {
+ throw new RuntimeException("Unknown end dest " + destString + " for route " + route.code);
+ }
- String busID = null;
- Stop stop;
- Bus bus;
- if (stopObj.has("vehicle")) {
- busID = stopObj.getString("vehicle");
+ Date now = new Date(System.currentTimeMillis());
- QueryBuilder<Bus, Integer> busQueryBuilder = busDao.queryBuilder();
- busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID);
- PreparedQuery<Bus> busPreparedQuery = busQueryBuilder.prepare();
+ String busID = null;
+ Stop stop;
+ Bus bus;
+ if (stopObj.has("vehicle")) {
+ busID = stopObj.getString("vehicle");
- bus = busDao.queryForFirst(busPreparedQuery);
+ QueryBuilder<Bus, Integer> busQueryBuilder = busDao.queryBuilder();
+ busQueryBuilder.where().eq(Bus.ID_FIELD_NAME, busID);
+ PreparedQuery<Bus> busPreparedQuery = busQueryBuilder.prepare();
- if (bus == null) {
- bus = new Bus(busID, route, dir);
- bus.destination = destStop;
- } else {
- bus.destination = destStop;
- bus.route = route;
- bus.direction = dir;
- }
+ bus = busDao.queryForFirst(busPreparedQuery);
+ if (bus == null) {
+ bus = new Bus(busID, route, dir);
+ bus.destination = destStop;
} else {
- bus = new Bus(null, route, dir);
+ bus.destination = destStop;
+ bus.route = route;
+ bus.direction = dir;
}
- busDao.update(bus);
-
- stop = new Stop(bus, busStop, calender.getTime(), now, live);
+ } else {
+ bus = new Bus(null, route, dir);
+ }
- return stop;
+ busDao.update(bus);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- Log.e(TAG, "Error parsing stop " + stopObj, e);
- return null;
- }
+ stop = new Stop(bus, busStop, calender.getTime(), now, live);
+ return stop;
}
public static Timetable getTimetable(Context context, String busStop, boolean keepUniLink, boolean keepNonUniLink) throws SQLException,