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