Search in sources :

Example 6 with ApiCallException

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

the class PushController method push.

public void push(final IPushControllerCallback callback) {
    if (!ServerAPIController.isNetworkAvailable()) {
        Log.d(TAG, "No network");
        callback.onError(new NetworkException());
    } else {
        Log.d(TAG, "Network connected");
        List<Survey> surveys = Survey.getAllCompletedSurveysNoReceiptReset();
        Boolean isUserClosed = false;
        User loggedUser = User.getLoggedUser();
        if (loggedUser != null && loggedUser.getUid() != null) {
            try {
                isUserClosed = ServerAPIController.isUserClosed(User.getLoggedUser().getUid());
            } catch (ApiCallException e) {
                isUserClosed = null;
            }
        }
        if (isUserClosed == null) {
            callback.onError(new ApiCallException("The user api call returns a exception"));
            return;
        }
        if (isUserClosed) {
            Log.d(TAG, "The user is closed, Surveys not sent");
            callback.onError(new ClosedUserPushException());
        } else {
            if (surveys == null || surveys.size() == 0) {
                callback.onError(new SurveysToPushNotFoundException("Null surveys"));
                return;
            }
            Log.d(TAG, "wipe events");
            mPushDhisSDKDataSource.wipeEvents();
            try {
                Log.d(TAG, "convert surveys to sdk");
                convertToSDK(surveys);
            } catch (Exception ex) {
                callback.onError(new ConversionException(ex));
                return;
            }
            if (EventExtended.getAllEvents().size() == 0) {
                callback.onError(new ConvertedEventsToPushNotFoundException());
                return;
            } else {
                Log.d(TAG, "push data");
                pushData(callback);
            }
        }
    }
}
Also used : ConversionException(org.eyeseetea.malariacare.domain.exception.ConversionException) Survey(org.eyeseetea.malariacare.data.database.model.Survey) User(org.eyeseetea.malariacare.data.database.model.User) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) SurveysToPushNotFoundException(org.eyeseetea.malariacare.domain.exception.SurveysToPushNotFoundException) ClosedUserPushException(org.eyeseetea.malariacare.domain.exception.ClosedUserPushException) ConvertedEventsToPushNotFoundException(org.eyeseetea.malariacare.domain.exception.ConvertedEventsToPushNotFoundException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) ClosedUserPushException(org.eyeseetea.malariacare.domain.exception.ClosedUserPushException) ConvertedEventsToPushNotFoundException(org.eyeseetea.malariacare.domain.exception.ConvertedEventsToPushNotFoundException) PushReportException(org.eyeseetea.malariacare.domain.exception.push.PushReportException) SurveysToPushNotFoundException(org.eyeseetea.malariacare.domain.exception.SurveysToPushNotFoundException) ConversionException(org.eyeseetea.malariacare.domain.exception.ConversionException) PushDhisException(org.eyeseetea.malariacare.domain.exception.push.PushDhisException)

Example 7 with ApiCallException

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

the class PushClient method pushData.

/**
 * Pushes data to DHIS Server
 */
private JSONObject pushData(JSONObject data) throws ApiCallException {
    Response response = null;
    final String DHIS_URL = getDhisURL();
    OkHttpClient client = UnsafeOkHttpsClientFactory.getUnsafeOkHttpClient();
    // connect timeout
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    // socket timeout
    client.setReadTimeout(30, TimeUnit.SECONDS);
    // write timeout
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    // Cancel retry on failure
    client.setRetryOnConnectionFailure(false);
    BasicAuthenticator basicAuthenticator = new BasicAuthenticator();
    client.setAuthenticator(basicAuthenticator);
    RequestBody body = RequestBody.create(JSON, data.toString());
    Request request = new Request.Builder().header(basicAuthenticator.AUTHORIZATION_HEADER, basicAuthenticator.getCredentials()).url(DHIS_URL).post(body).build();
    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        throw new ApiCallException(e);
    }
    return ServerApiUtils.getApiResponseAsJSONObject(response);
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 8 with ApiCallException

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

the class PushClient method push.

