Search in sources :

Example 91 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class RemoveCurrentService method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    Date today = removeTime(new Date());
    boolean hasEntryToday;
    for (AgencyAndId aai : dao.getAllServiceIds()) {
        hasEntryToday = false;
        for (ServiceCalendarDate calDate : dao.getCalendarDatesForServiceId(aai)) {
            Date date = removeTime(calDate.getDate().getAsDate());
            if (date.equals(today)) {
                hasEntryToday = true;
                if (calDate.getExceptionType() == 1) {
                    calDate.setExceptionType(2);
                    dao.saveOrUpdateEntity(calDate);
                }
            }
        }
        if (!hasEntryToday) {
            _log.info("No entry for today, adding one, id: {}", aai);
            ServiceCalendarDate calendarDate = new ServiceCalendarDate();
            calendarDate.setServiceId(aai);
            calendarDate.setDate(new ServiceDate(today));
            calendarDate.setExceptionType(2);
            dao.saveOrUpdateEntity(calendarDate);
        }
    }
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate)

Example 92 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class CountAndTestSubway method constructDate.

private Date constructDate(ServiceDate date) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, date.getYear());
    calendar.set(Calendar.MONTH, date.getMonth() - 1);
    calendar.set(Calendar.DATE, date.getDay());
    Date date1 = calendar.getTime();
    date1 = removeTime(date1);
    return date1;
}
Also used : Calendar(java.util.Calendar) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date)

Example 93 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class CheckForFutureService method constructDate.

private Date constructDate(ServiceDate date) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, date.getYear());
    calendar.set(Calendar.MONTH, date.getMonth() - 1);
    calendar.set(Calendar.DATE, date.getDay());
    Date date1 = calendar.getTime();
    date1 = removeTime(date1);
    return date1;
}
Also used : Calendar(java.util.Calendar) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) Date(java.util.Date)

