use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-application-modules by camsys.
the class BundleBuildingServiceImpl method build.
/**
* call FederatedTransitDataBundleCreator
*/
@Override
public int build(BundleBuildRequest request, BundleBuildResponse response) {
/*
* this follows the example from FederatedTransitDataBundleCreatorMain
*/
PrintStream stdOut = System.out;
PrintStream logFile = null;
// pass a mini spring context to the bundle builder so we can cleanup
ConfigurableApplicationContext context = null;
try {
File outputPath = new File(response.getBundleDataDirectory());
File loggingPath = new File(response.getBundleOutputDirectory());
// beans assume bundlePath is set -- this will be where files are written!
System.setProperty("bundlePath", outputPath.getAbsolutePath());
String logFilename = outputPath + File.separator + "bundleBuilder.out.txt";
logFile = new PrintStream(new FileOutputStream(new File(logFilename)));
// swap standard out for logging
System.setOut(logFile);
configureLogging(System.out);
FederatedTransitDataBundleCreator creator = new FederatedTransitDataBundleCreator();
Map<String, BeanDefinition> beans = new HashMap<String, BeanDefinition>();
creator.setContextBeans(beans);
List<GtfsBundle> gtfsBundles = createGtfsBundles(response);
List<String> contextPaths = new ArrayList<String>();
contextPaths.add(BUNDLE_RESOURCE);
BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsBundles.class);
bean.addPropertyValue("bundles", gtfsBundles);
beans.put("gtfs-bundles", bean.getBeanDefinition());
bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsRelationalDaoImpl.class);
beans.put("gtfsRelationalDaoImpl", bean.getBeanDefinition());
BeanDefinitionBuilder multiCSVLogger = BeanDefinitionBuilder.genericBeanDefinition(MultiCSVLogger.class);
multiCSVLogger.addPropertyValue("basePath", loggingPath);
beans.put("multiCSVLogger", multiCSVLogger.getBeanDefinition());
BeanDefinitionBuilder entityReplacementLogger = BeanDefinitionBuilder.genericBeanDefinition(EntityReplacementLoggerImpl.class);
beans.put("entityReplacementLogger", entityReplacementLogger.getBeanDefinition());
BeanDefinitionBuilder requestDef = BeanDefinitionBuilder.genericBeanDefinition(BundleRequestResponse.class);
requestDef.addPropertyValue("request", request);
requestDef.addPropertyValue("response", response);
beans.put("bundleRequestResponse", requestDef.getBeanDefinition());
// configure for NYC specifics
BeanDefinitionBuilder bundle = BeanDefinitionBuilder.genericBeanDefinition(FederatedTransitDataBundle.class);
bundle.addPropertyValue("path", outputPath);
beans.put("bundle", bundle.getBeanDefinition());
BeanDefinitionBuilder outputDirectoryReference = BeanDefinitionBuilder.genericBeanDefinition(String.class);
outputDirectoryReference.addPropertyValue("", response.getBundleOutputDirectory());
// TODO move this to application-context-bunlde-admin.xml and have it look for config to turn on/off
BeanDefinitionBuilder task = null;
if (isStifTaskApplicable()) {
addStifTask(beans, request, response);
}
if (isStopConsolidationApplicable()) {
addStopConsolidationMappings(beans, request, response);
}
_log.debug("setting outputPath=" + outputPath);
creator.setOutputPath(outputPath);
creator.setContextPaths(contextPaths);
// manage our own overrides, as we use our own context
Properties cmdOverrides = new Properties();
cmdOverrides.setProperty(ARG_THROW_EXCEPTION_INVALID_STOPS, "false");
cmdOverrides.setProperty(ARG_LENIENT_ARRIVAL_DEPARTURE, "true");
if (this.getStopVerificationURL() != null) {
cmdOverrides.setProperty("stopVerificationTask.path", this.getStopVerificationURL());
cmdOverrides.setProperty("stopVerificationDistanceTask.path", this.getStopVerificationURL());
}
String stopMappingUrl = getStopMappingUrl();
if (stopMappingUrl != null) {
cmdOverrides.setProperty("stopConsolidationFileTask.stopConsolidationUrl", stopMappingUrl);
}
creator.setAdditionalBeanPropertyOverrides(cmdOverrides);
BeanDefinitionBuilder propertyOverrides = BeanDefinitionBuilder.genericBeanDefinition(PropertyOverrideConfigurer.class);
propertyOverrides.addPropertyValue("properties", cmdOverrides);
beans.put("myCustomPropertyOverrides", propertyOverrides.getBeanDefinition());
// manage our own context to recover from exceptions
Map<String, BeanDefinition> contextBeans = new HashMap<String, BeanDefinition>();
contextBeans.putAll(beans);
context = ContainerLibrary.createContext(contextPaths, contextBeans);
creator.setContext(context);
response.addStatusMessage("building bundle");
monitorStatus(response, creator.getStatusMessages());
creator.run();
demonitorStatus();
// If this is a rebuild of a bundle, re-use the previous bundle id.
updateBundleId(request, response);
response.addStatusMessage("bundle build complete");
return 0;
} catch (Exception e) {
_log.error(e.toString(), e);
response.setException(e);
return 1;
} catch (Throwable t) {
_log.error(t.toString(), t);
response.setException(new RuntimeException(t.toString()));
return -1;
} finally {
if (context != null) {
try {
/*
* here we cleanup the spring context so we can process follow on requests.
*/
context.stop();
context.close();
} catch (Throwable t) {
_log.error("buried context close:", t);
}
}
// restore standard out
deconfigureLogging(System.out);
System.setOut(stdOut);
if (logFile != null) {
logFile.close();
}
}
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project OpenTripPlanner by opentripplanner.
the class GtfsModule method buildGraph.
@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
// we're about to add another agency to the graph, so clear the cached timezone
// in case it should change
// OTP doesn't currently support multiple time zones in a single graph;
// at least this way we catch the error and log it instead of silently ignoring
// because the time zone from the first agency is cached
graph.clearTimeZone();
MultiCalendarServiceImpl service = new MultiCalendarServiceImpl();
GtfsStopContext stopContext = new GtfsStopContext();
try {
for (GtfsBundle gtfsBundle : gtfsBundles) {
// apply global defaults to individual GTFSBundles (if globals have been set)
if (cacheDirectory != null && gtfsBundle.cacheDirectory == null)
gtfsBundle.cacheDirectory = cacheDirectory;
if (useCached != null && gtfsBundle.useCached == null)
gtfsBundle.useCached = useCached;
GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
GtfsContext context = GtfsLibrary.createContext(gtfsBundle.getFeedId(), dao, service);
GTFSPatternHopFactory hf = new GTFSPatternHopFactory(context);
hf.setStopContext(stopContext);
hf.setFareServiceFactory(_fareServiceFactory);
hf.setMaxStopToShapeSnapDistance(gtfsBundle.getMaxStopToShapeSnapDistance());
loadBundle(gtfsBundle, graph, dao);
CalendarServiceDataFactoryImpl csfactory = new CalendarServiceDataFactoryImpl();
csfactory.setGtfsDao(dao);
CalendarServiceData data = csfactory.createData();
service.addData(data, dao);
hf.subwayAccessTime = gtfsBundle.subwayAccessTime;
hf.maxInterlineDistance = gtfsBundle.maxInterlineDistance;
hf.run(graph);
if (gtfsBundle.doesTransfersTxtDefineStationPaths()) {
hf.createTransfersTxtTransfers();
}
if (gtfsBundle.linkStopsToParentStations) {
hf.linkStopsToParentStations(graph);
}
if (gtfsBundle.parentStationTransfers) {
hf.createParentStationTransfers();
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// We need to save the calendar service data so we can use it later
CalendarServiceData data = service.getData();
graph.putService(CalendarServiceData.class, data);
graph.updateTransitFeedValidity(data);
graph.hasTransit = true;
graph.calculateTransitCenter();
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class GtfsMerger method run.
public void run(List<File> inputPaths, File outputPath) throws IOException {
GtfsRelationalDaoImpl mergedDao = new GtfsRelationalDaoImpl();
mergedDao.setPackShapePoints(true);
mergedDao.setPackStopTimes(true);
List<EntityMergeStrategy> strategies = new ArrayList<EntityMergeStrategy>();
buildStrategies(strategies);
/**
* For each entity merge strategy, we keep track of a mapping from raw GTFS
* ids to entities, if the particular entity type has an identifier. This
* will be used to detect id conflicts between subsequent runs of each merge
* strategy on different feeds. We can't use the AgencyAndId ids in the DAO
* because it might be possible for two entities with the same id but
* different agency prefixes to sneak in. Since we ultimately serialize the
* data to a GTFS feed with no agency prefixes, we need to track the raw id.
*/
Map<EntityMergeStrategy, Map<String, Object>> rawEntityIdMapsByMergeStrategy = new HashMap<EntityMergeStrategy, Map<String, Object>>();
for (EntityMergeStrategy strategy : strategies) {
rawEntityIdMapsByMergeStrategy.put(strategy, new HashMap<String, Object>());
}
/**
* We iterate over the input feeds in reverse order, such that entities from
* the newest feeds are added first and older entities are potentially
* dropped.
*/
long newestFile = Long.MIN_VALUE;
for (int index = inputPaths.size() - 1; index >= 0; --index) {
File inputPath = inputPaths.get(index);
String prefix = getIndexAsPrefix(index, inputPaths.size());
FileTime fileTime = null;
if (inputPath.isFile()) {
fileTime = ((FileTime) Files.readAttributes(inputPath.toPath(), "lastModifiedTime").get("lastModifiedTime"));
if (fileTime != null && fileTime.toMillis() > newestFile) {
newestFile = fileTime.toMillis();
}
}
_log.info("reading input: " + inputPath + " with lastModifiedTime " + fileTime);
GtfsReader reader = new GtfsReader();
reader.setInputLocation(inputPath);
GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
dao.setPackShapePoints(true);
dao.setPackStopTimes(true);
reader.setEntityStore(dao);
reader.run();
for (EntityMergeStrategy strategy : strategies) {
_log.info("strategy=" + strategy.getClass());
GtfsMergeContext context = new GtfsMergeContext(dao, mergedDao, prefix, rawEntityIdMapsByMergeStrategy.get(strategy));
strategy.merge(context);
}
}
_log.info("writing merged output: " + outputPath);
GtfsWriter writer = new GtfsWriter();
writer.setOutputLocation(outputPath);
writer.run(mergedDao);
if (outputPath.isFile()) {
_log.info("setting merged file lastModified to " + new Date(newestFile));
Files.setAttribute(outputPath.toPath(), "lastModifiedTime", FileTime.fromMillis(newestFile));
} else {
_log.info("outputPath not a file, skipping");
}
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class FareAtrributeAgencyTest method testAgenciesOnBartGtfs.
@Test
public void testAgenciesOnBartGtfs() throws Exception {
GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
GtfsReader gtfsReader = new GtfsReader();
gtfsReader.setEntityStore(dao);
gtfsReader.setInputLocation(GtfsTestData.getBartGtfs());
gtfsReader.run();
final Collection<FareAttribute> fareAttributes = dao.getAllFareAttributes();
final Set<String> agencyIdsInFareAttr = fareAttributes.stream().map(FareAttribute::getAgencyId).collect(Collectors.toSet());
boolean isContainsBothExpectedAgencies = agencyIdsInFareAttr.contains("AirBART") && agencyIdsInFareAttr.contains("BART");
if (!isContainsBothExpectedAgencies) {
throw new Exception("Does not contain one of the expected agencies in the fare attributes");
}
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class UpdateLibrary method clearDaoCache.
public static void clearDaoCache(GtfsRelationalDao dao) {
if (dao instanceof GtfsRelationalDaoImpl) {
GtfsRelationalDaoImpl daoImpl = (GtfsRelationalDaoImpl) dao;
daoImpl.clearAllCaches();
}
}
Aggregations