Search in sources :

Example 1 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class PushServiceStrategy method executePush.

protected void executePush() {
    IPushController pushController;
    try {
        pushController = new WSPushController();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        showInDialog(PreferencesState.getInstance().getContext().getString(R.string.webservice_url_error_title), String.format(PreferencesState.getInstance().getContext().getString(R.string.webservice_url_error), e.getMessage()));
        return;
    }
    IAsyncExecutor asyncExecutor = new AsyncExecutor();
    IMainExecutor mainExecutor = new UIThreadExecutor();
    ISurveyRepository surveyRepository = new SurveyLocalDataSource();
    IOrganisationUnitRepository orgUnitRepository = new OrganisationUnitRepository();
    SurveysThresholds surveysThresholds = new SurveysThresholds(BuildConfig.LimitSurveysCount, BuildConfig.LimitSurveysTimeHours);
    PushUseCase pushUseCase = new PushUseCase(pushController, asyncExecutor, mainExecutor, surveysThresholds, surveyRepository, orgUnitRepository);
    SurveyChecker.launchQuarantineChecker();
    pushUseCase.execute(new PushUseCase.Callback() {

        @Override
        public void onComplete() {
            Log.d(TAG, "PUSHUSECASE WITHOUT ERROR push complete");
            mPushService.onPushFinished();
        }

        @Override
        public void onPushInProgressError() {
            Log.d(TAG, "PUSHUSECASE ERROR Push stopped, There is already a push in progress");
        }

        @Override
        public void onPushError() {
            onError("PUSHUSECASE ERROR Unexpected error has occurred in push process");
        }

        @Override
        public void onSurveysNotFoundError() {
            onError("PUSHUSECASE ERROR Pending surveys not found");
        }

        @Override
        public void onConversionError() {
            showInDialog(PreferencesState.getInstance().getContext().getString(R.string.error_conflict_title), PreferencesState.getInstance().getContext().getString(R.string.ws_conversion_error));
        }

        @Override
        public void onNetworkError() {
            onError("PUSHUSECASE ERROR Network not available");
        }

        @Override
        public void onInformativeError(String message) {
            showInDialog(PreferencesState.getInstance().getContext().getString(R.string.error_conflict_title), "PUSHUSECASE ERROR " + message + PreferencesState.getInstance().isPushInProgress());
        }

        @Override
        public void onBannedOrgUnit() {
            showInDialog("", PreferencesState.getInstance().getContext().getString(R.string.exception_org_unit_banned));
        }

        @Override
        public void onReOpenOrgUnit() {
            showInDialog("", String.format(PreferencesState.getInstance().getContext().getString(R.string.dialog_reopen_org_unit), PreferencesState.getInstance().getOrgUnit()));
        }

        @Override
        public void onApiCallError() {
            onError("API call error");
        }

        @Override
        public void onApiCallError(ApiCallException e) {
            onError("PUSHUSECASE ERROR " + e.getMessage());
            e.printStackTrace();
        }

        @Override
        public void onClosedUser() {
            onError("PUSHUSECASE ERROR on closedUser " + PreferencesState.getInstance().isPushInProgress());
            closeUserLogout();
        }
    });
}
Also used : OrganisationUnitRepository(org.eyeseetea.malariacare.data.repositories.OrganisationUnitRepository) IOrganisationUnitRepository(org.eyeseetea.malariacare.domain.boundary.repositories.IOrganisationUnitRepository) PushUseCase(org.eyeseetea.malariacare.domain.usecase.push.PushUseCase) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) SurveyLocalDataSource(org.eyeseetea.malariacare.data.database.datasources.SurveyLocalDataSource) IAsyncExecutor(org.eyeseetea.malariacare.domain.boundary.executors.IAsyncExecutor) IMainExecutor(org.eyeseetea.malariacare.domain.boundary.executors.IMainExecutor) IPushController(org.eyeseetea.malariacare.domain.boundary.IPushController) UIThreadExecutor(org.eyeseetea.malariacare.presentation.executors.UIThreadExecutor) IOrganisationUnitRepository(org.eyeseetea.malariacare.domain.boundary.repositories.IOrganisationUnitRepository) WSPushController(org.eyeseetea.malariacare.data.sync.exporter.WSPushController) ISurveyRepository(org.eyeseetea.malariacare.domain.boundary.repositories.ISurveyRepository) SurveysThresholds(org.eyeseetea.malariacare.domain.usecase.push.SurveysThresholds) IAsyncExecutor(org.eyeseetea.malariacare.domain.boundary.executors.IAsyncExecutor) AsyncExecutor(org.eyeseetea.malariacare.presentation.executors.AsyncExecutor)

Example 2 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class ServerAPIController method patchDescriptionClosedDate.

