use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class SubsectionTripTransformStrategy 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()) {
String routeId = trip.getRoute().getId().getId();
List<SubsectionOperation> operations = _operationsByRouteId.get(routeId);
if (operations == null) {
continue;
}
List<StopTime> stopTimes = dao.getStopTimesForTrip(trip);
Map<String, Integer> stopToIndex = getStopIndices(stopTimes);
boolean removeOriginalTrip = false;
for (SubsectionOperation operation : operations) {
Integer fromIndex = stopToIndex.get(operation.getFromStopId());
Integer toIndex = stopToIndex.get(operation.getToStopId());
if (fromIndex == null && toIndex == null) {
if (operation.removeUnmatchedTrips) {
removeOriginalTrip = true;
}
continue;
}
removeOriginalTrip = true;
Trip newTrip = new Trip(trip);
String id = newTrip.getId().getId();
if (fromIndex != null) {
id += "-" + operation.getFromStopId();
}
if (toIndex != null) {
id += "-" + operation.getToStopId();
}
if (fromIndex == null) {
fromIndex = 0;
} else if (!operation.isIncludeFromStop()) {
fromIndex++;
}
if (toIndex == null) {
toIndex = stopTimes.size() - 1;
} else if (!operation.isIncludeToStop()) {
toIndex--;
}
newTrip.setId(new AgencyAndId("1", id));
tripsToAdd.add(newTrip);
List<StopTime> newStopTimes = new ArrayList<StopTime>();
for (int i = fromIndex; i <= toIndex; ++i) {
StopTime stopTime = new StopTime(stopTimes.get(i));
stopTime.setId(0);
stopTime.setTrip(newTrip);
newStopTimes.add(stopTime);
}
updateShape(dao, newTrip, newStopTimes, newShapeIds);
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);
}
((GtfsRelationalDaoImpl) dao).clearAllCaches();
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);
}
}
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class EntityRetentionGraphTest method setup.
@Before
public void setup() throws IOException, URISyntaxException {
_dao = new GtfsRelationalDaoImpl();
_graph = new EntityRetentionGraph(_dao);
GtfsReader reader = new GtfsReader();
File path = new File(getClass().getResource("/org/onebusaway/gtfs_transformer/testagency").toURI());
reader.setInputLocation(path);
reader.setEntityStore(_dao);
reader.run();
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class AbstractTestSupport method transform.
protected GtfsRelationalDao transform() {
try {
_transformer.run();
GtfsReader reader = new GtfsReader();
reader.setInputLocation(_outputPath);
GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
reader.setEntityStore(dao);
reader.run();
return dao;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class TransformFactoryTest method testReplaceValueInUpdate.
@Test
public void testReplaceValueInUpdate() throws IOException, TransformSpecificationException {
_factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_headsign': 's/Downtown/Uptown/'}}");
GtfsTransformStrategy transform = _transformer.getLastTransform();
TransformContext context = new TransformContext();
GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
Trip trip = new Trip();
trip.setId(new AgencyAndId("1", "1"));
trip.setTripHeadsign("Downtown Express");
dao.saveEntity(trip);
transform.run(context, dao);
assertEquals("Uptown Express", trip.getTripHeadsign());
}
use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.
the class TransformFactoryTest method testReplaceIdInUpdateComplex.
@Test
public void testReplaceIdInUpdateComplex() throws IOException, TransformSpecificationException {
_factory.addModificationsFromString("{'op':'update', " + "'match':{'file':'trips.txt'}, " + "'update':{'trip_id': 's/^([^_]*)_([0-9]*).*/$2/'}}");
GtfsTransformStrategy transform = _transformer.getLastTransform();
TransformContext context = new TransformContext();
context.setDefaultAgencyId("2");
GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
Trip trip = new Trip();
trip.setId(new AgencyAndId("2", "1234-this-text-to-remove"));
dao.saveEntity(trip);
transform.run(context, dao);
assertEquals("1234", trip.getId().getId());
}
Aggregations