/**
   * 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;
  }
  /**
   * 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;
  }
  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;
  }
  /**
   * 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;
  }