PageRenderTime 90ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/it/polito/wifidata/datafiltering/busstopsapi/LineManager.java

https://bitbucket.org/Francescino/thesis
Java | 113 lines | 101 code | 8 blank | 4 comment | 12 complexity | 59b6dceda727a60926fc3b708661b083 MD5 | raw file
  1. package it.polito.wifidata.datafiltering.busstopsapi;
  2. import com.mongodb.MongoClient;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.MongoDatabase;
  5. import it.polito.wifidata.MongoClientSingleton;
  6. import it.polito.wifidata.datafiltering.busstopsapi.pojo.BusLineData;
  7. import it.polito.wifidata.datafiltering.busstopsapi.pojo.Itinerary;
  8. import it.polito.wifidata.datafiltering.busstopsapi.pojo.Response;
  9. import org.bson.Document;
  10. import retrofit2.Retrofit;
  11. import retrofit2.converter.gson.GsonConverterFactory;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Comparator;
  15. import java.util.List;
  16. import java.util.function.Predicate;
  17. public class LineManager {
  18. // TODO add locality id in the future
  19. private static final String BASE_URL = "https://www.oise-mobilite.fr/";
  20. private static final String COLLECTION_NAME = "routerIdToLineAssociation";
  21. private static final String LINE_ID_FIELD = "linesId";
  22. public static List<String> getLineIds(String routerId) {
  23. MongoClient mongoClient = MongoClientSingleton.getInstance();
  24. MongoDatabase lineMappingConnections = mongoClient.getDatabase(COLLECTION_NAME);
  25. MongoCollection<Document> lineIdsCollection = lineMappingConnections.getCollection(routerId);
  26. List<String> linesId = new ArrayList<>();
  27. for (Document document : lineIdsCollection.find()) {
  28. linesId = (ArrayList<String>) document.get(LINE_ID_FIELD);
  29. }
  30. return linesId;
  31. }
  32. public static List<String> getRegisteredRoutersId() {
  33. MongoClient mongoClient = MongoClientSingleton.getInstance();
  34. MongoDatabase lineMappingConnections = mongoClient.getDatabase(COLLECTION_NAME);
  35. List<String> res = new ArrayList<>();
  36. for (String listCollectionName : lineMappingConnections.listCollectionNames()) {
  37. res.add(listCollectionName);
  38. }
  39. return res;
  40. }
  41. public static List<BusLineData> getLinePaths(String routerId) {
  42. // use mongo to discover which lines are associated to routerId
  43. Retrofit retrofit = retrofit = new Retrofit.Builder()
  44. .baseUrl(BASE_URL)
  45. .addConverterFactory(GsonConverterFactory.create())
  46. .build();
  47. BusPathService busPathService = retrofit.create(BusPathService.class);
  48. List<BusLineData> result = new ArrayList<>();
  49. List<String> linesId = LineManager.getLineIds(routerId);
  50. for (String lineId : linesId) {
  51. retrofit2.Response<Response> response = null;
  52. try {
  53. response = busPathService.getBusLine(lineId).execute();
  54. it.polito.wifidata.datafiltering.busstopsapi.pojo.Response responseBusPath = response.body();
  55. if (responseBusPath != null) {
  56. BusLineData busData = responseBusPath.getBusData();
  57. orderAndFilter(busData);
  58. result.add(busData);
  59. }
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. return result;
  65. }
  66. private static void orderAndFilter(BusLineData lineData) {
  67. // order by size
  68. lineData.getItineraries().sort(new Comparator<Itinerary>() {
  69. @Override
  70. public int compare(Itinerary o1, Itinerary o2) {
  71. return o2.getStopPointIds().size() - o1.getStopPointIds().size();
  72. }
  73. });
  74. // and do cleaning
  75. List<Integer> toRemove = new ArrayList<>();
  76. for (int i = 0; i < lineData.getItineraries().size() - 1; i++) {
  77. Itinerary itinerary1 = lineData.getItineraries().get(i);
  78. for (int j = i + 1; j < lineData.getItineraries().size(); j++) {
  79. Itinerary itinerary2 = lineData.getItineraries().get(j);
  80. if (itinerary1.getStopPointIds().size()
  81. == itinerary2.getStopPointIds().size()) {
  82. boolean diff = false;
  83. for (int k = 0; k < itinerary1.getStopPointIds().size(); k++) {
  84. if (!itinerary1.getStopPointIds()
  85. .get(k)
  86. .equals(itinerary2.getStopPointIds()
  87. .get(k))) {
  88. diff = true;
  89. break;
  90. }
  91. }
  92. if (!diff) {
  93. toRemove.add(itinerary2.getId());
  94. }
  95. }
  96. }
  97. }
  98. lineData.getItineraries().removeIf(new Predicate<Itinerary>() {
  99. @Override
  100. public boolean test(Itinerary itinerary) {
  101. return toRemove.contains(itinerary.getId());
  102. }
  103. });
  104. }
  105. }