StoreImpl store = new StoreImpl(dao);
store.open();
LOG.info("reading {}", gtfsBundle.toString());
GtfsReader reader = new GtfsReader();
reader.setInputSource(gtfsBundle.getCsvInputSource());
reader.setEntityStore(store);
reader.setInternStrings(true);
if (LOG.isDebugEnabled())
reader.addEntityHandler(counter);
if (gtfsBundle.getDefaultBikesAllowed())
reader.addEntityHandler(new EntityBikeability(true));
for (Class<?> entityClass : reader.getEntityClasses()) {
LOG.info("reading entities: " + entityClass.getName());
reader.readEntities(entityClass);
store.flush();
// NOTE that agencies are first in the list and read before all other entity types, so it is effective to
// set the agencyId here. Each feed ("bundle") is loaded by a separate reader, so there is no risk of
// agency mappings accumulating.
if (entityClass == Agency.class) {
String defaultAgencyId = null;
for (Agency agency : reader.getAgencies()) {
String agencyId = agency.getId();
LOG.info("This Agency has the ID {}", agencyId);
// Somehow, when the agency's id field is missing, OBA replaces it with the agency's name.
// TODO Figure out how and why this is happening.
if (agencyId == null || agencyIdsSeen.contains(agencyId)) {
// Loop in case generated name is already in use.
String generatedAgencyId = null;
while (generatedAgencyId == null || agencyIdsSeen.contains(generatedAgencyId)) {
generatedAgencyId = "F" + nextAgencyId;
nextAgencyId++;
}
LOG.warn("The agency ID '{}' was already seen, or I think it's bad. Replacing with '{}'.", agencyId, generatedAgencyId);
reader.addAgencyIdMapping(agencyId, generatedAgencyId); // NULL key should work
agency.setId(generatedAgencyId);
agencyId = generatedAgencyId;
}
if (agencyId != null) agencyIdsSeen.add(agencyId);
if (defaultAgencyId == null) defaultAgencyId = agencyId;
}
reader.setDefaultAgencyId(defaultAgencyId); // not sure this is a good idea, setting it to the first-of-many IDs.
}
}
for (ShapePoint shapePoint : store.getAllEntitiesForType(ShapePoint.class)) {
shapePoint.getShapeId().setAgencyId(reader.getDefaultAgencyId());
}
for (Route route : store.getAllEntitiesForType(Route.class)) {
route.getId().setAgencyId(reader.getDefaultAgencyId());
}
for (Stop stop : store.getAllEntitiesForType(Stop.class)) {
stop.getId().setAgencyId(reader.getDefaultAgencyId());
}
for (Trip trip : store.getAllEntitiesForType(Trip.class)) {
trip.getId().setAgencyId(reader.getDefaultAgencyId());
}
for (ServiceCalendar serviceCalendar : store.getAllEntitiesForType(ServiceCalendar.class)) {
serviceCalendar.getServiceId().setAgencyId(reader.getDefaultAgencyId());
}
for (ServiceCalendarDate serviceCalendarDate : store.getAllEntitiesForType(ServiceCalendarDate.class)) {
serviceCalendarDate.getServiceId().setAgencyId(reader.getDefaultAgencyId());
}
for (FareAttribute fareAttribute : store.getAllEntitiesForType(FareAttribute.class)) {
fareAttribute.getId().setAgencyId(reader.getDefaultAgencyId());
}
for (Pathway pathway : store.getAllEntitiesForType(Pathway.class)) {
pathway.getId().setAgencyId(reader.getDefaultAgencyId());
}
store.close();
}