aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusRoute.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/net/cbaines/suma/BusRoute.java')
-rw-r--r--src/net/cbaines/suma/BusRoute.java361
1 files changed, 323 insertions, 38 deletions
diff --git a/src/net/cbaines/suma/BusRoute.java b/src/net/cbaines/suma/BusRoute.java
index 5af89e7..4152605 100644
--- a/src/net/cbaines/suma/BusRoute.java
+++ b/src/net/cbaines/suma/BusRoute.java
@@ -20,7 +20,11 @@
package net.cbaines.suma;
import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import android.content.Context;
import android.util.Log;
@@ -41,6 +45,8 @@ import com.j256.ormlite.table.DatabaseTable;
@DatabaseTable(tableName = "busroutes")
public class BusRoute {
+ private static final String TAG = "BusRoute";
+
final static String ID_FIELD_NAME = "id";
final static String CODE_FIELD_NAME = "code";
final static String LABEL_FIELD_NAME = "label";
@@ -57,25 +63,46 @@ public class BusRoute {
@DatabaseField(canBeNull = false)
String label;
+ /**
+ * The direction the bus is travelling if it is moving through the route and sequence is increasing.
+ * <ul>
+ * <li>U1 = A</li>
+ * <li>U2 = B</li>
+ * <li>U6 = H</li>
+ * </ul>
+ */
@DatabaseField(canBeNull = true)
String forwardDirection;
+ /**
+ * The direction the bus is travelling if it is moving through the route and sequence is decreasing.
+ * <ul>
+ * <li>U1 = C</li>
+ * <li>U2 = C</li>
+ * <li>U6 = C</li>
+ * </ul>
+ */
@DatabaseField(canBeNull = true)
String reverseDirection;
+ @DatabaseField(canBeNull = false)
+ boolean uniLink;
+
BusRoute() {
}
- public BusRoute(Integer id, String code, String label, String forwardDirection, String reverseDirection) {
+ public BusRoute(Integer id, String code, String label, String forwardDirection, String reverseDirection,
+ boolean uniLink) {
this.id = id.intValue();
this.code = code;
this.label = label;
this.forwardDirection = forwardDirection;
this.reverseDirection = reverseDirection;
+ this.uniLink = uniLink;
}
- public BusRoute(Integer id, String code, String label) {
- this(id, code, label, null, null);
+ public BusRoute(Integer id, String code, String label, boolean uniLink) {
+ this(id, code, label, null, null, uniLink);
}
public String toString() {
@@ -108,33 +135,107 @@ public class BusRoute {
* Untested?
*
* @param context
- * @param stop
+ * @param busStop
* @param moveAmount
* @return
*/
- BusStop moveInRoute(Context context, BusStop stop, String direction, int moveAmount) {
+ Set<BusStop> moveInRoute(final Context context, final BusStop busStop, final int moveAmount) {
+
+ Set<BusStop> busStops = new HashSet<BusStop>();
+
if (moveAmount == 0) {
- return stop;
+ busStops.add(busStop);
+ return busStops;
}
DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
- if (forwardDirection != null) {
+ 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);
- if (direction == null) {
- return null;
+ ArrayList<Integer> stopIndexs = new ArrayList<Integer>();
+
+ for (RouteStops routeStop : routeStopsFound) {
+ if (routeStop.busStop.id.equals(busStop.id)) {
+ stopIndexs.add(routeStop.sequence - 1);
+ }
}
- if (forwardDirection.equals(direction)) {
+ 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).busStop;
+
+ 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).busStop);
+
+ Log.v(TAG,
+ "Moving backwards " + moveAmount + " stops from " + busStop + " to "
+ + routeStopsFound.get(stopWanted).busStop + " in route " + this);
+
+ busStops.add(routeStopsFound.get(stopWanted).busStop);
+ }
+ }
- } else if (reverseDirection.equals(direction)) {
- moveAmount = -moveAmount;
+ return busStops;
+ } 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 (moveAmount == 0) {
+ return busStop;
+ }
+
+ DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
+
+ if (forwardDirection != null) {
+
+ if (direction != null) {
+
+ if (direction.equals("E"))
+ direction = "A"; // Quick hack for U1E
} else {
- Log.e("BusRoute", "Direction (" + direction + ") doesnt match either the forward direction (" + forwardDirection + ") or reverse direction ("
- + reverseDirection + ")");
- return null;
+ throw new NullPointerException("direction is null");
}
-
}
try {
@@ -146,48 +247,232 @@ public class BusRoute {
PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();
List<RouteStops> routeStopsFound = routeStopsDao.query(routeStopsPreparedQuery);
- Log.v("BusRoute", "Found " + routeStopsFound.size() + " stops");
-
- int stopIndex = 0;
-
+
+ int stopIndex = -1;
+
for (RouteStops routeStop : routeStopsFound) {
- if (routeStop.stop.id.equals(stop.id)) {
- stopIndex = routeStop.sequence -1;
+ if (routeStop.busStop.id.equals(busStop.id)) {
+ if (stopIndex == -1) {
+ stopIndex = routeStop.sequence - 1;
+ } else { // ARGH, weird route
+ if (busStop.id.equals("HAA13651") && id == 327) { // U6 by Wessex Lane
+ if (direction.equals(forwardDirection)) {
+ stopIndex = 23;
+ } else {
+ stopIndex = 68;
+ }
+ } else if (busStop.id.equals("SN120134") && id == 327) { // U6 opposite the Stile
+ if (direction.equals(forwardDirection)) {
+ stopIndex = 30;
+ } else {
+ stopIndex = 59;
+ }
+ } else if (busStop.id.equals("SN120163") && id == 327) { // U6 just up past wessex lane
+ if (direction.equals(forwardDirection)) {
+ stopIndex = 22;
+ } else {
+ stopIndex = 67;
+ }
+ } else if (busStop.id.equals("SNA19482") && id == 327) { // U6 General Hosp West Door
+ if (moveAmount > 0) {
+ stopIndex = 44;
+ } else {
+ stopIndex = 43;
+ }
+ } else if (busStop.id.equals("SN120134") && id == 329) { // U2 opposite the Stile
+ if (direction.equals(forwardDirection)) {
+ stopIndex = 13;
+ } else {
+ stopIndex = 30;
+ }
+ } else if (busStop.id.equals("SN120527") && id == 329) { // U2 Civic Centre Rd os stop AO Civic
+ // Ctr E
+ if (moveAmount > 0) {
+ stopIndex = 0;
+ } else {
+ stopIndex = 42;
+ }
+ } else if (busStop.id.equals("SNA09298") && id == 329) { // U2 Bassett Green Rd nr Bassett Green
+ // Cl SE
+ if (moveAmount > 0) {
+ stopIndex = 22;
+ } else {
+ stopIndex = 21;
+ }
+ } else if (busStop.id.equals("SN120520") && id == 326) { // U1 By the station
+ if (direction.equals(forwardDirection)) {
+ stopIndex = 7;
+ } else {
+ stopIndex = 80;
+ }
+ } else if (busStop.id.equals("HA030183") && id == 326) { // U1 Up past Wessex Lane
+ if (direction.equals(forwardDirection)) {
+ stopIndex = 35;
+ } else {
+ stopIndex = 50;
+ }
+ } else if (busStop.id.equals("HA030212") && id == 326) { // U1 At Eastleigh
+ if (moveAmount > 0) {
+ stopIndex = 43;
+ } else {
+ stopIndex = 42;
+ }
+ } else if (busStop.id.equals("SN120171") && id == 354) { // U9
+ if (moveAmount > 0) {
+ stopIndex = 0;
+ } else {
+ stopIndex = 73;
+ }
+ } else {
+ Log.e(TAG, "Error, unknown bus stop " + busStop.id + " (" + busStop.description
+ + ") that appears mutiple times in " + toString());
+ throw new RuntimeException("Error, unknown bus stop " + busStop.id
+ + " that appears mutiple times in " + toString());
+ }
+ Log.v(TAG, "Selecting " + stopIndex + " for " + busStop.id + " as direction == " + direction);
+ }
}
}
if (moveAmount > 0) {
- Log.v("BusStop", "stopIndex " + stopIndex);
- int stopWanted = (stopIndex + moveAmount) % (routeStopsFound.size() + 1);
- Log.v("BusStop", "stopWanted " + stopWanted);
- busStopDao.refresh(routeStopsFound.get(stopWanted).stop);
+ 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).busStop;
+
+ busStopDao.refresh(busStopWanted);
- Log.v("BusRoute",
- "Moving forward in direction " + direction + " " + moveAmount + " stops from " + stop + " to " + routeStopsFound.get(stopWanted).stop
- + " in route " + this);
+ Log.v(TAG, " Moving to " + busStopWanted + " (" + stopWanted + ") in route " + this);
- return routeStopsFound.get(stopWanted).stop;
+ return busStopWanted;
} else {
- Log.v("BusStop", "stopIndex " + stopIndex);
+ Log.v(TAG, "stopIndex " + stopIndex);
int stopWanted = stopIndex + moveAmount;
if (stopWanted < 0) {
stopWanted = routeStopsFound.size() - (Math.abs(stopWanted) % routeStopsFound.size());
}
- Log.v("BusStop", "stopWanted " + stopWanted);
- busStopDao.refresh(routeStopsFound.get(stopWanted).stop);
+ Log.v(TAG, "stopWanted " + stopWanted);
+ busStopDao.refresh(routeStopsFound.get(stopWanted).busStop);
+
+ Log.v(TAG,
+ "Moving backwards " + moveAmount + " stops from " + busStop + " to "
+ + routeStopsFound.get(stopWanted).busStop + " in route " + this);
+
+ return routeStopsFound.get(stopWanted).busStop;
+ }
+
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ Log.e(TAG, "Error moving in route");
+ return null;
+ }
+
+ /**
+ * Untested?
+ *
+ * @param context
+ * @param busStop
+ * @param moveAmount
+ * @return
+ */
+ List<BusStop> getRouteSection(final Context context, String direction) {
+
+ DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
+
+ if (forwardDirection != null) {
+
+ if (direction != null) {
+
+ if (direction.equals("E"))
+ direction = "A"; // Quick hack for U1E
+ } else {
+ throw new NullPointerException("direction is null");
+ }
+ }
- Log.v("BusRoute",
- "Moving backwards in direction " + direction + " " + moveAmount + " stops from " + stop + " to " + routeStopsFound.get(stopWanted).stop
- + " in route " + this);
+ List<BusStop> busStops = new ArrayList<BusStop>();
- return routeStopsFound.get(stopWanted).stop;
+ 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);
+
+ int startStopSeq = -1;
+ int endStopSeq = -1;
+
+ if (id == 326) { // U1
+ if (direction.equals(forwardDirection)) {
+ startStopSeq = 1;
+ endStopSeq = 43;
+ } else if (direction.equals(reverseDirection)) {
+ startStopSeq = 44;
+ endStopSeq = 88;
+ } else {
+ Log.e(TAG, "Error, unrecognised direction " + direction);
+ }
+ } else if (id == 468) { // U1N
+ startStopSeq = 1;
+ endStopSeq = 29;
+ } else if (id == 329) { // U2
+ if (direction.equals(forwardDirection)) {
+ startStopSeq = 1;
+ endStopSeq = 22;
+ } else if (direction.equals(reverseDirection)) {
+ startStopSeq = 23;
+ endStopSeq = 43;
+ } else {
+ Log.e(TAG, "Error, unrecognised direction " + direction);
+ }
+ } else if (id == 327) { // U6
+ if (direction.equals(forwardDirection)) {
+ startStopSeq = 1;
+ endStopSeq = 44;
+ } else if (direction.equals(reverseDirection)) {
+ startStopSeq = 45;
+ endStopSeq = 93;
+ } else {
+ Log.e(TAG, "Error, unrecognised direction " + direction);
+ }
+ } else if (id == 354) { // U9
+ Calendar rightNow = Calendar.getInstance();
+ if (rightNow.get(Calendar.HOUR_OF_DAY) < 12) {
+ startStopSeq = 1;
+ endStopSeq = 40; // TODO: Guess, and untested
+ } else {
+ startStopSeq = 41; // TODO: Guess, and untested
+ endStopSeq = 74;
+ }
+ } else {
+ Log.e(TAG, "Error, unrecognised route " + id);
}
+ for (RouteStops routeStop : routeStopsFound) {
+ if (routeStop.sequence >= startStopSeq && routeStop.sequence <= endStopSeq) {
+ busStopDao.refresh(routeStop.busStop);
+ busStops.add(routeStop.busStop);
+ }
+ }
+
+ return busStops;
+
} catch (SQLException e) {
e.printStackTrace();
}
- Log.e("BusRoute", "Error moving in route");
+ Log.e(TAG, "Error moving in route");
return null;
+
}
@Override