Search in sources :

Example 56 with AgencyAndId

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

the class TrimTripTransformStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    List<Trip> tripsToAdd = new ArrayList<Trip>();
    List<StopTime> stopTimesToAdd = new ArrayList<StopTime>();
    List<Trip> tripsToRemove = new ArrayList<Trip>();
    List<StopTime> stopTimesToRemove = new ArrayList<StopTime>();
    Set<AgencyAndId> newShapeIds = new HashSet<AgencyAndId>();
    for (Trip trip : dao.getAllTrips()) {
        List<TrimOperation> operations = getMatchingOperations(trip);
        if (operations.isEmpty()) {
            continue;
        }
        List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
        Map<String, Integer> stopToIndex = getStopIndices(stopTimes);
        boolean removeOriginalTrip = false;
        for (TrimOperation operation : operations) {
            Integer preIndex = stopToIndex.get(operation.getToStopId());
            Integer postIndex = stopToIndex.get(operation.getFromStopId());
            if (postIndex == null && preIndex == null) {
                continue;
            }
            removeOriginalTrip = true;
            Trip newTrip = new Trip(trip);
            String id = newTrip.getId().getId();
            if (preIndex != null) {
                id += "-" + operation.getToStopId();
            }
            if (postIndex != null) {
                id += "-" + operation.getFromStopId();
            }
            if (preIndex == null) {
                preIndex = 0;
            } else {
                preIndex++;
            }
            if (postIndex == null) {
                postIndex = stopTimes.size() - 1;
            } else {
                postIndex--;
            }
            newTrip.setId(new AgencyAndId(trip.getId().getAgencyId(), id));
            List<StopTime> newStopTimes = new ArrayList<StopTime>();
            for (int i = preIndex; i <= postIndex; ++i) {
                StopTime stopTime = new StopTime(stopTimes.get(i));
                stopTime.setId(0);
                stopTime.setTrip(newTrip);
                newStopTimes.add(stopTime);
            }
            /**
         * If the entire trip was trimmed, we just drop it.
         */
            if (newStopTimes.isEmpty()) {
                continue;
            }
            updateShape(dao, newTrip, newStopTimes, newShapeIds);
            tripsToAdd.add(newTrip);
            stopTimesToAdd.addAll(newStopTimes);
        }
        if (removeOriginalTrip) {
            tripsToRemove.add(trip);
            stopTimesToRemove.addAll(stopTimes);
        }
    }
    for (StopTime stopTime : stopTimesToRemove) {
        dao.removeEntity(stopTime);
    }
    for (Trip trip : tripsToRemove) {
        dao.removeEntity(trip);
    }
    for (Trip trip : tripsToAdd) {
        dao.saveEntity(trip);
    }
    for (StopTime stopTime : stopTimesToAdd) {
        dao.saveEntity(stopTime);
    }
    UpdateLibrary.clearDaoCache(dao);
    Set<AgencyAndId> shapeIds = new HashSet<AgencyAndId>(dao.getAllShapeIds());
    for (Trip trip : dao.getAllTrips()) {
        shapeIds.remove(trip.getShapeId());
    }
    for (AgencyAndId shapeId : shapeIds) {
        for (ShapePoint point : dao.getShapePointsForShapeId(shapeId)) {
            dao.removeEntity(point);
        }
    }
}
Also used : Trip(org.onebusaway.gtfs.model.Trip) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) ArrayList(java.util.ArrayList) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) StopTime(org.onebusaway.gtfs.model.StopTime) HashSet(java.util.HashSet)

Example 57 with AgencyAndId

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

the class AddEntityTest method test.

@Test
public void test() throws IOException {
    _gtfs.putAgencies(1);
    _gtfs.putStops(1);
    _gtfs.putRoutes(1);
    _gtfs.putTrips(1, "r0", "sid0");
    _gtfs.putStopTimes("t0", "s0");
    addModification("{'op':'add','obj':{'class':'Frequency','trip':'t0','startTime':'08:00:00','endTime':'10:00:00','headwaySecs':600}}");
    GtfsRelationalDao dao = transform();
    Collection<Frequency> frequencies = dao.getAllFrequencies();
    assertEquals(1, frequencies.size());
    Frequency frequency = frequencies.iterator().next();
    assertSame(dao.getTripForId(new AgencyAndId("a0", "t0")), frequency.getTrip());
    assertEquals(StopTimeFieldMappingFactory.getStringAsSeconds("08:00:00"), frequency.getStartTime());
    assertEquals(StopTimeFieldMappingFactory.getStringAsSeconds("10:00:00"), frequency.getEndTime());
    assertEquals(600, frequency.getHeadwaySecs());
}
Also used : GtfsRelationalDao(org.onebusaway.gtfs.services.GtfsRelationalDao) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Frequency(org.onebusaway.gtfs.model.Frequency) Test(org.junit.Test)

