From dcba065c99d91ce6fc41db7581ffb91c4270814e Mon Sep 17 00:00:00 2001 From: Scott Berkley <981852+sberkley@users.noreply.github.com> Date: Mon, 2 Dec 2024 16:38:57 -0800 Subject: [PATCH 1/2] clean up capitalization and whitespace --- .../gtfs/impl/HibernateGtfsRelationalDaoImpl.java | 13 +++++++------ .../java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java | 4 +++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java index 6d89eb10..d2fc9129 100644 --- a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java +++ b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java @@ -70,12 +70,12 @@ public T getEntityForId(Class type, Serializable id) { @Override public List getAllAgencies() { - return _ops.find("from Agency"); + return _ops.find("FROM Agency"); } @Override public List getAllBlocks() { - return _ops.find("from Block"); + return _ops.find("FROM Block"); } @Override @@ -294,7 +294,7 @@ public Trip getTripForId(AgencyAndId id) { @Override public Collection getAllAreas() { - return _ops.find("from Area"); + return _ops.find("FROM Area"); } @Deprecated @@ -327,7 +327,7 @@ public Collection getAllLocationGroups() { } @Override public Collection getAllStopAreas() { - return _ops.find("from StopArea"); + return _ops.find("FROM StopArea"); } @Override public Collection getAllLocations() { @@ -336,12 +336,13 @@ public Collection getAllLocations() { @Override public Collection getAllBookingRules() { - return _ops.find("from BookingRule"); + return _ops.find("FROM BookingRule"); } @Override public Collection getAllTranslations() { - return _ops.find("from Translation"); + return _ops.find("FROM Translation"); + } } @Override diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java index 772a21dd..637b967d 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java @@ -146,7 +146,9 @@ public Collection getAllRiderships() { return getAllEntitiesForType(Ridership.class); } - public Collection getAllVehicles() { return getAllEntitiesForType(Vehicle.class); } + public Collection getAllVehicles() { + return getAllEntitiesForType(Vehicle.class); + } public Collection getAllLevels() { return getAllEntitiesForType(Level.class); From 9b10e5b445e799960ef37da742f74b03c18751d2 Mon Sep 17 00:00:00 2001 From: Scott Berkley <981852+sberkley@users.noreply.github.com> Date: Mon, 2 Dec 2024 16:39:48 -0800 Subject: [PATCH 2/2] Add Networks model Network model for networks.txt File has network_id and network_name --- .../impl/HibernateGtfsRelationalDaoImpl.java | 4 + .../org/onebusaway/gtfs/impl/GtfsDaoImpl.java | 3 + .../gtfs/impl/GtfsDataServiceImpl.java | 5 + .../org/onebusaway/gtfs/model/Network.java | 55 ++++++++ .../GtfsEntitySchemaFactory.java | 1 + .../gtfs/serialization/GtfsReader.java | 1 + .../org/onebusaway/gtfs/services/GtfsDao.java | 6 + .../onebusaway/gtfs/model/NetworkTest.java | 117 ++++++++++++++++++ 8 files changed, 192 insertions(+) create mode 100644 onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/Network.java create mode 100644 onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/model/NetworkTest.java diff --git a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java index d2fc9129..67bcdfbb 100644 --- a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java +++ b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java @@ -343,6 +343,10 @@ public Collection getAllBookingRules() { public Collection getAllTranslations() { return _ops.find("FROM Translation"); } + + @Override + public Collection getAllNetworks() { + return _ops.find("FROM Network"); } @Override diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java index 637b967d..f814ee12 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDaoImpl.java @@ -259,6 +259,9 @@ public Vehicle getVehicleForId(AgencyAndId id) { return getEntityForId(Vehicle.class, id); } + public Collection getAllNetworks() { + return getAllEntitiesForType(Network.class); + } public Facility getFacilityForId(AgencyAndId id) { return getEntityForId(Facility.class, id);} diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDataServiceImpl.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDataServiceImpl.java index 2cd915d9..d67aa9df 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDataServiceImpl.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/impl/GtfsDataServiceImpl.java @@ -403,6 +403,11 @@ public Collection getAllTranslations() { return _dao.getAllTranslations(); } + @Override + public Collection getAllNetworks() { + return _dao.getAllNetworks(); + } + @Override public List getOptionalMetadataFilenames() { return _dao.getOptionalMetadataFilenames(); diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/Network.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/Network.java new file mode 100644 index 00000000..f07be594 --- /dev/null +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/Network.java @@ -0,0 +1,55 @@ +/** + * Copyright (C) 2024 Sound Transit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onebusaway.gtfs.model; + +import org.onebusaway.csv_entities.schema.annotations.CsvField; +import org.onebusaway.csv_entities.schema.annotations.CsvFields; +import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory; + +@CsvFields(filename = "networks.txt", required = false) +public final class Network extends IdentityBean { + + private static final long serialVersionUID = 1L; + + @CsvField(name = "network_id", mapping = DefaultAgencyIdFieldMappingFactory.class) + private AgencyAndId id; + + @CsvField(name = "network_name", optional = true) + private String name; + + @Override + public AgencyAndId getId() { + return id; + } + + @Override + public void setId(AgencyAndId id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return ""; + } +} diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java index f630683d..c6df1c68 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java @@ -76,6 +76,7 @@ public static List> getEntityClasses() { entityClasses.add(DirectionEntry.class); entityClasses.add(AlternateStopNameException.class); entityClasses.add(Icon.class); + entityClasses.add(Network.class); return entityClasses; } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java index d44571bc..a3a09b8a 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java @@ -104,6 +104,7 @@ public GtfsReader() { _entityClasses.add(WrongWayConcurrency.class); _entityClasses.add(DirectionEntry.class); _entityClasses.add(AlternateStopNameException.class); + _entityClasses.add(Network.class); CsvTokenizerStrategy tokenizerStrategy = new CsvTokenizerStrategy(); tokenizerStrategy.getCsvParser().setTrimInitialWhitespace(true); diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/services/GtfsDao.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/services/GtfsDao.java index 9311b738..9d51be15 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/services/GtfsDao.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/services/GtfsDao.java @@ -220,6 +220,12 @@ public interface GtfsDao extends GenericDao { public Collection getAllTranslations(); + /**** + * {@link Network} Methods + ****/ + + public Collection getAllNetworks(); + public Collection getAllDirectionEntries(); public Collection getAllWrongWayConcurrencies(); diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/model/NetworkTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/model/NetworkTest.java new file mode 100644 index 00000000..55290655 --- /dev/null +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/model/NetworkTest.java @@ -0,0 +1,117 @@ +/** + * Copyright (C) 2024 Sound Transit + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onebusaway.gtfs.model; + +import java.io.File; +import java.io.IOException; +import java.util.*; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.onebusaway.gtfs.serialization.GtfsWriter; +import org.onebusaway.gtfs.serialization.GtfsWriterTest; +import org.onebusaway.gtfs.services.MockGtfs; +import org.onebusaway.gtfs.services.GtfsRelationalDao; + +import static org.junit.jupiter.api.Assertions.*; + +public class NetworkTest { + + private MockGtfs _gtfs; + + private File _tmpDirectory; + + @BeforeEach + public void before() throws IOException { + _gtfs = MockGtfs.create(); + + //make temp directory for gtfs writing output + _tmpDirectory = File.createTempFile("NetworkTest-", "-tmp"); + if (_tmpDirectory.exists()) + GtfsWriterTest.deleteFileRecursively(_tmpDirectory); + _tmpDirectory.mkdirs(); + } + + @Test + public void testBasicNetworks() throws IOException { + _gtfs.putMinimal(); + _gtfs.putDefaultTrips(); + _gtfs.putDefaultStops(); + _gtfs.putLines("networks.txt", + "network_id,network_name", + "rail_network,Rail Network", "bus_network,Bus Network"); + + GtfsRelationalDao dao = _gtfs.read(); + assertEquals(2, dao.getAllNetworks().size()); + + GtfsWriter writer = new GtfsWriter(); + writer.setOutputLocation(_tmpDirectory); + writer.run(dao); + + Scanner scan = new Scanner(new File(_tmpDirectory + "/networks.txt")); + Set expectedNetworkNames = new HashSet(); + Set foundNetworkNames = new HashSet(); + expectedNetworkNames.add("Rail Network"); + expectedNetworkNames.add("Bus Network"); + boolean onHeader = true; + while(scan.hasNext()){ + String line = scan.nextLine(); + if (onHeader) { + onHeader = false; + } else { + String[] lineParts = line.split(","); + + if (lineParts.length > 1) { + foundNetworkNames.add(lineParts[1]); + } + } + } + scan.close(); + + assertEquals(expectedNetworkNames, foundNetworkNames, "Did not find network names in output"); + } + + @Test + public void testPutMinimal() throws IOException { + _gtfs.putMinimal(); + // Just make sure it parses without throwing an error. + _gtfs.read(); + } + + @AfterEach + public void teardown() { + deleteFileRecursively(_tmpDirectory); + } + + public static void deleteFileRecursively(File file) { + + if (!file.exists()) + return; + + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (files != null) { + for (File child : files) + deleteFileRecursively(child); + } + } + + file.delete(); + } + +} +