use of org.onebusaway.gtfs.model.ServiceCalendarDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceDataFactoryImpl method getServiceDatesForServiceId.
public Set<ServiceDate> getServiceDatesForServiceId(AgencyAndId serviceId, TimeZone serviceIdTimeZone) {
Set<ServiceDate> activeDates = new HashSet<ServiceDate>();
ServiceCalendar c = _dao.getCalendarForServiceId(serviceId);
if (c != null) {
addDatesFromCalendar(c, serviceIdTimeZone, activeDates);
}
for (ServiceCalendarDate cd : _dao.getCalendarDatesForServiceId(serviceId)) {
addAndRemoveDatesFromCalendarDate(cd, serviceIdTimeZone, activeDates);
}
return activeDates;
}
use of org.onebusaway.gtfs.model.ServiceCalendarDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceDataFactoryImpl method addAndRemoveDatesFromCalendarDate.
private void addAndRemoveDatesFromCalendarDate(ServiceCalendarDate calendarDate, TimeZone serviceIdTimeZone, Set<ServiceDate> activeDates) {
ServiceDate serviceDate = calendarDate.getDate();
Date targetDate = calendarDate.getDate().getAsDate();
Calendar c = Calendar.getInstance();
c.setTime(targetDate);
switch(calendarDate.getExceptionType()) {
case ServiceCalendarDate.EXCEPTION_TYPE_ADD:
addServiceDate(activeDates, serviceDate, serviceIdTimeZone);
break;
case ServiceCalendarDate.EXCEPTION_TYPE_REMOVE:
activeDates.remove(serviceDate);
break;
default:
_log.warn("unknown CalendarDate exception type: " + calendarDate.getExceptionType());
break;
}
}
use of org.onebusaway.gtfs.model.ServiceCalendarDate in project onebusaway-gtfs-modules by OneBusAway.
the class GtfsRelationalDaoImplTest method testBart.
@Test
public void testBart() throws IOException {
GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
GtfsTestData.readGtfs(dao, GtfsTestData.getBartGtfs(), "BART");
List<String> tripAgencyIds = dao.getTripAgencyIdsReferencingServiceId(new AgencyAndId("BART", "WKDY"));
assertEquals(1, tripAgencyIds.size());
assertEquals("BART", tripAgencyIds.get(0));
Agency agency = dao.getAgencyForId("BART");
List<Route> routes = dao.getRoutesForAgency(agency);
assertEquals(10, routes.size());
agency = dao.getAgencyForId("AirBART");
routes = dao.getRoutesForAgency(agency);
assertEquals(1, routes.size());
Route route = dao.getRouteForId(new AgencyAndId("BART", "01"));
List<Trip> trips = dao.getTripsForRoute(route);
assertEquals(225, trips.size());
Trip trip = dao.getTripForId(new AgencyAndId("BART", "15PB1"));
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
assertEquals(12, stopTimes.size());
// Ensure the stopTimes are in stop sequence order
for (int i = 0; i < stopTimes.size() - 1; i++) assertTrue(stopTimes.get(i).getStopSequence() < stopTimes.get(i + 1).getStopSequence());
Stop stop = dao.getStopForId(new AgencyAndId("BART", "DBRK"));
stopTimes = dao.getStopTimesForStop(stop);
assertEquals(584, stopTimes.size());
List<ShapePoint> shapePoints = dao.getShapePointsForShapeId(new AgencyAndId("BART", "airbart-dn.csv"));
assertEquals(50, shapePoints.size());
for (int i = 0; i < shapePoints.size() - 1; i++) assertTrue(shapePoints.get(i).getSequence() < shapePoints.get(i + 1).getSequence());
trip = dao.getTripForId(new AgencyAndId("AirBART", "M-FSAT1DN"));
List<Frequency> frequencies = dao.getFrequenciesForTrip(trip);
assertEquals(1, frequencies.size());
Frequency frequency = frequencies.get(0);
assertEquals(5 * 60 * 60, frequency.getStartTime());
assertEquals(6 * 60 * 60, frequency.getEndTime());
assertEquals(trip, frequency.getTrip());
assertEquals(1200, frequency.getHeadwaySecs());
ServiceCalendar calendar = dao.getCalendarForServiceId(new AgencyAndId("BART", "WKDY"));
assertEquals(new ServiceDate(2007, 1, 1), calendar.getStartDate());
List<ServiceCalendarDate> calendarDates = dao.getCalendarDatesForServiceId(new AgencyAndId("BART", "WKDY"));
assertEquals(7, calendarDates.size());
}
use of org.onebusaway.gtfs.model.ServiceCalendarDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarExtensionStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
CalendarSimplicationLibrary simplication = new CalendarSimplicationLibrary();
for (AgencyAndId serviceId : dao.getAllServiceIds()) {
ServiceCalendarSummary summary = simplication.getSummaryForServiceDates(service.getServiceDatesForServiceId(serviceId));
/**
* If a service id has no active dates at all, we don't extend it.
*/
if (summary.allServiceDates.isEmpty()) {
continue;
}
ServiceCalendar calendar = dao.getCalendarForServiceId(serviceId);
if (calendar == null) {
ServiceDate lastDate = summary.serviceDatesInOrder.get(summary.serviceDatesInOrder.size() - 1);
if (lastDate.compareTo(inactiveCalendarCutoff) < 0) {
continue;
}
/**
* We only want days of the week that are in service past our stale
* calendar cutoff.
*/
Set<Integer> daysOfTheWeekToUse = getDaysOfTheWeekToUse(summary);
if (daysOfTheWeekToUse.isEmpty()) {
continue;
}
ServiceDate firstMissingDate = lastDate.next();
for (ServiceDate serviceDate = firstMissingDate; serviceDate.compareTo(endDate) <= 0; serviceDate = serviceDate.next()) {
Calendar serviceDateAsCalendar = serviceDate.getAsCalendar(_utcTimeZone);
// Move the calendar forward to "noon" to mitigate the effects of DST
// (though the shouldn't be a problem for UTC?)
serviceDateAsCalendar.add(Calendar.HOUR_OF_DAY, 12);
int dayOfWeek = serviceDateAsCalendar.get(Calendar.DAY_OF_WEEK);
if (daysOfTheWeekToUse.contains(dayOfWeek)) {
ServiceCalendarDate scd = new ServiceCalendarDate();
scd.setDate(serviceDate);
scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
scd.setServiceId(serviceId);
dao.saveEntity(scd);
}
}
} else {
if (calendar.getEndDate().compareTo(inactiveCalendarCutoff) >= 0) {
calendar.setEndDate(endDate);
}
}
}
UpdateLibrary.clearDaoCache(dao);
}
use of org.onebusaway.gtfs.model.ServiceCalendarDate in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarSimplicationLibrary method computeSimplifiedCalendar.
public void computeSimplifiedCalendar(AgencyAndId updatedServiceId, ServiceCalendarSummary summary, List<Object> newEntities) {
List<ServiceDate> serviceDatesInOrder = summary.serviceDatesInOrder;
Set<Integer> daysOfTheWeekToUse = summary.daysOfTheWeekToUse;
if (serviceDatesInOrder.isEmpty()) {
return;
}
ServiceDate fromDate = serviceDatesInOrder.get(0);
ServiceDate toDate = serviceDatesInOrder.get(serviceDatesInOrder.size() - 1);
boolean useDateRange = summary.maxDayOfWeekCount >= _minNumberOfWeeksForCalendarEntry;
if (useDateRange) {
ServiceCalendar sc = createServiceCalendar(updatedServiceId, daysOfTheWeekToUse, fromDate, toDate);
newEntities.add(sc);
}
TimeZone tz = TimeZone.getTimeZone("UTC");
for (ServiceDate serviceDate = fromDate; serviceDate.compareTo(toDate) <= 0; serviceDate = serviceDate.next()) {
boolean isActive = summary.allServiceDates.contains(serviceDate);
Calendar serviceDateAsCalendar = serviceDate.getAsCalendar(tz);
if (useDateRange) {
int dayOfWeek = serviceDateAsCalendar.get(Calendar.DAY_OF_WEEK);
boolean dateRangeIncludesServiceDate = daysOfTheWeekToUse.contains(dayOfWeek);
if (isActive && !dateRangeIncludesServiceDate) {
ServiceCalendarDate scd = new ServiceCalendarDate();
scd.setDate(serviceDate);
scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
scd.setServiceId(updatedServiceId);
newEntities.add(scd);
}
if (!isActive && dateRangeIncludesServiceDate) {
ServiceCalendarDate scd = new ServiceCalendarDate();
scd.setDate(serviceDate);
scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_REMOVE);
scd.setServiceId(updatedServiceId);
newEntities.add(scd);
}
} else {
if (isActive) {
ServiceCalendarDate scd = new ServiceCalendarDate();
scd.setDate(serviceDate);
scd.setExceptionType(ServiceCalendarDate.EXCEPTION_TYPE_ADD);
scd.setServiceId(updatedServiceId);
newEntities.add(scd);
}
}
}
}
Aggregations