PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/traveldashboard/server/data/BusStopGetterUtils.java

https://bitbucket.org/ambientic/traveldashboard-server
Java | 100 lines | 59 code | 32 blank | 9 comment | 13 complexity | 08eb772a857657feec61ba731e7ca272 MD5 | raw file
  1. package traveldashboard.server.data;
  2. import org.ibicoop.sdp.config.NetworkMessage;
  3. import traveldashboard.data.BusStationsCollection;
  4. import traveldashboard.data.TransportArea;
  5. import com.google.gson.Gson;
  6. public class BusStopGetterUtils extends ResponseUtils {
  7. private static final String TAG = "BusStopGetterUtils";
  8. /**
  9. * Get bus stop json string
  10. * @param req Request network message
  11. * @return jsons string
  12. */
  13. public static String getBusStopJsonString(NetworkMessage req) {
  14. String methodTag = "getBusStopJsonString";
  15. TdServerLogger.print(TAG, methodTag, "Start get bus stop message");
  16. String latitude = req.getPayload(DataConstants.PARAM_KEY_STOP_LAT);
  17. String longitude = req.getPayload(DataConstants.PARAM_KEY_STOP_LON);
  18. String radius = req.getPayload(DataConstants.PARAM_KEY_RADIUS);
  19. Gson gson = new Gson();
  20. String json = "null";
  21. if (TransportArea.getArea(Double.parseDouble(latitude), Double.parseDouble(longitude)).equals(TransportArea.LONDON)) {
  22. //In London
  23. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "#####Bus stop in London#####");
  24. BusStationsCollection collection = MongoDbManager.getLondonBusStationsCollection(latitude, longitude, radius);
  25. int collectionSize = collection.getBusStations().length;
  26. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "Collection size = " + collectionSize);
  27. json = gson.toJson(collection);
  28. } else {
  29. //In Paris
  30. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "#####Bus stop in Paris#####");
  31. BusStationsCollection collection = MongoDbManager.getParisBusStationsCollection(latitude, longitude, radius);
  32. int collectionSize = collection.getBusStations().length;
  33. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "Collection size = " + collectionSize);
  34. json = gson.toJson(collection);
  35. }
  36. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "json string = " + json);
  37. return json;
  38. }
  39. public static NetworkMessage getBusStopMessage(NetworkMessage req) {
  40. String methodTag = "getBusStopMessage";
  41. TdServerLogger.print(TAG, methodTag, "Start get bus stop message");
  42. NetworkMessage resp = prepareMsgReturn(req);
  43. String latitude = req.getPayload(DataConstants.PARAM_KEY_STOP_LAT);
  44. String longitude = req.getPayload(DataConstants.PARAM_KEY_STOP_LON);
  45. String radius = req.getPayload(DataConstants.PARAM_KEY_RADIUS);
  46. Gson gson = new Gson();
  47. String json = "null";
  48. if (TransportArea.getArea(Double.parseDouble(latitude), Double.parseDouble(longitude)).equals(TransportArea.LONDON)) {
  49. //In London
  50. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag,"#####Bus stop in London#####");
  51. BusStationsCollection collection = MongoDbManager.getLondonBusStationsCollection(latitude, longitude, radius);
  52. int collectionSize = collection.getBusStations().length;
  53. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag,"Collection size = " + collectionSize);
  54. json = gson.toJson(collection);
  55. } else {
  56. //In Paris
  57. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag,"#####Bus stop in Paris#####");
  58. BusStationsCollection collection = MongoDbManager.getParisBusStationsCollection(latitude, longitude, radius);
  59. int collectionSize = collection.getBusStations().length;
  60. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag,"Collection size = " + collectionSize);
  61. json = gson.toJson(collection);
  62. }
  63. resp.addPayload(DataConstants.BUS_STOP_MESSAGE, json);
  64. TdServerLogger.print(TAG, methodTag,"End get bus stop message : " + new String(resp.encode()));
  65. TdServerLogger.print(TAG, methodTag,"size of bus stop message : " + resp.encode().length);
  66. return resp;
  67. }
  68. }