use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class HibernateGtfsRelationalDaoImplTest method testStopsForStation.
@Test
public void testStopsForStation() {
Stop stationA = new Stop();
stationA.setId(new AgencyAndId("X", "A"));
_dao.saveEntity(stationA);
Stop stationB = new Stop();
stationB.setId(new AgencyAndId("X", "B"));
_dao.saveEntity(stationB);
Stop stopA1 = new Stop();
stopA1.setId(new AgencyAndId("X", "A1"));
stopA1.setParentStation("A");
_dao.saveEntity(stopA1);
Stop stopA2 = new Stop();
stopA2.setId(new AgencyAndId("X", "A2"));
stopA2.setParentStation("A");
_dao.saveEntity(stopA2);
Stop stopB1 = new Stop();
stopB1.setId(new AgencyAndId("X", "B1"));
stopB1.setParentStation("B");
_dao.saveEntity(stopB1);
_dao.flush();
Stop station2 = _dao.getStopForId(new AgencyAndId("X", "A"));
List<Stop> stops = _dao.getStopsForStation(station2);
assertEquals(2, stops.size());
Set<String> ids = new HashSet<String>();
ids.add("A1");
ids.add("A2");
assertTrue(ids.contains(stops.get(0).getId().getId()));
assertTrue(ids.contains(stops.get(1).getId().getId()));
}
use of org.onebusaway.gtfs.model.AgencyAndId 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.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarSimplicationLibrary method groupTripKeysByServiceIds.
public Map<Set<AgencyAndId>, List<TripKey>> groupTripKeysByServiceIds(Map<TripKey, List<Trip>> tripsByKey) {
Map<Set<AgencyAndId>, List<TripKey>> tripKeysByServiceIds = new FactoryMap<Set<AgencyAndId>, List<TripKey>>(new ArrayList<TripKey>());
for (Map.Entry<TripKey, List<Trip>> entry : tripsByKey.entrySet()) {
TripKey key = entry.getKey();
List<Trip> tripsForKey = entry.getValue();
Set<AgencyAndId> serviceIds = new HashSet<AgencyAndId>();
for (Trip trip : tripsForKey) {
serviceIds.add(trip.getServiceId());
}
tripKeysByServiceIds.get(serviceIds).add(key);
}
return tripKeysByServiceIds;
}
use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarSimplicationStrategy method run.
@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
RemoveEntityLibrary removeEntityLibrary = new RemoveEntityLibrary();
Map<Set<AgencyAndId>, AgencyAndId> serviceIdsToUpdatedServiceId = new HashMap<Set<AgencyAndId>, AgencyAndId>();
Map<AgencyAndId, List<AgencyAndId>> mergeToolIdMapping = computeMergeToolIdMapping(dao);
for (Route route : dao.getAllRoutes()) {
Map<TripKey, List<Trip>> tripsByKey = TripKey.groupTripsForRouteByKey(dao, route);
Map<Set<AgencyAndId>, List<TripKey>> tripKeysByServiceIds = _library.groupTripKeysByServiceIds(tripsByKey);
for (Set<AgencyAndId> serviceIds : tripKeysByServiceIds.keySet()) {
AgencyAndId updatedServiceId = createUpdatedServiceId(serviceIdsToUpdatedServiceId, serviceIds);
for (TripKey tripKey : tripKeysByServiceIds.get(serviceIds)) {
List<Trip> tripsForKey = tripsByKey.get(tripKey);
Trip tripToKeep = tripsForKey.get(0);
tripToKeep.setServiceId(updatedServiceId);
for (int i = 1; i < tripsForKey.size(); i++) {
Trip trip = tripsForKey.get(i);
removeEntityLibrary.removeTrip(dao, trip);
}
if (undoGoogleTransitDataFeedMergeTool) {
AgencyAndId updatedTripId = computeUpdatedTripIdForMergedTripsIfApplicable(mergeToolIdMapping, tripsForKey);
if (updatedTripId != null) {
tripToKeep.setId(updatedTripId);
}
}
}
}
}
CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(dao);
List<Object> newEntities = new ArrayList<Object>();
for (Map.Entry<Set<AgencyAndId>, AgencyAndId> entry : serviceIdsToUpdatedServiceId.entrySet()) {
Set<ServiceDate> allServiceDates = getServiceDatesForServiceIds(calendarService, entry.getKey());
ServiceCalendarSummary summary = _library.getSummaryForServiceDates(allServiceDates);
_library.computeSimplifiedCalendar(entry.getValue(), summary, newEntities);
}
saveUpdatedCalendarEntities(dao, newEntities);
}
use of org.onebusaway.gtfs.model.AgencyAndId in project onebusaway-gtfs-modules by OneBusAway.
the class CalendarSimplicationStrategy method computeMergeToolIdMapping.
private Map<AgencyAndId, List<AgencyAndId>> computeMergeToolIdMapping(GtfsDao dao) {
if (!undoGoogleTransitDataFeedMergeTool)
return Collections.emptyMap();
Map<AgencyAndId, List<AgencyAndId>> mergedIdMapping = new FactoryMap<AgencyAndId, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
Map<AgencyAndId, List<AgencyAndId>> unmergedIdMapping = new FactoryMap<AgencyAndId, List<AgencyAndId>>(new ArrayList<AgencyAndId>());
for (Trip trip : dao.getAllTrips()) {
AgencyAndId tripId = trip.getId();
AgencyAndId unmergedTripId = computeUnmergedTripId(tripId);
if (unmergedTripId.equals(tripId)) {
unmergedIdMapping.get(unmergedTripId).add(tripId);
} else {
mergedIdMapping.get(unmergedTripId).add(tripId);
}
}
Set<AgencyAndId> intersection = new HashSet<AgencyAndId>(mergedIdMapping.keySet());
intersection.retainAll(unmergedIdMapping.keySet());
if (!intersection.isEmpty()) {
throw new IllegalStateException("some ids appeared both in the merged and unmerged case: " + intersection);
}
mergedIdMapping.putAll(unmergedIdMapping);
return mergedIdMapping;
}
Aggregations