Package com.nnamo.services
Class DatabaseService
java.lang.Object
com.nnamo.services.DatabaseService
Service class for managing database operations
related to GTFS data, users, favorites, routes,
stops, and metrics.
- Initializes DAOs and tables
- Imports and caches GTFS data
- Provides query methods for models
- Handles favorite stops and routes
- Supports fuzzy search for stops and routes
- Manages realtime metrics and trip updates
- Author:
- Samuele Lombardi, Davide Galilei
-
Constructor Summary
ConstructorsConstructorDescriptionDatabaseService
(com.j256.ormlite.jdbc.JdbcConnectionSource connection) Creates the DatabaseService by creating a connection to the SQLite database and by initializing tables and DAOsDatabaseService
(String connectionUrl) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addFavRoute
(int userId, String routeId) Add route to user's favorite routesvoid
addFavStop
(int userId, String stopId) Add favorite stop to a user's favorite stops listvoid
addFavStop
(UserModel user, StopModel stop) Add favorite stop to a user's favorite stops listvoid
Creates user from modelvoid
Creates user from username and password<ID,
MODEL>
voidbatchCreate
(Class<MODEL> modelClass, List<MODEL> data) Batch create or update multiple models<ID,
MODEL>
voidbatchCreateOrUpdate
(Class<MODEL> modelClass, List<MODEL> data) Batch create or update multiple modelsvoid
createTripUpdateDelays
(List<com.google.transit.realtime.GtfsRealtime.FeedEntity> tripEntities) Create trip update delays from GTFS realtime feed entitiesGet all realtime metrics grouped by typeReturns every stop instance from the databaseint
getAverageDelayForRoute
(String routeId) Calculate average delay for a route based on trip updates<T,
ID> com.j256.ormlite.dao.Dao <T, ID> Returns a Model's DAO (Database Access Object) by providing the Class of the modelgetDirectionedRoutes
(List<RouteModel> routes) For each route in the provided list, this method creates a directioned route for each available direction.getDirectionTrip
(String routeId, Direction direction) Get a trip for a specific route and directiongetFavoriteDirectionRoutes
(int userId) Get user's favorite routes with direction informationgetFavoriteRoutes
(int userId) Get user's favorite routesgetFavoriteRoutesByName
(int userId, String searchTerm) Get user's favorite routes.getFavoriteStops
(int userId) Get user's favorite stopsgetFavoriteStops
(UserModel user) Get user's favorite stopsgetFavoriteStopsByName
(int userId, String searchTerm) Get user's favorite stops.getMetrics
(RealtimeMetricType type) Get metrics of a specific typegetNextStopTimes
(String stopId, LocalTime time) Gets the next 6 hours stop times for a specific stop based on the time providedgetNextStopTimes
(String stopId, LocalTime time, int hours) Gets the next X hours stop times for a specific stop based on the time providedgetNextStopTimes
(String stopId, LocalTime time, int hours, Date date, List<RealtimeStopUpdate> tripUpdates) Gets the next X hours stop times for a specific stop based on the time provided and the date provided.getNextStopTimes
(String stopId, LocalTime time, Date date, int hours) Gets the next X hours stop times for a specific stop based on the time provided and the date provided.getOrderedStopsForRoute
(String routeId, Direction direction) Get ordered stops for a route in a specific directiongetRouteById
(String id) Get route by IDgetRoutesByName
(String searchTerm) Fuzzy search routes by its name.getRouteTripUpdates
(String routeId) Get trip updates for a specific routegetServicesById
(String serviceId) Get services by IDgetStaticPosition
(String routeId, Direction direction, LocalTime fromTime) getStopById
(String id) Returns a stop by its IDgetStopRoutes
(String stopId) Gets the unique routes that go through a specific stopgetStopsByName
(String searchTerm) Fuzzy search stops by its name.getStopTimes
(String stopId) Gets the stop times for a specific stopgetTripServices
(TripModel trip) Get services of a specific trip by modelgetTripServices
(String tripId) Get services of a specific trip by IDgetUserById
(int id) Get user by IDgetUserByName
(String username) Get user by the usernamevoid
boolean
isFavoriteStop
(int userId, String stopId) Check if a stop is in user's favoritesboolean
isFavouriteRoute
(int userId, String routeId) Check if a route is in user's favoritesboolean
Checks if database needs cachingvoid
Loads static GTFS data in the databasevoid
removeFavRoute
(int userId, String routeId) Remove route from user's favorite routesvoid
removeFavStop
(int userId, String stopId) Remove favorite stop from a user's favorite stops listvoid
saveMetric
(RealtimeMetricType type, int value) Save a realtime metric to the databasevoid
-
Constructor Details
-
DatabaseService
Creates the DatabaseService by creating a connection to the SQLite database and by initializing tables and DAOs- Throws:
SQLException
-
DatabaseService
- Throws:
SQLException
-
DatabaseService
- Throws:
SQLException
-
-
Method Details
-
initTables
- Throws:
SQLException
-
wipeTables
- Throws:
SQLException
-
preloadGtfsData
public void preloadGtfsData(StaticGtfsService gtfs) throws SQLException, IOException, URISyntaxException Loads static GTFS data in the database- Parameters:
gtfs
- StaticGtfsService the GTFS service- Throws:
SQLException
- if query to the connection goes wrongIOException
- if static gtfs data can be loadedURISyntaxException
- if static gtfs data can be loaded
-
needsCaching
Checks if database needs caching- Throws:
SQLException
- if query to the connection goes wrong
-
getDao
Returns a Model's DAO (Database Access Object) by providing the Class of the model- Parameters:
modelClass
- the Class of the model to return- Returns:
- Dao if the model's dao exists, null otherwise
-
getAllStops
Returns every stop instance from the database- Returns:
- stop models (empty list if no stop found)
- Throws:
SQLException
-
getStopById
Returns a stop by its ID- Parameters:
id
- String- Returns:
- StopModel
- Throws:
SQLException
-
getStopsByName
Fuzzy search stops by its name. Fuzzy finding means finding strings that match a pattern approximately- Parameters:
searchTerm
- String- Returns:
- the list of models with the approximate match
- Throws:
SQLException
- if query fails
-
getRoutesByName
Fuzzy search routes by its name. Fuzzy finding means finding strings that match a pattern approximately- Parameters:
searchTerm
- The search term for the route- Returns:
- the list of routes with the approximate match
- Throws:
SQLException
- if query fails
-
getDirectionedRoutes
For each route in the provided list, this method creates a directioned route for each available direction. Which means that the method returns all the provided routes but for each directions. It is pretty useful in order to display routes' trips based on the direction (OUTGOING, INGOING)- Parameters:
routes
- The routes which we want for each direction- Returns:
- the list of directioned routes
- Throws:
SQLException
- if query fails
-
getStopRoutes
Gets the unique routes that go through a specific stop- Parameters:
stopId
- The ID of the stop- Returns:
- the list of routes that go through that stop
- Throws:
SQLException
- if query fails
-
getStopTimes
Gets the stop times for a specific stop- Parameters:
stopId
- The ID of the stop- Returns:
- the list of ALL stoptimes for that stop
- Throws:
SQLException
- if query fails
-
getNextStopTimes
Gets the next 6 hours stop times for a specific stop based on the time provided- Parameters:
stopId
- The ID of the stoptime
- the time from when we want the stop times to start- Returns:
- the list of next 6 hours (based on the time provided) stoptimes for that stop
- Throws:
SQLException
- if query fails
-
getNextStopTimes
public List<StopTimeModel> getNextStopTimes(String stopId, LocalTime time, int hours) throws SQLException Gets the next X hours stop times for a specific stop based on the time provided- Parameters:
stopId
- The ID of the stoptime
- the time from when we want the stop times to starthours
- the hours range from the time provided- Returns:
- the list of next X hours (based on the time provided) stoptimes for that stop
- Throws:
SQLException
- if query fails
-
getNextStopTimes
public List<StopTimeModel> getNextStopTimes(String stopId, LocalTime time, Date date, int hours) throws SQLException Gets the next X hours stop times for a specific stop based on the time provided and the date provided. This returns only the stop times for trips that run on the provided day.- Parameters:
stopId
- The ID of the stoptime
- the time from when we want the stop times to startdate
- the date of the returned stop timeshours
- the hours range from the time provided- Returns:
- the list of next X hours (based on the time and date provided) stoptimes for that stop
- Throws:
SQLException
- if query fails
-
getNextStopTimes
public List<StopTimeModel> getNextStopTimes(String stopId, LocalTime time, int hours, Date date, List<RealtimeStopUpdate> tripUpdates) throws SQLException Gets the next X hours stop times for a specific stop based on the time provided and the date provided. This returns only the stop times for trips that run on the provided day. It also provides the static stop times of realtime trips- Parameters:
stopId
- The ID of the stoptime
- the time from when we want the stop times to starthours
- the hours range from the time provideddate
- the date of the returned stop timestripUpdates
- the trip updates for that stop- Returns:
- the list of next X hours (based on the time and date provided + the realtime trips) stoptimes for that stop
- Throws:
SQLException
- if query fails
-
getServicesById
Get services by ID- Parameters:
serviceId
- The id of the services to get- Returns:
- services with that ID
- Throws:
SQLException
- if query fails
-
getTripServices
Get services of a specific trip by model- Parameters:
trip
- The model trip with the services- Returns:
- trip's services
- Throws:
SQLException
- if query fails
-
getTripServices
Get services of a specific trip by ID- Parameters:
tripId
- The ID of the trip with the services- Returns:
- trip's services
- Throws:
SQLException
- if query fails
-
addUser
Creates user from model- Parameters:
user
- User model- Throws:
SQLException
- if query fails
-
addUser
Creates user from username and password- Parameters:
username
- User's usernamepasswordHash
- User's password- Throws:
SQLException
- if query fails
-
getUserByName
Get user by the username- Parameters:
username
- User's username- Returns:
- the user with the provided name, or null if doesn't exist
- Throws:
SQLException
- if query fails
-
getUserById
Get user by ID- Parameters:
id
- User's id- Returns:
- the user with the provided ID, or null if doesn't exist
- Throws:
SQLException
- if query fails
-
addFavStop
Add favorite stop to a user's favorite stops list- Parameters:
userId
- The id of the user adding the favorite stopstopId
- The id of the favorite stop- Throws:
SQLException
- if query fails
-
addFavStop
Add favorite stop to a user's favorite stops list- Parameters:
user
- User modelstop
- Stop model- Throws:
SQLException
- if query fails
-
removeFavStop
Remove favorite stop from a user's favorite stops list- Parameters:
userId
- The id of the user adding the favorite stopstopId
- The id of the favorite stop- Throws:
SQLException
- if query fails
-
getFavoriteStops
Get user's favorite stops- Parameters:
userId
- The id of the user who has the favorite stops- Returns:
- list of user's favorite stops
- Throws:
SQLException
- if query fails
-
getFavoriteStops
Get user's favorite stops- Parameters:
user
- The model of the user who has the favorite stops- Returns:
- list of user's favorite stops
- Throws:
SQLException
- if query fails
-
getFavoriteStopsByName
Get user's favorite stops. Fuzzy search- Parameters:
userId
- The id of the user adding the favorite stopsearchTerm
- Fuzzy pattern to search the stops- Throws:
SQLException
- if query fails
-
getFavoriteRoutesByName
public List<RouteDirection> getFavoriteRoutesByName(int userId, String searchTerm) throws SQLException Get user's favorite routes. Fuzzy search- Parameters:
userId
- The id of the user adding the favorite stopsearchTerm
- Fuzzy pattern to search the stops tram, etc.)- Throws:
SQLException
- if query fails
-
getRouteById
Get route by ID- Parameters:
id
- route ID- Returns:
- RouteModel with the specified ID, or null if not found
- Throws:
SQLException
- if query fails
-
getDirectionTrip
Get a trip for a specific route and direction- Parameters:
routeId
- String route IDdirection
- Direction enum (INBOUND/OUTBOUND)- Returns:
- TripModel for the route in the specified direction, or null if not found
- Throws:
SQLException
- if query fails
-
getStaticPosition
public StaticVehiclePosition getStaticPosition(String routeId, Direction direction, LocalTime fromTime) throws SQLException - Parameters:
routeId
- String route IDdirection
- Direction enum (INBOUND/OUTBOUND)- Returns:
- TripModel for the route in the specified direction, or null if not found
- Throws:
SQLException
- if query fails
-
getOrderedStopsForRoute
public List<StopModel> getOrderedStopsForRoute(String routeId, Direction direction) throws SQLException Get ordered stops for a route in a specific direction- Parameters:
routeId
- String route IDdirection
- Direction enum (INBOUND/OUTBOUND)- Returns:
- List of stops ordered by arrival time for the route
- Throws:
SQLException
- if query fails
-
getAverageDelayForRoute
Calculate average delay for a route based on trip updates- Parameters:
routeId
- String route ID- Returns:
- average delay in seconds, or 0 if no delays found
- Throws:
SQLException
- if query fails
-
getRouteTripUpdates
Get trip updates for a specific route- Parameters:
routeId
- String route ID- Returns:
- List of trip updates for the route
- Throws:
SQLException
- if query fails
-
createTripUpdateDelays
public void createTripUpdateDelays(List<com.google.transit.realtime.GtfsRealtime.FeedEntity> tripEntities) throws SQLException Create trip update delays from GTFS realtime feed entities- Parameters:
tripEntities
- List of GTFS realtime feed entities- Throws:
SQLException
- if query fails
-
saveMetric
Save a realtime metric to the database- Parameters:
type
- RealtimeMetricType the type of metricvalue
- int the metric value- Throws:
SQLException
- if query fails
-
getMetrics
Get metrics of a specific type- Parameters:
type
- RealtimeMetricType the type of metric to retrieve- Returns:
- List of metrics ordered by creation date (newest first)
- Throws:
SQLException
- if query fails
-
batchCreateOrUpdate
Batch create or update multiple models- Type Parameters:
ID
- ID typeMODEL
- Model type- Parameters:
modelClass
- Class of the modeldata
- List of models to create or update
-
batchCreate
Batch create or update multiple models- Type Parameters:
ID
- ID typeMODEL
- Model type- Parameters:
modelClass
- Class of the modeldata
- List of models to create or update
-
isFavoriteStop
Check if a stop is in user's favorites- Parameters:
userId
- int user IDstopId
- String stop ID- Returns:
- true if stop is favorite, false otherwise
- Throws:
SQLException
- if query fails
-
addFavRoute
Add route to user's favorite routes- Parameters:
userId
- int user IDrouteId
- String route ID- Throws:
SQLException
- if query fails
-
removeFavRoute
Remove route from user's favorite routes- Parameters:
userId
- int user IDrouteId
- String route ID- Throws:
SQLException
- if query fails
-
getFavoriteRoutes
Get user's favorite routes- Parameters:
userId
- int user ID- Returns:
- List of user's favorite routes
- Throws:
SQLException
- if query fails
-
getFavoriteDirectionRoutes
Get user's favorite routes with direction information- Parameters:
userId
- int user ID- Returns:
- List of directioned routes from user's favorites
- Throws:
SQLException
- if query fails
-
isFavouriteRoute
Check if a route is in user's favorites- Parameters:
userId
- int user IDrouteId
- String route ID- Returns:
- true if route is favorite, false otherwise
- Throws:
SQLException
- if query fails
-
getAllMetrics
Get all realtime metrics grouped by type- Returns:
- Map of RealtimeMetricType to list of RealtimeMetricModel
- Throws:
SQLException
- if query fails
-