My favorites | Sign in
Project Logo
                
Search
for
Updated Dec 03, 2009 by bdferris
ModuleGtfsHibernateExamples  
Documentation and examples for the `org.onebusaway:onebusaway-gtfs-hibernate` module.

Introduction

The org.onebusaway:onebusaway-gtfs-hiberate module includes code for extending org.onebusaway:onebusaway-gtfs to load GTFS entities into a database and access them using the Hibernate persistence framework.

The onebusaway-gtfs-hibernate module can be found in onebusaway project hierarchy at modules/onebusaway-gtfs-hibernate. If interested, you can check out the source with:

svn co http://onebusaway.googlecode.com/svn/trunk/modules/onebusaway-gtfs-hibernate

Or look at the relevant Javadoc:

org.onebusaway.gtfs.services

Example Code

The org.onebusaway:onebusaway-gtfs-hibernate-examples module includes some example code demonstrating the use of the onebusaway-gtfs-hibernate module.

You can check out the example code from the source repository:

svn co http://onebusaway.googlecode.com/svn/trunk/modules/onebusaway-gtfs-hibernate-examples

The resulting maven project has source files in the standard location: src/main/java and be compiled like any Maven project:

mvn compile

Example #1 - Basic Reading

The class org.onebusaway.gtfs.examples.GtfsHibernateReaderExampleMain includes basic code for reading a GTFS feed into a database and querying the resulting entities.

The sample code has been summarized for length and clarity:

public class GtfsHibernateReaderExampleMain {

  public static void main(String[] args) throws IOException {

    // Check args and construct application context paths
    ...

    ApplicationContext applicationContext = ContainerLibrary.createContext(paths);
    SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");

    GtfsReader reader = new GtfsReader();
    reader.setInputLocation(new File(args[0]));

    HibernateEntityStore store = new HibernateEntityStore();

    store.setSessionFactory(sessionFactory);

    reader.setEntityStore(store);

    store.open();
    reader.run();
    store.close();

    GtfsRelationalDao dao = getBeanOfType(applicationContext,
        GtfsRelationalDao.class);

    Collection<Stop> stops = dao.getAllStops();

    for (Stop stop : stops)
      System.out.println(stop.getName());

    CalendarService calendarService = getBeanOfType(applicationContext,
        CalendarService.class);

    Set<AgencyAndId> serviceIds = calendarService.getServiceIds();

    for (AgencyAndId serviceId : serviceIds) {
      Set<Date> dates = calendarService.getDatesForServiceId(serviceId);
      Date from = null;
      Date to = null;
      for (Date date : dates) {
        from = min(from, date);
        to = max(to, date);
      }

      System.out.println("serviceId=" + serviceId + " from=" + from + " to="
          + to);
    }
  }

  // Other methods
  ...
}

This code is roughly similar to the GtfsReaderExample shown in ModuleGtfsExamples, with the main difference being the use of HibernateEntityStore, which writes entities to the database as they are read by GtfsReader.

You can run the above example with the following Maven command:

mvn exec:java -Dexec.mainClass=org.onebusaway.gtfs.examples.GtfsHibernateReaderExampleMain -Dexec.args=path/to/gtfs.zip

Configuring Your Database

By default, the example above is setup to run with an in-memory HSQLDB database. Obviously, it'd be great to configure it to use a different database and you totally can. I use the Spring IOC Container for app configuration, so I'll couch this configuration example in those terms. However, notice that all the Hibernate GTFS Store really needs is an instance of Hibernate's SessionFactory, which you can construct in the manner of your own choosing. But back to the example. In the code snippet above, I cut the following:

    if (!(args.length == 1 || args.length == 2)) {
      System.err.println("usage: gtfsPath [application-context.xml]");
      System.exit(-1);
    }

    List<String> paths = new ArrayList<String>();
    paths.add("classpath:org/onebusaway/gtfs/examples/application-context.xml");

    if (args.length == 2)
      paths.add("file:" + args[1]);

    ApplicationContext applicationContext = ContainerLibrary.createContext(paths);
    SessionFactory sessionFactory = (SessionFactory) applicationContext.getBean("sessionFactory");

By default, the example program takes one argument: the path to a GTFS feed. However, you can specify a second additional argument that gets added to the Spring ApplicationContext construction phase. You can use this to override and add additional beans to the app context. For example, you could specify the path of an XML file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="overrideHibernateProperties" class="org.onebusaway.container.spring.PropertiesBeanPostProcessor">
        <property name="target" value="hibernateProperties" />
        <property name="properties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>

    <!-- Database Connection Configuration -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://127.0.01/your_db_name_here?characterEncoding=UTF-8" />
        <property name="username" value="USERNAME" />
        <property name="password" value="PASSWORD" />
    </bean>

</beans>

This configuration file specifies a MySQL data source, allowing you to pick your database host, location, username and password. Additionally, it overrides the hibernate.dialect for MySQL use.

Using the Library Through Maven

If you are using Maven, adding this library is easy. Step 1) add the OneBusAway Maven repository:

<repositories>
    <repository>
        <id>releases.developer.onebusaway.org</id>
        <url>http://developer.onebusaway.org/maven/repository/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
    <repository>
        <id>snapshots.developer.onebusaway.org</id>
        <url>http://developer.onebusaway.org/maven/snapshot-repository/</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    ...
</repositories>

Step 2) add the dependencies:

<dependencies>
    <dependency>
        <groupId>org.onebusaway</groupId>
        <artifactId>onebusaway-gtfs-hibernate</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>1.9.0-rc4-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>3.1.14</version>
    </dependency>
    <!-- Or your DB of choice -->
    ...
</dependencies>

Sign in to add a comment
Hosted by Google Code