/**
   * Fetches the full list of Posts stored in the local Database
   *
   * @param context
   * @return
   */
  public static List<Post> getPosts(Context context) {
    DatabaseWrapper databaseWrapper = new DatabaseWrapper(context);
    SQLiteDatabase database = databaseWrapper.getReadableDatabase();

    List<Post> postList = null;

    if (database != null) {
      Cursor cursor = database.rawQuery("SELECT * FROM " + PostORM.TABLE_NAME, null);

      Log.i(TAG, "Loaded " + cursor.getCount() + " Posts...");
      if (cursor.getCount() > 0) {
        postList = new ArrayList<Post>();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
          Post post = cursorToPost(cursor);
          post.setTags(TagORM.getTagsForPost(context, post));

          postList.add(post);
          cursor.moveToNext();
        }
        Log.i(TAG, "Posts loaded successfully.");
      }

      database.close();
    }

    return postList;
  }
  /**
   * Inserts a Post object into the local database
   *
   * @param context
   * @param post
   * @return
   */
  public static boolean insertPost(Context context, Post post) {
    if (findPostById(context, post.getId()) != null) {
      Log.i(TAG, "Post already exists in database, not inserting!");
      return updatePost(context, post);
    }

    ContentValues values = postToContentValues(post);

    DatabaseWrapper databaseWrapper = new DatabaseWrapper(context);
    SQLiteDatabase database = databaseWrapper.getWritableDatabase();

    boolean success = false;
    try {
      if (database != null) {
        long postId = database.insert(PostORM.TABLE_NAME, "null", values);
        Log.i(TAG, "Inserted new Post with ID: " + postId);

        for (Tag tag : post.getTags()) {
          TagORM.insertTag(context, tag, post.getId());
        }
        success = true;
      }
    } catch (NullPointerException ex) {
      Log.e(TAG, "Failed to insert Post[" + post.getId() + "] due to: " + ex);
    } finally {
      if (database != null) {
        database.close();
      }
    }

    return success;
  }
  /**
   * Fetches a single Post identified by the specified ID
   *
   * @param context
   * @param postId
   * @return
   */
  public static Post findPostById(Context context, long postId) {
    DatabaseWrapper databaseWrapper = new DatabaseWrapper(context);
    SQLiteDatabase database = databaseWrapper.getReadableDatabase();

    Post post = null;
    if (database != null) {
      Log.i(TAG, "Loading Post[" + postId + "]...");
      Cursor cursor =
          database.rawQuery(
              "SELECT * FROM "
                  + PostORM.TABLE_NAME
                  + " WHERE "
                  + PostORM.COLUMN_ID
                  + " = "
                  + postId,
              null);

      if (cursor.getCount() > 0) {
        cursor.moveToFirst();
        post = cursorToPost(cursor);
        post.setTags(TagORM.getTagsForPost(context, post));
        Log.i(TAG, "Post loaded successfully!");
      }

      database.close();
    }

    return post;
  }
  /**
   * Populates a Post object with data from a Cursor
   *
   * @param cursor
   * @return
   */
  private static Post cursorToPost(Cursor cursor) {
    Post post = new Post();
    post.setId(cursor.getLong(cursor.getColumnIndex(COLUMN_ID)));
    post.setTitle(cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)));
    post.setPreview(cursor.getString(cursor.getColumnIndex(COLUMN_PREVIEW)));
    post.setBody(cursor.getString(cursor.getColumnIndex(COLUMN_BODY)));
    post.setUrl(cursor.getString(cursor.getColumnIndex(COLUMN_URL)));

    String date = cursor.getString(cursor.getColumnIndex(COLUMN_DATE));
    try {
      post.setDate(_dateFormat.parse(date));
    } catch (ParseException ex) {
      Log.e(TAG, "Failed to parse date " + date + " for Post " + post.getId());
      post.setDate(null);
    }

    return post;
  }
  public static boolean updatePost(Context context, Post post) {
    ContentValues values = postToContentValues(post);
    DatabaseWrapper databaseWrapper = new DatabaseWrapper(context);
    SQLiteDatabase database = databaseWrapper.getWritableDatabase();

    boolean success = false;
    try {
      if (database != null) {
        Log.i(TAG, "Updating Post[" + post.getId() + "]...");
        database.update(PostORM.TABLE_NAME, values, PostORM.COLUMN_ID + " = " + post.getId(), null);
        success = true;
      }
    } catch (NullPointerException ex) {
      Log.e(TAG, "Failed to update Post[" + post.getId() + "] due to: " + ex);
    } finally {
      if (database != null) {
        database.close();
      }
    }

    return success;
  }
  /**
   * Packs a Post object into a ContentValues map for use with SQL inserts.
   *
   * @param post
   * @return
   */
  private static ContentValues postToContentValues(Post post) {
    ContentValues values = new ContentValues();
    values.put(PostORM.COLUMN_ID, post.getId());
    values.put(PostORM.COLUMN_TITLE, post.getTitle());
    values.put(PostORM.COLUMN_PREVIEW, post.getPreview());
    values.put(PostORM.COLUMN_BODY, post.getBody());
    values.put(PostORM.COLUMN_URL, post.getUrl());
    values.put(PostORM.COLUMN_DATE, _dateFormat.format(post.getDate()));

    return values;
  }