Example 94 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class UpdateTripsForSdon method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    GtfsMutableRelationalDao reference = (GtfsMutableRelationalDao) context.getReferenceReader().getEntityStore();
    CalendarService refCalendarService = CalendarServiceDataFactoryImpl.createService(reference);
    String agency = dao.getAllTrips().iterator().next().getId().getAgencyId();
    HashMap<String, Trip> referenceTrips = new HashMap<>();
    for (Trip trip : reference.getAllTrips()) {
        referenceTrips.put(trip.getId().getId(), trip);
    }
    HashMap<String, Trip> atisTrips = new HashMap<>();
    for (Trip trip : dao.getAllTrips()) {
        atisTrips.put(trip.getMtaTripId(), trip);
    }
    // Unique list of calendar dates to add where the key = new service id
    HashMap<String, ArrayList<ServiceDate>> serviceDates = new HashMap<>();
    // for each reference trip, if it contails SDon
    // create a nonSdon string
    // is that id in ATIS?
    // no, do nothing
    // yes - ok, for each calendar date, we need to do something...
    // if the calendar_date is "in" the SDON, then move it there
    // if the calendar_date isn't in SDON, then keep it as it is
    // NO!! can't move them.  The numbers might be used somewhere else.  Arg.  Create new.
    // Don't be sloppy and create duplicates
    // all the stop times for this trip, create new identical ones for the new Sdon trip
    int service_id = getNextServiceId(dao);
    int countdao = 0;
    for (HashMap.Entry<String, Trip> referenceTrip : referenceTrips.entrySet()) {
        Trip refTrip = referenceTrip.getValue();
        if (refTrip.getId().getId().contains("SDon")) {
            String refId = refTrip.getId().getId();
            String refIdMinusSDon = refId.replace("-SDon", "");
            if (atisTrips.containsKey(refIdMinusSDon)) {
                Trip atisTrip = atisTrips.get(refIdMinusSDon);
                // we have an atis trip that 'matches' a reference Sdon trip
                // create a new trip with this id and copy over everything about the trip
                Trip sdonAtisTrip = createTrip(dao, refTrip, atisTrip);
                // create a list of dates that are the service dates for this Sdon trip
                Set<ServiceDate> refServiceDates = refCalendarService.getServiceDatesForServiceId(refTrip.getServiceId());
                // then, for each calendar date from the ATIS trip, if it matches the Sdon trip,
                // create a new list of calendar dates for this service id
                ArrayList<ServiceDate> sdForTrip = new ArrayList<>();
                List<ServiceCalendarDate> atisCalendarDates = dao.getCalendarDatesForServiceId(atisTrip.getServiceId());
                for (ServiceCalendarDate atisScd : atisCalendarDates) {
                    ServiceDate atisDate = atisScd.getDate();
                    if (refServiceDates.contains(atisDate)) {
                        // _log.info("Date match. Ref id: {}, Atis id {}, Atis date {}", refTrip.getId(), atisTrip.getId(), atisDate.toString());
                        sdForTrip.add(atisDate);
                    }
                }
                // see if this list of sdForTrip is unique
                // have a hashmap of service dates, where the service id is the key and the value is the list of dates
                // does the  hashmap have this list of dates?
                // yes, great, take the id and use that for the trip
                // no? ok, create the calendar dates
                AgencyAndId newServiceId = new AgencyAndId(agency, Integer.toString(service_id));
                if (serviceDates.isEmpty()) {
                    serviceDates.put(Integer.toString(service_id), sdForTrip);
                    newServiceId = new AgencyAndId(agency, Integer.toString(service_id));
                    service_id++;
                } else {
                    boolean addNewServiceDate = true;
                    for (HashMap.Entry<String, ArrayList<ServiceDate>> serviceDate : serviceDates.entrySet()) {
                        ArrayList<ServiceDate> scds = (ArrayList<ServiceDate>) serviceDate.getValue();
                        // scds is a unique list of service dates in the map
                        if (new HashSet<ServiceDate>(sdForTrip).equals(new HashSet<ServiceDate>(scds))) {
                            // we already have a list of the same dates.  Re-use the service id
                            addNewServiceDate = false;
                            // set the service date id to be this one
                            newServiceId = new AgencyAndId(agency, serviceDate.getKey());
                            sdonAtisTrip.setServiceId(newServiceId);
                        }
                    }
                    // there was no match, update the date map and add new serviceId
                    if (addNewServiceDate) {
                        // dates don't exist, add new entry
                        serviceDates.put(Integer.toString(service_id), sdForTrip);
                        newServiceId = new AgencyAndId(agency, Integer.toString(service_id));
                        service_id++;
                    }
                }
                sdonAtisTrip.setServiceId(newServiceId);
                for (StopTime stopTime : dao.getStopTimesForTrip(atisTrip)) {
                    StopTime st = new StopTime();
                    st.setTrip(sdonAtisTrip);
                    st.setStop(stopTime.getStop());
                    st.setArrivalTime(stopTime.getArrivalTime());
                    st.setDepartureTime(stopTime.getDepartureTime());
                    st.setStopSequence(stopTime.getStopSequence());
                    st.setDropOffType(stopTime.getDropOffType());
                    st.setDepartureBuffer(stopTime.getDepartureBuffer());
                    dao.saveOrUpdateEntity(st);
                    countdao++;
                }
            }
        }
    }
    _log.info("Adding {} service date ids. Next id {}", serviceDates.size(), getNextServiceId(dao));
    int serviceIds = 0;
    // Now the list is compete, add the new service id and dates
    for (HashMap.Entry<String, ArrayList<ServiceDate>> serviceDatesToAdd : serviceDates.entrySet()) {
        AgencyAndId newServiceId = new AgencyAndId(agency, serviceDatesToAdd.getKey());
        ArrayList<ServiceDate> scds = serviceDatesToAdd.getValue();
        // need a list of the service cal dates, iterate, add
        for (ServiceDate sd : scds) {
            serviceIds++;
            ServiceCalendarDate newScd = new ServiceCalendarDate();
            newScd.setServiceId(newServiceId);
            newScd.setDate(sd);
            // add
            newScd.setExceptionType(1);
            dao.saveOrUpdateEntity(newScd);
        }
    }
    _log.error("Dao count: {}.  Added Service dates: {}", countdao, serviceIds);
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) CalendarService(org.onebusaway.gtfs.services.calendar.CalendarService) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate)

Example 95 with ServiceDate

use of org.onebusaway.gtfs.model.calendar.ServiceDate in project onebusaway-gtfs-modules by OneBusAway.

the class UpdateCalendarDatesForDuplicateTrips method constructDate.

private Date constructDate(ServiceDate date) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, date.getYear());
    calendar.set(Calendar.MONTH, date.getMonth() - 1);
    calendar.set(Calendar.DATE, date.getDay());
    Date date1 = calendar.getTime();
    date1 = removeTime(date1);
    return date1;
}
Also used : ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate)

Aggregations

ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)134 Test (org.junit.Test)49 Date (java.util.Date)46 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)45 ArrayList (java.util.ArrayList)23 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)23 Calendar (java.util.Calendar)22 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)22 HashSet (java.util.HashSet)17 ServiceIdActivation (org.onebusaway.transit_data_federation.services.transit_graph.ServiceIdActivation)14 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)13 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)13 Set (java.util.Set)11 Trip (org.onebusaway.gtfs.model.Trip)11 TimeZone (java.util.TimeZone)10 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)10 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)9 LocalizedServiceId (org.onebusaway.gtfs.model.calendar.LocalizedServiceId)9 Agency (org.onebusaway.gtfs.model.Agency)8 TripDescriptor (com.google.transit.realtime.GtfsRealtime.TripDescriptor)7