Android Open Source - ProjectStudio Course 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  www .  j av  a 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.DecimalFormat;
import java.text.SimpleDateFormat;

import model.Course;
import model.Professor;

/**
 * Created by desmond on 1/29/14.
 */
public class CourseDataSource {

    //DATEBASE FIELDS
    private SQLiteDatabase database;
    private DBManager course_dbHelper;
    /*
    WILL BE USED TO INSERT ALL DATA RELATED TO A GENEREAL COURSE
     */
    private String allColumns[] = {
            DB_ABSTRACTS.DBCourse.KEY_ID, DB_ABSTRACTS.DBCourse.NAME_COLUMN,
            DB_ABSTRACTS.DBCourse.ROOM_COLUMN, DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN,
            DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN, DB_ABSTRACTS.DBCourse.PASSED_COLUMN,
            DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN
    };

    /*
    WILL BE USED TO GET ALL DATA RELATED TO A COURSE THAT IS ALREADY IN THE DATABASE
     */
    private String[] courseColumns = {
            DB_ABSTRACTS.DBCourse.KEY_ID, DB_ABSTRACTS.DBCourse.NAME_COLUMN, DB_ABSTRACTS.DBCourse.ROOM_COLUMN,
            DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN, DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN,
    };

    /*
    WILL BE USED TO GET ALL EXAMS RELATED TO A COURSE THAT IS ALREADY IN THE DATABASE
     */
    private String[] examColumns = {
            DB_ABSTRACTS.DBCourse.KEY_ID, DB_ABSTRACTS.DBCourse.NAME_COLUMN,
            DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN,
            DB_ABSTRACTS.DBCourse.PASSED_COLUMN, DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN
    };

    public CourseDataSource(Context context) {
        course_dbHelper = new DBManager(context);
    }

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

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

    /*
    INSERT INTO THE DATABASE
     */
    public long createCourse(Course course, Professor prof) {

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

        ContentValues values = new ContentValues();
        values.put(DB_ABSTRACTS.DBCourse.NAME_COLUMN, course.getName());
        values.put(DB_ABSTRACTS.DBCourse.ROOM_COLUMN, course.getRoom());
        values.put(DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN, prof.getProfessor_name());
        values.put(DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN, course.getSemester());
        values.put(DB_ABSTRACTS.DBCourse.PASSED_COLUMN, course.getPassed());
        values.put(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN, course.getPass_mark());

        long id = database.insert(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, null, values);

        return id;
    }

