Search in sources :

Example 1 with ArchivedTrip

use of org.onebusaway.admin.service.bundle.task.model.ArchivedTrip in project onebusaway-application-modules by camsys.

the class CompareBundlesAction method buildModes.

private List<DataValidationMode> buildModes(int buildId) {
    List<DataValidationMode> modes = new ArrayList<>();
    // Check service ids
    List<ArchivedCalendar> calendars = _gtfsArchiveService.getAllCalendarsByBundleId(buildId);
    // Get dates for checking trips for days of the week
    LocalDate startDate = getStartDate(calendars);
    LocalDate firstMon = getFirstDay(DateTimeConstants.MONDAY, startDate);
    LocalDate firstTues = getFirstDay(DateTimeConstants.TUESDAY, startDate);
    LocalDate firstWed = getFirstDay(DateTimeConstants.WEDNESDAY, startDate);
    LocalDate firstThur = getFirstDay(DateTimeConstants.THURSDAY, startDate);
    LocalDate firstFri = getFirstDay(DateTimeConstants.FRIDAY, startDate);
    LocalDate firstSat = getFirstDay(DateTimeConstants.SATURDAY, startDate);
    LocalDate firstSun = getFirstDay(DateTimeConstants.SUNDAY, startDate);
    // Get the service ids for weekdays, Saturdays, and Sundays
    Set<AgencyAndId> weekdaySvcIds = new HashSet<>();
    Set<AgencyAndId> saturdaySvcIds = new HashSet<>();
    Set<AgencyAndId> sundaySvcIds = new HashSet<>();
    for (ArchivedCalendar calendar : calendars) {
        Date svcStartDate = calendar.getStartDate().getAsDate();
        LocalDate jodaStartDate = new LocalDate(svcStartDate);
        Date svcEndDate = calendar.getEndDate().getAsDate();
        LocalDate jodaEndDate = new LocalDate(svcEndDate);
        if (calendar.getMonday() == 1 && !firstMon.isBefore(jodaStartDate) && !firstMon.isAfter(jodaEndDate)) {
            weekdaySvcIds.add(calendar.getServiceId());
        }
        if (calendar.getTuesday() == 1 && !firstTues.isBefore(jodaStartDate) && !firstTues.isAfter(jodaEndDate)) {
            weekdaySvcIds.add(calendar.getServiceId());
        }
        if (calendar.getWednesday() == 1 && !firstWed.isBefore(jodaStartDate) && !firstWed.isAfter(jodaEndDate)) {
            weekdaySvcIds.add(calendar.getServiceId());
        }
        if (calendar.getThursday() == 1 && !firstThur.isBefore(jodaStartDate) && !firstThur.isAfter(jodaEndDate)) {
            weekdaySvcIds.add(calendar.getServiceId());
        }
        if (calendar.getFriday() == 1 && !firstFri.isBefore(jodaStartDate) && !firstFri.isAfter(jodaEndDate)) {
            weekdaySvcIds.add(calendar.getServiceId());
        }
        if (calendar.getSaturday() == 1 && !firstSat.isBefore(jodaStartDate) && !firstSat.isAfter(jodaEndDate)) {
            saturdaySvcIds.add(calendar.getServiceId());
        }
        if (calendar.getSunday() == 1 && !firstSun.isBefore(jodaStartDate) && !firstSun.isAfter(jodaEndDate)) {
            sundaySvcIds.add(calendar.getServiceId());
        }
    }
    // Get all the routes for this build id
    List<ArchivedRoute> allRoutes = _gtfsArchiveService.getAllRoutesByBundleId(buildId);
    // Get all the trips for this build id
    List<ArchivedTrip> allTrips = _gtfsArchiveService.getAllTripsByBundleId(buildId);
    // Get trip stop countt for all the trips in this build
    // This call returns a list of arrays of Objects of the form
    // [String trip_agencyId, String trip_id, int stop_count]
    List allStopCts = _gtfsArchiveService.getTripStopCounts(buildId);
    Map<AgencyAndId, Integer> tripStopCounts = new HashMap<>();
    for (int i = 0; i < allStopCts.size(); ++i) {
        Object[] tripStopCt = (Object[]) allStopCts.get(i);
        String tripAgencyId = (String) tripStopCt[0];
        String tripId = (String) tripStopCt[1];
        int stopCt = (int) tripStopCt[2];
        AgencyAndId agencyAndId = new AgencyAndId(tripAgencyId, tripId);
        tripStopCounts.put(agencyAndId, stopCt);
    }
    Map<String, List<String>> reportModes = getReportModes();
    Collection<ArchivedAgency> agencies = _gtfsArchiveService.getAllAgenciesByBundleId(buildId);
    for (String currentMode : reportModes.keySet()) {
        DataValidationMode newMode = new DataValidationMode();
        newMode.setModeName(currentMode);
        SortedSet<DataValidationRouteCounts> newModeRouteCts = new TreeSet<DataValidationRouteCounts>();
        // Note: currentRoutes entries might be just an agency id
        List<String> agenciesOrRoutes = reportModes.get(currentMode);
        for (String agencyOrRoute : agenciesOrRoutes) {
            // or a route, i.e. <agencyId>_<routeId>
            // List<ArchivedRoute> routes = null;
            // check if agency or route id
            int idx = agencyOrRoute.indexOf("_");
            int routeCt = allRoutes.size();
            int currentRouteCt = 0;
            for (ArchivedRoute route : allRoutes) {
                if (!route.getAgencyId().equals(agencyOrRoute)) {
                    continue;
                }
                currentRouteCt++;
                int[] wkdayTrips = null;
                int[] satTrips = null;
                int[] sunTrips = null;
                Map<String, TripTotals> tripMap = new HashMap<>();
                String routeId = route.getAgencyId() + ID_SEPARATOR + route.getId();
                DataValidationRouteCounts newRouteCts = new DataValidationRouteCounts();
                String routeName = route.getDesc();
                if (routeName == null || routeName.equals("null") || routeName.isEmpty()) {
                    routeName = route.getLongName();
                }
                if (routeName == null || routeName.equals("null")) {
                    routeName = "";
                }
                newRouteCts.setRouteName(routeName);
                String routeNum = route.getShortName();
                if (routeNum == null || routeNum.equals("null") || routeNum.isEmpty()) {
                    routeNum = route.getId();
                }
                if (routeNum == null || routeNum.equals("null")) {
                    routeNum = "";
                }
                newRouteCts.setRouteNum(routeNum);
                // Build DataValidationHeadsignCts
                SortedSet<DataValidationHeadsignCts> headsignCounts = new TreeSet<>();
                int stopCtIdx = 0;
                for (ArchivedTrip trip : allTrips) {
                    if (trip.getRoute_agencyId().compareTo(route.getAgencyId()) > 0 || (trip.getRoute_agencyId().equals(route.getAgencyId()) && trip.getRoute_id().compareTo(route.getId()) > 0)) {
                        break;
                    }
                    if (!trip.getRoute_agencyId().equals(route.getAgencyId()) || !trip.getRoute_id().equals(route.getId())) {
                        continue;
                    }
                    AgencyAndId tripAgencyAndId = new AgencyAndId(trip.getAgencyId(), trip.getId());
                    int stopCt = tripStopCounts.get(tripAgencyAndId) != null ? tripStopCounts.get(tripAgencyAndId) : 0;
                    if (stopCt > MAX_STOP_CT) {
                        stopCt = MAX_STOP_CT;
                    }
                    TripTotals tripTotals = null;
                    String tripHeadsign = trip.getTripHeadsign();
                    tripHeadsign = tripHeadsign == null ? "" : tripHeadsign;
                    if (tripMap.containsKey(tripHeadsign)) {
                        tripTotals = tripMap.get(tripHeadsign);
                    } else {
                        tripTotals = new TripTotals();
                        tripMap.put(tripHeadsign, tripTotals);
                    }
                    /*
             * TODO: if stopCt exceeds array sizes, resize arrays
             */
                    if (trip.getDirectionId() == null || trip.getDirectionId().equals("0")) {
                        wkdayTrips = tripTotals.wkdayTrips_0;
                        satTrips = tripTotals.satTrips_0;
                        sunTrips = tripTotals.sunTrips_0;
                    } else {
                        wkdayTrips = tripTotals.wkdayTrips_1;
                        satTrips = tripTotals.satTrips_1;
                        sunTrips = tripTotals.sunTrips_1;
                    }
                    AgencyAndId tripSvcId = new AgencyAndId(trip.getServiceId_agencyId(), trip.getServiceId_id());
                    if (weekdaySvcIds.contains(tripSvcId)) {
                        ++wkdayTrips[stopCt];
                    } else if (saturdaySvcIds.contains(tripSvcId)) {
                        ++satTrips[stopCt];
                    } else if (sundaySvcIds.contains(tripSvcId)) {
                        ++sunTrips[stopCt];
                    }
                    tripMap.put(tripHeadsign, tripTotals);
                }
                // End of trips loop.  Stop counts by direction for this route have been set.
                for (String headSign : tripMap.keySet()) {
                    TripTotals tripTotals = tripMap.get(headSign);
                    DataValidationHeadsignCts newHeadsignCt = new DataValidationHeadsignCts();
                    newHeadsignCt.setHeadsign(headSign);
                    SortedSet<DataValidationDirectionCts> newDirCountSet = new TreeSet<DataValidationDirectionCts>();
                    DataValidationDirectionCts newDirCt_0 = new DataValidationDirectionCts();
                    newDirCt_0.setDirection("0");
                    SortedSet<DataValidationStopCt> stopCounts_0 = new TreeSet<>();
                    for (int i = 0; i < MAX_STOP_CT; ++i) {
                        if (tripTotals.wkdayTrips_0[i] > 0 || tripTotals.satTrips_0[i] > 0 || tripTotals.sunTrips_0[i] > 0) {
                            DataValidationStopCt stopCt_0 = new DataValidationStopCt();
                            stopCt_0.setStopCt(i);
                            stopCt_0.setTripCts(new int[] { tripTotals.wkdayTrips_0[i], tripTotals.satTrips_0[i], tripTotals.sunTrips_0[i] });
                            stopCounts_0.add(stopCt_0);
                        }
                    }
                    if (stopCounts_0.size() > 0) {
                        newDirCt_0.setStopCounts(stopCounts_0);
                        newDirCountSet.add(newDirCt_0);
                    }
                    DataValidationDirectionCts newDirCt_1 = new DataValidationDirectionCts();
                    newDirCt_1.setDirection("1");
                    SortedSet<DataValidationStopCt> stopCounts_1 = new TreeSet<>();
                    for (int i = 0; i < MAX_STOP_CT; ++i) {
                        if (tripTotals.wkdayTrips_1[i] > 0 || tripTotals.satTrips_1[i] > 0 || tripTotals.sunTrips_1[i] > 0) {
                            DataValidationStopCt stopCt_1 = new DataValidationStopCt();
                            stopCt_1.setStopCt(i);
                            stopCt_1.setTripCts(new int[] { tripTotals.wkdayTrips_1[i], tripTotals.satTrips_1[i], tripTotals.sunTrips_1[i] });
                            stopCounts_1.add(stopCt_1);
                        }
                        if (stopCounts_1.size() > 0) {
                            newDirCt_1.setStopCounts(stopCounts_1);
                            newDirCountSet.add(newDirCt_1);
                        }
                    }
                    if (newDirCountSet.size() > 0) {
                        newHeadsignCt.setDirCounts(newDirCountSet);
                        headsignCounts.add(newHeadsignCt);
                    }
                }
                if (headsignCounts.size() > 0) {
                    newRouteCts.setHeadsignCounts(headsignCounts);
                    newModeRouteCts.add(newRouteCts);
                }
            }
        }
        if (newModeRouteCts.size() > 0) {
            newMode.setRoutes(newModeRouteCts);
            modes.add(newMode);
        }
    }
    return modes;
}
Also used : DataValidationRouteCounts(org.onebusaway.admin.model.ui.DataValidationRouteCounts) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LocalDate(org.joda.time.LocalDate) ArchivedRoute(org.onebusaway.admin.service.bundle.task.model.ArchivedRoute) ArchivedCalendar(org.onebusaway.admin.service.bundle.task.model.ArchivedCalendar) DataValidationMode(org.onebusaway.admin.model.ui.DataValidationMode) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) ArchivedAgency(org.onebusaway.admin.service.bundle.task.model.ArchivedAgency) HashSet(java.util.HashSet) ArchivedTrip(org.onebusaway.admin.service.bundle.task.model.ArchivedTrip) DataValidationDirectionCts(org.onebusaway.admin.model.ui.DataValidationDirectionCts) Date(java.util.Date) LocalDate(org.joda.time.LocalDate) DataValidationHeadsignCts(org.onebusaway.admin.model.ui.DataValidationHeadsignCts) DataValidationStopCt(org.onebusaway.admin.model.ui.DataValidationStopCt)