public PushResult push() {
    try {
        JSONObject data = prepareMetadata();
        data = prepareDataElements(data);
        PushResult result = new PushResult(pushData(data));
        if (result.isSuccessful()) {
            updateSurveyState();
        }
        return result;
    } catch (EmptyLocationException ex) {
        new ApiCallException(ex);
        return new PushResult(ex);
    } catch (JSONException ex) {
        new ApiCallException(ex);
        return new PushResult(ex);
    } catch (ApiCallException ex) {
        return new PushResult(ex);
    }
}
Also used : JSONObject(org.json.JSONObject) EmptyLocationException(org.eyeseetea.malariacare.domain.exception.EmptyLocationException) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Example 9 with ApiCallException

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

the class ServerAPIController method getServerVersion.

/**
 * Returns the version of the given server.
 * Null if something went wrong
 */
public static String getServerVersion(String url) throws ApiCallException {
    String serverVersion = null;
    String urlServerInfo = url + DHIS_SERVER_INFO;
    Response response = ServerApiCallExecution.executeCall(null, urlServerInfo, "GET");
    JSONObject body = ServerApiUtils.getApiResponseAsJSONObject(response);
    if (body.has(TAG_VERSION)) {
        try {
            serverVersion = body.getString(TAG_VERSION);
        } catch (JSONException e) {
            new ApiCallException(e);
        }
    }
    if (serverVersion != null) {
        Log.i(TAG, String.format("getServerVersion(%s) -> %s", url, serverVersion));
    }
    return serverVersion;
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONException(org.json.JSONException)

Example 10 with ApiCallException

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

the class ServerAPIController method parseOrgUnit.

private static OrganisationUnit parseOrgUnit(JSONObject orgUnitJO) throws ApiCallException {
    if (orgUnitJO != null) {
        String uid = null;
        try {
            uid = orgUnitJO.getString(TAG_ID);
            String name = orgUnitJO.has(TAG_NAME) ? orgUnitJO.getString(TAG_NAME) : "";
            String code = orgUnitJO.has(CODE) ? orgUnitJO.getString(CODE) : "";
            String description = orgUnitJO.has(TAG_DESCRIPTIONCLOSEDATE) ? orgUnitJO.getString(TAG_DESCRIPTIONCLOSEDATE) : "";
            Date closedDate = orgUnitJO.has(TAG_CLOSEDDATE) ? Utils.parseStringToDate(orgUnitJO.getString(TAG_CLOSEDDATE)) : null;
            JSONArray attributeValues = orgUnitJO.has(ATTRIBUTE_VALUES) ? orgUnitJO.getJSONArray(ATTRIBUTE_VALUES) : null;
            String pin = "";
            for (int i = 0; attributeValues != null && i < attributeValues.length(); i++) {
                JSONObject attributeValue = attributeValues.getJSONObject(i);
                JSONObject attribute = attributeValue.has(ATTRIBUTE) ? attributeValue.getJSONObject(ATTRIBUTE) : null;
                String attributeCode = (attribute != null && attribute.has(CODE)) ? attribute.getString(CODE) : "";
                if (attributeCode.equals(OU_PIN)) {
                    pin = attributeValue.has(VALUE) ? attributeValue.getString(VALUE) : "";
                }
            }
            org.eyeseetea.malariacare.domain.entity.Program program = new org.eyeseetea.malariacare.domain.entity.Program();
            JSONArray ancestors = orgUnitJO.has(ANCESTORS) ? orgUnitJO.getJSONArray(ANCESTORS) : null;
            for (int i = 0; ancestors != null && i < ancestors.length(); i++) {
                if (ancestors.getJSONObject(i).has(LEVEL) && ancestors.getJSONObject(i).getInt(LEVEL) == ORG_UNIT_LEVEL) {
                    program.setId(ancestors.getJSONObject(i).has(TAG_ID) ? ancestors.getJSONObject(i).getString(TAG_ID) : "");
                    program.setCode(ancestors.getJSONObject(i).has(CODE) ? ancestors.getJSONObject(i).getString(CODE) : "");
                }
            }
            return new OrganisationUnit(uid, name, code, description, closedDate, pin, program);
        } catch (JSONException e) {
            throw new ApiCallException(e);
        }
    } else {
        return null;
    }
}
Also used : OrganisationUnit(org.eyeseetea.malariacare.domain.entity.OrganisationUnit) Program(org.eyeseetea.malariacare.data.database.model.Program) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Date(java.util.Date) JSONObject(org.json.JSONObject)

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