diff --git a/core/api/core.api b/core/api/core.api index 0842f50155..6ffd5d5c16 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -10829,6 +10829,9 @@ public final class org/hisp/dhis/android/core/relationship/RelationshipCollectio public synthetic fun blockingAdd (Ljava/lang/Object;)Ljava/lang/String; public fun blockingAdd (Lorg/hisp/dhis/android/core/relationship/Relationship;)Ljava/lang/String; public final fun byCreated ()Lorg/hisp/dhis/android/core/arch/repositories/filters/internal/DateFilterConnector; + public final fun byDeleted ()Lorg/hisp/dhis/android/core/arch/repositories/filters/internal/BooleanFilterConnector; + public final fun byItem (Lorg/hisp/dhis/android/core/relationship/RelationshipItem;)Lorg/hisp/dhis/android/core/relationship/RelationshipCollectionRepository; + public final fun byItem (Lorg/hisp/dhis/android/core/relationship/RelationshipItem;Z)Lorg/hisp/dhis/android/core/relationship/RelationshipCollectionRepository; public final fun byLastUpdated ()Lorg/hisp/dhis/android/core/arch/repositories/filters/internal/DateFilterConnector; public final fun byName ()Lorg/hisp/dhis/android/core/arch/repositories/filters/internal/StringFilterConnector; public final fun byRelationshipType ()Lorg/hisp/dhis/android/core/arch/repositories/filters/internal/StringFilterConnector; diff --git a/core/src/androidTest/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepositoryMockIntegrationShould.java b/core/src/androidTest/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepositoryMockIntegrationShould.java index 5e29cee7c8..f9a1117e43 100644 --- a/core/src/androidTest/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepositoryMockIntegrationShould.java +++ b/core/src/androidTest/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepositoryMockIntegrationShould.java @@ -136,6 +136,22 @@ public void get_by_item_including_all_linked() { assertThat(relationships.size()).isEqualTo(2); } + @Test + public void filter_by_item() { + RelationshipItem item = RelationshipItem.builder() + .trackedEntityInstance( + RelationshipItemTrackedEntityInstance.builder().trackedEntityInstance("nWrB0TfWlvh").build() + ) + .relationshipItemType(RelationshipConstraintType.FROM) + .build(); + + List relationships = d2.relationshipModule().relationships() + .byItem(item) + .blockingGet(); + + assertThat(relationships.size()).isEqualTo(1); + } + @Test public void filter_by_object_repository() { Relationship relationship = diff --git a/core/src/main/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepository.kt b/core/src/main/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepository.kt index c2842c9f11..d2adeffe07 100644 --- a/core/src/main/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepository.kt +++ b/core/src/main/java/org/hisp/dhis/android/core/relationship/RelationshipCollectionRepository.kt @@ -29,12 +29,14 @@ package org.hisp.dhis.android.core.relationship import io.reactivex.Single import org.hisp.dhis.android.core.arch.db.access.DatabaseAdapter +import org.hisp.dhis.android.core.arch.db.querybuilders.internal.WhereClauseBuilder import org.hisp.dhis.android.core.arch.handlers.internal.HandleAction import org.hisp.dhis.android.core.arch.helpers.DateUtils.toJavaDate import org.hisp.dhis.android.core.arch.helpers.UidGeneratorImpl import org.hisp.dhis.android.core.arch.repositories.children.internal.ChildrenAppenderGetter import org.hisp.dhis.android.core.arch.repositories.collection.ReadWriteWithUidCollectionRepository import org.hisp.dhis.android.core.arch.repositories.collection.internal.BaseReadOnlyWithUidCollectionRepositoryImpl +import org.hisp.dhis.android.core.arch.repositories.filters.internal.BooleanFilterConnector import org.hisp.dhis.android.core.arch.repositories.filters.internal.DateFilterConnector import org.hisp.dhis.android.core.arch.repositories.filters.internal.EnumFilterConnector import org.hisp.dhis.android.core.arch.repositories.filters.internal.FilterConnectorFactory @@ -195,6 +197,32 @@ class RelationshipCollectionRepository internal constructor( return relationshipManager.getByItem(searchItem, includeDeleted, onlyAccessible) } + fun byItem(searchItem: RelationshipItem): RelationshipCollectionRepository { + return byItem(searchItem, includeDeleted = false) + } + + fun byItem(searchItem: RelationshipItem, includeDeleted: Boolean): RelationshipCollectionRepository { + return cf.subQuery(IdentifiableColumns.UID).inTableWhere( + RelationshipItemTableInfo.TABLE_INFO.name(), + RelationshipItemTableInfo.Columns.RELATIONSHIP, + WhereClauseBuilder().apply { + searchItem.relationship()?.uid()?.let { relationshipUid -> + appendKeyStringValue(RelationshipItemTableInfo.Columns.RELATIONSHIP, relationshipUid) + } + searchItem.relationshipItemType()?.let { itemType -> + appendKeyStringValue(RelationshipItemTableInfo.Columns.RELATIONSHIP_ITEM_TYPE, itemType.name) + } + searchItem.elementUid()?.let { elementId -> + appendKeyStringValue(searchItem.elementType(), elementId) + } + }.build(), + ).apply { + if (!includeDeleted) { + byDeleted().isFalse + } + } + } + fun byUid(): StringFilterConnector { return cf.string(IdentifiableColumns.UID) } @@ -219,6 +247,10 @@ class RelationshipCollectionRepository internal constructor( return cf.enumC(RelationshipTableInfo.Columns.SYNC_STATE) } + fun byDeleted(): BooleanFilterConnector { + return cf.bool(RelationshipTableInfo.Columns.DELETED) + } + fun withItems(): RelationshipCollectionRepository { return cf.withChild(RelationshipFields.ITEMS) }