    /*
    GET A SINGLE COURSE
     */
    public Course getCourse(long course_id) {

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

        String where = DB_ABSTRACTS.DBCourse.KEY_ID + " = " + course_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);
        Course course = new Course();
        course.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID)));
        course.setName(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
        prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN)));
        course.setProfessor(prof);
        course.setSemester(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN)));
        course.setRoom(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.ROOM_COLUMN)));
        course.setPassed(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASSED_COLUMN)));

        return course;
    }

    /*
    GET COURSES WITH ALL COLUMNS
     */
    public Cursor getAllCourseData() {

        database = course_dbHelper.getWritableDatabase();

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, allColumns, null, null, null, null, DB_ABSTRACTS.DBCourse.NAME_COLUMN + " ASC");
        Professor prof = new Professor();

        if (cursor.moveToFirst()) {
            do {
                Course course = new Course();
                course.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID)));
                course.setName(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
                prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN)));
                course.setProfessor(prof);
                course.setSemester(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN)));
                course.setRoom(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.ROOM_COLUMN)));
                course.setPassed(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASSED_COLUMN)));
            } while (cursor.moveToNext());
        }
        return cursor;
    }

    /*
        GET COURSE WITH SPECIFIC COURSE COLUMNS
        RETURNS A TABLE CONTAINING COURSE RELATED INFORMATION
     */
    public Cursor getAllCourses() {

        database = course_dbHelper.getWritableDatabase();

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, courseColumns, null, null, null, null, DB_ABSTRACTS.DBCourse.NAME_COLUMN + " ASC");
        Professor prof = new Professor();

        if (cursor.moveToFirst()) {
            do {
                Course course = new Course();
                course.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID)));
                course.setName(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
                prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN)));
                course.setProfessor(prof);
                course.setRoom(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.ROOM_COLUMN)));
                course.setSemester(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN)));
            } while (cursor.moveToNext());
        }

        return cursor;
    }

    /*
    GET ALL EXAMS
    RETURNS A TABLE CONTAING EXAM RELATED INFORMATION
     */
    public Cursor getAllExamsData() {

        database = course_dbHelper.getWritableDatabase();

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, examColumns, null, null, null, null, DB_ABSTRACTS.DBCourse.NAME_COLUMN + " ASC");


        if (cursor.moveToFirst()) {
            do {
                Course course = new Course();
                course.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID)));
                course.setName(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
                course.setPassed(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASSED_COLUMN)));
                course.setPass_mark(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN)));
            } while (cursor.moveToNext());
        }

        return cursor;
    }

    /*
    GET PENDING EXAMS
     */

    public Cursor getPendingExams() {

        database = course_dbHelper.getWritableDatabase();
        String where = DB_ABSTRACTS.DBCourse.PASSED_COLUMN + " = " + 0;

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, examColumns, where, null, null, null, DB_ABSTRACTS.DBCourse.NAME_COLUMN + " ASC");

        if (cursor.moveToFirst()) {
            do {
                Course course = new Course();
                course.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID)));
                course.setName(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
                course.setPassed(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASSED_COLUMN)));
                course.setPass_mark(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN)));
            } while (cursor.moveToNext());
        }

        return cursor;
    }

    /*
    GET PASSED EXAMS
     */
    public Cursor getPassedExams() {

        database = course_dbHelper.getWritableDatabase();

        String where = DB_ABSTRACTS.DBCourse.PASSED_COLUMN + " = " + 1;

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, examColumns, where, null, null, null, DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN + " ASC");

        if (cursor.moveToFirst()) {
            do {
                Course course = new Course();
                course.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.KEY_ID)));
                course.setName(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.NAME_COLUMN)));
                course.setPassed(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASSED_COLUMN)));
                course.setPass_mark(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN)));
            } while (cursor.moveToNext());
        }
        return cursor;
    }

    /*
    GET NUMBER OF PENDING EXAMS
     */
    public int getPendingExamsCount() {

        database = course_dbHelper.getWritableDatabase();
        String where = DB_ABSTRACTS.DBCourse.PASSED_COLUMN + " = " + 0;

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, examColumns, where, null, null, null, null);
        int counter = cursor.getCount();
        cursor.close();
        return counter;
    }


    /*
    GET NUMBER OF PASSED EXAMS
     */

    public int getPassedExamsCount() {

        database = course_dbHelper.getWritableDatabase();
        String where = DB_ABSTRACTS.DBCourse.PASSED_COLUMN + " = " + 1;

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, examColumns, where, null, null, null, null);
        int counter = cursor.getCount();
        cursor.close();
        return counter;
    }


    /*
    GET AVERAGE OF PASSED EXAMS
     */

    public float getPassedExamsAverge() {

        database = course_dbHelper.getWritableDatabase();
        String where = DB_ABSTRACTS.DBCourse.PASSED_COLUMN + " = " + 1;

        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.DATABASE_TABLE, examColumns, where, null, null, null, null);
        float count = cursor.getCount();
        float score_sum = 0;
        if (count > 0) {
            if (cursor.moveToFirst()) {
                 do {
                   // String pass_mark = DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN;
                    score_sum += cursor.getFloat(cursor.getColumnIndex(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN));
                } while (cursor.moveToNext());
            }
        } else {
            return 0;
        }
        float avg = score_sum/count;
        //ROUND THE VALUE
        DecimalFormat df = new DecimalFormat("#.##");
        avg = Float.valueOf(df.format(avg));
        return avg;
    }


    /*
    UPDATE COURSE
     */

    public int updateCourse(Course course) {

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

        ContentValues values = new ContentValues();
        values.put(DB_ABSTRACTS.DBCourse.NAME_COLUMN, course.getName());
        values.put(DB_ABSTRACTS.DBCourse.ROOM_COLUMN, course.getRoom());
        values.put(DB_ABSTRACTS.DBCourse.PROFESSOR_COLUMN, course.getProfessor().getProfessor_name());
        values.put(DB_ABSTRACTS.DBCourse.SEMESTER_COLUMN, course.getSemester());
        values.put(DB_ABSTRACTS.DBCourse.PASSED_COLUMN, course.getPassed());
        values.put(DB_ABSTRACTS.DBCourse.PASS_MARK_COLUMN, course.getPass_mark());

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

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

    /*
   DELETE A COURSE
    */
    public void deleteCourse(long course_id) {

        database = course_dbHelper.getWritableDatabase();

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

    /*
   GET # OF ALL COURSES
    */
    public int getCourseCount() {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Cursor cursor = database.query(DB_ABSTRACTS.DBCourse.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