use of org.onebusaway.admin.service.bundle.task.model.ArchivedAgency 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;
}
use of org.onebusaway.admin.service.bundle.task.model.ArchivedAgency in project onebusaway-application-modules by camsys.
the class GtfsArchiveDaoImpl method getAllAgenciesByBundleId.
/**
* Accesses the database to get the all the archived agencies for the
* specified bundle id (gid from gtfs_bundle_info).
*
* @return a List of the ArchivedCalendar entries for this buildId
*/
@Override
public List<ArchivedAgency> getAllAgenciesByBundleId(int buildId) {
Session session = _template.getSessionFactory().getCurrentSession();
Transaction tx = session.beginTransaction();
List<ArchivedAgency> archivedAgencies = _template.findByNamedParam("from ArchivedAgency where gtfsBundleInfoId=:id", "id", buildId);
tx.commit();
return archivedAgencies;
}
Aggregations