Android Open Source - Gradulator Evaluation 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)//from ww w  .ja v a  2s .co  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.Evaluation;
import com.gannon.gradulator.database.CourseContract.CourseEntry;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EvaluationDataSource{

    private SQLiteDatabase database;
    private CourseDbHelper dbHelper;
    private String[] allColumns =
            {CourseEntry._ID,
             CourseEntry.COLUMN_COURSE_ID,
             CourseEntry.COLUMN_EVALUATION_NAME,
             CourseEntry.COLUMN_EVALUATION_GRADE,
            CourseEntry.COLUMN_EVALUATION_VALUE};

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

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

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

    public Evaluation createEvaluation(String name, double grade, double value, long courseId) {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ContentValues values = new ContentValues();
        values.put(CourseEntry.COLUMN_EVALUATION_NAME, name);
        values.put(CourseEntry.COLUMN_EVALUATION_VALUE, value);
        values.put(CourseEntry.COLUMN_EVALUATION_GRADE, grade);
        values.put(CourseEntry.COLUMN_COURSE_ID, courseId);

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

        Cursor cursor = database.query(CourseEntry.TABLE_EVALUATION, allColumns, CourseEntry._ID + " = " + newRowId, null, null, null, null);
        cursor.moveToFirst();
        Evaluation evaluation = cursorToEvaluation(cursor);
        cursor.close();
        return evaluation;
    }

    public void deleteEvaluation(Evaluation evaluation) {
        long id = evaluation.getId();
        database.delete(CourseEntry.TABLE_EVALUATION, CourseEntry._ID + " = " + id, null);
    }

    /*
    * Updating a evaluation
    */
    public void updateEvaluation(Evaluation evaluation) {
        try {
            open();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        long id = evaluation.getId();

        ContentValues values = new ContentValues();
        values.put(CourseEntry.COLUMN_EVALUATION_NAME, evaluation.getName());
        values.put(CourseEntry.COLUMN_EVALUATION_GRADE, evaluation.getGrade());
        values.put(CourseEntry.COLUMN_EVALUATION_VALUE, evaluation.getValue());

        //todo = get rid of this stuff
        String updateQuery = "UPDATE " + CourseEntry.TABLE_EVALUATION + " SET "
                + CourseEntry.COLUMN_EVALUATION_NAME + " = " + evaluation.getName() + ", "
                + CourseEntry.COLUMN_EVALUATION_GRADE + " = " + evaluation.getGrade() + ", "
                + CourseEntry.COLUMN_EVALUATION_VALUE + " = " + evaluation.getValue()
                + " WHERE " + CourseEntry._ID + " = " + evaluation.getId() + ";";

        // updating row
        database.update(CourseEntry.TABLE_EVALUATION, values, CourseEntry._ID + " =?",
                new String[] { String.valueOf(evaluation.getId()) });
        //database.rawQuery(updateQuery, null);
    }

    public List<Evaluation> getAllEvaluationsByCourse(long courseId) {
        List<Evaluation> evaluations = new ArrayList<Evaluation>();

        // Query to get all evaluations from a given course
        String selectQuery = " SELECT * FROM " + CourseEntry.TABLE_EVALUATION +
                " WHERE " + CourseEntry.COLUMN_COURSE_ID + " = " + courseId + ";";

        Cursor cursor = database.rawQuery(selectQuery, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            Evaluation evaluation = cursorToEvaluation(cursor);
            evaluations.add(evaluation);
            cursor.moveToNext();
        }

        cursor.close();
        return evaluations;
    }

    private Evaluation cursorToEvaluation(Cursor cursor) {
        Evaluation evaluation = new Evaluation();
        evaluation.setId(cursor.getLong(0));
        evaluation.setCourseId(cursor.getLong(1));
        evaluation.setName(cursor.getString(2));
        evaluation.setGrade(cursor.getDouble(3));
        evaluation.setValue(cursor.getDouble(4));
        return evaluation;
    }

}




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