Search in sources :

Example 1 with OrganisationUnit

use of org.eyeseetea.malariacare.domain.entity.OrganisationUnit 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)

Example 2 with OrganisationUnit

use of org.eyeseetea.malariacare.domain.entity.OrganisationUnit in project pictureapp by EyeSeeTea.

the class APushUseCaseStrategy method banOrgUnit.

private void banOrgUnit() {
    OrganisationUnit organisationUnit = null;
    try {
        organisationUnit = mOrganisationUnitRepository.getCurrentOrganisationUnit(ReadPolicy.CACHE);
    } catch (NetworkException e) {
        e.printStackTrace();
    } catch (ApiCallException e) {
        e.printStackTrace();
    }
    if (organisationUnit != null) {
        organisationUnit.ban();
        mOrganisationUnitRepository.saveOrganisationUnit(organisationUnit);
        System.out.println("OrgUnit banned successfully");
    }
}
Also used : OrganisationUnit(org.eyeseetea.malariacare.domain.entity.OrganisationUnit) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException)

Example 3 with OrganisationUnit

use of org.eyeseetea.malariacare.domain.entity.OrganisationUnit in project pictureapp by EyeSeeTea.

the class OrganisationUnitRepository method getCurrentOrganisationUnit.

@Override
public OrganisationUnit getCurrentOrganisationUnit(ReadPolicy readPolicy) throws NetworkException, ApiCallException {
    OrganisationUnit organisationUnit;
    if (readPolicy == ReadPolicy.REMOTE) {
        organisationUnit = getFromRemote();
        verifyBanChanged(organisationUnit);
        OrgUnit.refresh(organisationUnit);
    } else {
        organisationUnit = OrgUnit.getByName(PreferencesState.getInstance().getOrgUnit());
    }
    return organisationUnit;
}
Also used : OrganisationUnit(org.eyeseetea.malariacare.domain.entity.OrganisationUnit)

Example 4 with OrganisationUnit

use of org.eyeseetea.malariacare.domain.entity.OrganisationUnit in project pictureapp by EyeSeeTea.

the class LoginUseCase method pullOrganisationCredentials.

private void pullOrganisationCredentials() {
    Credentials orgUnitCredentials = null;
    try {
        OrganisationUnit orgUnit = mOrgUnitDataSource.getUserOrgUnit(insertedCredentials);
        if (orgUnit == null) {
            notifyInvalidCredentials();
            return;
        }
        orgUnitCredentials = new Credentials(insertedCredentials.getServerURL(), orgUnit.getCode(), orgUnit.getPin());
    } catch (ApiCallException e) {
        if (e.getCause() instanceof IOException) {
            notifyUnexpectedError();
        } else {
            e.printStackTrace();
            notifyConfigJsonNotPresent();
        }
    } catch (NetworkException e) {
        e.printStackTrace();
        checkUserCredentialsWithOrgUnit(mCredentialsLocalDataSource.getOrganisationCredentials(), true);
    }
    mCredentialsLocalDataSource.saveOrganisationCredentials(orgUnitCredentials);
    checkUserCredentialsWithOrgUnit(orgUnitCredentials, false);
}
Also used : OrganisationUnit(org.eyeseetea.malariacare.domain.entity.OrganisationUnit) ApiCallException(org.eyeseetea.malariacare.domain.exception.ApiCallException) IOException(java.io.IOException) ConfigJsonIOException(org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException) NetworkException(org.eyeseetea.malariacare.domain.exception.NetworkException) Credentials(org.eyeseetea.malariacare.domain.entity.Credentials)

Aggregations

OrganisationUnit (org.eyeseetea.malariacare.domain.entity.OrganisationUnit)4 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)3 NetworkException (org.eyeseetea.malariacare.domain.exception.NetworkException)2 IOException (java.io.IOException)1 Date (java.util.Date)1 Program (org.eyeseetea.malariacare.data.database.model.Program)1 Credentials (org.eyeseetea.malariacare.domain.entity.Credentials)1 ConfigJsonIOException (org.eyeseetea.malariacare.domain.exception.ConfigJsonIOException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1