Android Open Source - ProjectStudio Task Data Source






From Project

Back to project page ProjectStudio.

License

The source code is released under:

Apache License

If you think the Android project ProjectStudio 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 DB_Provider;
/*from   w ww .j a  va  2  s. co  m*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import model.Professor;
import model.Task;

/**
 * Created by desmond on 1/27/14.
 */
public class TaskDataSource {

    //Database Fields
    private SQLiteDatabase database;
    private DBManager task_dbHelper;
    private String[] allColumns = {DB_ABSTRACTS.DBTasks.KEY_ID, DB_ABSTRACTS.DBTasks.TITLE_COLUMN,
            DB_ABSTRACTS.DBTasks.NOTE_COLUMN, DB_ABSTRACTS.DBTasks.LOCATION_COLUMN,
            DB_ABSTRACTS.DBTasks.TIME_COLUMN, DB_ABSTRACTS.DBTasks.DAY_COLUMN,
            DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN
    };

    public TaskDataSource(Context context) {
        task_dbHelper = new DBManager(context);
    }

    public void open() throws SQLException {
        database = task_dbHelper.getWritableDatabase();
    }

    public void close() {
        if (database != null && database.isOpen()) {
            task_dbHelper.close();
        }
    }

    /*
    INSERT A MEETING INTO THE DATABASE
     */
    public long createTask(Task task, Professor professor) {
        try {
            this.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ContentValues values = new ContentValues();
        values.put(DB_ABSTRACTS.DBTasks.TITLE_COLUMN, task.getTitle());
        values.put(DB_ABSTRACTS.DBTasks.NOTE_COLUMN, task.getNote());
        values.put(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN, task.getLocation());
        values.put(DB_ABSTRACTS.DBTasks.TIME_COLUMN, task.getTime().toString());
        values.put(DB_ABSTRACTS.DBTasks.DAY_COLUMN, task.getDay());
        values.put(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN, professor.getProfessor_name());

        //INSERT ROW
        long id = database.insert(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, null, values);

        return id;
    }

    /*
    GET A SINGLE MEETING
     */

    public Task getTask(long meeting_id) throws ParseException {

        database = task_dbHelper.getWritableDatabase();

        String where = DB_ABSTRACTS.DBTasks.KEY_ID + " = " + meeting_id;

        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Professor prof = new Professor();

        Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, where, null, null, null, null);
        Task task = new Task();
        task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID)));
        task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN))));
        // task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))));
        task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)));
        prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN)));
        task.setProfessor(prof);

        return task;
    }

    /*
    GET ALL MEETINGS
     */
    public Cursor getAllTasks() {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, null, null, null, null, null);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Professor prof = new Professor();

        //LOOP THROUGH ALL THE ROWS AND ADD TO CURSOR
        if (cursor.moveToFirst()) {
            do {
                Task task = new Task();
                task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID)));
                try {
                    task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN))));
                    //task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                task.setTitle(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN)));
                task.setNote(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN)));
                task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)));
                task.setLocation(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN)));
                prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN)));
                task.setProfessor(prof);
            } while (cursor.moveToNext());
        }

        return cursor;
    }

    /*
    GET MEETINGS BY DAY
     */
    public Cursor getTaskByDay(String day){
        database = task_dbHelper.getWritableDatabase();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        String where = DB_ABSTRACTS.DBTasks.DAY_COLUMN + " = ?";

        Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, where, new String[] {day},null, null, DB_ABSTRACTS.DBTasks.TIME_COLUMN + " ASC");
        if(cursor.moveToFirst()){
            do{
                Task task = new Task();
                Professor prof = new Professor();
                task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID)));
                try {
                    task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN))));
                    //task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                task.setTitle(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN)));
                task.setNote(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN)));
                task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)));
                task.setLocation(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN)));
                prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN)));
                task.setProfessor(prof);
            }while(cursor.moveToNext());
        }
        return cursor;
    }

    /*
    GET MEETINGS BY CURRENT DAY
     */
    public Cursor getTaskByCurrentDate() {
        database = task_dbHelper.getWritableDatabase();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH:mm");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Professor prof = new Professor();
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        //c.set(year, month, day, 00, 00, 00);
        Date actual_date = new Date();

        try {
            actual_date = dateFormat.parse(year + " " + (month + 1) + " " + day + " 00" + ":" + "00" + ":" + "00");

            //actual_date = dateFormat.format(c.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //PARSE THE ACTUAL DATE TO GET THE DAY, AND THEN USE IT IN THE WHERE CLAUSE TO GET DATA
        String where = DB_ABSTRACTS.DBTasks.DAY_COLUMN + " = '" + actual_date + "'";
        Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, where, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                Task task = new Task();
                task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID)));
                task.setTitle(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN)));
                task.setNote(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN)));
                task.setLocation(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN)));
                task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)));
                try {
                    task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN))));
                    //task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN)));
                task.setProfessor(prof);
                //Log.e("includes", " where string " + where);

            } while (cursor.moveToNext());
        }
        return cursor;
    }

    /*
    UPDATE A MEETING
     */
    public int updateTask(Task task) {
        try {
            this.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ContentValues values = new ContentValues();
        values.put(DB_ABSTRACTS.DBTasks.TIME_COLUMN, task.getTime().toString());
        //values.put(DB_ABSTRACTS.DBTasks.DAY_COLUMN, task.getDate().toString());
        values.put(DB_ABSTRACTS.DBTasks.DAY_COLUMN, task.getDay());
        values.put(DB_ABSTRACTS.DBTasks.TITLE_COLUMN, task.getTitle());
        values.put(DB_ABSTRACTS.DBTasks.NOTE_COLUMN, task.getNote());
        values.put(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN, task.getLocation());
        values.put(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN, task.getProfessor().getProfessor_name());

        String where = DB_ABSTRACTS.DBTasks.KEY_ID + " = ?";

        //update row
        return database.update(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, values, where, new String[]{String.valueOf(task.getId())});

    }

    /*
    UPDATE TASK BY PROFESSOR
     */
    public int updateByProfessor(Professor prof) {

        try {
            this.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ContentValues values = new ContentValues();
        values.put(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN, prof.getProfessor_name());

        String where = DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN + " = ?";

        return database.update(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, values, where, new String[]{String.valueOf(prof.getProfessor_name())});
    }

    /*
    DELETE A MEETING
     */
    public void deleteTask(long task_id) {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String where = DB_ABSTRACTS.DBTasks.KEY_ID + " = ?";
        database.delete(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, where, new String[]{String.valueOf(task_id)});
    }

    /*
    GET # OF ALL MEETINGS
     */
    public int getTaskCount() {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, null, null, null, null, null);
        int counter = cursor.getCount();
        cursor.close();

        return counter;
    }

}




