PageRenderTime 176ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/foreveryoung/mongodb/MongoDBTask.java

https://bitbucket.org/keypointt/foreveryoung
Java | 220 lines | 138 code | 60 blank | 22 comment | 7 complexity | f818fa70b010c962a7d47c26ad93e657 MD5 | raw file
  1. package com.foreveryoung.mongodb;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.concurrent.ExecutionException;
  5. import org.json.JSONException;
  6. import org.json.JSONObject;
  7. import android.os.AsyncTask;
  8. import android.util.Log;
  9. import com.foreveryoung.HealthData;
  10. import com.foreveryoung.models.Doctor;
  11. import com.foreveryoung.models.Patient;
  12. import com.foreveryoung.models.User;
  13. import com.foreveryoung.models.notices.Suggestion;
  14. import com.foreveryoung.mongodb.RESTClient.RequestMethod;
  15. import com.google.gson.JsonArray;
  16. import com.google.gson.JsonParser;
  17. /**
  18. * DB Operations class runs common operations on the mongoDB web server.
  19. *
  20. * @author jeremywallace
  21. *
  22. */
  23. public class MongoDBTask {
  24. private final String DB_URI = "api.mongolab.com/api/1/databases/foreveryoung/collections/";
  25. private final String API_KEY = "PDO8WGmFvKD3v5CU0LxIA_Qu2acHZock";
  26. private JsonArray responseJSON = null;
  27. private JSONObject firstRecord = null;
  28. protected MongoDBClient client;
  29. public MongoDBTask() {
  30. }
  31. public User getUser(String email, String passSHA1)
  32. throws MongoDBException, NoRecordFoundException {
  33. String db = "FYUsers";
  34. client = new MongoDBClient(DB_URI + db + "/", API_KEY);
  35. client.addMongoParam("email", email);
  36. client.addMongoParam("password", passSHA1);
  37. Log.d("FY:", passSHA1);
  38. // running = true;
  39. // while (running==this.commit());
  40. commit();
  41. String userType;
  42. try {
  43. userType = firstRecord.getString("userType");
  44. Log.d("User Type:",userType);
  45. if (userType.equals("patient")) {
  46. return new Patient(firstRecord);
  47. } else if (userType.equals("doctor")) {
  48. return new Doctor(firstRecord);
  49. } else {
  50. return null;
  51. }
  52. } catch (JSONException e) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. throw new MongoDBException();
  56. }
  57. }
  58. public HealthData getAllHealthData(int userID) throws NoRecordFoundException, MongoDBException {
  59. //??? for here, not sure this query will return expected result
  60. // responseJSON is assumed to be exactly the same collection of mongoDB
  61. client = new MongoDBClient(DB_URI + "HealthData/", API_KEY);
  62. client.addMongoParam("userID", String.valueOf(userID));
  63. Log.d("FY:", DB_URI + "HealthData/");
  64. commit();
  65. return new HealthData(firstRecord);
  66. }
  67. public Doctor getPatientDoctor(int userID) throws NoRecordFoundException, MongoDBException {
  68. //??? for here, not sure this query will return expected result
  69. // responseJSON is assumed to be exactly the same collection of mongoDB
  70. client = new MongoDBClient(DB_URI + "FYUsers/", API_KEY);
  71. client.addMongoParam("_id", String.valueOf(userID));
  72. commit();
  73. try {
  74. return new Doctor(firstRecord);
  75. } catch (JSONException e) {
  76. // TODO Auto-generated catch block
  77. e.printStackTrace();
  78. throw new MongoDBException();
  79. }
  80. }
  81. public List<Suggestion> getSuggestions() throws NoRecordFoundException, MongoDBException {
  82. client = new MongoDBClient(DB_URI + "Suggestions/", API_KEY);
  83. commit();
  84. List<Suggestion> allSuggestions = new ArrayList<Suggestion>();
  85. for (int i = 0; i < responseJSON.size(); i++) {
  86. try {
  87. allSuggestions.add(new Suggestion(new JSONObject(responseJSON.get(i).toString())));
  88. //return new (responseJSON);
  89. } catch (JSONException e) {
  90. // TODO Auto-generated catch block
  91. JSONObject json;
  92. try {
  93. json = new JSONObject(responseJSON.get(i).toString());
  94. Log.e("JSON ERROR","Problem with one or more values in record "+i+": "+json.getString("Suggestion"));
  95. } catch (JSONException e1) {
  96. Log.e("ERROR","Could not import record:"+i);
  97. }
  98. continue;
  99. }
  100. }
  101. return allSuggestions;
  102. }
  103. public List<Patient> getPatients(int doctorID) throws NoRecordFoundException, MongoDBException {
  104. //??? for here, not sure this query will return expected result
  105. // responseJSON is assumed to be exactly the same collection of mongoDB
  106. client = new MongoDBClient(DB_URI + "FYUsers/", API_KEY);
  107. client.addMongoParam("doctor", String.valueOf(doctorID));
  108. commit();
  109. List<Patient> patients = new ArrayList<Patient>();
  110. try {
  111. for (int i = 0; i < responseJSON.size(); i++) {
  112. patients.add(new Patient(new JSONObject(responseJSON.get(i).toString())));
  113. }
  114. return patients;
  115. //return new (responseJSON);
  116. } catch (JSONException e) {
  117. // TODO Auto-generated catch block
  118. e.printStackTrace();
  119. throw new MongoDBException();
  120. }
  121. }
  122. public void commit() throws NoRecordFoundException {
  123. try {
  124. RestTask rt = new RestTask();
  125. rt.execute().get();
  126. } catch (InterruptedException e) {
  127. // TODO Auto-generated catch block
  128. e.printStackTrace();
  129. } catch (ExecutionException e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. if (responseJSON==null) {
  134. throw new NoRecordFoundException();
  135. }
  136. }
  137. class RestTask extends AsyncTask<Void, Void, Boolean> {
  138. @Override
  139. protected Boolean doInBackground(Void... thisClient) {
  140. try {
  141. client.initiateRESTRequest(RequestMethod.GET);
  142. Log.d("Response:", client.getResponse());
  143. } catch (Exception e) {
  144. Log.e("REST","Problem with REST");
  145. }
  146. try {
  147. responseJSON = new JsonParser().parse(client.getResponse()).getAsJsonArray();
  148. Log.d("JsonString",responseJSON.get(0).toString());
  149. firstRecord = new JSONObject(responseJSON.get(0).toString());
  150. } catch (Exception e) {
  151. e.printStackTrace();
  152. Log.e("JSON","Problem with JSON");
  153. }
  154. return true;
  155. }
  156. }
  157. public JsonArray getResponseJSON() {
  158. return responseJSON;
  159. }
  160. }