Android Open Source - GradeCalculator-Android Course Storage Adapter






From Project

Back to project page GradeCalculator-Android.

License

The source code is released under:

Copyright (C) 2012 Jimmy Theis 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 ...

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

/*
 * Copyright (C) 2012 Jimmy Theis. Licensed under the MIT License:
 * /* w ww  .  j av a 2 s .  c  o  m*/
 * 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.jetheis.android.grades.storage;

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

import com.jetheis.android.grades.model.Course;
import com.jetheis.android.grades.model.Course.CourseType;

public class CourseStorageAdapter extends StorageAdapter {

    private static final String TABLE_NAME = "courses";
    private static final String ID_COLUMN = "_id";
    private static final String NAME_COLUMN = "name";
    private static final String GRADE_TYPE_COLUMN = "grade_type";

    /**
     * Save a {@link Course} that has never been saved in the database as a new
     * database record. The provided {@link Course} will have its database
     * identifier updated with the value stored in the database.
     * 
     * @param course
     *            An unsaved {@link Course}. This course will have its
     *            identifier field (accessed through {@link Course#getId()})
     *            updated in place with the appropriate value.
     */
    public void createCourse(Course course) {
        course.setId(getDb().insert(TABLE_NAME, null, getContentValuesFromCourse(course)));
    }

    /**
     * Update the values of an existing {@link Course} in the database.
     * 
     * @param course
     *            The {@link Course} to be updated.
     */
    public void updateCourse(Course course) {
        getDb().update(TABLE_NAME, getContentValuesFromCourse(course), ID_COLUMN + " = ?",
                new String[] { Long.toString(course.getId()) });
    }

    /**
     * Convenience method for saving any type of {@link Course}. Based on the
     * unique identifier of the provided {@link Course}, this method will decide
     * whether to create a new database record or update an existing one.
     * 
     * @param course
     *            The {@link Course} (new or existing) to be saved.
     */
    public void saveCourse(Course course) {
        if (course.getId() < 1) {
            createCourse(course);
        } else {
            updateCourse(course);
        }
    }

    /**
     * Get all existing {@link Course}s, in {@link CourseStorageIterator} form.
     * The results will be sorted alphabetically by name.
     * 
     * @return A {@link CourseStorageIterator} containing all {@link Course}s in
     *         the database, sorted by name, descending.
     */
    public CourseStorageIterator getAllCourses() {
        return new CourseStorageIterator(getDb().query(TABLE_NAME, null, null, null, null, null,
                NAME_COLUMN + " ASC"));
    }

    /**
     * Retrieve a {@link Course} from the database based on its unique
     * identifier.
     * 
     * @param id
     *            The {@link Course} to be retrieved's unique identifer.
     * @return The desired {@link Course}, if found, and {@code null} otherwise.
     */
    public Course getCourseById(long id) {
        CourseStorageIterator result = new CourseStorageIterator(getDb().query(TABLE_NAME, null,
                ID_COLUMN + " = ?", new String[] { Long.toString(id) }, null, null, null));

        if (result.getCount() < 1)
            return null;

        return result.next();
    }

    /**
     * Delete all {@link Course} records from the database.
     * 
     * @return The number of {@link Course}s deleted.
     */
    public int deleteAllCourses() {
        return getDb().delete(TABLE_NAME, null, null);
    }

    /**
     * Delete a given {@link Course}, settings its unique identifier to 0 after
     * the deletion.
     * 
     * @param course
     *            The {@link Course} to delete.
     * 
     * @return The number of database records affected.
     */
    public int deleteCourse(Course course) {
        int result = deleteCourseById(course.getId());

        if (result > 0) {
            course.setId(0);
        }

        return result;
    }

    /**
     * Delete the {@link Course} with the given unique identifier from the
     * database, if it exists.
     * 
     * @param id
     *            The unique identifier of the {@link Course} to be deleted.
     * @return The number of database records affected.
     */
    public int deleteCourseById(long id) {
        return getDb().delete(TABLE_NAME, ID_COLUMN + " = ?", new String[] { Long.toString(id) });
    }

    /**
     * Convert a {@link Course} to a {@link ContentValues} object for storage in
     * a {@link SQLiteDatabase}. This conversion leaves out the unique database
     * identifier, because this field will never be changed in the database, and
     * as such does not belong in a {@link ContentValues} object to be written
     * to the database.
     * 
     * @param course
     *            A {@link Course} to be converted.
     * @return
     */
    private ContentValues getContentValuesFromCourse(Course course) {
        ContentValues result = new ContentValues();

        result.put(NAME_COLUMN, course.getName());
        result.put(GRADE_TYPE_COLUMN, course.getCourseType().toInt());

        return result;
    }

    /**
     * A {@link StorageIterator} subclass for iterating over {@link Course}
     * results generated by a database query.
     * 
     */
    public class CourseStorageIterator extends StorageIterator<Course> {

        public CourseStorageIterator(Cursor cursor) {
            super(cursor);
        }

        @Override
        protected Course getObjectFromNextRow(Cursor cursor) {
            Course result = new Course();

            result.setId(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)));
            result.setName(cursor.getString(cursor.getColumnIndex(NAME_COLUMN)));
            result.setCourseType(CourseType.fromInt(cursor.getInt(cursor
                    .getColumnIndex(GRADE_TYPE_COLUMN))));

            return result;
        }

    }

}




Java Source Code List

com.jetheis.android.grades.Constants.java
com.jetheis.android.grades.activity.AboutActivity.java
com.jetheis.android.grades.activity.CourseActivity.java
com.jetheis.android.grades.activity.CourseListActivity.java
com.jetheis.android.grades.billing.BillingWrapper.java
com.jetheis.android.grades.billing.FreeBillingWrapper.java
com.jetheis.android.grades.billing.Security.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingConstants.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingReceiver.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingService.java
com.jetheis.android.grades.billing.googleplay.GooglePlayBillingWrapper.java
com.jetheis.android.grades.fragment.AboutFragment.java
com.jetheis.android.grades.fragment.AddCourseDialogFragment.java
com.jetheis.android.grades.fragment.AddGradeComponentDialogFragment.java
com.jetheis.android.grades.fragment.BuyFullVersionFragment.java
com.jetheis.android.grades.fragment.CourseListFragment.java
com.jetheis.android.grades.fragment.CourseOverviewFragment.java
com.jetheis.android.grades.fragment.EditCourseDialogFragment.java
com.jetheis.android.grades.fragment.EditGradeComponentDialogFragment.java
com.jetheis.android.grades.fragment.GradeComponentListFragment.java
com.jetheis.android.grades.listadapter.CourseArrayAdapter.java
com.jetheis.android.grades.model.Course.java
com.jetheis.android.grades.model.GradeComponent.java
com.jetheis.android.grades.model.PercentageGradeComponent.java
com.jetheis.android.grades.model.PointTotalGradeComponent.java
com.jetheis.android.grades.storage.CourseStorageAdapter.java
com.jetheis.android.grades.storage.DatabaseHelper.java
com.jetheis.android.grades.storage.GradeComponentStorageAdapter.java
com.jetheis.android.grades.storage.Storable.java
com.jetheis.android.grades.storage.StorageAdapter.java
com.jetheis.android.grades.storage.StorageIterator.java