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