Example 58 with AgencyAndId

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

the class AgencyMergeStrategy method bulkRenameAgencyIdInProperties.

private <T> void bulkRenameAgencyIdInProperties(Iterable<T> elements, String oldAgencyId, String newAgencyId, String... properties) {
    for (Object element : elements) {
        BeanWrapper wrapped = BeanWrapperFactory.wrap(element);
        for (String property : properties) {
            AgencyAndId id = (AgencyAndId) wrapped.getPropertyValue(property);
            if (id != null && id.getAgencyId().equals(oldAgencyId)) {
                AgencyAndId updatedId = new AgencyAndId(newAgencyId, id.getId());
                wrapped.setPropertyValue(property, updatedId);
            }
        }
    }
}
Also used : BeanWrapper(org.onebusaway.csv_entities.schema.BeanWrapper) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId)

Example 59 with AgencyAndId

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

the class AgencyMergeStrategy method bulkRenameAgencyId.

private <T extends IdentityBean<AgencyAndId>> void bulkRenameAgencyId(Iterable<T> elements, String oldAgencyId, String newAgencyId) {
    for (T element : elements) {
        AgencyAndId id = element.getId();
        if (id.getAgencyId().equals(oldAgencyId)) {
            AgencyAndId newId = new AgencyAndId(newAgencyId, id.getId());
            element.setId(newId);
        }
    }
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId)

Example 60 with AgencyAndId

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

the class DeferredValueMatcher method matches.

public boolean matches(Class<?> parentEntityType, String propertyName, Object value) {
    if (value == null) {
        return _value == null;
    } else if (_value == null) {
        return false;
    }
    if (_resolvedValueSet) {
        return value.equals(_resolvedValue);
    }
    Class<?> expectedValueType = value.getClass();
    Class<?> actualValueType = _value.getClass();
    if (expectedValueType.isAssignableFrom(actualValueType)) {
        return value.equals(_value);
    }
    if (actualValueType == String.class) {
        String actualValue = (String) _value;
        if (expectedValueType == AgencyAndId.class) {
            AgencyAndId expectedId = (AgencyAndId) value;
            return expectedId.getId().equals(actualValue);
        } else if (IdentityBean.class.isAssignableFrom(expectedValueType)) {
            IdentityBean<?> bean = (IdentityBean<?>) value;
            Object expectedId = bean.getId();
            if (expectedId == null) {
                return false;
            }
            if (expectedId instanceof AgencyAndId) {
                AgencyAndId expectedFullId = (AgencyAndId) expectedId;
                return expectedFullId.getId().equals(actualValue);
            } else if (expectedId instanceof String) {
                return expectedId.equals(actualValue);
            }
        } else {
            Converter converter = _support.resolveConverter(parentEntityType, propertyName, expectedValueType);
            if (converter != null) {
                _resolvedValue = converter.convert(expectedValueType, _value);
                _resolvedValueSet = true;
                return value.equals(_resolvedValue);
            } else {
                throw new IllegalStateException("no type conversion from type String to type \"" + expectedValueType.getName() + "\" for value comparison");
            }
        }
    }
    throw new IllegalStateException("no type conversion from type \"" + actualValueType.getName() + "\" to type \"" + expectedValueType.getName() + "\" for value comparison");
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Converter(org.apache.commons.beanutils.Converter) IdentityBean(org.onebusaway.gtfs.model.IdentityBean)

Aggregations

AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)102 Test (org.junit.Test)63 Trip (org.onebusaway.gtfs.model.Trip)37 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)25 Stop (org.onebusaway.gtfs.model.Stop)24 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)17 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)16 ArrayList (java.util.ArrayList)15 Route (org.onebusaway.gtfs.model.Route)15 StopTime (org.onebusaway.gtfs.model.StopTime)15 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)15 Agency (org.onebusaway.gtfs.model.Agency)13 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)12 GtfsRelationalDao (org.onebusaway.gtfs.services.GtfsRelationalDao)11 HashSet (java.util.HashSet)10 GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)10 List (java.util.List)9 Frequency (org.onebusaway.gtfs.model.Frequency)9 CalendarService (org.onebusaway.gtfs.services.calendar.CalendarService)7 Set (java.util.Set)6