Skip to content

Commit

Permalink
Merge pull request #220 from dhis2/tei-capture-scope
Browse files Browse the repository at this point in the history
Tei capture scope
  • Loading branch information
vgarciabnz authored May 16, 2018
2 parents 3315a16 + 5354d82 commit e3f6e07
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
import android.support.annotation.NonNull;

import org.hisp.dhis.android.core.common.GenericCallData;
import org.hisp.dhis.android.core.common.IdentifiableObjectStore;
import org.hisp.dhis.android.core.common.ObjectWithoutUidStore;
import org.hisp.dhis.android.core.common.SyncCall;
import org.hisp.dhis.android.core.data.database.DatabaseAdapter;
import org.hisp.dhis.android.core.data.database.Transaction;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnitModel;
import org.hisp.dhis.android.core.organisationunit.OrganisationUnitStore;
import org.hisp.dhis.android.core.resource.ResourceModel;
import org.hisp.dhis.android.core.systeminfo.SystemInfo;
import org.hisp.dhis.android.core.systeminfo.SystemInfoCall;
import org.hisp.dhis.android.core.trackedentity.TeiQuery;
import org.hisp.dhis.android.core.trackedentity.TeisEndPointCall;
import org.hisp.dhis.android.core.user.UserOrganisationUnitLinkModel;
import org.hisp.dhis.android.core.user.UserOrganisationUnitLinkStore;

import java.util.HashSet;
import java.util.Set;

import retrofit2.Response;
Expand All @@ -26,15 +28,16 @@ public final class TrackerEntitiesDataCall extends SyncCall<Response> {
private final int teiLimitByOrgUnit;
private final DatabaseAdapter databaseAdapter;
private final Retrofit retrofit;
private final IdentifiableObjectStore<OrganisationUnitModel> organisationUnitStore;
private final ObjectWithoutUidStore<UserOrganisationUnitLinkModel> userOrganisationUnitLinkStore;

private TrackerEntitiesDataCall(@NonNull DatabaseAdapter databaseAdapter,
@NonNull Retrofit retrofit,
@NonNull IdentifiableObjectStore<OrganisationUnitModel> organisationUnitStore,
int teiLimitByOrgUnit) {
private TrackerEntitiesDataCall(
@NonNull DatabaseAdapter databaseAdapter,
@NonNull Retrofit retrofit,
@NonNull ObjectWithoutUidStore<UserOrganisationUnitLinkModel> userOrganisationUnitLinkStore,
int teiLimitByOrgUnit) {
this.databaseAdapter = databaseAdapter;
this.retrofit = retrofit;
this.organisationUnitStore = organisationUnitStore;
this.userOrganisationUnitLinkStore = userOrganisationUnitLinkStore;
this.teiLimitByOrgUnit = teiLimitByOrgUnit;
}

Expand Down Expand Up @@ -74,7 +77,7 @@ public Response call() throws Exception {
private Response trackerCall(GenericCallData genericCallData) throws Exception {
Response response = null;

Set<String> organisationUnitUids = organisationUnitStore.selectUids();
Set<String> organisationUnitUids = getOrgUnitUids();

int pageSize = TeiQuery.Builder.create().build().getPageSize();

Expand Down Expand Up @@ -118,14 +121,30 @@ private Response trackerCall(GenericCallData genericCallData) throws Exception {
return response;
}

private Set<String> getOrgUnitUids() {
Set<UserOrganisationUnitLinkModel> userOrganisationUnitLinks = userOrganisationUnitLinkStore.selectAll(
UserOrganisationUnitLinkModel.factory);

Set<String> organisationUnitUids = new HashSet<>();

for (UserOrganisationUnitLinkModel linkModel: userOrganisationUnitLinks) {
if (linkModel.organisationUnitScope().equals(
OrganisationUnitModel.Scope.SCOPE_DATA_CAPTURE.name())) {
organisationUnitUids.add(linkModel.organisationUnit());
}
}

return organisationUnitUids;
}


public static TrackerEntitiesDataCall create(DatabaseAdapter databaseAdapter, Retrofit retrofit,
int teiLimitByOrgUnit) {
return new TrackerEntitiesDataCall(
databaseAdapter,
retrofit,
OrganisationUnitStore.create(databaseAdapter),
UserOrganisationUnitLinkStore.create(databaseAdapter),
teiLimitByOrgUnit
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.auto.value.AutoValue;

import org.hisp.dhis.android.core.common.BaseModel;
import org.hisp.dhis.android.core.common.CursorModelFactory;
import org.hisp.dhis.android.core.utils.Utils;

import static org.hisp.dhis.android.core.utils.StoreUtils.sqLiteBind;
Expand Down Expand Up @@ -78,6 +79,14 @@ public static UserOrganisationUnitLinkModel create(Cursor cursor) {
return AutoValue_UserOrganisationUnitLinkModel.createFromCursor(cursor);
}

public static final CursorModelFactory<UserOrganisationUnitLinkModel> factory
= new CursorModelFactory<UserOrganisationUnitLinkModel>() {
@Override
public UserOrganisationUnitLinkModel fromCursor(Cursor cursor) {
return create(cursor);
}
};

@Override
public void bindToStatement(@NonNull SQLiteStatement sqLiteStatement) {
sqLiteBind(sqLiteStatement, 1, user());
Expand Down

0 comments on commit e3f6e07

Please sign in to comment.