Back to project page KeepMySecret.
The source code is released under:
GNU General Public License
If you think the Android project KeepMySecret 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 keepmysecretapp.app.com.keepmysecretapp.dao; //from ww w . j a v a2 s. c o m import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import java.util.ArrayList; import keepmysecretapp.app.com.keepmysecretapp.db.DbContext; import keepmysecretapp.app.com.keepmysecretapp.db.EntryType; import keepmysecretapp.app.com.keepmysecretapp.other.Tools; import keepmysecretapp.app.com.keepmysecretapp.tables.DataTable; import keepmysecretapp.app.com.keepmysecretapp.tables.GroupTable; import keepmysecretapp.app.com.keepmysecretapp.types.ListEntry; public class GroupDao extends Dao { public GroupDao(DbContext inContext) { super(inContext); } @Override public void create(Object objToCreate) { ListEntry entry = (ListEntry) objToCreate; SQLiteDatabase db = this.dbContext.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(GroupTable.KEY_GROUP_NAME, entry.getName()); values.put(GroupTable.KEY_GROUP_DESCRIBE, entry.getDescribe()); db.insert(EntryType.GROUPS.getName(), null, values); db.close(); } @Override public void delete(Object obj) { ListEntry entry = (ListEntry) obj; SQLiteDatabase db = this.dbContext.getWritableDatabase(); db.delete(EntryType.GROUPS.getName(), GroupTable.KEY_GROUP_NAME + " = ?", new String[]{entry.getName()}); /* also removing all data entries, which that group contains */ db.delete(EntryType.DATA.getName(), DataTable.KEY_ENTRY_GROUP_ID + " = ?", new String[]{entry.getID() + ""}); db.close(); } @Override public int update(Object obj) { ListEntry entry = (ListEntry) obj; SQLiteDatabase db = this.dbContext.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(GroupTable.KEY_GROUP_NAME, entry.getName()); values.put(GroupTable.KEY_GROUP_DESCRIBE, entry.getDescribe()); return db.update(EntryType.GROUPS.getName(), values, GroupTable.KEY_ID + " = ?", new String[]{String.valueOf(entry.getID())}); } @Override public Object read(Object obj) { SQLiteDatabase db = this.dbContext.getReadableDatabase(); ListEntry entry = (ListEntry) obj; if (Tools.isStringEmpty(entry.getName())) return null; Cursor cursor = db.query(EntryType.GROUPS.getName(), new String[]{GroupTable.KEY_ID, GroupTable.KEY_GROUP_NAME, GroupTable.KEY_GROUP_DESCRIBE}, GroupTable.KEY_GROUP_NAME + "=?", new String[]{entry.getName()}, null, null, null, null); if (cursor != null) { entry = null; if (cursor.moveToFirst()) { entry = new ListEntry(); entry.setID(Integer.parseInt(cursor.getString(0))); entry.setName(cursor.getString(1)); entry.setDescribe(cursor.getString(2)); } } return entry; } @Override public ArrayList<Object> getAllEntries() { ArrayList<Object> groups = new ArrayList<>(); StringBuilder sb = new StringBuilder(); sb.append("SELECT * FROM " + EntryType.GROUPS.getName()); SQLiteDatabase db = this.dbContext.getReadableDatabase(); Cursor cursor = db.rawQuery(sb.toString(), null); if (cursor.moveToFirst()) { do { ListEntry group = new ListEntry(); group.setID(Integer.parseInt(cursor.getString(0))); group.setName(cursor.getString(1)); group.setDescribe(cursor.getString(2)); groups.add(group); } while (cursor.moveToNext()); } return groups; } }