/src/main/java/atomic/user/UserCrudServlet.java

https://gitlab.com/NealsSBUProjects/AtomicComics · Java · 248 lines · 140 code · 84 blank · 24 comment · 21 complexity · 77595f5137e918944da43af09d4d7cbc MD5 · raw file

  1. package atomic.user;
  2. import atomic.comic.ComicRequest;
  3. import atomic.crud.CrudResult;
  4. import atomic.crud.CrudServlet;
  5. import atomic.data.EntityKind;
  6. import atomic.json.JsonProperty;
  7. import atomic.json.NoUniqueKeyException;
  8. import com.google.appengine.api.datastore.Query;
  9. import com.google.appengine.repackaged.com.google.api.client.json.Json;
  10. import com.google.gson.JsonArray;
  11. import com.google.gson.JsonElement;
  12. import com.google.gson.JsonObject;
  13. import java.util.List;
  14. /**
  15. * CrudServlet implementation which will be used to create, retrieve, update, and delete User data.
  16. *
  17. * @author Anthony G. Musco
  18. */
  19. public class UserCrudServlet extends CrudServlet {
  20. @Override
  21. protected JsonElement create(JsonElement json) {
  22. // Convert Json to object and retrieve user via their gmail account.
  23. JsonObject obj = json.getAsJsonObject();
  24. // Attempt to create user.
  25. try {
  26. new User(obj.get(JsonProperty.GMAIL.toString()).getAsString()); // creates user.
  27. // Successful create.
  28. return successfulRequest();
  29. } catch (NoUniqueKeyException nuke) {
  30. return failedRequest();
  31. }
  32. }
  33. @Override
  34. protected JsonElement retrieve(JsonElement json) {
  35. JsonElement response;
  36. // Construct the user - This will create in the data store if non-existent.
  37. try {
  38. // Grab the UserService.
  39. User user = User.getCurrentUser();
  40. response = successfulRequest();
  41. ((JsonObject) response).add(JsonProperty.USER.toString(), user.toJson());
  42. } catch (Exception e) {
  43. // Problem occurred while retrieving the user.
  44. System.err.println(e.getMessage());
  45. response = failedRequest();
  46. ((JsonObject) response).add(JsonProperty.USER.toString(), null);
  47. }
  48. return response;
  49. }
  50. @Override
  51. protected JsonElement update(JsonElement json) {
  52. // Retrieve the user as a JSON.
  53. JsonObject request = json.getAsJsonObject();
  54. JsonObject response = new JsonObject();
  55. // Attempt to update the user.
  56. try {
  57. // Determine what the request was.
  58. if (request.has(JsonProperty.REQUEST.toString())) {
  59. String req = request.get(JsonProperty.REQUEST.toString()).getAsString();
  60. // Request to update comic data.
  61. if (req.equals(UserRequest.SUBSCRIBE.toString()) ||
  62. req.equals(UserRequest.UNSUBSCRIBE.toString())) {
  63. processSubscribeRequest(request, response, req);
  64. } else if (req.equals(UserRequest.GET_SUBSCRIPTION_LIST.toString())) {
  65. processGetSubscriptionListRequest(request, response);
  66. } else if (req.equals(UserRequest.GET_USER_BY_GMAIL.toString())) {
  67. processGetUserByGmailRequest(request, response);
  68. return response;
  69. } else if (req.equals(UserRequest.IS_USER_SUBSCRIBED.toString())) {
  70. processIsUserSubscribedRequest(request, response);
  71. }
  72. } else {
  73. // Simply construct a user from the Json passed.
  74. JsonObject userObj = request.getAsJsonObject(JsonProperty.USER.toString());
  75. new User(userObj);
  76. }
  77. return response;
  78. } catch (NoUniqueKeyException nuke) {
  79. return failedRequest();
  80. }
  81. }
  82. @Override
  83. protected JsonElement delete(JsonElement json) {
  84. // Cannot delete users as of yet.
  85. return unsupportedRequest();
  86. }
  87. private void processSubscribeRequest(JsonObject request, JsonObject response, String which) {
  88. try {
  89. User currentUser = User.getCurrentUser();
  90. String userGmail;
  91. if (request.has(JsonProperty.USER_GMAIL.toString())) {
  92. userGmail = request.get(JsonProperty.USER_GMAIL.toString()).getAsString();
  93. } else {
  94. throw new IllegalArgumentException("Request must include user gmail.");
  95. }
  96. if(UserRequest.SUBSCRIBE.toString().equals(which)) {
  97. currentUser.subscribeTo(userGmail);
  98. } else {
  99. currentUser.unsubscribeFrom(userGmail);
  100. }
  101. response.addProperty(JsonProperty.RESULT.toString(), CrudResult.SUCCESS.toString());
  102. } catch (Exception e) {
  103. processGeneralException(response, e);
  104. }
  105. }
  106. private void processGetSubscriptionListRequest(JsonObject request, JsonObject response) {
  107. try {
  108. User currentUser = User.getCurrentUser();
  109. Preferences currentUserPreferences = currentUser.getPreferences();
  110. JsonArray subscriptionList = new JsonArray();
  111. for(String s : currentUserPreferences.getSubscriptions()) {
  112. JsonObject subscription = new JsonObject();
  113. User u = User.retrieveUser(s);
  114. if(u == null) {
  115. System.err.println("No User with gmail " + s);
  116. continue;
  117. }
  118. // Add all the properties to the subscription object.
  119. subscription.addProperty(JsonProperty.GMAIL.toString(), u.getGmail());
  120. subscription.addProperty(JsonProperty.HANDLE.toString(), u.getHandle());
  121. subscription.addProperty(JsonProperty.PROFILE_PIC_URL.toString(), u.getProfilePicUrl());
  122. subscriptionList.add(subscription);
  123. }
  124. response.addProperty(JsonProperty.RESULT.toString(), CrudResult.SUCCESS.toString());
  125. response.add(JsonProperty.SUBSCRIPTIONS.toString(), subscriptionList);
  126. } catch (Exception e) {
  127. processGeneralException(response, e);
  128. }
  129. }
  130. private void processGetUserByGmailRequest(JsonObject request, JsonObject response) {
  131. if(request.has(JsonProperty.USER_GMAIL.toString())) {
  132. String gmail = request.get(JsonProperty.USER_GMAIL.toString()).getAsString();
  133. try {
  134. User user = new User(gmail);
  135. response.addProperty(JsonProperty.RESULT.toString(), CrudResult.SUCCESS.toString());
  136. response.add(JsonProperty.USER.toString(), user.toJson());
  137. } catch (NoUniqueKeyException e){
  138. processGeneralException(response, e);
  139. }
  140. }
  141. }
  142. private void processIsUserSubscribedRequest(JsonObject request, JsonObject response) {
  143. try {
  144. // Get the current user.
  145. User currentUser = User.getCurrentUser();
  146. // Make sure the request is valid.
  147. if (request.has(JsonProperty.USER_GMAIL.toString())) {
  148. // Grab the queried gmail, as well as the list of subscriptions for the current user.
  149. String userGmail = request.get(JsonProperty.USER_GMAIL.toString()).getAsString();
  150. List<String> subscriptions = currentUser.getSubscriptions();
  151. // If the subsriptions exist and contain the user gmail, return success.
  152. if(subscriptions != null && subscriptions.contains(userGmail)) {
  153. response.addProperty(JsonProperty.RESULT.toString(), CrudResult.SUCCESS.toString());
  154. return;
  155. }
  156. }
  157. // In all other cases, return failure.
  158. response.addProperty(JsonProperty.RESULT.toString(), CrudResult.FAILURE.toString());
  159. } catch (Exception e) {
  160. // Returns failure.
  161. processGeneralException(response, e);
  162. }
  163. }
  164. }