use of org.onebusaway.gtfs.model.ServiceCalendar in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarServiceDataFactoryImplSyntheticTest method calendar.
private ServiceCalendar calendar(AgencyAndId serviceId, ServiceDate startDate, ServiceDate endDate, String days) {
if (days.length() != 7)
throw new IllegalStateException("invalid days string: " + days);
ServiceCalendar calendar = new ServiceCalendar();
calendar.setStartDate(startDate);
calendar.setEndDate(endDate);
calendar.setServiceId(serviceId);
calendar.setMonday(days.charAt(0) == '1' ? 1 : 0);
calendar.setTuesday(days.charAt(1) == '1' ? 1 : 0);
calendar.setWednesday(days.charAt(2) == '1' ? 1 : 0);
calendar.setThursday(days.charAt(3) == '1' ? 1 : 0);
calendar.setFriday(days.charAt(4) == '1' ? 1 : 0);
calendar.setSaturday(days.charAt(5) == '1' ? 1 : 0);
calendar.setSunday(days.charAt(5) == '1' ? 1 : 0);
return calendar;
}
use of org.onebusaway.gtfs.model.ServiceCalendar in project onebusaway-gtfs-modules by OneBusAway.
the class PropertyMethodResolverImplTest method testTripCalendarsVirtualMethod.
@Test
public void testTripCalendarsVirtualMethod() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Trip trip = new Trip();
trip.setId(new AgencyAndId("1", "t0"));
trip.setServiceId(new AgencyAndId("1", "sid0"));
_dao.saveEntity(trip);
ServiceCalendar calendar = new ServiceCalendar();
calendar.setServiceId(trip.getServiceId());
_dao.saveEntity(calendar);
PropertyMethod method = _resolver.getPropertyMethod(Trip.class, "calendar");
assertEquals(calendar, method.invoke(trip));
}
use of org.onebusaway.gtfs.model.ServiceCalendar 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.ServiceCalendar 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);
}
}
}
}
use of org.onebusaway.gtfs.model.ServiceCalendar in project onebusaway-gtfs-modules by OneBusAway.
the class ServiceIdTransformStrategyImpl method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao, Object entity) {
AgencyAndId oldServiceId = context.resolveId(ServiceIdKey.class, _oldServiceId);
AgencyAndId newServiceId = context.resolveId(ServiceIdKey.class, _newServiceId);
ServiceCalendar calendar = dao.getCalendarForServiceId(oldServiceId);
if (calendar != null) {
calendar.setServiceId(newServiceId);
}
for (ServiceCalendarDate calendarDate : dao.getCalendarDatesForServiceId(oldServiceId)) {
calendarDate.setServiceId(newServiceId);
}
for (Trip trip : dao.getTripsForServiceId(oldServiceId)) {
trip.setServiceId(newServiceId);
}
}
Aggregations