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;
}
}
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");
}
}
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;
}
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);
}
Aggregations