Search in sources :

Example 11 with GtfsMutableRelationalDao

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

the class CalendarExtensionStrategyTest method testCalendarDateSelectiveExtension.

@Test
public void testCalendarDateSelectiveExtension() throws IOException {
    /**
     * Tuesday is only partially active for sid0
     */
    _gtfs.putCalendarDates("sid0=20121210,20121211,20121212,20121213,20121214," + "20121217,20121219,20121220,20121221," + "20121224,20121226,20121227,20121228", "sid1=20121208,20121209,20121215,20121216");
    GtfsMutableRelationalDao dao = _gtfs.read();
    ServiceDate endDate = new ServiceDate(2013, 01, 06);
    _strategy.setEndDate(endDate);
    _strategy.run(_context, dao);
    CalendarService service = CalendarServiceDataFactoryImpl.createService(dao);
    {
        Set<ServiceDate> dates = service.getServiceDatesForServiceId(new AgencyAndId("a0", "sid0"));
        assertEquals(17, dates.size());
        assertTrue(dates.contains(new ServiceDate(2012, 12, 31)));
        assertFalse(dates.contains(new ServiceDate(2013, 01, 01)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 02)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 03)));
        assertTrue(dates.contains(new ServiceDate(2013, 01, 04)));
    }
    {
        Set<ServiceDate> dates = service.getServiceDatesForServiceId(new AgencyAndId("a0", "sid1"));
        assertEquals(4, dates.size());
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Set(java.util.Set) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) Test(org.junit.Test)

Example 12 with GtfsMutableRelationalDao

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

the class CalendarExtensionStrategyTest method testCalendarSelectiveExtension.

