Back to project page silent-meeting.
The source code is released under:
GNU General Public License
If you think the Android project silent-meeting 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 uk.co.bensproule.silentmeeting.dao; //w w w. j ava 2s. c o m import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.provider.CalendarContract; import android.util.Log; import uk.co.bensproule.silentmeeting.domain.Instance; import java.util.ArrayList; import java.util.List; import static android.provider.CalendarContract.Instances.*; import static uk.co.bensproule.silentmeeting.dao.SQLiteHelper.INSTANCES_TABLE_NAME; import static uk.co.bensproule.silentmeeting.dao.SQLiteHelper.RUNNING; public class InstanceDao { private SQLiteDatabase db; public InstanceDao(Context context) { SQLiteHelper sqLiteHelper = SQLiteHelper.instance(context); db = sqLiteHelper.getWritableDatabase(); assert db != null; } public List<Instance> getAll() { Log.d("InstanceDao", "Getting all Instances"); Cursor cursor = db.query(INSTANCES_TABLE_NAME, null, null, null, null, null, null); List<Instance> instances = new ArrayList<>(); while (cursor.moveToNext()) { Instance instance = createInstance(cursor); instances.add(instance); } return instances; } public Instance get(int instanceId) { String selection = _ID + " = ?"; String[] selectionArgs = {"" + instanceId}; Cursor cursor = db.query(INSTANCES_TABLE_NAME, null, selection, selectionArgs, null, null, null); while (cursor.moveToNext()) { return createInstance(cursor); } return null; } private Instance createInstance(Cursor cursor) { int id = cursor.getInt(cursor.getColumnIndex(_ID)); int eventId = cursor.getInt(cursor.getColumnIndex(EVENT_ID)); String title = cursor.getString(cursor.getColumnIndex(TITLE)); long begin = cursor.getLong(cursor.getColumnIndex(BEGIN)); long end = cursor.getLong(cursor.getColumnIndex(END)); int availability = cursor.getInt(cursor.getColumnIndex(AVAILABILITY)); String organiser = cursor.getString(cursor.getColumnIndex(ORGANIZER)); boolean running = cursor.getInt(cursor.getColumnIndex(RUNNING)) == 1; return new Instance(id, eventId, title, begin, end, availability, organiser, running); } public void save(Instance instance) { Log.d("InstanceDao", "Saving " + instance.toString()); ContentValues values = new ContentValues(); values.put(_ID, instance.getId()); values.put(EVENT_ID, instance.getEventId()); values.put(TITLE, instance.getTitle()); values.put(BEGIN, instance.getStartDate()); values.put(END, instance.getEndDate()); values.put(AVAILABILITY, instance.getAvailability()); values.put(ORGANIZER, instance.getOrganiser()); if (get(instance.getId()) == null) { db.insert(INSTANCES_TABLE_NAME, null, values); } } public void delete(int id) { Log.d("InstanceDao", "Deleting " + id); String selection = _ID + " = ? AND " + RUNNING + " = ?"; String[] selectionArgs = {String.valueOf(id), "0"}; db.delete(INSTANCES_TABLE_NAME, selection, selectionArgs); } public void setRunning(int instanceId, boolean running) { String whereClause = CalendarContract.Instances._ID + " = ?"; String[] whereArgs = {"" + instanceId}; ContentValues values = new ContentValues(); values.put(RUNNING, running ? 1 : 0); db.update(INSTANCES_TABLE_NAME, values, whereClause, whereArgs); } }