use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.
the class TrackerDataCallMockIntegrationShould method givenATrackedEntityInstancesInDatabase.
private TrackedEntityInstance givenATrackedEntityInstancesInDatabase(String uid, State state) {
Date date = new Date();
String dateString = BaseIdentifiableObject.DATE_FORMAT.format(date);
TrackedEntityInstance trackedEntityInstance = TrackedEntityInstance.create(uid, date, date, dateString, dateString, "DiszpKrYNg8", "nEenWmSyUEp", "[9,9]", FeatureType.POINT, false, null, null, null);
trackedEntityInstanceStore.insert(trackedEntityInstance.uid(), trackedEntityInstance.created(), trackedEntityInstance.lastUpdated(), trackedEntityInstance.createdAtClient(), trackedEntityInstance.lastUpdatedAtClient(), trackedEntityInstance.organisationUnit(), trackedEntityInstance.trackedEntity(), trackedEntityInstance.coordinates(), trackedEntityInstance.featureType(), state);
return trackedEntityInstance;
}
use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.
the class TrackerDataCallMockIntegrationShould method download_all_synced_tracked_entity_instances_that_exists_in_database.
@Test
public void download_all_synced_tracked_entity_instances_that_exists_in_database() throws Exception {
givenAMetadataInDatabase();
List<TrackedEntityInstance> trackedEntityInstances = givenASyncedTrackedEntityInstancesInDatabase();
dhis2MockServer.enqueueMockResponse("system_info.json");
dhis2MockServer.enqueueMockResponse("tracked_entity_instance.json");
dhis2MockServer.enqueueMockResponse("tracked_entity_instance_2.json");
Response response = d2.syncTrackerData().call();
verifyHaveSynchronized(response, trackedEntityInstances, Collections.EMPTY_LIST);
}
use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.
the class TrackerDataCallMockIntegrationShould method verifyHaveSynchronized.
private void verifyHaveSynchronized(Response response, List<TrackedEntityInstance> syncedExpected, List<TrackedEntityInstance> toPostExpected) {
Map<String, TrackedEntityInstance> toPostInDatabase = trackedEntityInstanceStore.queryToPost();
Map<String, TrackedEntityInstance> syncedInDatabase = trackedEntityInstanceStore.querySynced();
String lastUpdated = resourceStore.getLastUpdated(ResourceModel.Type.TRACKED_ENTITY_INSTANCE);
Map<String, List<Enrollment>> enrollmentsMap = enrollmentStore.queryAll();
assertThat(response.isSuccessful(), is(true));
assertThat(syncedInDatabase.size(), is(syncedExpected.size()));
assertThat(lastUpdated, is(notNullValue()));
assertThat(toPostInDatabase.size(), is(toPostExpected.size()));
for (TrackedEntityInstance trackedEntityInstance : syncedExpected) {
assertThat(syncedInDatabase.containsKey(trackedEntityInstance.uid()), is(true));
assertThat(enrollmentsMap.get(trackedEntityInstance.uid()), is(notNullValue()));
}
for (TrackedEntityInstance trackedEntityInstance : toPostExpected) {
assertThat(toPostInDatabase.containsKey(trackedEntityInstance.uid()), is(true));
}
}
use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.
the class TrackedEntityInstancePostCall method call.
@Override
public Response<WebResponse> call() throws Exception {
synchronized (this) {
if (isExecuted) {
throw new IllegalStateException("Call is already executed");
}
isExecuted = true;
}
List<TrackedEntityInstance> trackedEntityInstancesToPost = queryDataToSync();
// if size is 0, then no need to do network request
if (trackedEntityInstancesToPost.isEmpty()) {
return null;
}
TrackedEntityInstancePayload trackedEntityInstancePayload = new TrackedEntityInstancePayload();
trackedEntityInstancePayload.trackedEntityInstances = trackedEntityInstancesToPost;
Response<WebResponse> response = trackedEntityInstanceService.postTrackedEntityInstances(trackedEntityInstancePayload).execute();
if (response.isSuccessful()) {
handleWebResponse(response);
}
return response;
}
use of org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance in project dhis2-android-sdk by dhis2.
the class TrackedEntityInstancePostCall method queryDataToSync.
@NonNull
private List<TrackedEntityInstance> queryDataToSync() {
Map<String, List<TrackedEntityDataValue>> dataValueMap = trackedEntityDataValueStore.queryTrackedEntityDataValues(Boolean.FALSE);
Map<String, List<Event>> eventMap = eventStore.queryEventsAttachedToEnrollmentToPost();
Map<String, List<Enrollment>> enrollmentMap = enrollmentStore.query();
Map<String, List<TrackedEntityAttributeValue>> attributeValueMap = trackedEntityAttributeValueStore.query();
Map<String, TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceStore.queryToPost();
List<TrackedEntityInstance> trackedEntityInstancesRecreated = new ArrayList<>();
List<Relationship> relationshipRecreated = new ArrayList<>();
// EMPTY LISTS TO REPLACE NULL VALUES SO THAT API DOESN'T BREAK.
List<TrackedEntityAttributeValue> emptyAttributeValueList = new ArrayList<>();
for (Map.Entry<String, TrackedEntityInstance> teiUid : trackedEntityInstances.entrySet()) {
List<Enrollment> enrollmentsRecreated = new ArrayList<>();
List<Enrollment> enrollments = enrollmentMap.get(teiUid.getKey());
// if enrollments is not null, then they exist for this tracked entity instance
if (enrollments != null) {
List<Event> eventRecreated = new ArrayList<>();
// building enrollment
int enrollmentSize = enrollments.size();
for (int i = 0; i < enrollmentSize; i++) {
Enrollment enrollment = enrollments.get(i);
// building events for enrollment
List<Event> eventsForEnrollment = eventMap.get(enrollment.uid());
// if eventsForEnrollment is not null, then they exist for this enrollment
if (eventsForEnrollment != null) {
int eventSize = eventsForEnrollment.size();
for (int j = 0; j < eventSize; j++) {
Event event = eventsForEnrollment.get(j);
List<TrackedEntityDataValue> dataValuesForEvent = dataValueMap.get(event.uid());
eventRecreated.add(Event.create(event.uid(), event.enrollmentUid(), event.created(), event.lastUpdated(), event.createdAtClient(), event.lastUpdatedAtClient(), event.program(), event.programStage(), event.organisationUnit(), event.eventDate(), event.status(), event.coordinates(), event.completedDate(), event.dueDate(), event.deleted(), dataValuesForEvent, event.attributeCategoryOptions(), event.attributeOptionCombo(), event.trackedEntityInstance()));
}
}
// TODO recreate with notes
enrollmentsRecreated.add(Enrollment.create(enrollment.uid(), enrollment.created(), enrollment.lastUpdated(), enrollment.createdAtClient(), enrollment.lastUpdatedAtClient(), enrollment.organisationUnit(), enrollment.program(), enrollment.dateOfEnrollment(), enrollment.dateOfIncident(), enrollment.followUp(), enrollment.enrollmentStatus(), enrollment.trackedEntityInstance(), enrollment.coordinate(), enrollment.deleted(), eventRecreated, null));
}
}
// Building TEI WITHOUT (new ArrayList) relationships
List<TrackedEntityAttributeValue> attributeValues = attributeValueMap.get(teiUid.getKey());
// Then we need to set it to empty arrayList so that API doesn't break
if (attributeValues == null) {
attributeValues = emptyAttributeValueList;
}
TrackedEntityInstance trackedEntityInstance = trackedEntityInstances.get(teiUid.getKey());
trackedEntityInstancesRecreated.add(TrackedEntityInstance.create(trackedEntityInstance.uid(), trackedEntityInstance.created(), trackedEntityInstance.lastUpdated(), trackedEntityInstance.createdAtClient(), trackedEntityInstance.lastUpdatedAtClient(), trackedEntityInstance.organisationUnit(), trackedEntityInstance.trackedEntity(), trackedEntityInstance.coordinates(), trackedEntityInstance.featureType(), trackedEntityInstance.deleted(), attributeValues, relationshipRecreated, enrollmentsRecreated));
}
return trackedEntityInstancesRecreated;
}
Aggregations