Android Open Source - silent-meeting Instance Dao






From Project

Back to project page silent-meeting.

License

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.

Java Source Code

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);
    }
}




Java Source Code List

uk.co.bensproule.silentmeeting.ApplicationTest.java
uk.co.bensproule.silentmeeting.activity.PreferencesActivity.java
uk.co.bensproule.silentmeeting.constants.Actions.java
uk.co.bensproule.silentmeeting.constants.Extra.java
uk.co.bensproule.silentmeeting.constants.Setting.java
uk.co.bensproule.silentmeeting.dao.EventDao.java
uk.co.bensproule.silentmeeting.dao.InstanceDao.java
uk.co.bensproule.silentmeeting.dao.SQLiteHelper.java
uk.co.bensproule.silentmeeting.domain.Attendee.java
uk.co.bensproule.silentmeeting.domain.Calendar.java
uk.co.bensproule.silentmeeting.domain.DialogCloseListener.java
uk.co.bensproule.silentmeeting.domain.Event.java
uk.co.bensproule.silentmeeting.domain.Instance.java
uk.co.bensproule.silentmeeting.fragment.TimePickerFragment.java
uk.co.bensproule.silentmeeting.receiver.CalendarEventsReceiver.java
uk.co.bensproule.silentmeeting.receiver.MutePhoneBroadcastReceiver.java
uk.co.bensproule.silentmeeting.receiver.UnmutePhoneBroadcastReceiver.java
uk.co.bensproule.silentmeeting.util.StringUtils.java