package org.hisp.dhis.android.core.calls;

import android.support.annotation.NonNull;

import org.hisp.dhis.android.core.common.D2CallException;
import org.hisp.dhis.android.core.common.SyncCall;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstance;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceByUidEndPointCall;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceStore;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityInstanceStoreImpl;

import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;

public final class TrackerDataCall extends SyncCall<List<TrackedEntityInstance>> {

    private final DatabaseAdapter databaseAdapter;
    private final Retrofit retrofit;
    private final TrackedEntityInstanceStore trackedEntityInstanceStore;

    private TrackerDataCall(@NonNull DatabaseAdapter databaseAdapter,
                            @NonNull Retrofit retrofit,
                            @NonNull TrackedEntityInstanceStore trackedEntityInstanceStore) {
        this.databaseAdapter = databaseAdapter;
        this.retrofit = retrofit;
        this.trackedEntityInstanceStore = trackedEntityInstanceStore;
    }

    @Override
    public List<TrackedEntityInstance> call() throws D2CallException {
        super.setExecuted();

        Map<String, TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceStore.querySynced();

        try {
            return TrackedEntityInstanceByUidEndPointCall.create(databaseAdapter,
                    retrofit, trackedEntityInstances.keySet()).call();
        } catch (D2CallException d2E) {
            throw d2E;
        } catch (Exception e) {
            throw D2CallException
                    .builder()
                    .errorDescription("Unexpected exception in TrackedEntityInstanceByUidEndPointCall")
                    .originalException(e)
                    .isHttpError(false).build();
        }
    }

    public static TrackerDataCall create(DatabaseAdapter databaseAdapter, Retrofit retrofit) {
        return new TrackerDataCall(
                databaseAdapter,
                retrofit,
                new TrackedEntityInstanceStoreImpl(databaseAdapter)
        );
    }
}

@@ -138,16 +138,73 @@
 eption;%0A
+import org.hisp.dhis.android.core.common.D2CallExecutor;%0A
 import o
@@ -447,36 +447,45 @@
 tityInstance
-ByUidEndPoin
+ListDownloadAndPersis
 tCall;%0Aimpor
@@ -1445,25 +1445,24 @@
 Executed();%0A
-%0A
         Map%3C
@@ -1559,25 +1559,24 @@
 nced();%0A
-%0A
         
 try %7B%0A  
@@ -1571,71 +1571,113 @@
     
-try %7B%0A            return TrackedEntityInstanceByUidEndPointCall
+Call%3CList%3CTrackedEntityInstance%3E%3E call = TrackedEntityInstanceListDownloadAndPersistCall%0A                
 .cre
@@ -1696,36 +1696,16 @@
 Adapter,
-%0A                   
  retrofi
@@ -1743,15 +1743,8 @@
 t())
-.call()
 ;%0A  
@@ -1753,364 +1753,56 @@
     
-%7D catch (D2CallException d2E) %7B%0A            throw d2E;%0A        %7D catch (Exception e) %7B%0A            throw D2CallException%0A                    .builder()%0A                    .errorDescription(%22Unexpected exception in TrackedEntityInstanceByUidEndPointCall%22)%0A                    .originalException(e)%0A                    .isHttpError(false).build();%0A        %7D
+return new D2CallExecutor().executeD2Call(call);
 %0A   

simplify TrackerDataCall