diff options
-rw-r--r-- | src/net/cbaines/suma/BusRoute.java | 87 | ||||
-rw-r--r-- | src/net/cbaines/suma/SouthamptonUniversityMapActivity.java | 6 |
2 files changed, 86 insertions, 7 deletions
diff --git a/src/net/cbaines/suma/BusRoute.java b/src/net/cbaines/suma/BusRoute.java index 94e8e37..ed6deeb 100644 --- a/src/net/cbaines/suma/BusRoute.java +++ b/src/net/cbaines/suma/BusRoute.java @@ -20,7 +20,10 @@ package net.cbaines.suma; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import android.content.Context; import android.util.Log; @@ -117,7 +120,87 @@ public class BusRoute { * @param moveAmount * @return */ - BusStop moveInRoute(final Context context, final BusStop busStop, String direction, int moveAmount) { + Set<BusStop> moveInRoute(final Context context, final BusStop busStop, final int moveAmount) { + + if (!code.startsWith("U") || code.equals("U9")) { + throw new RuntimeException("Route Movement not supported for " + code); + } + + Set<BusStop> busStops = new HashSet<BusStop>(); + + if (moveAmount == 0) { + busStops.add(busStop); + return busStops; + } + + DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class); + + try { + Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao(); + Dao<BusStop, String> busStopDao = helper.getBusStopDao(); + + QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder(); + routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id); + PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare(); + + List<RouteStops> routeStopsFound = routeStopsDao.query(routeStopsPreparedQuery); + + ArrayList<Integer> stopIndexs = new ArrayList<Integer>(); + + for (RouteStops routeStop : routeStopsFound) { + if (routeStop.stop.id.equals(busStop.id)) { + stopIndexs.add(routeStop.sequence - 1); + } + } + + for (int stopIndex : stopIndexs) { + + if (moveAmount > 0) { + Log.v(TAG, "Moving forward " + moveAmount + " stops from " + busStop + " (" + stopIndex + "/" + routeStopsFound.size() + ")"); + int stopWanted = stopIndex + moveAmount; + if ((stopWanted + 1) > routeStopsFound.size()) { + Log.v(TAG, "Off the end of the route"); + stopWanted = stopWanted % (routeStopsFound.size() - 1); + } + Log.v(TAG, " Stop wanted " + stopWanted); + BusStop busStopWanted = routeStopsFound.get(stopWanted).stop; + + busStopDao.refresh(busStopWanted); + + Log.v(TAG, " Moving to " + busStopWanted + " (" + stopWanted + ") in route " + this); + + busStops.add(busStopWanted); + } else { + Log.v(TAG, "stopIndex " + stopIndex); + int stopWanted = stopIndex + moveAmount; + if (stopWanted < 0) { + stopWanted = routeStopsFound.size() - (Math.abs(stopWanted) % routeStopsFound.size()); + } + Log.v(TAG, "stopWanted " + stopWanted); + busStopDao.refresh(routeStopsFound.get(stopWanted).stop); + + Log.v(TAG, "Moving backwards " + moveAmount + " stops from " + busStop + " to " + routeStopsFound.get(stopWanted).stop + " in route " + + this); + + busStops.add(routeStopsFound.get(stopWanted).stop); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + Log.e(TAG, "Error moving in route"); + return null; + } + + /** + * Untested? + * + * @param context + * @param busStop + * @param moveAmount + * @return + */ + BusStop moveInRoute(final Context context, final BusStop busStop, String direction, final int moveAmount) { if (!code.startsWith("U") || code.equals("U9")) { throw new RuntimeException("Route Movement not supported for " + code); @@ -135,6 +218,8 @@ public class BusRoute { if (direction.equals("E")) direction = "A"; // Quick hack for U1E + } else { + throw new NullPointerException("direction is null"); } } diff --git a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java index d4754a4..26996a0 100644 --- a/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java +++ b/src/net/cbaines/suma/SouthamptonUniversityMapActivity.java @@ -384,12 +384,6 @@ public class SouthamptonUniversityMapActivity extends OrmLiteBaseActivity<Databa Log.i(TAG, "Begining setting up the static values " + (System.currentTimeMillis() - startTime)); - try { - DataManager.routeMovementTest(this); - } catch (SQLException e) { - e.printStackTrace(); - } - Log.i(TAG, "Finished the database thread " + (System.currentTimeMillis() - startTime)); } |