Android Open Source - nl.fhict.intellicloud.answers.android Answer Data Source






From Project

Back to project page nl.fhict.intellicloud.answers.android.

License

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.

Java Source Code

package nl.fhict.intellicloud.answers.backendcommunication;
//  w ww  .j  av a 2  s.c  om
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import nl.fhict.intellicloud.answers.Answer;
import nl.fhict.intellicloud.answers.AnswerState;
import nl.fhict.intellicloud.answers.Question;
import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.*;


public class AnswerDataSource implements IAnswerService {
// Database fields
    private SQLiteDatabase database;
    private LocalStorageSQLiteHelper dbHelper;
    private String[] allColumns = { AnswersEntry.COLUMN_ID, 
                    AnswersEntry.COLUMN_BACKEND_ID,
                    AnswersEntry.COLUMN_ANSWER, 
                    AnswersEntry.COLUMN_ANSWERER_ID,
                    AnswersEntry.COLUMN_ANSWERSTATE,
                    AnswersEntry.COLUMN_DATE,
                    AnswersEntry.COLUMN_QUESTION_ID};
    
    
    
    
    public AnswerDataSource(Context context) {
      dbHelper = new LocalStorageSQLiteHelper(context);
      //
    }
    
    private void open() throws SQLException {
      database = dbHelper.getWritableDatabase();
    }
    
    private void close() {
      dbHelper.close();
    }

    @Override
    public void CreateAnswer(Answer answer, int questionId) {
      ContentValues values = new ContentValues();
      Date currentDate = new Date();
      values.put(AnswersEntry.COLUMN_ANSWER, answer.getAnswer());
      values.put(AnswersEntry.COLUMN_ANSWERSTATE, answer.getAnwserState().toString());
      if (answer.getAnswerer() != null)
      {
        values.put(AnswersEntry.COLUMN_ANSWERER_ID, answer.getAnswerer().getId());
      }
      values.put(AnswersEntry.COLUMN_DATE, currentDate.getTime());
      values.put(AnswersEntry.COLUMN_QUESTION_ID, questionId);
      
      open();
      database.insert(AnswersEntry.TABLE_NAME, null,
          values);
      
      close();

    }

    @Override
    public Answer GetAnswer(int id) {
      open();
      Answer answer = null;
      Cursor cursor = database.query(AnswersEntry.TABLE_NAME, allColumns, AnswersEntry.COLUMN_BACKEND_ID + " = " + id, null, null, null, null);
      if (cursor.moveToFirst())
      {
        
        answer = getNextAnswerFromCursor(cursor);
        
      }
      cursor.close();
      close();
      return answer;
    }
    
    @Override
    public Answer GetAnswerUsingQuestion(int questionId) {
      open();
      Answer answer = null;
      Boolean answerFound = false;
      Cursor cursor = database.query(AnswersEntry.TABLE_NAME, allColumns, AnswersEntry.COLUMN_QUESTION_ID + " = " + questionId, null, null, null, null);
      cursor.moveToFirst();
      while (!cursor.isAfterLast() && !answerFound)
      {
        if (!cursor.isNull(1))
        {
          answer = getNextAnswerFromCursor(cursor);
          answerFound = true;
        }
        else
        {
          cursor.moveToNext();
        }
        
        
      }
      if (!answerFound)
      {
        cursor.moveToFirst();
        answer = getNextAnswerFromCursor(cursor);
      }
      cursor.close();
      close();
      return answer;
      
    }
    @Override
    public ArrayList<Answer> GetAnswers() {
      return GetAnswers(-1, null);
    }

    @Override
    public ArrayList<Answer> GetAnswers(int employeeId, AnswerState answerState) {
    
      String answerFilter = null;
    
      if (answerState != null)
      {
        answerFilter += AnswersEntry.COLUMN_ANSWERSTATE + " = " + answerState.toString();
      }
      if (employeeId >= 0)
      {
        if (answerFilter.length() > 0)
        {
          answerFilter += " AND ";
        }
        answerFilter += AnswersEntry.COLUMN_ANSWERER_ID + " = " + employeeId;
      }
      ArrayList<Answer> filteredAnswers = new ArrayList<Answer>();
      
      open();
      Cursor cursor = database.query(AnswersEntry.TABLE_NAME, allColumns, answerFilter, null, null, null, null);
      cursor.moveToFirst();
      
      while (!cursor.isAfterLast()) {
        filteredAnswers.add(getNextAnswerFromCursor(cursor));    
        cursor.moveToNext();
      }
      cursor.close();
      close();
      return filteredAnswers;
      
      
    }

