PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/co/uk/wmb/db/StorageManager.java

https://gitlab.com/rjschu/wheres-my-bus-
Java | 101 lines | 85 code | 16 blank | 0 comment | 13 complexity | fb7e0f7cbaa64cb77989da57f55ffdf0 MD5 | raw file
  1. package co.uk.wmb.db;
  2. import java.util.List;
  3. import co.uk.wmb.log.Logger;
  4. import co.uk.wmb.model.Bus;
  5. import co.uk.wmb.model.Route;
  6. import co.uk.wmb.model.RouteList;
  7. import co.uk.wmb.util.GsonUtils;
  8. import com.google.common.collect.Lists;
  9. import com.mongodb.CommandResult;
  10. public class StorageManager {
  11. private Database database;
  12. public StorageManager() {
  13. database = Database.getDatabase();
  14. }
  15. public CommandResult getStorageStatus() {
  16. return database.getStatus();
  17. }
  18. public void insertOrUpdateBus(Bus bus) {
  19. String jsonBus = GsonUtils.toJson(bus);
  20. database.insert(jsonBus, Collection.BUS);
  21. }
  22. public void insertOrUpdateBusStopList(Route route) {
  23. if (route.getStops() != null) {
  24. String jsonBus = GsonUtils.toJson(route);
  25. database.insert(jsonBus, Collection.BUS_STOP);
  26. } else {
  27. Logger.DEBUG(StorageManager.class, "No bus stops on route");
  28. }
  29. }
  30. public void insertOrUpdateRouteList(RouteList routeList) {
  31. String jsonBus = GsonUtils.toJson(routeList);
  32. database.insert(jsonBus, Collection.ROUTE_LIST);
  33. }
  34. public Bus[] getBusesOnRoute(String route) {
  35. List<String> busesJson = database.getAll(Collection.BUS, "{Route: \"" + route + "\"}");
  36. if (!busesJson.isEmpty()) {
  37. List<Bus> busList = Lists.newArrayList();
  38. for (String busJson : busesJson) {
  39. busList.add(GsonUtils.fromJson(busJson, Bus.class));
  40. }
  41. return busList.toArray(new Bus[0]);
  42. } else {
  43. Logger.DEBUG(StorageManager.class, "No buses for route " + route);
  44. return new Bus[0];
  45. }
  46. }
  47. public RouteList getRouteList() {
  48. String routesJson = database.getFirst(Collection.ROUTE_LIST);
  49. if (!routesJson.isEmpty()) {
  50. return GsonUtils.fromJson(routesJson, RouteList.class);
  51. }
  52. return new RouteList();
  53. }
  54. public Route getRouteWithStops(String routeName) {
  55. List<String> routesJson = database.getAll(Collection.BUS_STOP, "{Name: \"" + routeName + "\"}");
  56. if (!routesJson.isEmpty()) {
  57. Route route = GsonUtils.fromJson(routesJson.get(0), Route.class);
  58. if (routesJson.size() > 1) {
  59. Logger.DEBUG(StorageManager.class, "Memory Leak! More than one file for BUS_STOP file for route: " + routeName);
  60. }
  61. return route;
  62. } else {
  63. return null;
  64. }
  65. }
  66. public Bus getBusWithId(int id) {
  67. List<String> busesJson = database.getAll(Collection.BUS, "{Id: " + id + "}");
  68. if (!busesJson.isEmpty()) {
  69. if (busesJson.size() > 1) {
  70. Logger.DEBUG(StorageManager.class, "DB Memory Leak! Bus Id: " + id + " is stored " + busesJson.size() + " times!");
  71. }
  72. return GsonUtils.fromJson(busesJson.get(0), Bus.class);
  73. } else {
  74. return null;
  75. }
  76. }
  77. public void clearStorage() {
  78. Logger.INFO(StorageManager.class, "Clearing database");
  79. database.clearDatabase();
  80. }
  81. public void deleteBus(Bus bus) {
  82. String jsonBus = GsonUtils.toJson(bus);
  83. database.remove(jsonBus, Collection.BUS);
  84. }
  85. }