Java Source Code List

DB_Provider.CourseDataSource.java
DB_Provider.DBManager.java
DB_Provider.DB_ABSTRACTS.java
DB_Provider.ProfessorDataSource.java
DB_Provider.TaskDataSource.java
Drawables.DrawableAlignedButton.java
FontPackage.TypefaceSpan.java
adapters.CourseCardAdapter.java
adapters.ExamsPassedCardAdapter.java
adapters.ExamsPendingCardAdapter.java
adapters.Exams_TabsPagerAdapter.java
adapters.HomeCardAdapter.java
adapters.MatesCardAdapter.java
adapters.NavDrawerListAdapter.java
adapters.ProfessorCardAdapter.java
adapters.TaskCardAdapter.java
adapters.TasksTabPagerAdapter.java
com.example.uniutilproject.BuildConfig.java
com.example.uniutilproject.UniUtil_MainActivity.java
days_fragments.FridayTaskFragment.java
days_fragments.MondayTaskFragment.java
days_fragments.SaturdayTaskFragment.java
days_fragments.SundayTaskFragment.java
days_fragments.ThursdayTaskFragment.java
days_fragments.TuesdayTaskFragment.java
days_fragments.WednesdayTaskFragment.java
dialog_fragments.DatePickerDialogFragment.java
dialog_fragments.MatesDialogFragment.java
dialog_fragments.ProfessorsDialogFragment.java
dialog_fragments.TimePickerDialogFragment.java
fragments.AddCourseFragment.java
fragments.AddProfFragment.java
fragments.AddTaskFragment.java
fragments.CoursesFragment.java
fragments.CursorLoaderListFragment.java
fragments.ExamsFragment.java
fragments.ExamsPassedFragment.java
fragments.ExamsPendingFragment.java
fragments.HomeFragment.java
fragments.MatesFragment.java
fragments.ProfessorsFragment.java
fragments.StudyPlanFragment.java
fragments.TasksFragment.java
fragments.UpdateCourseFragment.java
fragments.UpdateProfessorFragment.java
interfaces.NoticeDialogListener.java
interfaces.package-info.java
model.Course.java
model.Days.java
model.MyParcelable.java
model.NavDrawerItem.java
model.Professor.java
model.Task.java