PageRenderTime 123ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/traveldashboard/server/data/IncentiveGetterUtils.java

https://bitbucket.org/ambientic/traveldashboard-server
Java | 96 lines | 57 code | 29 blank | 10 comment | 13 complexity | 2759f43e53510a95bf068729cb38f023 MD5 | raw file
  1. package traveldashboard.server.data;
  2. import org.ibicoop.sdp.config.NetworkMessage;
  3. import traveldashboard.data.TubeStation;
  4. import traveldashboard.data.incentive.Incentive;
  5. import traveldashboard.data.incentive.IncentiveType;
  6. import com.google.gson.Gson;
  7. public class IncentiveGetterUtils extends ResponseUtils {
  8. private static final String TAG = "IncentiveGetterUtils";
  9. public static NetworkMessage getIncentiveMessage(NetworkMessage req) {
  10. String methodTag = "getIncentiveMessage";
  11. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "Getting incentive message...");
  12. NetworkMessage resp = prepareMsgReturn(req);
  13. String city = req.getPayload(DataConstants.PARAM_KEY_CITY_TYPE);
  14. String stopId = req.getPayload(DataConstants.PARAM_VALUE_STOP_ID);
  15. String incentiveType = req.getPayload(DataConstants.PARAM_INCENTIVE_TYPE);
  16. Gson gson = new Gson();
  17. String json = "null";
  18. if (incentiveType.equals(IncentiveType.FACT.toString())) {
  19. //Fact
  20. if (city.equals(DataConstants.LONDON)) {
  21. //Get london tube station fact
  22. json = gson.toJson(MongoDbManager.getLondonTubeStationFact(stopId));
  23. } else {
  24. //Paris fact not available, just show thank you message
  25. Incentive parisFact = new Incentive(stopId,
  26. MongoDbManager.getParisMetroStationName(stopId),
  27. IncentiveType.FACT,
  28. "Thank you for reporting!");
  29. json = gson.toJson(parisFact);
  30. }
  31. } else if (incentiveType.equals(IncentiveType.JOKE.toString())) {
  32. //Joke not available
  33. //Joke
  34. if (city.equals(DataConstants.LONDON)) {
  35. //Get real time joke
  36. TubeStation station = MongoDbManager.getLondonMetroStation(stopId);
  37. String stationName = station.getName();
  38. //Split station name
  39. String[] nameArray = stationName.split(" ");
  40. String firstName = nameArray[0];
  41. String jsonString = JokeSearcherForTubeStation.getJokeJsonContent(firstName);
  42. String stationDesc = JokeSearcherForTubeStation.processJsonContent(jsonString);
  43. if ((stationDesc == null) || (stationDesc.equals(""))) {
  44. //Get london tube station static joke
  45. json = gson.toJson(MongoDbManager.getLondonTubeStationJoke(stopId));
  46. } else {
  47. //Give real time joke
  48. Incentive londonJoke = new Incentive(stopId,
  49. stationName,
  50. IncentiveType.JOKE,
  51. stationDesc);
  52. json = gson.toJson(londonJoke);
  53. }
  54. } else {
  55. //Paris joke not available, just show thank you message
  56. Incentive parisJoke = new Incentive(stopId,
  57. MongoDbManager.getParisMetroStationName(stopId),
  58. IncentiveType.JOKE,
  59. "Thank you for reporting!");
  60. json = gson.toJson(parisJoke);
  61. }
  62. }
  63. resp.addPayload(DataConstants.INCENTIVE_MESSAGE, json);
  64. if (DataConstants.DEBUG) TdServerLogger.print(TAG, methodTag, "End get incentive message : " + new String(resp.encode()));
  65. return resp;
  66. }
  67. }