    @Override
    public void UpdateAnswer(Answer answer) {
      
      ContentValues values = new ContentValues();
      values.put(AnswersEntry.COLUMN_ANSWERSTATE, answer.getAnwserState().toString());
      values.put(AnswersEntry.COLUMN_ANSWERER_ID, answer.getAnswerer().getId());
      values.put(AnswersEntry.COLUMN_ANSWER, answer.getAnswer());
      
      open();
      database.update(AnswersEntry.TABLE_NAME, values, AnswersEntry.COLUMN_BACKEND_ID + " = " + answer.getId(), null);
      close();
      
    }
    private Answer getNextAnswerFromCursor(Cursor cursor)
    {
      //Question questionForAnswer = questionDataSource.GetQuestion(cursor.getInt(2));
      
      Answer foundAnswer = new Answer(cursor.getString(2), 
                      UserDataSource.GetUser(cursor.getInt(3), database), 
                      AnswerState.valueOf(cursor.getString(4)));
      foundAnswer.setId(cursor.getInt(1));
      
      
      return foundAnswer;
    }

}




Java Source Code List

nl.fhict.intellicloud.answers.AddReviewActivity.java
nl.fhict.intellicloud.answers.AnswerState.java
nl.fhict.intellicloud.answers.Answer.java
nl.fhict.intellicloud.answers.AuthorizationActivity.java
nl.fhict.intellicloud.answers.FeedbackState.java
nl.fhict.intellicloud.answers.FeedbackType.java
nl.fhict.intellicloud.answers.Feedback.java
nl.fhict.intellicloud.answers.FilterList.java
nl.fhict.intellicloud.answers.GetUserIdTask.java
nl.fhict.intellicloud.answers.IUserFoundObserver.java
nl.fhict.intellicloud.answers.IncomingQuestionsListAdapter.java
nl.fhict.intellicloud.answers.ListFragment.java
nl.fhict.intellicloud.answers.MainActivity.java
nl.fhict.intellicloud.answers.OriginalAnswerActivity.java
nl.fhict.intellicloud.answers.QuestionListOnClickListener.java
nl.fhict.intellicloud.answers.QuestionState.java
nl.fhict.intellicloud.answers.Question.java
nl.fhict.intellicloud.answers.RejectedAnswerActivity.java
nl.fhict.intellicloud.answers.ReviewListAdapter.java
nl.fhict.intellicloud.answers.ReviewOverviewActivity.java
nl.fhict.intellicloud.answers.ReviewState.java
nl.fhict.intellicloud.answers.Review.java
nl.fhict.intellicloud.answers.SendAnswerActivity.java
nl.fhict.intellicloud.answers.SlidingMenuListAdapter.java
nl.fhict.intellicloud.answers.UserType.java
nl.fhict.intellicloud.answers.User.java
nl.fhict.intellicloud.answers.backendcommunication.AnswerDataSource.java
nl.fhict.intellicloud.answers.backendcommunication.AnswerSync.java
nl.fhict.intellicloud.answers.backendcommunication.AuthenticatorService.java
nl.fhict.intellicloud.answers.backendcommunication.Authenticator.java
nl.fhict.intellicloud.answers.backendcommunication.BackendContentProvider.java
nl.fhict.intellicloud.answers.backendcommunication.BackendSyncAdapter.java
nl.fhict.intellicloud.answers.backendcommunication.BackendSyncService.java
nl.fhict.intellicloud.answers.backendcommunication.DummyBackend.java
nl.fhict.intellicloud.answers.backendcommunication.IAnswerService.java
nl.fhict.intellicloud.answers.backendcommunication.IClaimService.java
nl.fhict.intellicloud.answers.backendcommunication.IQuestionService.java
nl.fhict.intellicloud.answers.backendcommunication.IReviewService.java
nl.fhict.intellicloud.answers.backendcommunication.IUserService.java
nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.java
nl.fhict.intellicloud.answers.backendcommunication.LocalStorageSQLiteHelper.java
nl.fhict.intellicloud.answers.backendcommunication.QuestionDataSource.java
nl.fhict.intellicloud.answers.backendcommunication.QuestionsSync.java
nl.fhict.intellicloud.answers.backendcommunication.ReviewDataSource.java
nl.fhict.intellicloud.answers.backendcommunication.ReviewSync.java
nl.fhict.intellicloud.answers.backendcommunication.ServerAccessor.java
nl.fhict.intellicloud.answers.backendcommunication.SyncHelper.java
nl.fhict.intellicloud.answers.backendcommunication.UserDataSource.java
nl.fhict.intellicloud.answers.backendcommunication.UserSync.java
nl.fhict.intellicloud.answers.backendcommunication.oauth.AuthenticationManager.java