Search in sources :

Example 1 with HibernateGtfsFactory

use of org.onebusaway.gtfs.services.HibernateGtfsFactory in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsHibernateReaderExampleMain method main.

public static void main(String[] args) throws IOException {
    if (!(args.length == 1 || args.length == 2)) {
        System.err.println("usage: gtfsPath [hibernate-config.xml]");
        System.exit(-1);
    }
    String resource = "classpath:org/onebusaway/gtfs/examples/hibernate-configuration-examples.xml";
    if (args.length == 2)
        resource = args[1];
    HibernateGtfsFactory factory = createHibernateGtfsFactory(resource);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));
    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    reader.run();
    Collection<Stop> stops = dao.getAllStops();
    for (Stop stop : stops) System.out.println(stop.getName());
    CalendarService calendarService = factory.getCalendarService();
    Set<AgencyAndId> serviceIds = calendarService.getServiceIds();
    for (AgencyAndId serviceId : serviceIds) {
        Set<ServiceDate> dates = calendarService.getServiceDatesForServiceId(serviceId);
        ServiceDate from = null;
        ServiceDate to = null;
        for (ServiceDate date : dates) {
            from = min(from, date);
            to = max(to, date);
        }
        System.out.println("serviceId=" + serviceId + " from=" + from + " to=" + to);
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService)

Example 2 with HibernateGtfsFactory

use of org.onebusaway.gtfs.services.HibernateGtfsFactory in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsDatabaseLoaderMain method run.

private void run(String[] args) throws IOException {
    CommandLine cli = parseCommandLineOptions(args);
    args = cli.getArgs();
    if (args.length != 1) {
        printUsage();
        System.exit(-1);
    }
    Configuration config = new Configuration();
    config.setProperty("hibernate.connection.driver_class", cli.getOptionValue(ARG_DRIVER_CLASS));
    config.setProperty("hibernate.connection.url", cli.getOptionValue(ARG_URL));
    if (cli.hasOption(ARG_USERNAME)) {
        config.setProperty("hibernate.connection.username", cli.getOptionValue(ARG_USERNAME));
    }
    if (cli.hasOption(ARG_PASSWORD)) {
        config.setProperty("hibernate.connection.password", cli.getOptionValue(ARG_PASSWORD));
    }
    config.setProperty("hibernate.connection.pool_size", "1");
    config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");
    config.setProperty("hibernate.hbm2ddl.auto", "update");
    config.addResource("org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml");
    config.addResource("org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.hibernate.xml");
    SessionFactory sessionFactory = config.buildSessionFactory();
    HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);
    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));
    GtfsMutableRelationalDao dao = factory.getDao();
    reader.setEntityStore(dao);
    reader.run();
    reader.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) CommandLine(org.apache.commons.cli.CommandLine) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File)

Example 3 with HibernateGtfsFactory

use of org.onebusaway.gtfs.services.HibernateGtfsFactory in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsHibernateReaderExampleMain method createHibernateGtfsFactory.

private static HibernateGtfsFactory createHibernateGtfsFactory(String resource) {
    Configuration config = new Configuration();
    if (resource.startsWith(KEY_CLASSPATH)) {
        resource = resource.substring(KEY_CLASSPATH.length());
        config = config.configure(resource);
    } else if (resource.startsWith(KEY_FILE)) {
        resource = resource.substring(KEY_FILE.length());
        config = config.configure(new File(resource));
    } else {
        config = config.configure(new File(resource));
    }
    SessionFactory sessionFactory = config.buildSessionFactory();
    return new HibernateGtfsFactory(sessionFactory);
}
Also used : SessionFactory(org.hibernate.SessionFactory) Configuration(org.hibernate.cfg.Configuration) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) File(java.io.File)

Aggregations

File (java.io.File)3 HibernateGtfsFactory (org.onebusaway.gtfs.services.HibernateGtfsFactory)3 SessionFactory (org.hibernate.SessionFactory)2 Configuration (org.hibernate.cfg.Configuration)2 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)2 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)2 CommandLine (org.apache.commons.cli.CommandLine)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 Stop (org.onebusaway.gtfs.model.Stop)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)1