Back to project page Mamytas.
The source code is released under:
GNU General Public License
If you think the Android project Mamytas listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package mn.aug.restfulandroid.provider; //w ww.j ava 2 s . c o m import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.util.ArrayList; import java.util.List; import mn.aug.restfulandroid.rest.resource.Comment; /** * Created by Paul on 09/11/2014. */ public class CommentsDBAccess { private SQLiteDatabase bdd; private ProviderDbHelper myHelper; public CommentsDBAccess(Context context) { //On crer la BDD et sa table myHelper = new ProviderDbHelper(context); } public void open() { //on ouvre la BDD en criture bdd = myHelper.openBDD(); } public void close() { //on ferme l'accs la BDD myHelper.closeBDD(); } public SQLiteDatabase getBDD() { return bdd; } /** * Store a new comment into the database * * @param comment The comment to be stored * @return The comment stored with its ID */ public Comment storeComment(Comment comment) { if (!commentIsInDB(comment)) { try { ContentValues values = new ContentValues(); values.put(ProviderDbHelper.COMMENTS_TEXT, comment.getText()); values.put(ProviderDbHelper.COMMENTS_TASK_ID, comment.getTask_id()); long id = bdd.insert(ProviderDbHelper.TABLE_COMMENTS, null, values); comment.setId((int) id); return comment; } catch (Exception e) { e.printStackTrace(); return null; } } return null; } /** * Retrieve the comments relative to a task * * @param taskID The id of the task * @return The comments */ public List<Comment> retrieveTaskComments(int taskID) { List<Comment> list = new ArrayList<Comment>(); Cursor c = null; try { c = bdd.query(ProviderDbHelper.TABLE_COMMENTS, new String[]{ProviderDbHelper.COMMENTS_ID, ProviderDbHelper.COMMENTS_TEXT}, ProviderDbHelper.COMMENTS_TASK_ID + " ='" + taskID + "'", null, null, null, null); } catch (Exception e) { e.printStackTrace(); return null; } if (c.getCount() == 0) return null; else { c.moveToFirst(); do{ list.add(new Comment(c.getInt(0), taskID, c.getString(1))); }while(c.moveToNext()); return list; } } /** * Delete a comment from its ID * * @param commentID The ID of the comment to be deleted * @return Whether it was successful or not */ public boolean deleteComment(int commentID) { try { bdd.delete(ProviderDbHelper.TABLE_COMMENTS, ProviderDbHelper.COMMENTS_ID + " = '" + commentID+"'", null); } catch (Exception e) { e.printStackTrace(); return false; } return true; } public boolean commentIsInDB(Comment comment) { Cursor c = null; try { c = bdd.query(ProviderDbHelper.TABLE_COMMENTS, new String[]{ProviderDbHelper.COMMENTS_TEXT}, ProviderDbHelper.COMMENTS_ID + " ='" + comment.getId() + "'", null, null, null, null); } catch (Exception e) { e.printStackTrace(); return false; } return c.getCount() != 0; } public boolean setStatus(Comment comment, String state) { if (commentIsInDB(comment)) { try { ContentValues values = new ContentValues(); values.put(ProviderDbHelper.COMMENTS_STATE, state); bdd.update(ProviderDbHelper.TABLE_COMMENTS, values, ProviderDbHelper.COMMENTS_ID + " = '" + comment.getId()+"'", null); return true; } catch (Exception e) { e.printStackTrace(); return false; } } return false; } public String getStatus(Comment comment) { if (commentIsInDB(comment)) { Cursor c = null; try { c = bdd.query(ProviderDbHelper.TABLE_COMMENTS, new String[]{ProviderDbHelper.COMMENTS_STATE}, ProviderDbHelper.COMMENTS_ID + " ='" + comment.getId() + "'", null, null, null, null); } catch (Exception e) { e.printStackTrace(); return null; } if (c.getCount() == 0) return null; else { c.moveToFirst(); return c.getString(0); } } return "not_existing"; } }