public List<InstagramPost> getAllInstagramPosts() {
    List<InstagramPost> posts = new ArrayList<>();

    String LEFT_OUTER_JOIN_FORMAT_STRING = "LEFT OUTER JOIN %s ON %s.%s = %s.%s";

    String userJoin =
        String.format(
            LEFT_OUTER_JOIN_FORMAT_STRING,
            TABLE_USERS,
            TABLE_POSTS,
            KEY_POST_USER_ID_FK,
            TABLE_USERS,
            KEY_USER_ID);

    String imageJoin =
        String.format(
            LEFT_OUTER_JOIN_FORMAT_STRING,
            TABLE_IMAGES,
            TABLE_POSTS,
            KEY_POST_IMAGE_ID_FK,
            TABLE_IMAGES,
            KEY_IMAGE_ID);

    String postsSelectQuery = "SELECT * FROM " + TABLE_POSTS + " " + userJoin + " " + imageJoin;

    String commentJoin =
        String.format(
            LEFT_OUTER_JOIN_FORMAT_STRING,
            TABLE_COMMENTS,
            TABLE_POST_COMMENTS,
            KEY_POST_COMMENT_COMMENT_ID_FK,
            TABLE_COMMENTS,
            KEY_COMMENT_ID);
    String commentUserJoin =
        String.format(
            LEFT_OUTER_JOIN_FORMAT_STRING,
            TABLE_USERS,
            TABLE_USERS,
            KEY_USER_ID,
            TABLE_COMMENTS,
            KEY_COMMENT_USER_ID_FK);

    String commentsSelectQuery =
        String.format(
            "SELECT * FROM %s %s %s WHERE %s = ?",
            TABLE_POST_COMMENTS, commentJoin, commentUserJoin, KEY_POST_COMMENT_POST_ID_FK);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor postsCursor = db.rawQuery(postsSelectQuery, null);

    try {
      if (postsCursor.moveToFirst()) {
        do {
          InstagramPost post = new InstagramPost();
          post.mediaId =
              postsCursor.getString(postsCursor.getColumnIndexOrThrow(KEY_POST_MEDIA_ID));
          post.caption = postsCursor.getString(postsCursor.getColumnIndexOrThrow(KEY_POST_CAPTION));
          post.likesCount =
              postsCursor.getInt(postsCursor.getColumnIndexOrThrow(KEY_POST_LIKES_COUNT));
          post.commentsCount =
              postsCursor.getInt(postsCursor.getColumnIndexOrThrow(KEY_POST_COMMENTS_COUNT));
          post.createdTime =
              postsCursor.getLong(postsCursor.getColumnIndexOrThrow(KEY_POST_CREATED_TIME));

          InstagramUser user = new InstagramUser();
          user.userName = postsCursor.getString(postsCursor.getColumnIndexOrThrow(KEY_USER_NAME));
          user.profilePictureUrl =
              postsCursor.getString(
                  postsCursor.getColumnIndexOrThrow(KEY_USER_PROFILE_PICTURE_URL));
          post.user = user;

          InstagramImage image = new InstagramImage();
          image.imageUrl = postsCursor.getString(postsCursor.getColumnIndexOrThrow(KEY_IMAGE_URL));
          image.imageHeight =
              postsCursor.getInt(postsCursor.getColumnIndexOrThrow(KEY_IMAGE_HEIGHT));
          image.imageWidth = postsCursor.getInt(postsCursor.getColumnIndexOrThrow(KEY_IMAGE_WIDTH));
          post.image = image;

          int key = postsCursor.getInt(0);

          // Get all comments for this post
          Cursor commentsCursor =
              db.rawQuery(commentsSelectQuery, new String[] {String.valueOf(key)});
          try {
            if (commentsCursor.moveToFirst()) {
              do {
                InstagramComment comment = new InstagramComment();
                comment.text =
                    commentsCursor.getString(
                        commentsCursor.getColumnIndexOrThrow(KEY_COMMENT_TEXT));
                comment.createdTime =
                    commentsCursor.getLong(
                        commentsCursor.getColumnIndexOrThrow(KEY_COMMENT_CREATED_TIME));

                InstagramUser commentUser = new InstagramUser();
                commentUser.userName =
                    commentsCursor.getString(commentsCursor.getColumnIndexOrThrow(KEY_USER_NAME));
                commentUser.profilePictureUrl =
                    commentsCursor.getString(
                        commentsCursor.getColumnIndexOrThrow(KEY_USER_PROFILE_PICTURE_URL));
                comment.user = commentUser;

                post.appendComment(comment);
              } while (commentsCursor.moveToNext());
            }
          } catch (Exception e) {
            Log.wtf(TAG, "Error while trying to get comments from database");
            e.printStackTrace();
          } finally {
            commentsCursor.close();
          }
          posts.add(post);
        } while (postsCursor.moveToNext());
      }
    } catch (Exception e) {
      Log.wtf(TAG, "Error while trying to get posts from database");
      e.printStackTrace();
    } finally {
      closeCursor(postsCursor);
    }

    return posts;
  }
예제 #2
0
 public static List<InstagramPost> decodePostsFromJsonResponse(JSONObject jsonObject) {
   List<InstagramPost> posts = InstagramPost.fromJson(getDataJsonArray(jsonObject));
   return posts == null ? new ArrayList<InstagramPost>() : posts;
 }