try {
String viewer = token.getViewerId(); // viewer
String user = id.getUserId(token); // person to update
if (!viewerCanUpdatePerson(viewer,user)) {
throw new ProtocolException(HttpServletResponse.SC_FORBIDDEN, "User '" + viewer + "' does not have enough privileges to update person '"+user+"'");
}
JSONArray people = db.getJSONArray(PEOPLE_TABLE);
for (int i = 0; i < people.length(); i++) {
JSONObject curPerson = people.getJSONObject(i);
if (user != null && curPerson.getString(Person.Field.ID.toString()).equals(user)) {
// Convert user to JSON and set ID
JSONObject jsonPerson = convertToJson(person);
// go through all properties to update in the submitted person object
// and change them in the current person object
for (String key : JSONObject.getNames(jsonPerson)) {
curPerson.put(key,jsonPerson.get(key));
}
people.put(i,curPerson);
return ImmediateFuture.newInstance(converter.convertToObject(curPerson.toString(), Person.class));
}
}
// Error - no album found to update with given ID
throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "User ID " + user + " does not exist");
} catch (JSONException je) {
throw new ProtocolException(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
je.getMessage(), je);
}
}