Android Open Source - Gradulator Course Data Source






From Project

Back to project page Gradulator.

License

The source code is released under:

MIT License

If you think the Android project Gradulator 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

/*
The MIT License (MIT)//  w w  w. j  a  v a 2 s .c o  m

Copyright (c) 2014 Gannon Lawlor

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.gannon.gradulator.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gannon.gradulator.models.Course;
import com.gannon.gradulator.database.CourseContract.CourseEntry;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CourseDataSource {

    private SQLiteDatabase database;
    private CourseDbHelper dbHelper;
    private String[] allColumns =
            {CourseEntry._ID,
                    CourseEntry.COLUMN_COURSE_NAME,
            CourseEntry.COLUMN_COURSE_GRADE};

    public CourseDataSource(Context context) {
        dbHelper = CourseDbHelper.getInstance(context);
    }

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

    public void close() {
        dbHelper.close();
    }

    public Course createCourse(String name, double grade) {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ContentValues values = new ContentValues();
        values.put(CourseEntry.COLUMN_COURSE_NAME, name);
        values.put(CourseEntry.COLUMN_COURSE_GRADE, grade);



        // Insert the new row, returning the primary key value of the new row
        long newRowId;
        newRowId = database.insert(
                CourseEntry.TABLE_COURSES,
                CourseEntry.COLUMN_NAME_NULLABLE,
                values);

        Cursor cursor = database.query(CourseEntry.TABLE_COURSES, allColumns, CourseEntry._ID + " = " + newRowId, null, null, null, null);
        cursor.moveToFirst();
        Course newCourse = cursorToCourse(cursor);
        cursor.close();
        return newCourse;
    }

    /*
     * Updating a Course
     */
    public void updateCourse(Course course) {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ContentValues values = new ContentValues();
        values.put(CourseEntry.COLUMN_COURSE_NAME, course.getName());
        values.put(CourseEntry.COLUMN_COURSE_GRADE, course.getGrade());

        database.update(CourseEntry.TABLE_COURSES, values, CourseEntry._ID + " =?", new String[]{ String.valueOf(course.getId())});
    }

    public void deleteCourse(Course course) {
        long id = course.getId();


        // Delete Course from course table
        database.delete(CourseEntry.TABLE_COURSES, CourseEntry._ID + " = " + id, null);

        // Delete all the evaluations for that course
        database.execSQL("DELETE FROM " + CourseEntry.TABLE_EVALUATION + " WHERE " + CourseEntry.COLUMN_COURSE_ID + " = " + id + ";");
    }

    public List<Course> getAllCourses() {
        List<Course> courses = new ArrayList<Course>();

        Cursor cursor = database.query(CourseEntry.TABLE_COURSES, allColumns, null, null, null, null, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Course course = cursorToCourse(cursor);
            courses.add(course);
            cursor.moveToNext();
        }

        cursor.close();
        return courses;
    }

    private Course cursorToCourse(Cursor cursor) {
        Course course = new Course();
        course.setId(cursor.getLong(0));
        course.setName(cursor.getString(1));
        course.setGrade(cursor.getDouble(2));
        return course;
    }

}




Java Source Code List

com.gannon.gradulator.ApplicationTest.java
com.gannon.gradulator.CourseActivity.java
com.gannon.gradulator.EvaluationActivity.java
com.gannon.gradulator.MainActivity.java
com.gannon.gradulator.adapters.CourseAdapter.java
com.gannon.gradulator.adapters.EvaluationAdapter.java
com.gannon.gradulator.database.CourseContract.java
com.gannon.gradulator.database.CourseDataSource.java
com.gannon.gradulator.database.CourseDbHelper.java
com.gannon.gradulator.database.EvaluationDataSource.java
com.gannon.gradulator.dialogfragments.AddCourseDialogFragment.java
com.gannon.gradulator.dialogfragments.DeleteCourseDialogFragment.java
com.gannon.gradulator.dialogfragments.DeleteEvaluationDialogFragment.java
com.gannon.gradulator.models.Course.java
com.gannon.gradulator.models.Evaluation.java