// Getters // Getting specific assignment public Assignment getAssignment(int aid) { Log.d("Went into getAssignment", "yes: " + Integer.toString(aid)); String selectQuery = "SELECT * FROM Assignment " + " WHERE aid = " + aid; Log.d("selectQuery: ", selectQuery); SQLiteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery, null); cursor.moveToFirst(); Log.d("getAssignment cursor size: ", Integer.toString(cursor.getCount())); Log.d("getAssignment cursor 1st entry: ", cursor.getString(0)); Assignment assignment = new Assignment(); assignment.setaid(Integer.parseInt(cursor.getString(0))); assignment.settitle(cursor.getString(1)); assignment.settype(cursor.getString(2)); assignment.setmonth(Integer.parseInt(cursor.getString(3))); assignment.setday(Integer.parseInt(cursor.getString(4))); assignment.setyear(Integer.parseInt(cursor.getString(5))); assignment.setpriority(Integer.parseInt(cursor.getString(6))); Log.d("Got priority", cursor.getString(6)); Log.d("Course = ", cursor.getString(7)); if (cursor.getInt(7) > 0) assignment.setCourse(getCourse(Integer.parseInt(cursor.getString(7)))); else assignment.setCourse(null); if (cursor.getInt(8) > 0) assignment.setInstructor(getInstructor(Integer.parseInt(cursor.getString(8)))); else assignment.setInstructor(null); assignment.setPartner(getPartners(assignment.getaid())); assignment.setnotes(cursor.getString(9)); assignment.setCompleted(cursor.getInt(10)); if (cursor.getInt(10) == 1) { assignment.setGrade(getGrade(assignment.getaid())); } Log.d("Got everything successfully", "yes"); return assignment; }
// Setters // Adding new Assignment public void addAssignment(Assignment assignment) { Log.d("cane into addAssignment", "yes"); SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(TITLE, assignment.gettitle()); values.put(TYPE, assignment.gettype()); values.put(MONTH, assignment.getmonth()); values.put(DAY, assignment.getday()); values.put(YEAR, assignment.getyear()); values.put(PRIORITY, assignment.getpriority()); Log.d("set priority", "yes"); if (assignment.getCourse() != null) values.put(C_ID, assignment.getCourse().getcid()); else values.put(C_ID, 0); if (assignment.getinstructor() != null) values.put(I_ID, assignment.getinstructor().getiid()); else values.put(I_ID, 0); values.put(NOTES, assignment.getnotes()); values.put(COMPLETED, assignment.getcompleted()); Log.d("ContentValues:", values.toString()); Log.d("Am about to input values to database", "yes"); long aid = db.insert(TABLE_ASSIGNMENT, null, values); assignment.setaid((int) aid); db.close(); // Closing database connection if (assignment.getpartners().size() > 0) { for (int count = 0; count < assignment.getpartners().size(); count++) { assignment.getpartners().get(count).setaid(assignment.getaid()); } addPartner(assignment.getpartners()); } if (assignment.getgrade() != null) { assignment.getgrade().setaid(assignment.getaid()); addGrade(assignment.getgrade()); } }
// Delete public void deleteAssignment(Assignment assignment) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_ASSIGNMENT, A_ID + "=?", new String[] {String.valueOf(assignment.getaid())}); db.close(); deletePartners(assignment.getpartners()); deleteGrade(assignment.getgrade()); }
// Update public int updateAssignment(Assignment assignment) { int update; Log.d("Went into updateAssignment", "yes"); SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(TITLE, assignment.gettitle()); values.put(TYPE, assignment.gettype()); values.put(MONTH, assignment.getmonth()); values.put(DAY, assignment.getday()); values.put(YEAR, assignment.getyear()); values.put(PRIORITY, assignment.getpriority()); if (assignment.getCourse() != null) values.put(C_ID, assignment.getCourse().getcid()); if (assignment.getinstructor() != null) values.put(I_ID, assignment.getinstructor().getiid()); values.put(NOTES, assignment.getnotes()); values.put(COMPLETED, assignment.getcompleted()); update = db.update( TABLE_ASSIGNMENT, values, A_ID + "= ?", new String[] {String.valueOf(assignment.getaid())}); if (assignment.getpartners() != null) updatePartner(assignment.getpartners()); if (assignment.getgrade() != null) updateGrade(assignment.getgrade()); return update; }