Android Open Source - nl.fhict.intellicloud.answers.android Questions Sync






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;
/*from   ww  w  . j a  v  a 2 s .  c om*/
import java.io.IOException;
import java.util.ArrayList;

import nl.fhict.intellicloud.answers.QuestionState;
import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.AnswersEntry;
import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.QuestionsEntry;

import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthenticationException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.ContentProviderClient;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.ParseException;
import android.net.Uri;
import android.os.RemoteException;
import android.util.Log;

public class QuestionsSync {
  private Context context;
  private int idColumn;
  //  private int timestampColumn;
  //  private int questionColumn;
  //  private int dateColumn;
  //  private int titleColumn;
  //  private int answerIdColumn;
  //  private int answererIdColumn;
  //  private int isPrivateColumn;
  //  private int askerIdColumn;
  //  private int questionStateColumn;
  private ContentProviderClient contentProviderClient;

  public QuestionsSync(Context context, ContentProviderClient contentProviderClient)
  {
    this.context = context;
    this.contentProviderClient = contentProviderClient;
  }

  public void syncQuestions(JSONArray questionResultArray) throws AuthenticationException, ParseException, OperationCanceledException, AuthenticatorException, JSONException, IOException, RemoteException
  {

    //JSONArray questionsToUpload = new JSONArray();
    ArrayList<JSONObject> questionsToAddToDB = new ArrayList<JSONObject>();
    //Log.d("sync", questionResultArray.toString());
    Uri uri = BackendContentProvider.CONTENT_QUESTIONS;
    
    Cursor questionsCursor;
    
    questionsCursor = contentProviderClient.query(uri, null, null, null, null);
    Log.d("sync", "uri");
    idColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_BACKEND_ID);
    int localIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_BACKEND_ID);
    
    //      questionColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_QUESTION);
    //      dateColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_DATE);
    //      titleColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_TITLE);
    //      answerIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_ANSWER_ID);
    //      answererIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_ANSWERER_ID);
    //      isPrivateColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_IS_PRIVATE);
    //      askerIdColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_ASKER_ID);
    //      questionStateColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_QUESTIONSTATE);

    questionsCursor.moveToFirst();
    
    for (int i = 0; i < questionResultArray.length(); i++)
    {
      questionsToAddToDB.add(questionResultArray.getJSONObject(i));
    }
    
    while (!questionsCursor.isAfterLast()) {
      
      JSONObject serverQuestion = null;
      boolean questionFoundInResult = false;

      for (int i = 0; i < questionsToAddToDB.size(); i++)
      {
        
        serverQuestion = questionsToAddToDB.get(i);
        
        if (SyncHelper.getIdFromURI(serverQuestion.getString("Id")) == (questionsCursor.getInt(idColumn)))
        {
          questionFoundInResult = true;
          questionsToAddToDB.remove(i);
          
          
          if (SyncHelper.isServerObjectNewer(serverQuestion, questionsCursor))
          {
            updateQuestion(serverQuestion, questionsCursor.getInt(localIdColumn));
          }
          break;

        }
      }
      if (!questionFoundInResult)
      {
        
        String deleteUri = uri + "/" + questionsCursor.getInt(localIdColumn);
        questionsCursor.moveToNext();
        contentProviderClient.delete(Uri.parse(deleteUri), null, null);
        
      }
      questionsCursor.moveToNext();
    }

    questionsCursor.close();


    for (int i = 0; i < questionsToAddToDB.size(); i++)
    {
      addQuestionToDb(questionsToAddToDB.get(i));
    }



  }
  private ContentValues getQuestionContentValues(JSONObject question) throws RemoteException, AuthenticationException, ParseException, OperationCanceledException, AuthenticatorException, JSONException, IOException
  {
    Log.d("QuestionSync", question.toString());
    ContentValues values = new ContentValues();
    values.put(QuestionsEntry.COLUMN_TIMESTAMP, SyncHelper.getUnixMillisecondsFromJsonDate(question.optString("LastChangedTime")));
    values.put(QuestionsEntry.COLUMN_QUESTION, question.optString("Content"));
    values.put(QuestionsEntry.COLUMN_DATE, SyncHelper.getUnixMillisecondsFromJsonDate(question.optString("CreationTime")));
    
    values.put(QuestionsEntry.COLUMN_TITLE, question.optString("Title"));
    values.put(QuestionsEntry.COLUMN_IS_PRIVATE, question.optString("IsPrivate"));
    values.put(QuestionsEntry.COLUMN_QUESTIONSTATE, getQuestionState(question.optInt("QuestionState")).toString());
    
    String backendid = question.optString("Id");
    if (backendid != null)
    {
      values.put(QuestionsEntry.COLUMN_BACKEND_ID, SyncHelper.getIdFromURI(backendid));
    }
    
    String answer = question.optString("AnswerId");
    if (answer != null && !answer.equals("null") && answer.length() > 0)
    {
      int answerId = Integer.parseInt(answer);
      values.put(QuestionsEntry.COLUMN_ANSWER_ID, answerId);
      addQuestionIdToAnswer(answerId, SyncHelper.getIdFromURI(backendid));
    }
    String answerer = question.optString("Answerer");
    //NOTE: This returns a badrequest.
//    if (answerer != null && !answerer.equals("null") && answerer.length() > 0) 
//    {
//      values.put(QuestionsEntry.COLUMN_ANSWERER_ID, SyncHelper.getRealIdForObjectURI(answerer,context));
//    }
    String asker = question.optString("User");
    if (asker != null && !asker.equals("null") && asker.length() > 0)
    {
      values.put(QuestionsEntry.COLUMN_ASKER_ID, SyncHelper.getRealIdForObjectURI(asker,context));
    }
    
    return values;
  }
  private void updateQuestion(JSONObject question, int rowId) throws JSONException, RemoteException, AuthenticationException, ParseException, OperationCanceledException, AuthenticatorException, IOException
  {
    String updateUri = BackendContentProvider.CONTENT_QUESTIONS + "/" + rowId;
    ContentValues values = getQuestionContentValues(question);
    contentProviderClient.update(Uri.parse(updateUri), values, null, null);
  }
  private void addQuestionToDb(JSONObject question) throws JSONException, RemoteException, AuthenticationException, ParseException, OperationCanceledException, AuthenticatorException, IOException
  {
    ContentValues values = getQuestionContentValues(question);
    contentProviderClient.insert(BackendContentProvider.CONTENT_QUESTIONS, values);
  }
  private void addQuestionIdToAnswer(int answerId, int questionId) throws RemoteException
  {
    ContentValues values = new ContentValues();
    values.put(AnswersEntry.COLUMN_QUESTION_ID, questionId);
    contentProviderClient.update(BackendContentProvider.CONTENT_ANSWERS, values, AnswersEntry.COLUMN_BACKEND_ID + " = " + answerId, null);
  }
  
  
  private QuestionState getQuestionState(int serverState)
  {
    QuestionState foundState = QuestionState.Open;
    switch (serverState) {
    case 0:
      foundState = QuestionState.Open;
      break;
    case 1:
      foundState = QuestionState.UpForAnswer;
      break;
    case 2:
      foundState = QuestionState.UpForFeedback;
      break;
    case 3:
      foundState = QuestionState.Closed;
      break;
    default:
      break;
    }
    return foundState;
  }
  
}




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