Back to project page nl.fhict.intellicloud.answers.android.
The source code is released under:
Apache License
If you think the Android project nl.fhict.intellicloud.answers.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.
package nl.fhict.intellicloud.answers.backendcommunication; //w w w . j av a2 s. c o m import java.util.ArrayList; import java.util.Date; import nl.fhict.intellicloud.answers.Question; import nl.fhict.intellicloud.answers.QuestionState; import nl.fhict.intellicloud.answers.User; import nl.fhict.intellicloud.answers.UserType; import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.*; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; public class QuestionDataSource implements IQuestionService { private SQLiteDatabase database; private LocalStorageSQLiteHelper dbHelper; private Context context; IAnswerService answerService; private final String[] allColumns = { QuestionsEntry.COLUMN_ID, QuestionsEntry.COLUMN_BACKEND_ID, QuestionsEntry.COLUMN_ANSWERER_ID, QuestionsEntry.COLUMN_ASKER_ID, QuestionsEntry.COLUMN_DATE, QuestionsEntry.COLUMN_QUESTION, QuestionsEntry.COLUMN_QUESTIONSTATE, QuestionsEntry.COLUMN_IS_PRIVATE, QuestionsEntry.COLUMN_TITLE, QuestionsEntry.COLUMN_ANSWER_ID}; public QuestionDataSource(Context context) { dbHelper = new LocalStorageSQLiteHelper(context); this.context = context; } private void open() throws SQLException { database = dbHelper.getWritableDatabase(); } private void close() { dbHelper.close(); } @Override public Question GetQuestion(int id) { answerService = new AnswerDataSource(context); open(); Question question = null; Cursor cursor = database.query(QuestionsEntry.TABLE_NAME, allColumns, QuestionsEntry.COLUMN_BACKEND_ID + " == " + id, null, null, null, null); if (cursor.moveToFirst()) { question = getNextQuestionFromCursor(cursor); } cursor.close(); close(); return question; } @Override public ArrayList<Question> GetQuestions() { return GetQuestions(-1); } @Override public ArrayList<Question> GetQuestions(int employeeId) { answerService = new AnswerDataSource(context); String employeeFilter = null; if (employeeId >= 0) { employeeFilter = QuestionsEntry.COLUMN_ANSWERER_ID + " = " + employeeId; } ArrayList<Question> filteredQuestions = new ArrayList<Question>(); open(); Cursor cursor = database.query(QuestionsEntry.TABLE_NAME, allColumns, employeeFilter, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { filteredQuestions.add(getNextQuestionFromCursor(cursor)); cursor.moveToNext(); } cursor.close(); close(); return filteredQuestions; } private Question getNextQuestionFromCursor(Cursor cursor) { Long unixMilliSeconds = cursor.getLong(4); Question question = new Question(cursor.getInt(1), cursor.getString(5), UserDataSource.GetUser(cursor.getInt(3), database), UserDataSource.GetUser(cursor.getInt(2), database), QuestionState.valueOf(cursor.getString(6)), new Date(unixMilliSeconds)); question.setIsPrivate(cursor.getInt(7) > 0); question.setTitle(cursor.getString(8)); int answerId = cursor.getInt(9); question.setAnswer(answerService.GetAnswer(answerId)); return question; } @Override public void UpdateQuestion(Question question) { ContentValues values = new ContentValues(); values.put(QuestionsEntry.COLUMN_QUESTIONSTATE, question.getQuestionState().toString()); if (question.getAnswer() != null) { values.put(QuestionsEntry.COLUMN_ANSWER_ID, question.getAnswer().getId()); } open(); database.update(QuestionsEntry.TABLE_NAME, values, QuestionsEntry.COLUMN_BACKEND_ID + " = " + question.getId(), null); close(); } @Override public Question GetQuestionUsingAnswer(int answerId) { open(); Question question = null; Cursor cursor = database.query(QuestionsEntry.TABLE_NAME, allColumns, QuestionsEntry.COLUMN_ANSWER_ID + " = " + answerId, null, null, null, null); if (cursor.moveToFirst()) { question = getNextQuestionFromCursor(cursor); } cursor.close(); close(); return question; } }