@Test
public void testCalendarSelectiveExtension() throws IOException {
    _gtfs.putCalendars(2, "start_date=20110630,20120701", "end_date=20120630,20121231", "mask=1111111");
    GtfsMutableRelationalDao dao = _gtfs.read();
    ServiceDate endDate = new ServiceDate(2013, 12, 31);
    _strategy.setEndDate(endDate);
    _strategy.run(_context, dao);
    {
        ServiceCalendar calendar = dao.getCalendarForServiceId(new AgencyAndId("a0", "sid0"));
        assertEquals(new ServiceDate(2012, 6, 30), calendar.getEndDate());
    }
    {
        ServiceCalendar calendar = dao.getCalendarForServiceId(new AgencyAndId("a0", "sid1"));
        assertEquals(endDate, calendar.getEndDate());
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 13 with GtfsMutableRelationalDao

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

the class CalendarSimplicationLibraryTest method test.

@Test
public void test() throws IOException {
    MockGtfs gtfs = MockGtfs.create();
    gtfs.putAgencies(1, "agency_timezone=America/New_York");
    gtfs.putRoutes(1);
    gtfs.putTrips(1, "r0", "sid0");
    gtfs.putStops(1);
    gtfs.putStopTimes("t0", "s0");
    gtfs.putCalendarDates("sid0=20120305,20120306,20120307,20120308,20120309," + "20120312,20120313,20120314,20120315,20120316,20120319,20120320,20120321,20120323," + "20120326,20120327,20120328,20120329,20120330");
    GtfsMutableRelationalDao dao = gtfs.read();
    CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(dao);
    AgencyAndId originalId = new AgencyAndId("a0", "sid0");
    AgencyAndId updatedId = new AgencyAndId("a0", "sidX");
    ServiceCalendarSummary summary = _library.getSummaryForServiceDates(calendarService.getServiceDatesForServiceId(originalId));
    List<Object> newEntities = new ArrayList<Object>();
    _library.computeSimplifiedCalendar(updatedId, summary, newEntities);
    List<ServiceCalendar> calendars = getEntities(newEntities, ServiceCalendar.class);
    assertEquals(1, calendars.size());
    ServiceCalendar calendar = calendars.get(0);
    assertEquals(updatedId, calendar.getServiceId());
    assertEquals(new ServiceDate(2012, 03, 05), calendar.getStartDate());
    assertEquals(new ServiceDate(2012, 03, 30), calendar.getEndDate());
    assertEquals(1, calendar.getMonday());
    assertEquals(1, calendar.getTuesday());
    assertEquals(1, calendar.getWednesday());
    assertEquals(1, calendar.getThursday());
    assertEquals(1, calendar.getFriday());
    assertEquals(0, calendar.getSaturday());
    assertEquals(0, calendar.getSunday());
    List<ServiceCalendarDate> calendarDates = getEntities(newEntities, ServiceCalendarDate.class);
    assertEquals(1, calendarDates.size());
    ServiceCalendarDate date = calendarDates.get(0);
    assertEquals(updatedId, date.getServiceId());
    assertEquals(new ServiceDate(2012, 03, 22), date.getDate());
    assertEquals(ServiceCalendarDate.EXCEPTION_TYPE_REMOVE, date.getExceptionType());
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) MockGtfs(org.onebusaway.gtfs.services.MockGtfs) ArrayList(java.util.ArrayList) ServiceCalendarSummary(org.onebusaway.gtfs_transformer.updates.CalendarSimplicationLibrary.ServiceCalendarSummary) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 14 with GtfsMutableRelationalDao

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

the class GtfsReaderTest method testDefaultAgencyForRoutes.

@Test
public void testDefaultAgencyForRoutes() throws IOException {
    MockGtfs gtfs = MockGtfs.create();
    gtfs.putAgencies(1);
    gtfs.putRoutes(1);
    gtfs.putTrips(1, "r0", "sid0");
    gtfs.putStops(1);
    gtfs.putStopTimes("t0", "s0");
    {
        GtfsMutableRelationalDao dao = gtfs.read(newReader("tacos"));
        assertNotNull(dao.getRouteForId(new AgencyAndId("a0", "r0")));
    }
    {
        gtfs.putAgencies(2);
        try {
            gtfs.read(newReader("tacos"));
            fail();
        } catch (CsvEntityIOException e) {
            MissingRequiredFieldException ex = (MissingRequiredFieldException) e.getCause();
            assertEquals("agency_id", ex.getFieldName());
            assertEquals(Route.class, ex.getEntityType());
        }
    }
    {
        gtfs.putAgencies(2);
        gtfs.putRoutes(1, "agency_id=a1");
        GtfsMutableRelationalDao dao = gtfs.read(newReader("tacos"));
        assertNotNull(dao.getRouteForId(new AgencyAndId("a1", "r0")));
    }
    {
        gtfs.putAgencies(2);
        gtfs.putRoutes(1, "agency_id=a2");
        try {
            gtfs.read(newReader("tacos"));
            fail();
        } catch (CsvEntityIOException e) {
            assertTrue(e.getCause() instanceof AgencyNotFoundForRouteException);
        }
    }
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) MissingRequiredFieldException(org.onebusaway.csv_entities.exceptions.MissingRequiredFieldException) CsvEntityIOException(org.onebusaway.csv_entities.exceptions.CsvEntityIOException) MockGtfs(org.onebusaway.gtfs.services.MockGtfs) AgencyNotFoundForRouteException(org.onebusaway.gtfs.serialization.mappings.AgencyNotFoundForRouteException) Test(org.junit.Test)

Example 15 with GtfsMutableRelationalDao

use of org.onebusaway.gtfs.services.GtfsMutableRelationalDao in project onebusaway-application-modules by camsys.

the class GtfsArchiveTask method run.

@Override
public void run() {
    if (!requestResponse.getRequest().getArchiveFlag()) {
        _log.info("archive flag not set, exiting");
        return;
    }
    long start = SystemTime.currentTimeMillis();
    _log.info("archiving gtfs");
    Configuration config = getConfiguration();
    if (config == null) {
        _log.error("missing configuration, GTFS will not be archived");
        return;
    }
    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    HibernateGtfsFactory factory = new HibernateGtfsFactory(sessionFactory);
    GtfsBundles gtfsBundles = getGtfsBundles(_applicationContext);
    Integer gtfsBundleInfoId = createMetaData(session, requestResponse);
    for (GtfsBundle gtfsBundle : gtfsBundles.getBundles()) {
        GtfsReader reader = new GtfsReader();
        reader.getEntityClasses().add(PatternPair.class);
        try {
            cleanTempTables(session);
            reader.setInputLocation(gtfsBundle.getPath());
            GtfsMutableRelationalDao dao = factory.getDao();
            reader.setEntityStore(dao);
            _log.info("running for gtfs=" + gtfsBundle.getPath());
            reader.run();
            reader.close();
            archiveData(session, gtfsBundleInfoId);
        } catch (IOException e) {
            _log.error("gtfs archive failure:", e);
        }
    }
    cleanTempTables(session);
    transaction.commit();
    session.flush();
    session.close();
    long stop = SystemTime.currentTimeMillis();
    _log.info("archiving gtfs complete in " + (stop - start) / 1000 + "s");
}
Also used : SessionFactory(org.hibernate.SessionFactory) GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) Configuration(org.hibernate.cfg.Configuration) Transaction(org.hibernate.Transaction) GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) GtfsBundles(org.onebusaway.transit_data_federation.bundle.model.GtfsBundles) IOException(java.io.IOException) HibernateGtfsFactory(org.onebusaway.gtfs.services.HibernateGtfsFactory) Session(org.hibernate.classic.Session)

Aggregations

GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)29 Test (org.junit.Test)16 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)15 Trip (org.onebusaway.gtfs.model.Trip)9 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)7 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)7 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)5 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)5 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)4 Stop (org.onebusaway.gtfs.model.Stop)4 MockGtfs (org.onebusaway.gtfs.services.MockGtfs)4 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)4 TransformContext (org.onebusaway.gtfs_transformer.services.TransformContext)4 Collection (java.util.Collection)3 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)3 StopTime (org.onebusaway.gtfs.model.StopTime)3 HibernateGtfsFactory (org.onebusaway.gtfs.services.HibernateGtfsFactory)3 TypedEntityMatch (org.onebusaway.gtfs_transformer.match.TypedEntityMatch)3 TrimOperation (org.onebusaway.gtfs_transformer.updates.TrimTripTransformStrategy.TrimOperation)3 File (java.io.File)2