@@ -0,0 +1,4043 @@
+package org.hisp.dhis.android.core.event;%0A%0Aimport android.support.annotation.NonNull;%0A%0Aimport org.hisp.dhis.android.core.common.D2CallException;%0Aimport org.hisp.dhis.android.core.common.IdentifiableObjectStore;%0Aimport org.hisp.dhis.android.core.common.SyncCall;%0Aimport org.hisp.dhis.android.core.data.database.DatabaseAdapter;%0Aimport org.hisp.dhis.android.core.data.database.Transaction;%0Aimport org.hisp.dhis.android.core.organisationunit.OrganisationUnitModel;%0Aimport org.hisp.dhis.android.core.organisationunit.OrganisationUnitStore;%0Aimport org.hisp.dhis.android.core.organisationunit.SearchOrganisationUnitCall;%0Aimport org.hisp.dhis.android.core.user.AuthenticatedUserModel;%0Aimport org.hisp.dhis.android.core.user.AuthenticatedUserStore;%0Aimport org.hisp.dhis.android.core.user.AuthenticatedUserStoreImpl;%0A%0Aimport java.util.Collection;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Aimport retrofit2.Retrofit;%0A%0Afinal class EventPersistenceCall extends SyncCall%3CVoid%3E %7B%0A%0A    private final DatabaseAdapter databaseAdapter;%0A    private final Retrofit retrofit;%0A    private final EventHandler eventHandler;%0A    private final AuthenticatedUserStore authenticatedUserStore;%0A    private final IdentifiableObjectStore%3COrganisationUnitModel%3E organisationUnitStore;%0A    private final SearchOrganisationUnitCall.Factory organisationUnitCallFactory;%0A%0A    private final Collection%3CEvent%3E events;%0A%0A    private EventPersistenceCall(%0A            @NonNull DatabaseAdapter databaseAdapter,%0A            @NonNull Retrofit retrofit,%0A            @NonNull EventHandler eventHandler,%0A            @NonNull AuthenticatedUserStore authenticatedUserStore,%0A            @NonNull IdentifiableObjectStore%3COrganisationUnitModel%3E organisationUnitStore,%0A            @NonNull SearchOrganisationUnitCall.Factory organisationUnitCallFactory,%0A            @NonNull Collection%3CEvent%3E events) %7B%0A        this.databaseAdapter = databaseAdapter;%0A        this.retrofit = retrofit;%0A        this.eventHandler = eventHandler;%0A        this.authenticatedUserStore = authenticatedUserStore;%0A        this.organisationUnitStore = organisationUnitStore;%0A        this.organisationUnitCallFactory = organisationUnitCallFactory;%0A        this.events = events;%0A    %7D%0A%0A    @Override%0A    public Void call() throws D2CallException %7B%0A        super.setExecuted();%0A%0A        Transaction transaction = databaseAdapter.beginNewTransaction();%0A%0A        try %7B%0A            eventHandler.handleMany(events);%0A%0A            Set%3CString%3E searchOrgUnitUids = getMissingOrganisationUnitUids(events);%0A%0A            if (!searchOrgUnitUids.isEmpty()) %7B%0A                AuthenticatedUserModel authenticatedUserModel = authenticatedUserStore.query().get(0);%0A                SearchOrganisationUnitCall organisationUnitCall = organisationUnitCallFactory.create(%0A                        databaseAdapter, retrofit, searchOrgUnitUids, authenticatedUserModel.user());%0A                organisationUnitCall.call();%0A            %7D%0A%0A            transaction.setSuccessful();%0A        %7D finally %7B%0A            transaction.end();%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    private Set%3CString%3E getMissingOrganisationUnitUids(Collection%3CEvent%3E events) %7B%0A        Set%3CString%3E uids = new HashSet%3C%3E();%0A        for (Event event : events) %7B%0A            if (event.organisationUnit() != null) %7B%0A                uids.add(event.organisationUnit());%0A            %7D%0A        %7D%0A        uids.removeAll(organisationUnitStore.selectUids());%0A        return uids;%0A    %7D%0A%0A    public static EventPersistenceCall create(DatabaseAdapter databaseAdapter,%0A                                              Retrofit retrofit,%0A                                              Collection%3CEvent%3E events) %7B%0A        return new EventPersistenceCall(%0A                databaseAdapter,%0A                retrofit,%0A                EventHandler.create(databaseAdapter),%0A                new AuthenticatedUserStoreImpl(databaseAdapter),%0A                OrganisationUnitStore.create(databaseAdapter),%0A                SearchOrganisationUnitCall.FACTORY,%0A                events%0A        );%0A    %7D%0A%7D%0A

add EventPersistenceCall