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 w ww .j a va 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.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import model.Professor; import model.Task; /** * Created by desmond on 1/27/14. */ public class TaskDataSource { //Database Fields private SQLiteDatabase database; private DBManager task_dbHelper; private String[] allColumns = {DB_ABSTRACTS.DBTasks.KEY_ID, DB_ABSTRACTS.DBTasks.TITLE_COLUMN, DB_ABSTRACTS.DBTasks.NOTE_COLUMN, DB_ABSTRACTS.DBTasks.LOCATION_COLUMN, DB_ABSTRACTS.DBTasks.TIME_COLUMN, DB_ABSTRACTS.DBTasks.DAY_COLUMN, DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN }; public TaskDataSource(Context context) { task_dbHelper = new DBManager(context); } public void open() throws SQLException { database = task_dbHelper.getWritableDatabase(); } public void close() { if (database != null && database.isOpen()) { task_dbHelper.close(); } } /* INSERT A MEETING INTO THE DATABASE */ public long createTask(Task task, Professor professor) { try { this.open(); } catch (SQLException e) { e.printStackTrace(); } ContentValues values = new ContentValues(); values.put(DB_ABSTRACTS.DBTasks.TITLE_COLUMN, task.getTitle()); values.put(DB_ABSTRACTS.DBTasks.NOTE_COLUMN, task.getNote()); values.put(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN, task.getLocation()); values.put(DB_ABSTRACTS.DBTasks.TIME_COLUMN, task.getTime().toString()); values.put(DB_ABSTRACTS.DBTasks.DAY_COLUMN, task.getDay()); values.put(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN, professor.getProfessor_name()); //INSERT ROW long id = database.insert(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, null, values); return id; } /* GET A SINGLE MEETING */ public Task getTask(long meeting_id) throws ParseException { database = task_dbHelper.getWritableDatabase(); String where = DB_ABSTRACTS.DBTasks.KEY_ID + " = " + meeting_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); Task task = new Task(); task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID))); task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN)))); // task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)))); task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))); prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN))); task.setProfessor(prof); return task; } /* GET ALL MEETINGS */ public Cursor getAllTasks() { try { open(); } catch (SQLException e) { e.printStackTrace(); } Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, null, null, null, null, null); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); Professor prof = new Professor(); //LOOP THROUGH ALL THE ROWS AND ADD TO CURSOR if (cursor.moveToFirst()) { do { Task task = new Task(); task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID))); try { task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN)))); //task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)))); } catch (ParseException e) { e.printStackTrace(); } task.setTitle(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN))); task.setNote(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN))); task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))); task.setLocation(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN))); prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN))); task.setProfessor(prof); } while (cursor.moveToNext()); } return cursor; } /* GET MEETINGS BY DAY */ public Cursor getTaskByDay(String day){ database = task_dbHelper.getWritableDatabase(); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); String where = DB_ABSTRACTS.DBTasks.DAY_COLUMN + " = ?"; Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, where, new String[] {day},null, null, DB_ABSTRACTS.DBTasks.TIME_COLUMN + " ASC"); if(cursor.moveToFirst()){ do{ Task task = new Task(); Professor prof = new Professor(); task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID))); try { task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN)))); //task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)))); } catch (ParseException e) { e.printStackTrace(); } task.setTitle(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN))); task.setNote(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN))); task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))); task.setLocation(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN))); prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN))); task.setProfessor(prof); }while(cursor.moveToNext()); } return cursor; } /* GET MEETINGS BY CURRENT DAY */ public Cursor getTaskByCurrentDate() { database = task_dbHelper.getWritableDatabase(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd HH:mm"); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); Professor prof = new Professor(); final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); //c.set(year, month, day, 00, 00, 00); Date actual_date = new Date(); try { actual_date = dateFormat.parse(year + " " + (month + 1) + " " + day + " 00" + ":" + "00" + ":" + "00"); //actual_date = dateFormat.format(c.getTime()); } catch (ParseException e) { e.printStackTrace(); } //PARSE THE ACTUAL DATE TO GET THE DAY, AND THEN USE IT IN THE WHERE CLAUSE TO GET DATA String where = DB_ABSTRACTS.DBTasks.DAY_COLUMN + " = '" + actual_date + "'"; Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, where, null, null, null, null); if (cursor.moveToFirst()) { do { Task task = new Task(); task.setId(cursor.getInt(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.KEY_ID))); task.setTitle(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TITLE_COLUMN))); task.setNote(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.NOTE_COLUMN))); task.setLocation(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN))); task.setDay(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN))); try { task.setTime(timeFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.TIME_COLUMN)))); //task.setDate(dateFormat.parse(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.DAY_COLUMN)))); } catch (ParseException e) { e.printStackTrace(); } prof.setProfessor_name(cursor.getString(cursor.getColumnIndex(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN))); task.setProfessor(prof); //Log.e("includes", " where string " + where); } while (cursor.moveToNext()); } return cursor; } /* UPDATE A MEETING */ public int updateTask(Task task) { try { this.open(); } catch (SQLException e) { e.printStackTrace(); } ContentValues values = new ContentValues(); values.put(DB_ABSTRACTS.DBTasks.TIME_COLUMN, task.getTime().toString()); //values.put(DB_ABSTRACTS.DBTasks.DAY_COLUMN, task.getDate().toString()); values.put(DB_ABSTRACTS.DBTasks.DAY_COLUMN, task.getDay()); values.put(DB_ABSTRACTS.DBTasks.TITLE_COLUMN, task.getTitle()); values.put(DB_ABSTRACTS.DBTasks.NOTE_COLUMN, task.getNote()); values.put(DB_ABSTRACTS.DBTasks.LOCATION_COLUMN, task.getLocation()); values.put(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN, task.getProfessor().getProfessor_name()); String where = DB_ABSTRACTS.DBTasks.KEY_ID + " = ?"; //update row return database.update(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, values, where, new String[]{String.valueOf(task.getId())}); } /* UPDATE TASK BY PROFESSOR */ public int updateByProfessor(Professor prof) { try { this.open(); } catch (SQLException e) { e.printStackTrace(); } ContentValues values = new ContentValues(); values.put(DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN, prof.getProfessor_name()); String where = DB_ABSTRACTS.DBTasks.PROFESSOR_COLUMN + " = ?"; return database.update(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, values, where, new String[]{String.valueOf(prof.getProfessor_name())}); } /* DELETE A MEETING */ public void deleteTask(long task_id) { try { open(); } catch (SQLException e) { e.printStackTrace(); } String where = DB_ABSTRACTS.DBTasks.KEY_ID + " = ?"; database.delete(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, where, new String[]{String.valueOf(task_id)}); } /* GET # OF ALL MEETINGS */ public int getTaskCount() { try { open(); } catch (SQLException e) { e.printStackTrace(); } Cursor cursor = database.query(DB_ABSTRACTS.DBTasks.DATABASE_TABLE, allColumns, null, null, null, null, null); int counter = cursor.getCount(); cursor.close(); return counter; } }