static void patchDescriptionClosedDate(String url, String orgUnitUID, String orgUnitDescription) throws ApiCallException {
    // https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
    try {
        String urlPathClosedDescription = getPatchClosedDescriptionUrl(url, orgUnitUID);
        JSONObject data = prepareClosingDescriptionValue(orgUnitDescription);
        Response response = ServerApiCallExecution.executeCall(data, urlPathClosedDescription, "PATCH");
        ServerApiUtils.checkResponse(response, null);
    } catch (JSONException e) {
        throw new ApiCallException(e);
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Example 3 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class ServerAPIController method patchClosedDate.

/**
 * Updates the orgUnit adding a closedDate
 */
static void patchClosedDate(String url, String orgUnitUID) throws ApiCallException {
    // https://malariacare.psi.org/api/organisationUnits/u5jlxuod8xQ/closedDate
    try {
        String urlPathClosedDate = getPatchClosedDateUrl(url, orgUnitUID);
        JSONObject data = prepareTodayDateValue();
        Response response = ServerApiCallExecution.executeCall(data, urlPathClosedDate, "PATCH");
        ServerApiUtils.checkResponse(response, null);
    } catch (JSONException e) {
        throw new ApiCallException(e);
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Example 4 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class SurveyChecker method checkAllQuarantineSurveys.

/**
 * Download the related events. and checks all the quarantine surveys.
 * If a survey is in the server, the survey should be set as sent. Else, the survey should be
 * set as completed and it will be resend.
 */
public static void checkAllQuarantineSurveys() {
    List<Program> programs = Program.getAllPrograms();
    for (Program program : programs) {
        for (OrgUnit orgUnit : Survey.getQuarantineOrgUnits(program.getId_program())) {
            List<Survey> quarantineSurveys = Survey.getAllQuarantineSurveysByProgramAndOrgUnit(program, orgUnit);
            if (quarantineSurveys.size() == 0) {
                continue;
            }
            Date minDate = Survey.getMinQuarantineCompletionDateByProgramAndOrgUnit(program, orgUnit);
            Date maxDate = Survey.getMaxQuarantineEventDateByProgramAndOrgUnit(program, orgUnit);
            List<EventExtended> events;
            try {
                events = getEvents(program.getUid(), orgUnit.getUid(), minDate, maxDate);
            } catch (ApiCallException e) {
                e.printStackTrace();
                return;
            }
            if (events == null) {
                return;
            }
            for (Survey survey : quarantineSurveys) {
                if (events.size() > 0) {
                    updateQuarantineSurveysStatus(events, survey);
                } else {
                    changeSurveyStatusFromQuarantineTo(survey, Constants.SURVEY_COMPLETED);
                }
            }
        }
    }
}
Also used : OrgUnit(org.eyeseetea.malariacare.data.database.model.OrgUnit) Survey(org.eyeseetea.malariacare.data.database.model.Survey) Program(org.eyeseetea.malariacare.data.database.model.Program) EventExtended(org.eyeseetea.malariacare.data.sync.importer.models.EventExtended) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) Date(java.util.Date)

Example 5 with ApiCallException

use of org.eyeseetea.malariacare.domain.exception.ApiCallException in project pictureapp by EyeSeeTea.

the class SurveyChecker method getEvents.

/**
 * Get events filtered by program orgUnit and between dates.
 */
public static List<EventExtended> getEvents(String program, String orgUnit, Date minDate, Date maxDate) throws ApiCallException {
    Response response;
    String DHIS_URL = PreferencesState.getInstance().getDhisURL();
    String startDate = EventExtended.format(minDate, EventExtended.AMERICAN_DATE_FORMAT);
    String endDate = EventExtended.format(new Date(maxDate.getTime() + (8 * 24 * 60 * 60 * 1000)), EventExtended.AMERICAN_DATE_FORMAT);
    String url = SurveyCheckerStrategy.getApiEventUrl(DHIS_URL, program, orgUnit, startDate, endDate);
    Log.d(TAG, url);
    url = ServerApiUtils.encodeBlanks(url);
    response = ServerApiCallExecution.executeCall(null, url, "GET");
    JSONObject events = null;
    try {
        events = new JSONObject(ServerApiUtils.getReadableBodyResponse(response));
    } catch (JSONException ex) {
        throw new ApiCallException(ex);
    }
    JsonNode jsonNode = ServerApiUtils.getJsonNodeMappedResponse(events);
    return getEvents(jsonNode);
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Date(java.util.Date)

Aggregations

ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)18 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)10 Response (com.squareup.okhttp.Response)8 NetworkException (org.eyeseetea.malariacare.domain.exception.NetworkException)4 IOException (java.io.IOException)3 Date (java.util.Date)3 OrganisationUnit (org.eyeseetea.malariacare.domain.entity.OrganisationUnit)3 ConfigJsonIOException (org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException)3 JSONArray (org.json.JSONArray)3 OkHttpClient (com.squareup.okhttp.OkHttpClient)2 Request (com.squareup.okhttp.Request)2 RequestBody (com.squareup.okhttp.RequestBody)2 SurveyLocalDataSource (org.eyeseetea.malariacare.data.database.datasources.SurveyLocalDataSource)2 Program (org.eyeseetea.malariacare.data.database.model.Program)2 Survey (org.eyeseetea.malariacare.data.database.model.Survey)2 OrganisationUnitRepository (org.eyeseetea.malariacare.data.repositories.OrganisationUnitRepository)2 IAsyncExecutor (org.eyeseetea.malariacare.domain.boundary.executors.IAsyncExecutor)2 IMainExecutor (org.eyeseetea.malariacare.domain.boundary.executors.IMainExecutor)2 IOrganisationUnitRepository (org.eyeseetea.malariacare.domain.boundary.repositories.IOrganisationUnitRepository)2