@Override public Comment getComment(String commentId) { FirebaseSyncRequester<Comment> commentRequester = newFirebaseSyncRequesterForComment(commentId); if (!commentRequester.exists()) { throw new IllegalArgumentException("Comment does not exist"); } return commentRequester.get(); }
@Override public String[] getCategories() { Firebase fb = new Firebase(FIREBASE_CATEGORIES_URL); FirebaseSyncRequester<Map> categoriesRequester = new FirebaseSyncRequester<>(fb, Map.class); Map<String, Boolean> categoryMap = categoriesRequester.get(); return categoryMap.keySet().toArray(new String[categoryMap.size()]); }
@Override public Comment downvoteComment(String commentId) { Firebase commentFirebaseRef = newFirebaseRefForComment(commentId); FirebaseSyncRequester<Comment> commentRequester = new FirebaseSyncRequester<>(commentFirebaseRef, Comment.class); Comment downvotedComment = null; if (commentRequester.exists()) { downvotedComment = newDownvotedComment(commentRequester.get()); commentFirebaseRef.setValue(newDownvotedComment(downvotedComment)); } else { throw new IllegalArgumentException("Comment with id " + commentId + " does not exist"); } return downvotedComment; }
@Override public Comment deleteComment(String commentId) { Firebase firebaseCommentRef = newFirebaseRefForComment(commentId); FirebaseSyncRequester<Comment> commentRequester = new FirebaseSyncRequester<>(firebaseCommentRef, Comment.class); Comment commentToReturn = null; if (commentRequester.exists()) { final CountDownLatch latch = new CountDownLatch(1); firebaseCommentRef.removeValue( new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { throw firebaseError.toException(); } latch.countDown(); } }); try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } commentToReturn = commentRequester.get(); } else { throw new IllegalArgumentException("Comment with id " + commentId + " does not exist."); } return commentToReturn; }
/** * Determines if the post that was passed to FirebaseCommentProvider#startCheckingIfPostExists() * does exists. This method waits for the post to be downloaded. * * @return true if it exists, false otherwise */ private boolean finishCheckingIfPostExists() { return requesterForCheckingPost.exists(); }