public void asyncResponse()

in Android/app/src/main/java/com/harvard/studyappmodule/StandaloneActivity.java [179:439]


  public <T> void asyncResponse(T response, int responseCode) {
    if (responseCode == SPECIFIC_STUDY) {
      if (response != null) {
        study = (Study) response;
        for (int i = 0; i < study.getStudies().size(); i++) {
          if (study.getStudies().get(i).getStudyId().equalsIgnoreCase(AppConfig.StudyId)) {
            studyListArrayList.add(study.getStudies().get(i));
            study.setStudies(studyListArrayList);
          }
        }
        AppController.getHelperProgressDialog().dismissDialog();
        if (!studyListArrayList.isEmpty()) {
          if (studyListArrayList.get(0).getStatus().equalsIgnoreCase(ACTIVE)) {
            AppController.getHelperProgressDialog()
                .showProgress(StandaloneActivity.this, "", "", false);

            dbServiceSubscriber.saveStudyListToDB(this, study);

            HashMap<String, String> header = new HashMap();
            header.put(
                "Authorization",
                "Bearer "
                    + AppController.getHelperSharedPreference()
                        .readPreference(
                            StandaloneActivity.this, getResources().getString(R.string.auth), ""));
            header.put(
                "userId",
                AppController.getHelperSharedPreference()
                    .readPreference(
                        StandaloneActivity.this, getResources().getString(R.string.userid), ""));
            header.put("deviceType", android.os.Build.MODEL);
            header.put("deviceOS", Build.VERSION.RELEASE);
            header.put("mobilePlatform", "ANDROID");

            ParticipantEnrollmentDatastoreConfigEvent participantEnrollmentDatastoreConfigEvent =
                new ParticipantEnrollmentDatastoreConfigEvent(
                    "get",
                    Urls.STUDY_STATE,
                    GET_PREFERENCES,
                    StandaloneActivity.this,
                    StudyData.class,
                    null,
                    header,
                    null,
                    false,
                    this);
            GetPreferenceEvent getPreferenceEvent = new GetPreferenceEvent();
            getPreferenceEvent.setParticipantEnrollmentDatastoreConfigEvent(
                participantEnrollmentDatastoreConfigEvent);
            UserModulePresenter userModulePresenter = new UserModulePresenter();
            userModulePresenter.performGetUserPreference(getPreferenceEvent);
          } else {
            Toast.makeText(
                    this,
                    "This study is " + studyListArrayList.get(0).getStatus(),
                    Toast.LENGTH_SHORT)
                .show();
            finish();
          }
        } else {
          Toast.makeText(this, "Study not found", Toast.LENGTH_SHORT).show();
          finish();
        }
      } else {
        AppController.getHelperProgressDialog().dismissDialog();
        Toast.makeText(StandaloneActivity.this, R.string.error_retriving_data, Toast.LENGTH_SHORT)
            .show();
        finish();
      }
    } else if (responseCode == GET_PREFERENCES) {
      StudyData studies = (StudyData) response;
      boolean userAlreadyJoined = false;
      studiesArrayList = studies.getStudies();
      if (studies != null) {
        studies.setUserId(
            AppController.getHelperSharedPreference()
                .readPreference(StandaloneActivity.this, getString(R.string.userid), ""));
        dbServiceSubscriber.saveStudyPreferencesToDB(this, studies);

        AppController.getHelperSharedPreference()
            .writePreference(
                StandaloneActivity.this,
                getString(R.string.title),
                "" + studyListArrayList.get(0).getTitle());
        AppController.getHelperSharedPreference()
            .writePreference(
                StandaloneActivity.this,
                getString(R.string.status),
                "" + studyListArrayList.get(0).getStatus());
        if (!studies.getStudies().isEmpty()) {
          AppController.getHelperSharedPreference()
              .writePreference(
                  StandaloneActivity.this,
                  getString(R.string.studyStatus),
                  "" + studies.getStudies().get(0).getStatus());
        } else {
          AppController.getHelperSharedPreference()
              .writePreference(
                  StandaloneActivity.this, getString(R.string.studyStatus), YET_TO_JOIN);
        }
        AppController.getHelperSharedPreference()
            .writePreference(StandaloneActivity.this, getString(R.string.position), "" + 0);
        AppController.getHelperSharedPreference()
            .writePreference(
                StandaloneActivity.this,
                getString(R.string.enroll),
                "" + studyListArrayList.get(0).getSetting().isEnrolling());
        AppController.getHelperSharedPreference()
            .writePreference(
                StandaloneActivity.this,
                getString(R.string.studyVersion),
                "" + studyListArrayList.get(0).getStudyVersion());

        RealmList<Studies> userPreferenceStudies = studies.getStudies();
        if (userPreferenceStudies != null) {
          for (int i = 0; i < userPreferenceStudies.size(); i++) {
            for (int j = 0; j < studyListArrayList.size(); j++) {
              if (userPreferenceStudies
                  .get(i)
                  .getStudyId()
                  .equalsIgnoreCase(studyListArrayList.get(j).getStudyId())) {
                studyListArrayList.get(j).setStudyStatus(userPreferenceStudies.get(i).getStatus());
                userAlreadyJoined = true;

                if (studyListArrayList.get(j).getStudyStatus().equalsIgnoreCase(IN_PROGRESS)) {
                  getStudyUpdate(
                      studyListArrayList.get(j).getStudyId(),
                      studyListArrayList.get(j).getStudyVersion(),
                      studyListArrayList.get(j).getTitle(),
                      "",
                      "",
                      "",
                      "");
                } else {
                  Intent intent =
                      new Intent(StandaloneActivity.this, StandaloneStudyInfoActivity.class);
                  intent.putExtra("flow", getIntent().getStringExtra("flow"));
                  intent.putExtra("studyId", studyListArrayList.get(j).getStudyId());
                  intent.putExtra("title", studyListArrayList.get(j).getTitle());
                  intent.putExtra("status", studyListArrayList.get(j).getStatus());
                  intent.putExtra("studyStatus", studyListArrayList.get(j).getStudyStatus());
                  intent.putExtra("position", "0");
                  intent.putExtra(
                      "enroll", "" + studyListArrayList.get(j).getSetting().isEnrolling());
                  startActivity(intent);
                  finish();
                }
                break;
              }
            }
          }
          if (!userAlreadyJoined && !studyListArrayList.isEmpty()) {

            studyListArrayList.get(0).setStudyStatus(YET_TO_JOIN);

            Intent intent = new Intent(StandaloneActivity.this, StandaloneStudyInfoActivity.class);
            intent.putExtra("flow", getIntent().getStringExtra("flow"));
            intent.putExtra("studyId", studyListArrayList.get(0).getStudyId());
            intent.putExtra("title", studyListArrayList.get(0).getTitle());
            intent.putExtra("status", studyListArrayList.get(0).getStatus());
            intent.putExtra("studyStatus", studyListArrayList.get(0).getStudyStatus());
            intent.putExtra("position", "0");
            intent.putExtra("enroll", "" + studyListArrayList.get(0).getSetting().isEnrolling());
            startActivity(intent);
            finish();
          }
        } else {
          Toast.makeText(StandaloneActivity.this, R.string.error_retriving_data, Toast.LENGTH_SHORT)
              .show();
          finish();
        }
        setStudyList(false);
        checkForNotification(getIntent());
        AppController.getHelperProgressDialog().dismissDialog();
      } else {
        AppController.getHelperProgressDialog().dismissDialog();
        Toast.makeText(StandaloneActivity.this, R.string.error_retriving_data, Toast.LENGTH_SHORT)
            .show();
        finish();
      }
    } else if (responseCode == STUDY_UPDATES) {
      StudyUpdate studyUpdate = (StudyUpdate) response;
      studyUpdate.setStudyId(studyId);
      StudyUpdateListdata studyUpdateListdata = new StudyUpdateListdata();
      RealmList<StudyUpdate> studyUpdates = new RealmList<>();
      studyUpdates.add(studyUpdate);
      studyUpdateListdata.setStudyUpdates(studyUpdates);
      dbServiceSubscriber.saveStudyUpdateListdataToDB(this, studyUpdateListdata);

      if (studyUpdate.getStudyUpdateData().isResources()) {
        dbServiceSubscriber.deleteResourcesFromDb(this, studyId);
      }
      if (studyUpdate.getStudyUpdateData().isInfo()) {
        dbServiceSubscriber.deleteStudyInfoFromDb(this, studyId);
      }
      if (studyUpdate.getStudyUpdateData().isConsent() && studyUpdate.isEnrollAgain()) {
        callConsentMetaDataWebservice();
      } else {
        AppController.getHelperProgressDialog().dismissDialog();
        Intent intent = new Intent(StandaloneActivity.this, SurveyActivity.class);
        addClearTopFlag(intent);
        intent.putExtra("studyId", studyId);
        intent.putExtra("to", calledFor);
        intent.putExtra("from", from);
        intent.putExtra("activityId", activityId);
        intent.putExtra("localNotification", localNotification);
        startActivity(intent);
        finish();
      }
    } else if (responseCode == GET_CONSENT_DOC) {
      ConsentDocumentData consentDocumentData = (ConsentDocumentData) response;
      consentDocumentData.setStudyId(studyId);
      dbServiceSubscriber.saveConsentDocumentToDB(this, consentDocumentData);
      latestConsentVersion = consentDocumentData.getConsent().getVersion();
      enrollAgain = consentDocumentData.isEnrollAgain();
      callGetConsentPdfWebservice();

    } else if (responseCode == CONSENTPDF) {
      String version = "";
      ConsentPDF consentPdfData = (ConsentPDF) response;
      StudyData studyData = dbServiceSubscriber.getStudyPreferencesListFromDB(realm);
      for (int i = 0; i < studyData.getStudies().size(); i++) {
        if (studyData.getStudies().get(i).getStudyId().equalsIgnoreCase(studyId)) {
          version = studyData.getStudies().get(i).getUserStudyVersion();
        }
      }
      if (version != null && (!latestConsentVersion.equalsIgnoreCase(version))) {
        callConsentMetaDataWebservice();
      } else if (enrollAgain
          && latestConsentVersion != null
          && consentPdfData != null
          && consentPdfData.getConsent() != null
          && consentPdfData.getConsent().getVersion() != null) {
        if (!consentPdfData.getConsent().getVersion().equalsIgnoreCase(latestConsentVersion)) {
          callConsentMetaDataWebservice();
        } else {
          AppController.getHelperProgressDialog().dismissDialog();
          Intent intent = new Intent(StandaloneActivity.this, SurveyActivity.class);
          addClearTopFlag(intent);
          intent.putExtra("studyId", studyId);
          intent.putExtra("to", calledFor);
          intent.putExtra("from", from);
          intent.putExtra("activityId", activityId);
          intent.putExtra("localNotification", localNotification);
          startActivity(intent);
          finish();
        }
      } else {
        AppController.getHelperProgressDialog().dismissDialog();
        Intent intent = new Intent(StandaloneActivity.this, SurveyActivity.class);
        addClearTopFlag(intent);
        intent.putExtra("studyId", studyId);
        intent.putExtra("to", calledFor);
        intent.putExtra("from", from);
        intent.putExtra("activityId", activityId);
        intent.putExtra("localNotification", localNotification);
        startActivity(intent);
        finish();
      }
    }
  }