private static void postTrackedEntityInstance( TrackedEntityInstance trackedEntityInstance, DhisApi dhisApi) throws APIException { try { Response response = dhisApi.postTrackedEntityInstance(trackedEntityInstance); if (response.getStatus() == 200) { ImportSummary importSummary = getImportSummary(response); handleImportSummary( importSummary, FailedItem.TRACKEDENTITYINSTANCE, trackedEntityInstance.getLocalId()); if (ImportSummary.SUCCESS.equals(importSummary.getStatus()) || ImportSummary.OK.equals(importSummary.getStatus())) { // change state and save trackedentityinstance // trackedEntityInstance.setState(State.SYNCED); trackedEntityInstance.setFromServer(true); trackedEntityInstance.save(); clearFailedItem(FailedItem.TRACKEDENTITYINSTANCE, trackedEntityInstance.getLocalId()); UpdateTrackedEntityInstanceTimestamp(trackedEntityInstance, dhisApi); } } } catch (APIException apiException) { NetworkUtils.handleTrackedEntityInstanceSendException(apiException, trackedEntityInstance); } }
private static void UpdateEventTimestamp(Event event, DhisApi dhisApi) throws APIException { try { final Map<String, String> QUERY_PARAMS = new HashMap<>(); QUERY_PARAMS.put("fields", "created,lastUpdated"); Event updatedEvent = dhisApi.getEvent(event.getEvent(), QUERY_PARAMS); // merging updated timestamp to local event model event.setCreated(updatedEvent.getCreated()); event.setLastUpdated(updatedEvent.getLastUpdated()); event.save(); } catch (APIException apiException) { NetworkUtils.handleApiException(apiException); } }
private static void UpdateTrackedEntityInstanceTimestamp( TrackedEntityInstance trackedEntityInstance, DhisApi dhisApi) throws APIException { try { final Map<String, String> QUERY_PARAMS = new HashMap<>(); QUERY_PARAMS.put("fields", "created,lastUpdated"); TrackedEntityInstance updatedTrackedEntityInstance = dhisApi.getTrackedEntityInstance( trackedEntityInstance.getTrackedEntityInstance(), QUERY_PARAMS); // merging updated timestamp to local trackedentityinstance model trackedEntityInstance.setCreated(updatedTrackedEntityInstance.getCreated()); trackedEntityInstance.setLastUpdated(updatedTrackedEntityInstance.getLastUpdated()); trackedEntityInstance.save(); } catch (APIException apiException) { NetworkUtils.handleApiException(apiException); } }
private static void putEvent(Event event, DhisApi dhisApi) throws APIException { try { Response response = dhisApi.putEvent(event.getEvent(), event); if (response.getStatus() == 200) { ImportSummary importSummary = getImportSummary(response); handleImportSummary(importSummary, FailedItem.EVENT, event.getLocalId()); if (ImportSummary.SUCCESS.equals(importSummary.getStatus()) || ImportSummary.OK.equals(importSummary.getStatus())) { event.setFromServer(true); event.save(); clearFailedItem(FailedItem.EVENT, event.getLocalId()); UpdateEventTimestamp(event, dhisApi); } } } catch (APIException apiException) { NetworkUtils.handleEventSendException(apiException, event); } }
private static void postEnrollment(Enrollment enrollment, DhisApi dhisApi) throws APIException { try { Response response = dhisApi.postEnrollment(enrollment); if (response.getStatus() == 200) { ImportSummary importSummary = getImportSummary(response); handleImportSummary(importSummary, FailedItem.ENROLLMENT, enrollment.getLocalId()); if (ImportSummary.SUCCESS.equals(importSummary.getStatus()) || ImportSummary.OK.equals(importSummary.getStatus())) { // change state and save enrollment // enrollment.setState(State.SYNCED); enrollment.setFromServer(true); enrollment.save(); clearFailedItem(FailedItem.ENROLLMENT, enrollment.getLocalId()); UpdateEnrollmentTimestamp(enrollment, dhisApi); } } } catch (APIException apiException) { NetworkUtils.handleEnrollmentSendException(apiException, enrollment); } }
private static void postEvent(Event event, DhisApi dhisApi) throws APIException { try { Response response = dhisApi.postEvent(event); if (response.getStatus() == 200) { ImportSummary importSummary = getImportSummary(response); handleImportSummary(importSummary, FailedItem.EVENT, event.getLocalId()); if (ImportSummary.SUCCESS.equals(importSummary.getStatus()) || ImportSummary.OK.equals(importSummary.getStatus())) { // also, we will need to find UUID of newly created event, // which is contained inside of HTTP Location header Header header = NetworkUtils.findLocationHeader(response.getHeaders()); // change state and save event event.setFromServer(true); event.save(); clearFailedItem(FailedItem.EVENT, event.getLocalId()); UpdateEventTimestamp(event, dhisApi); } } } catch (APIException apiException) { NetworkUtils.handleEventSendException(apiException, event); } }