public void updateHomeWork(HomeWork hw) { try { mDb.beginTransaction(); if (hw != null) { mUpdateHomeWorkStatement.clearBindings(); if (hw.isComplite()) { mUpdateHomeWorkStatement.bindLong(1, 1); NotificationManager.getInstance().removeNotification(hw); } else { mUpdateHomeWorkStatement.bindLong(1, 0); NotificationManager.getInstance().addNewNotification(hw); } if (hw.getMessage() != null) { mUpdateHomeWorkStatement.bindString(2, hw.getMessage()); } if (hw.getMessage() != null) { mUpdateHomeWorkStatement.bindBlob(3, Slipper.serializeObject(hw.getImages())); } mUpdateHomeWorkStatement.bindLong(4, hw.getId()); mUpdateHomeWorkStatement.execute(); } mDb.setTransactionSuccessful(); } catch (Exception e) { e.printStackTrace(); } finally { mDb.endTransaction(); } }
private HomeWork getHomeWorkFromCursor(Cursor c) { HomeWork result = new HomeWork(); result.setId(c.getLong(c.getColumnIndex(HomeWork.ID))); result.setDate( new Date(c.getLong(c.getColumnIndex(HomeWork.DATE)) * CalendarManager.MILISECONDS_PER_DAY)); result.setLessonId(c.getLong(c.getColumnIndex(HomeWork.LESSON_ID))); result.setMessage(c.getString(c.getColumnIndex(HomeWork.MESSAGE))); result.setImages( Slipper.deserializeObjectToString(c.getBlob(c.getColumnIndex(HomeWork.IMAGES)))); result.setComplite(c.getInt(c.getColumnIndex(HomeWork.COMPLITE)) > 0); result.setGroupId(c.getLong(c.getColumnIndex(HomeWork.GROUP_ID))); return result; }
public ArrayList<HomeWork> getAllHomeWorks() { Long groupId = PreferenceManager.getInstance().getGroupId(); ArrayList<HomeWork> result = new ArrayList<HomeWork>(); String query = TextUtils.concat( "SELECT h.*,l.* FROM ", HomeWork.TABLE_NAME, " as h ", "INNER JOIN ", LessonInformation.TABLE_NAME, " as l ON ", " h.", HomeWork.LESSON_ID, "=l.", LessonInformation.LESSON_ID, " WHERE h.", HomeWork.GROUP_ID, "=? AND h.", HomeWork.DATE, ">=? ", " GROUP BY l.", LessonInformation.LESSON_ID, " ORDER BY h.", HomeWork.DATE) .toString(); Cursor c = mDb.rawQuery( query, new String[] { groupId.toString(), Long.toString(Calendar.getInstance().getTimeInMillis()) }); while (c.moveToNext()) { HomeWork hw = new HomeWork(); hw.setId(c.getLong(c.getColumnIndex(HomeWork.ID))); hw.setDate(new Date(c.getLong(c.getColumnIndex(HomeWork.DATE)))); hw.setLessonId(c.getLong(c.getColumnIndex(HomeWork.LESSON_ID))); hw.setMessage(c.getString(c.getColumnIndex(HomeWork.MESSAGE))); hw.setImages(Slipper.deserializeObjectToString(c.getBlob(c.getColumnIndex(HomeWork.IMAGES)))); hw.setComplite(c.getInt(c.getColumnIndex(HomeWork.COMPLITE)) > 0); hw.setGroupId(c.getLong(c.getColumnIndex(HomeWork.GROUP_ID))); hw.setLessonTitle(c.getString(c.getColumnIndex(LessonInformation.LESSON_TITLE))); result.add(hw); } return result; }
public void saveHomeWork(HomeWork homeWork) { mDb.beginTransaction(); try { mSaveHomeWorkStatement.clearBindings(); if (homeWork.getDate() != null) { mSaveHomeWorkStatement.bindLong( 1, homeWork.getDate().getTime() / CalendarManager.MILISECONDS_PER_DAY); } mSaveHomeWorkStatement.bindLong(2, homeWork.getLessonId()); if (homeWork.getMessage() != null) { mSaveHomeWorkStatement.bindString(3, homeWork.getMessage()); } if (homeWork.getImages() != null) { mSaveHomeWorkStatement.bindBlob(4, Slipper.serializeObject(homeWork.getImages())); } if (homeWork.isComplite()) { mSaveHomeWorkStatement.bindLong(5, 1); } else { mSaveHomeWorkStatement.bindLong(5, 0); } mSaveHomeWorkStatement.bindLong(6, PreferenceManager.getInstance().getGroupId()); homeWork.setId(mSaveHomeWorkStatement.executeInsert()); mDb.setTransactionSuccessful(); NotificationManager.getInstance().addNewNotification(homeWork); } catch (Exception e) { e.printStackTrace(); } finally { mDb.endTransaction(); } }