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