take into account exception types from calendar_dates and modify the queries accordingly
This commit is contained in:
parent
0a6d8fdab5
commit
ea62c0050f
Binary file not shown.
Binary file not shown.
|
@ -6,8 +6,8 @@ import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
|
|||
|
||||
public class ChooChooDatabase extends SQLiteAssetHelper {
|
||||
|
||||
private static final String DATABASE_NAME = "caltrain-10.01.2017.db";
|
||||
private static final int DATABASE_VERSION = 4;
|
||||
private static final String DATABASE_NAME = "caltrain-10.01.2017.v3.db";
|
||||
private static final int DATABASE_VERSION = 6;
|
||||
|
||||
public ChooChooDatabase(Context context) {
|
||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
|
|
|
@ -2,55 +2,101 @@ package com.eleith.calchoochoo.utils;
|
|||
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.support.v4.util.Pair;
|
||||
import android.text.TextUtils;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import com.google.android.gms.common.internal.safeparcel.SafeParcelable;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.LocalDateTime;
|
||||
import org.joda.time.format.DateTimeFormat;
|
||||
import org.joda.time.format.DateTimeFormatter;
|
||||
import org.w3c.dom.Text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CalendarDateUtils {
|
||||
public final static DateTimeFormatter formatter = DateTimeFormat.forPattern("YYYYMMdd");
|
||||
|
||||
public static String getCalendarFilter(SQLiteDatabase db, Long dateTime) {
|
||||
String calendarFilter;
|
||||
int dayOfWeek = isExceptionDate(db, dateTime) ? 0 : new DateTime(dateTime).getDayOfWeek();
|
||||
public static String getFilterForDate(SQLiteDatabase db, Long dateTime) {
|
||||
String calendarFilter = null;
|
||||
String calendarExceptionAdded = null;
|
||||
String calendarExceptionRemoved = null;
|
||||
SparseArray<ArrayList<String>> exceptions = getExceptionsForDate(db, dateTime);
|
||||
int dayOfWeek = new DateTime(dateTime).getDayOfWeek();
|
||||
switch (dayOfWeek) {
|
||||
case 1:
|
||||
calendarFilter = " AND calendar.monday = 1 ";
|
||||
calendarFilter = " calendar.monday = 1 ";
|
||||
break;
|
||||
case 2:
|
||||
calendarFilter = " AND calendar.tuesday = 1 ";
|
||||
calendarFilter = " calendar.tuesday = 1 ";
|
||||
break;
|
||||
case 3:
|
||||
calendarFilter = " AND calendar.wednesday = 1 ";
|
||||
calendarFilter = " calendar.wednesday = 1 ";
|
||||
break;
|
||||
case 4:
|
||||
calendarFilter = " AND calendar.thursday = 1 ";
|
||||
calendarFilter = " calendar.thursday = 1 ";
|
||||
break;
|
||||
case 5:
|
||||
calendarFilter = " AND calendar.friday = 1 ";
|
||||
calendarFilter = " calendar.friday = 1 ";
|
||||
break;
|
||||
case 6:
|
||||
calendarFilter = " AND calendar.saturday = 1 ";
|
||||
calendarFilter = " calendar.saturday = 1 ";
|
||||
break;
|
||||
case 7:
|
||||
calendarFilter = " AND calendar.saturday = 1 ";
|
||||
break;
|
||||
default:
|
||||
calendarFilter = " AND calendar.sunday = 1 ";
|
||||
calendarFilter = " calendar.sunday = 1 ";
|
||||
break;
|
||||
}
|
||||
return calendarFilter;
|
||||
|
||||
for(Integer i = 0; i < exceptions.size(); i++) {
|
||||
Integer exception = exceptions.keyAt(i);
|
||||
ArrayList<String> service_ids = exceptions.valueAt(i);
|
||||
|
||||
if (exception == 1) {
|
||||
calendarExceptionAdded = TextUtils.join(",", service_ids);
|
||||
} else if (exception == 2) {
|
||||
calendarExceptionRemoved = TextUtils.join(",", service_ids);
|
||||
}
|
||||
}
|
||||
|
||||
if (calendarExceptionAdded != null) {
|
||||
calendarFilter += " OR trips.service_id in (" + calendarExceptionAdded + ")";
|
||||
}
|
||||
|
||||
if (calendarExceptionRemoved != null) {
|
||||
calendarFilter = "(" + calendarFilter + ")" + " AND trips.service_id not in (" + calendarExceptionRemoved + ")";
|
||||
}
|
||||
|
||||
return " AND (" + calendarFilter + ") ";
|
||||
}
|
||||
|
||||
public static boolean isExceptionDate(SQLiteDatabase db, Long dateTimeString) {
|
||||
String[] projection = {"date"};
|
||||
String[] selectionArgs = {Long.toString(dateTimeString)};
|
||||
public static SparseArray<ArrayList<String>> getExceptionsForDate(SQLiteDatabase db, Long dateTimeLong) {
|
||||
LocalDateTime dateTime = new LocalDateTime(dateTimeLong);
|
||||
String[] projection = {"service_id", "date", "exception_type"};
|
||||
String[] selectionArgs = {formatter.print(dateTime)};
|
||||
SparseArray<ArrayList<String>> exceptions = new SparseArray<>();
|
||||
|
||||
Cursor cursor = db.query("calendar_dates", projection, "date = ?", selectionArgs, null, null, null);
|
||||
|
||||
if (cursor != null) {
|
||||
Boolean isException = cursor.getCount() > 0;
|
||||
cursor.close();
|
||||
return isException;
|
||||
} else {
|
||||
return false;
|
||||
while (cursor.moveToNext()) {
|
||||
String serviceId = "'" + cursor.getString(cursor.getColumnIndex("service_id")) + "'";
|
||||
Integer exception = cursor.getInt(cursor.getColumnIndex("exception_type"));
|
||||
ArrayList<String> serviceIds = exceptions.get(exception);
|
||||
|
||||
if (serviceIds != null) {
|
||||
serviceIds.add(serviceId);
|
||||
} else {
|
||||
serviceIds = new ArrayList<>();
|
||||
serviceIds.add(serviceId);
|
||||
}
|
||||
|
||||
exceptions.put(exception, serviceIds);
|
||||
}
|
||||
|
||||
return exceptions;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,8 +64,9 @@ public class PossibleTrainUtils {
|
|||
"WHERE trips.trip_id = st1.trip_id " +
|
||||
" AND trips.route_id = routes.route_id " +
|
||||
" AND calendar.service_id = trips.service_id " +
|
||||
CalendarDateUtils.getCalendarFilter(db, dateTimeString) +
|
||||
CalendarDateUtils.getFilterForDate(db, dateTimeString) +
|
||||
"ORDER BY st1__departure_time ASC ";
|
||||
|
||||
String[] args = {stop_id};
|
||||
return db.rawQuery(query, args);
|
||||
}
|
||||
|
|
|
@ -131,7 +131,7 @@ public class PossibleTripUtils {
|
|||
}
|
||||
|
||||
public static Cursor getPossibleTripsByParentStopQuery(SQLiteDatabase db, Long dateTime, String stop1_id, String stop2_id) {
|
||||
String calendarFilter = CalendarDateUtils.getCalendarFilter(db, dateTime);
|
||||
String calendarFilter = CalendarDateUtils.getFilterForDate(db, dateTime);
|
||||
String query = "SELECT " +
|
||||
"routes.route_id as route_id, " +
|
||||
"routes.route_long_name as route_long_name, " +
|
||||
|
|
|
@ -5,7 +5,7 @@ buildscript {
|
|||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.0.0'
|
||||
classpath 'com.android.tools.build:gradle:3.0.1'
|
||||
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
// in the individual module build.gradle files
|
||||
|
|
Reference in New Issue