Example 2 with ArchivedTrip

use of org.onebusaway.admin.service.bundle.task.model.ArchivedTrip in project onebusaway-application-modules by camsys.

the class GtfsArchiveDaoImpl method getAllTripsByBundleId.

@Override
public List<ArchivedTrip> getAllTripsByBundleId(int buildId) {
    Session session = _template.getSessionFactory().getCurrentSession();
    Transaction tx = session.beginTransaction();
    List<ArchivedTrip> archivedTrips = _template.findByNamedParam("from ArchivedTrip where gtfsBundleInfoId=:id order by route_agencyId, route_id", "id", buildId);
    tx.commit();
    return archivedTrips;
}
Also used : Transaction(org.hibernate.Transaction) ArchivedTrip(org.onebusaway.admin.service.bundle.task.model.ArchivedTrip) Session(org.hibernate.classic.Session)

Example 3 with ArchivedTrip

use of org.onebusaway.admin.service.bundle.task.model.ArchivedTrip in project onebusaway-application-modules by camsys.

the class GtfsArchiveDaoImpl method getTripsForRouteAndBundleId.

@Override
public List<ArchivedTrip> getTripsForRouteAndBundleId(String routeAgencyAndId, int buildId) {
    String routeAgencyId = routeAgencyAndId.split("_")[0];
    String routeId = routeAgencyAndId.split("_")[1];
    Session session = _template.getSessionFactory().getCurrentSession();
    Transaction tx = session.beginTransaction();
    Query query = session.createQuery("from ArchivedTrip where gtfs_bundle_info_id=:build_id and route_agencyId=:routeAgencyId " + "and route_id=:routeId").setParameter("build_id", buildId).setParameter("routeAgencyId", routeAgencyId).setParameter("routeId", routeId);
    List<ArchivedTrip> archivedTrips = query.list();
    tx.commit();
    return archivedTrips;
}
Also used : Transaction(org.hibernate.Transaction) Query(org.hibernate.Query) ArchivedTrip(org.onebusaway.admin.service.bundle.task.model.ArchivedTrip) Session(org.hibernate.classic.Session)

Aggregations

ArchivedTrip (org.onebusaway.admin.service.bundle.task.model.ArchivedTrip)3 Transaction (org.hibernate.Transaction)2 Session (org.hibernate.classic.Session)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 TreeSet (java.util.TreeSet)1 Query (org.hibernate.Query)1 LocalDate (org.joda.time.LocalDate)1 DataValidationDirectionCts (org.onebusaway.admin.model.ui.DataValidationDirectionCts)1 DataValidationHeadsignCts (org.onebusaway.admin.model.ui.DataValidationHeadsignCts)1 DataValidationMode (org.onebusaway.admin.model.ui.DataValidationMode)1 DataValidationRouteCounts (org.onebusaway.admin.model.ui.DataValidationRouteCounts)1 DataValidationStopCt (org.onebusaway.admin.model.ui.DataValidationStopCt)1 ArchivedAgency (org.onebusaway.admin.service.bundle.task.model.ArchivedAgency)1 ArchivedCalendar (org.onebusaway.admin.service.bundle.task.model.ArchivedCalendar)1 ArchivedRoute (org.onebusaway.admin.service.bundle.task.model.ArchivedRoute)1 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1