for (int i = 0; i < userAlbums.length(); i++) { curAlbum = userAlbums.getJSONObject(i); if (curAlbum.getString(Album.Field.ID.toString()).equals(albumId)) { targetFound = true; } else {
for (int i = 0; i < userAlbums.length(); i++) { curAlbum = userAlbums.getJSONObject(i); if (curAlbum.getString(Album.Field.ID.toString()).equals(albumId)) { targetFound = true; } else {
for (int i = 0; i < userAlbums.length(); i++) { curAlbum = userAlbums.getJSONObject(i); if (curAlbum.getString(Album.Field.ID.toString()).equals(albumId)) { targetFound = true; } else {
/** {@inheritDoc} */ public Future<Void> updateAlbum(UserId userId, String appId, Album album, String albumId, SecurityToken token) throws ProtocolException { try { // First ensure user has a table String user = userId.getUserId(token); if (db.getJSONObject(ALBUMS_TABLE).has(user)) { // Retrieve user's albums JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); // Convert album to JSON and set ID JSONObject jsonAlbum = convertToJson(album); jsonAlbum.put(Album.Field.ID.toString(), albumId); // Iterate through albums to identify album to update for (int i = 0; i < userAlbums.length(); i++) { JSONObject curAlbum = userAlbums.getJSONObject(i); if (curAlbum.getString(Album.Field.ID.toString()).equals(albumId)) { userAlbums.put(i, jsonAlbum); return Futures.immediateFuture(null); } } } // Error - no album found to update with given ID throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Album ID " + albumId + " does not exist"); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Void> createAlbum(UserId userId, String appId, Album album, SecurityToken token) throws ProtocolException { try { // Get table of user's albums String user = userId.getUserId(token); JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); if (userAlbums == null) { userAlbums = new JSONArray(); db.getJSONObject(ALBUMS_TABLE).put(user, userAlbums); } // Convert album to JSON and set ID & owner JSONObject jsonAlbum = convertToJson(album); if (!jsonAlbum.has(Album.Field.ID.toString())) { jsonAlbum.put(Album.Field.ID.toString(), System.currentTimeMillis()); } if (!jsonAlbum.has(Album.Field.OWNER_ID.toString())) { jsonAlbum.put(Album.Field.OWNER_ID.toString(), user); } // Insert new album into table userAlbums.put(jsonAlbum); return Futures.immediateFuture(null); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Void> createAlbum(UserId userId, String appId, Album album, SecurityToken token) throws ProtocolException { try { // Get table of user's albums String user = userId.getUserId(token); JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); if (userAlbums == null) { userAlbums = new JSONArray(); db.getJSONObject(ALBUMS_TABLE).put(user, userAlbums); } // Convert album to JSON and set ID & owner JSONObject jsonAlbum = convertToJson(album); if (!jsonAlbum.has(Album.Field.ID.toString())) { jsonAlbum.put(Album.Field.ID.toString(), System.currentTimeMillis()); } if (!jsonAlbum.has(Album.Field.OWNER_ID.toString())) { jsonAlbum.put(Album.Field.OWNER_ID.toString(), user); } // Insert new album into table userAlbums.put(jsonAlbum); return ImmediateFuture.newInstance(null); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Album> getAlbum(UserId userId, String appId, Set<String> fields, String albumId, SecurityToken token) throws ProtocolException { try { // First ensure user has a table String user = userId.getUserId(token); if (db.getJSONObject(ALBUMS_TABLE).has(user)) { // Retrieve user's albums JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); // Search albums for given ID and owner JSONObject album; for (int i = 0; i < userAlbums.length(); i++) { album = userAlbums.getJSONObject(i); if (album.getString(Album.Field.ID.toString()).equals(albumId) && album.getString(Album.Field.OWNER_ID.toString()).equals(user)) { return ImmediateFuture.newInstance(filterFields(album, fields, Album.class)); } } } // Album wasn't found throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Album ID " + albumId + " does not exist"); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Void> updateAlbum(UserId userId, String appId, Album album, String albumId, SecurityToken token) throws ProtocolException { try { // First ensure user has a table String user = userId.getUserId(token); if (db.getJSONObject(ALBUMS_TABLE).has(user)) { // Retrieve user's albums JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); // Convert album to JSON and set ID JSONObject jsonAlbum = convertToJson(album); jsonAlbum.put(Album.Field.ID.toString(), albumId); // Iterate through albums to identify album to update for (int i = 0; i < userAlbums.length(); i++) { JSONObject curAlbum = userAlbums.getJSONObject(i); if (curAlbum.getString(Album.Field.ID.toString()).equals(albumId)) { userAlbums.put(i, jsonAlbum); return Futures.immediateFuture(null); } } } // Error - no album found to update with given ID throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Album ID " + albumId + " does not exist"); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<RestfulCollection<Album>> getAlbums(Set<UserId> userIds, GroupId groupId, String appId, Set<String> fields, CollectionOptions options, SecurityToken token) throws ProtocolException { try { List<Album> result = Lists.newArrayList(); Set<String> idSet = getIdSet(userIds, groupId, token); // Gather albums for all user IDs for (String id : idSet) { if (db.getJSONObject(ALBUMS_TABLE).has(id)) { JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(id); for (int i = 0; i < userAlbums.length(); i++) { JSONObject album = userAlbums.getJSONObject(i); if (album.getString(Album.Field.OWNER_ID.toString()).equals(id)) { result.add(filterFields(album, fields, Album.class)); } } } } return Futures.immediateFuture(new RestfulCollection<Album>(result)); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<RestfulCollection<Album>> getAlbums(Set<UserId> userIds, GroupId groupId, String appId, Set<String> fields, CollectionOptions options, SecurityToken token) throws ProtocolException { try { List<Album> result = Lists.newArrayList(); Set<String> idSet = getIdSet(userIds, groupId, token); // Gather albums for all user IDs for (String id : idSet) { if (db.getJSONObject(ALBUMS_TABLE).has(id)) { JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(id); for (int i = 0; i < userAlbums.length(); i++) { JSONObject album = userAlbums.getJSONObject(i); if (album.getString(Album.Field.OWNER_ID.toString()).equals(id)) { result.add(filterFields(album, fields, Album.class)); } } } } return Futures.immediateFuture(new RestfulCollection<Album>(result)); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Album> getAlbum(UserId userId, String appId, Set<String> fields, String albumId, SecurityToken token) throws ProtocolException { try { // First ensure user has a table String user = userId.getUserId(token); if (db.getJSONObject(ALBUMS_TABLE).has(user)) { // Retrieve user's albums JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); // Search albums for given ID and owner JSONObject album; for (int i = 0; i < userAlbums.length(); i++) { album = userAlbums.getJSONObject(i); if (album.getString(Album.Field.ID.toString()).equals(albumId) && album.getString(Album.Field.OWNER_ID.toString()).equals(user)) { return Futures.immediateFuture(filterFields(album, fields, Album.class)); } } } // Album wasn't found throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Album ID " + albumId + " does not exist"); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Void> updateAlbum(UserId userId, String appId, Album album, String albumId, SecurityToken token) throws ProtocolException { try { // First ensure user has a table String user = userId.getUserId(token); if (db.getJSONObject(ALBUMS_TABLE).has(user)) { // Retrieve user's albums JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); // Convert album to JSON and set ID JSONObject jsonAlbum = convertToJson(album); jsonAlbum.put(Album.Field.ID.toString(), albumId); // Iterate through albums to identify album to update JSONObject curAlbum = null; for (int i = 0; i < userAlbums.length(); i++) { curAlbum = userAlbums.getJSONObject(i); if (curAlbum.getString(Album.Field.ID.toString()).equals(albumId)) { userAlbums.put(i, jsonAlbum); return ImmediateFuture.newInstance(null); } } } // Error - no album found to update with given ID throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Album ID " + albumId + " does not exist"); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<RestfulCollection<Album>> getAlbums(Set<UserId> userIds, GroupId groupId, String appId, Set<String> fields, CollectionOptions options, SecurityToken token) throws ProtocolException { try { List<Album> result = Lists.newArrayList(); Set<String> idSet = getIdSet(userIds, groupId, token); // Gather albums for all user IDs for (String id : idSet) { if (db.getJSONObject(ALBUMS_TABLE).has(id)) { JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(id); for (int i = 0; i < userAlbums.length(); i++) { JSONObject album = userAlbums.getJSONObject(i); if (album.getString(Album.Field.OWNER_ID.toString()).equals(id)) { result.add(filterFields(album, fields, Album.class)); } } } } return ImmediateFuture.newInstance(new RestfulCollection<Album>(result)); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Album> getAlbum(UserId userId, String appId, Set<String> fields, String albumId, SecurityToken token) throws ProtocolException { try { // First ensure user has a table String user = userId.getUserId(token); if (db.getJSONObject(ALBUMS_TABLE).has(user)) { // Retrieve user's albums JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); // Search albums for given ID and owner JSONObject album; for (int i = 0; i < userAlbums.length(); i++) { album = userAlbums.getJSONObject(i); if (album.getString(Album.Field.ID.toString()).equals(albumId) && album.getString(Album.Field.OWNER_ID.toString()).equals(user)) { return Futures.immediateFuture(filterFields(album, fields, Album.class)); } } } // Album wasn't found throw new ProtocolException(HttpServletResponse.SC_BAD_REQUEST, "Album ID " + albumId + " does not exist"); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }
/** {@inheritDoc} */ public Future<Void> createAlbum(UserId userId, String appId, Album album, SecurityToken token) throws ProtocolException { try { // Get table of user's albums String user = userId.getUserId(token); JSONArray userAlbums = db.getJSONObject(ALBUMS_TABLE).getJSONArray(user); if (userAlbums == null) { userAlbums = new JSONArray(); db.getJSONObject(ALBUMS_TABLE).put(user, userAlbums); } // Convert album to JSON and set ID & owner JSONObject jsonAlbum = convertToJson(album); if (!jsonAlbum.has(Album.Field.ID.toString())) { jsonAlbum.put(Album.Field.ID.toString(), System.currentTimeMillis()); } if (!jsonAlbum.has(Album.Field.OWNER_ID.toString())) { jsonAlbum.put(Album.Field.OWNER_ID.toString(), user); } // Insert new album into table userAlbums.put(jsonAlbum); return Futures.immediateFuture(null); } catch (JSONException je) { throw new ProtocolException( HttpServletResponse.SC_INTERNAL_SERVER_ERROR, je.getMessage(), je); } }