Android Open Source - nl.fhict.intellicloud.answers.android User 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  va  2  s . c  om
import java.io.IOException;
import java.util.ArrayList;

import nl.fhict.intellicloud.answers.QuestionState;
import nl.fhict.intellicloud.answers.UserType;
import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.QuestionsEntry;
import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.ReviewsEntry;
import nl.fhict.intellicloud.answers.backendcommunication.IntellicloudDbContract.UsersEntry;

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 UserSync {

  private Context context;
  private int idColumn;
  private int localIdColumn;
  //    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 UserSync(Context context, ContentProviderClient contentProviderClient)
  {
    this.context = context;
    this.contentProviderClient = contentProviderClient;
  }

  public void syncUsers(JSONArray userResultArray) throws AuthenticationException, ParseException, OperationCanceledException, AuthenticatorException, JSONException, IOException, RemoteException
  {
    ArrayList<JSONObject> usersToAddToDB = new ArrayList<JSONObject>();

    Uri uri = BackendContentProvider.CONTENT_USERS;
    Cursor usersCursor;
    
    usersCursor = contentProviderClient.query(uri, null, null, null, null);

    idColumn = usersCursor.getColumnIndex(UsersEntry.COLUMN_BACKEND_ID);
    localIdColumn = usersCursor.getColumnIndex(UsersEntry.COLUMN_ID);
    //        timestampColumn = questionsCursor.getColumnIndex(QuestionsEntry.COLUMN_TIMESTAMP);
    
    usersCursor.moveToFirst();
    for (int i = 0; i < userResultArray.length(); i++)
    {
      usersToAddToDB.add(userResultArray.getJSONObject(i));
    }
    
    while (!usersCursor.isAfterLast()) {
  
      JSONObject serverUser = null;
      boolean userFoundInResult = false;
      
      for (int i = 0; i < usersToAddToDB.size(); i++)
      {
        
        serverUser = usersToAddToDB.get(i);

        if (SyncHelper.getIdFromURI(serverUser.getString("Id")) == (usersCursor.getInt(idColumn)))
        {
          userFoundInResult = true;
          usersToAddToDB.remove(i);
          if (SyncHelper.isServerObjectNewer(serverUser, usersCursor))
          {
            updateUser(serverUser, usersCursor.getInt(localIdColumn));
          }
          break;

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

    usersCursor.close();


    for (int i = 0; i < usersToAddToDB.size(); i++)
    {
      addUserToDb(usersToAddToDB.get(i));
    }


    
  }
  private ContentValues getUserContentValues(JSONObject user)
  {
    ContentValues values = new ContentValues();
    
    values.put(UsersEntry.COLUMN_FIRSTNAME, user.optString("FirstName"));
    values.put(UsersEntry.COLUMN_LASTNAME, user.optString("LastName"));
    values.put(UsersEntry.COLUMN_INFIX, user.optString("Infix"));
    values.put(UsersEntry.COLUMN_TIMESTAMP, SyncHelper.getUnixMillisecondsFromJsonDate(user.optString("CreationTime")));
    values.put(UsersEntry.COLUMN_USERTYPE,UserType.Employee.toString());
    
    String backendid = user.optString("Id");
    if (backendid != null)
    {
      values.put(UsersEntry.COLUMN_BACKEND_ID, SyncHelper.getIdFromURI(backendid));
    }
    return values;
  }
  private void updateUser(JSONObject user, int rowId) throws JSONException, RemoteException
  {
    String updateUri = BackendContentProvider.CONTENT_USERS + "/" + rowId;
    ContentValues values = getUserContentValues(user);
    contentProviderClient.update(Uri.parse(updateUri), values, null, null);
  }
  private void addUserToDb(JSONObject user) throws JSONException, RemoteException
  {
    ContentValues values = getUserContentValues(user);
    contentProviderClient.insert(BackendContentProvider.CONTENT_USERS, values);
  }
  
}




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