aboutsummaryrefslogtreecommitdiff
path: root/src/net/cbaines/suma/BusRoute.java
blob: 016f3bde1773c4fdf6c050da1833a653dcec63b6 (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
/*
 * 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.sql.SQLException;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "busroutes")
/**
 * This class represents a bus route (U1, U1N, ..). 
 * 
 * @author Christopher Baines <cbaines8@gmail.com>
 *
 */
public class BusRoute {

    final static String ID_FIELD_NAME = "id";
    final static String CODE_FIELD_NAME = "code";
    final static String LABEL_FIELD_NAME = "label";

    private static final String TAG = "BusRoute";

    @DatabaseField(id = true)
    int id;

    @DatabaseField
    String code;

    @DatabaseField
    String label;

    BusRoute() {
    }

    BusRoute(Integer id, String code, String label) {
	this.id = id.intValue();
	this.code = code;
	this.label = label;
    }

    public String toString() {
	return code;
    }

    /**
     * Untested?
     * 
     * @param context
     * @param stop
     * @return
     */
    BusStop getBusStopBefore(Context context, Stop stop) {
	return moveInRoute(context, stop, -1);
    }

    /**
     * Untested?
     * 
     * @param context
     * @param stop
     * @return
     */
    BusStop getStopAfter(Context context, Stop stop) {
	return moveInRoute(context, stop, 1);
    }

    /**
     * Untested?
     * 
     * @param context
     * @param stop
     * @param moveAmount
     * @return
     */
    BusStop moveInRoute(Context context, Stop stop, int moveAmount) {
	DatabaseHelper helper = OpenHelperManager.getHelper(context, DatabaseHelper.class);

	try {
	    Dao<RouteStops, Integer> routeStopsDao = helper.getRouteStopsDao();
	    // Dao<Stop, Integer> stopDao = helper.getStopDao();
	    Dao<BusStop, String> busStopDao = helper.getBusStopDao();

	    int stopSeq;
	    int beforeStopSeq = -1;

	    QueryBuilder<RouteStops, Integer> routeStopsQueryBuilder = routeStopsDao.queryBuilder();
	    routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id).and().eq(RouteStops.STOP_ID_FIELD_NAME, stop.busStop.id);
	    PreparedQuery<RouteStops> routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();

	    List<RouteStops> stopsFound = routeStopsDao.query(routeStopsPreparedQuery);
	    if (stopsFound.size() != 0) {
		Log.e(TAG, "Wierd, found more than one stop");
		return null;
	    }

	    long maxSeq = 0;

	    routeStopsQueryBuilder = routeStopsDao.queryBuilder();
	    routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id);
	    routeStopsQueryBuilder.setCountOf(true);
	    routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();

	    maxSeq = routeStopsDao.countOf(routeStopsPreparedQuery);

	    if (maxSeq == 0) {
		Log.e(TAG, "Something wierd has gone on, maxSeq equals 0");
		return null;
	    }

	    if (id == 326) { // U1

		if (stop.name.equals("U1C")) {// Seq 0 = End of route

		    stopSeq = routeStopsDao.query(routeStopsPreparedQuery).get(0).sequence;

		    beforeStopSeq = stopSeq + moveAmount;

		} else if (stop.name.equals("U1A")) { // seq 88 == end of route

		    stopSeq = routeStopsDao.query(routeStopsPreparedQuery).get(0).sequence;

		    beforeStopSeq = stopSeq - moveAmount;

		} else {
		    Log.e(TAG, "In route U1 but " + stop.name + " does not match U1A or U1C");
		    return null;
		}
	    } else {
		Log.e(TAG, "Route id not recognised " + id);
		return null;
	    }

	    if (beforeStopSeq == -1) {
		Log.e(TAG, "Something wierd has gone on, beforeStopSeq equals -1");
		return null;
	    }

	    routeStopsQueryBuilder = routeStopsDao.queryBuilder();
	    routeStopsQueryBuilder.where().eq(RouteStops.ROUTE_ID_FIELD_NAME, this.id).and().eq(RouteStops.SEQUENCE_ID_FIELD_NAME, beforeStopSeq);

	    routeStopsPreparedQuery = routeStopsQueryBuilder.prepare();

	    List<RouteStops> beforeStopsFound = routeStopsDao.query(routeStopsPreparedQuery);
	    if (stopsFound.size() != 0) {
		Log.e(TAG, "Wierd, found more than one before stop");
		return null;
	    }

	    busStopDao.refresh(beforeStopsFound.get(0).stop);

	    return beforeStopsFound.get(0).stop;

	} catch (SQLException e) {
	    e.printStackTrace();
	    return null;
	}
    }

}