2017-03-13 05:39:40 +00:00
package com.eleith.calchoochoo.utils ;
import android.database.Cursor ;
import android.database.sqlite.SQLiteDatabase ;
import android.support.annotation.Nullable ;
import com.eleith.calchoochoo.data.PossibleTrip ;
import org.joda.time.LocalDateTime ;
import org.joda.time.LocalTime ;
import java.util.ArrayList ;
public class PossibleTripUtils {
@Nullable
public static PossibleTrip getPossibleTripFromCursor ( Cursor cursor ) {
2017-03-16 02:32:39 +00:00
if ( cursor . moveToNext ( ) ) {
PossibleTrip possibleTrip = new PossibleTrip ( ) ;
2017-03-13 05:39:40 +00:00
Float price = cursor . getFloat ( cursor . getColumnIndex ( " price " ) ) ;
2017-04-02 07:05:52 +00:00
Integer direction = cursor . getInt ( cursor . getColumnIndex ( " direction " ) ) ;
2017-04-11 20:14:23 +00:00
String tripShortName = cursor . getString ( cursor . getColumnIndex ( " trip_short_name " ) ) ;
String routeLongName = cursor . getString ( cursor . getColumnIndex ( " route_long_name " ) ) ;
2017-03-13 05:39:40 +00:00
String routeId = cursor . getString ( cursor . getColumnIndex ( " route_id " ) ) ;
String stop1Id = cursor . getString ( cursor . getColumnIndex ( " st1__stop_id " ) ) ;
String stop2Id = cursor . getString ( cursor . getColumnIndex ( " st2__stop_id " ) ) ;
2017-04-13 03:00:52 +00:00
String stop1ParentId = cursor . getString ( cursor . getColumnIndex ( " st1__parent_station " ) ) ;
String stop2ParentId = cursor . getString ( cursor . getColumnIndex ( " st2__parent_station " ) ) ;
2017-03-15 17:54:28 +00:00
String stop1Name = cursor . getString ( cursor . getColumnIndex ( " st1__stop_name " ) ) ;
String stop2Name = cursor . getString ( cursor . getColumnIndex ( " st2__stop_name " ) ) ;
2017-03-13 06:10:16 +00:00
String tripId = cursor . getString ( cursor . getColumnIndex ( " st1__trip_id " ) ) ;
2017-03-13 05:39:40 +00:00
Integer stopOneSequence = cursor . getInt ( cursor . getColumnIndex ( " st1__stop_sequence " ) ) ;
Integer stopTwoSequence = cursor . getInt ( cursor . getColumnIndex ( " st2__stop_sequence " ) ) ;
2017-04-11 20:26:37 +00:00
LocalTime stopOneDepartureTime = new LocalTime ( DataStringUtils . adjustLateTimes ( cursor . getString ( cursor . getColumnIndex ( " st1__departure_time " ) ) ) ) ;
LocalTime stopOneArrivalTime = new LocalTime ( DataStringUtils . adjustLateTimes ( cursor . getString ( cursor . getColumnIndex ( " st1__arrival_time " ) ) ) ) ;
LocalTime stopTwoDepartureTime = new LocalTime ( DataStringUtils . adjustLateTimes ( cursor . getString ( cursor . getColumnIndex ( " st2__departure_time " ) ) ) ) ;
LocalTime stopTwoArrivalTime = new LocalTime ( DataStringUtils . adjustLateTimes ( cursor . getString ( cursor . getColumnIndex ( " st2__arrival_time " ) ) ) ) ;
2017-03-13 05:39:40 +00:00
if ( stopOneSequence < stopTwoSequence ) {
2017-04-27 04:49:44 +00:00
possibleTrip . setArrivalTime ( stopTwoArrivalTime ) ;
possibleTrip . setDepartureTime ( stopOneDepartureTime ) ;
2017-03-13 05:39:40 +00:00
possibleTrip . setFirstStopSequence ( stopOneSequence ) ;
possibleTrip . setLastStopSequence ( stopTwoSequence ) ;
possibleTrip . setFirstStopId ( stop1Id ) ;
2017-04-13 03:00:52 +00:00
possibleTrip . setFirstParentStopId ( stop1ParentId ) ;
2017-03-13 05:39:40 +00:00
possibleTrip . setLastStopId ( stop2Id ) ;
2017-04-13 03:00:52 +00:00
possibleTrip . setLastParentStopId ( stop2ParentId ) ;
2017-03-15 17:54:28 +00:00
possibleTrip . setFirstStopName ( stop1Name ) ;
possibleTrip . setLastStopName ( stop2Name ) ;
2017-03-13 05:39:40 +00:00
} else {
2017-04-27 04:49:44 +00:00
possibleTrip . setArrivalTime ( stopOneArrivalTime ) ;
possibleTrip . setDepartureTime ( stopTwoDepartureTime ) ;
2017-03-13 05:39:40 +00:00
possibleTrip . setFirstStopSequence ( stopTwoSequence ) ;
possibleTrip . setLastStopSequence ( stopOneSequence ) ;
2017-03-15 17:54:28 +00:00
possibleTrip . setFirstStopId ( stop2Id ) ;
2017-04-13 03:00:52 +00:00
possibleTrip . setFirstParentStopId ( stop2ParentId ) ;
2017-03-15 17:54:28 +00:00
possibleTrip . setLastStopId ( stop1Id ) ;
2017-04-13 03:00:52 +00:00
possibleTrip . setLastParentStopId ( stop1ParentId ) ;
2017-03-15 17:54:28 +00:00
possibleTrip . setFirstStopName ( stop2Name ) ;
possibleTrip . setLastStopName ( stop1Name ) ;
2017-03-13 05:39:40 +00:00
}
2017-03-15 17:54:28 +00:00
possibleTrip . setRouteLongName ( routeLongName ) ;
2017-03-13 05:39:40 +00:00
possibleTrip . setPrice ( price ) ;
possibleTrip . setTripId ( tripId ) ;
2017-04-11 20:14:23 +00:00
possibleTrip . setTripShortName ( tripShortName ) ;
2017-04-02 07:05:52 +00:00
possibleTrip . setTripDirection ( direction ) ;
2017-03-13 05:39:40 +00:00
possibleTrip . setRouteId ( routeId ) ;
return possibleTrip ;
}
return null ;
}
public static ArrayList < PossibleTrip > getPossibleTripsFromCursor ( Cursor cursor ) {
ArrayList < PossibleTrip > possibleTrips = new ArrayList < > ( ) ;
2017-03-16 02:32:39 +00:00
while ( cursor . getPosition ( ) < cursor . getCount ( ) ) {
2017-03-13 05:39:40 +00:00
PossibleTrip possibleTrip = getPossibleTripFromCursor ( cursor ) ;
2017-03-16 02:32:39 +00:00
if ( possibleTrip ! = null ) {
possibleTrips . add ( possibleTrip ) ;
}
2017-03-13 05:39:40 +00:00
}
return possibleTrips ;
}
public static Cursor getPossibleTripQuery ( SQLiteDatabase db , String trip_id , String stop1_id , String stop2_id ) {
String query = " SELECT " +
" routes.route_id as route_id, " +
2017-03-15 17:54:28 +00:00
" routes.route_long_name as route_long_name, " +
2017-03-13 05:39:40 +00:00
" fare_attributes.price as price, " +
2017-04-02 07:05:52 +00:00
" trips.direction_id as direction, " +
2017-04-11 20:14:23 +00:00
" trips.trip_short_name as trip_short_name, " +
2017-03-13 05:39:40 +00:00
" st1.trip_id as st1__trip_id, st1.arrival_time as st1__arrival_time, st1.departure_time as st1__departure_time, " +
2017-04-13 03:00:52 +00:00
" st1.stop_name as st1__stop_name, st1.stop_id as st1__stop_id, st1.stop_sequence as st1__stop_sequence, st1.parent_station as st1__parent_station, " +
2017-03-13 05:39:40 +00:00
" st2.trip_id as st2__trip_id, st2.arrival_time as st2__arrival_time, st2.departure_time as st2__departure_time, " +
2017-04-13 03:00:52 +00:00
" st2.stop_name as st2__stop_name, st2.stop_id as st2__stop_id, st2.stop_sequence as st2__stop_sequence, st2.parent_station as st2__parent_station " +
2017-03-13 05:39:40 +00:00
" FROM " +
" (SELECT * " +
" FROM stops, stop_times " +
" WHERE " +
" stops.stop_id = stop_times.stop_id " +
" AND stop_times.trip_id = ? " +
2017-04-02 07:05:52 +00:00
" AND stops.stop_id = ?) AS st1, " +
2017-03-13 05:39:40 +00:00
" (Select * " +
" FROM stops, stop_times " +
" WHERE " +
" stops.stop_id = stop_times.stop_id " +
" AND stop_times.trip_id = ? " +
2017-04-02 07:05:52 +00:00
" AND stops.stop_id = ?) AS st2, " +
2017-03-13 05:39:40 +00:00
" trips, " +
" routes, " +
" calendar, " +
" fare_rules, " +
" fare_attributes " +
" WHERE st1.trip_id = st2.trip_id " +
" AND trips.route_id = routes.route_id " +
2017-04-02 07:05:52 +00:00
" AND trips.trip_id = st1.trip_id " +
2017-03-13 05:39:40 +00:00
" AND calendar.service_id = trips.service_id " +
" AND fare_rules.origin_id = st1.zone_id " +
" AND fare_rules.destination_id = st2.zone_id " +
" AND fare_rules.route_id = routes.route_id " +
2017-04-16 05:30:54 +00:00
" AND fare_rules.fare_id = fare_attributes.fare_id " +
" ORDER BY st1.departure_time " ;
2017-03-13 05:39:40 +00:00
String [ ] args = { trip_id , stop1_id , trip_id , stop2_id } ;
return db . rawQuery ( query , args ) ;
}
2017-04-02 07:05:52 +00:00
public static Cursor getPossibleTripsByParentStopQuery ( SQLiteDatabase db , Long dateTime , String stop1_id , String stop2_id ) {
2017-11-24 19:00:46 +00:00
String calendarFilter = CalendarDateUtils . getFilterForDate ( db , dateTime ) ;
2017-03-13 05:39:40 +00:00
String query = " SELECT " +
" routes.route_id as route_id, " +
2017-03-15 17:54:28 +00:00
" routes.route_long_name as route_long_name, " +
2017-03-13 05:39:40 +00:00
" fare_attributes.price as price, " +
2017-04-02 07:05:52 +00:00
" trips.direction_id as direction, " +
2017-04-11 20:14:23 +00:00
" trips.trip_short_name as trip_short_name, " +
2017-03-15 17:54:28 +00:00
" st1.stop_name as st1__stop_name, st1.platform_code as st1__platform_code, st1.trip_id as st1__trip_id, st1.arrival_time as st1__arrival_time, st1.departure_time as st1__departure_time, " +
2017-04-13 03:00:52 +00:00
" st1.stop_id as st1__stop_id, st1.stop_sequence as st1__stop_sequence, st1.drop_off_type as st1__drop_off_type, st1.parent_station as st1__parent_station, " +
2017-03-15 17:54:28 +00:00
" st2.stop_name as st2__stop_name, st2.platform_code as st2__platform_code, st2.trip_id as st2__trip_id, st2.arrival_time as st2__arrival_time, st2.departure_time as st2__departure_time, " +
2017-04-13 03:00:52 +00:00
" st2.stop_id as st2__stop_id, st2.stop_sequence as st2__stop_sequence, st2.drop_off_type as st2__drop_off_type, st2.parent_station as st2__parent_station " +
2017-03-13 05:39:40 +00:00
" FROM " +
" (SELECT * " +
" FROM stops, stop_times " +
" WHERE " +
" stop_times.stop_id = stops.stop_id " +
" AND stops.parent_station = ?) AS st1, " +
" (Select * " +
" FROM stops, stop_times " +
" WHERE " +
" stop_times.stop_id = stops.stop_id " +
" AND stops.parent_station = ?) AS st2, " +
" trips, " +
" routes, " +
" calendar, " +
" fare_rules, " +
" fare_attributes " +
" WHERE st1.trip_id = st2.trip_id " +
" AND st1.platform_code = st2.platform_code " +
2017-04-22 03:41:17 +00:00
" AND cast(st1.stop_sequence as integer) < cast(st2.stop_sequence as integer) " +
2017-03-13 05:39:40 +00:00
" AND trips.trip_id = st1.trip_id " +
" AND trips.route_id = routes.route_id " +
" AND calendar.service_id = trips.service_id " +
" AND fare_rules.origin_id = st1.zone_id " +
" AND fare_rules.destination_id = st2.zone_id " +
" AND fare_rules.route_id = routes.route_id " +
" AND fare_rules.fare_id = fare_attributes.fare_id " +
2017-04-16 05:30:54 +00:00
calendarFilter +
" ORDER BY st1.departure_time " ;
2017-03-13 05:39:40 +00:00
String [ ] args = { stop1_id , stop2_id } ;
return db . rawQuery ( query , args ) ;
}
public static ArrayList < PossibleTrip > filterByDateTimeAndDirection ( ArrayList < PossibleTrip > possibleTrips , LocalDateTime dateTime , Boolean arriving ) {
ArrayList < PossibleTrip > possibleTripsFiltered = new ArrayList < > ( ) ;
for ( PossibleTrip possibleTrip : possibleTrips ) {
LocalTime departureTime = possibleTrip . getDepartureTime ( ) ;
LocalTime arrivalTime = possibleTrip . getArrivalTime ( ) ;
2017-06-26 06:44:58 +00:00
if ( ! arriving ) {
if ( departureTime . isAfter ( dateTime . toLocalTime ( ) ) ) {
2017-03-13 05:39:40 +00:00
possibleTripsFiltered . add ( possibleTrip ) ;
}
} else {
2017-06-26 06:44:58 +00:00
if ( arrivalTime . isBefore ( dateTime . toLocalTime ( ) ) & & arrivalTime . isAfter ( dateTime . minusHours ( 3 ) . toLocalTime ( ) ) ) {
2017-03-13 05:39:40 +00:00
possibleTripsFiltered . add ( possibleTrip ) ;
}
}
}
return possibleTripsFiltered ;
}
}