Back to project page ProjectStudio.
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.
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; } }