Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: [ANDROSDK-1959] Move CategoryCombo api logic to network #2263

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

import androidx.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.gabrielittner.auto.value.cursor.ColumnAdapter;
import com.google.auto.value.AutoValue;

Expand All @@ -45,15 +42,12 @@
import java.util.List;

@AutoValue
@JsonDeserialize(builder = AutoValue_Category.Builder.class)
public abstract class Category extends BaseIdentifiableObject implements CoreObject {

@Nullable
@JsonProperty()
public abstract String dataDimensionType();

@Nullable
@JsonProperty()
@ColumnAdapter(IgnoreCategoryOptionListColumnAdapter.class)
public abstract List<CategoryOption> categoryOptions();

Expand All @@ -68,7 +62,6 @@ public static Builder builder() {
}

@AutoValue.Builder
@JsonPOJOBuilder(withPrefix = "")
public abstract static class Builder extends BaseIdentifiableObject.Builder<Builder> {
public abstract Builder id(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@

import android.database.Cursor;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import androidx.annotation.Nullable;

import com.gabrielittner.auto.value.cursor.ColumnAdapter;
import com.google.auto.value.AutoValue;

Expand All @@ -43,25 +42,19 @@

import java.util.List;

import androidx.annotation.Nullable;

@AutoValue
@JsonDeserialize(builder = AutoValue_CategoryCombo.Builder.class)
public abstract class CategoryCombo extends BaseIdentifiableObject implements CoreObject {

public static final String DEFAULT_UID = "p0KPaWEg3cf";

@Nullable
@JsonProperty()
public abstract Boolean isDefault();

@Nullable
@JsonProperty()
@ColumnAdapter(IgnoreCategoryListColumnAdapter.class)
public abstract List<Category> categories();

@Nullable
@JsonProperty()
@ColumnAdapter(IgnoreCategoryOptionComboListColumnAdapter.class)
abstract List<CategoryOptionCombo> categoryOptionCombos();

Expand All @@ -76,7 +69,6 @@ public static Builder builder() {
}

@AutoValue.Builder
@JsonPOJOBuilder(withPrefix = "")
public abstract static class Builder extends BaseIdentifiableObject.Builder<Builder> {

public abstract Builder id(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@

import androidx.annotation.Nullable;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.gabrielittner.auto.value.cursor.ColumnAdapter;
import com.google.auto.value.AutoValue;

Expand All @@ -47,16 +44,13 @@
import java.util.List;

@AutoValue
@JsonDeserialize(builder = $$AutoValue_CategoryOptionCombo.Builder.class)
public abstract class CategoryOptionCombo extends BaseIdentifiableObject implements CoreObject {

@Nullable
@JsonProperty()
@ColumnAdapter(ObjectWithUidColumnAdapter.class)
public abstract ObjectWithUid categoryCombo();

@Nullable
@JsonProperty()
@ColumnAdapter(IgnoreCategoryOptionListColumnAdapter.class)
public abstract List<CategoryOption> categoryOptions();

Expand All @@ -71,7 +65,6 @@ public static Builder builder() {
}

@AutoValue.Builder
@JsonPOJOBuilder(withPrefix = "")
public abstract static class Builder extends BaseIdentifiableObject.Builder<Builder> {

public abstract Builder id(Long id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import org.koin.core.annotation.Singleton

@Singleton
internal class CategoryComboCall(
private val service: CategoryComboService,
private val networkHandler: CategoryComboNetworkHandler,
private val handler: CategoryComboHandler,
private val apiDownloader: APIDownloader,
) : UidsCallCoroutines<CategoryCombo> {
override suspend fun download(uids: Set<String>): List<CategoryCombo> {
return apiDownloader.downloadPartitioned(uids, MAX_UID_LIST_SIZE, handler) { partitionUids: Set<String> ->
service.getCategoryCombos(
networkHandler.getCategoryCombos(
CategoryComboFields.allFields,
CategoryComboFields.uid.`in`(partitionUids),
paging = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,18 @@ import org.hisp.dhis.android.core.category.Category
import org.hisp.dhis.android.core.category.CategoryCategoryComboLink
import org.hisp.dhis.android.core.category.CategoryCombo
import org.hisp.dhis.android.core.category.CategoryComboInternalAccessor
import org.hisp.dhis.android.core.category.CategoryOptionCombo
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.koin.core.annotation.Singleton

@Singleton
internal class CategoryComboHandler constructor(
internal class CategoryComboHandler(
store: CategoryComboStore,
private val optionComboHandler: CategoryOptionComboHandler,
private val categoryCategoryComboLinkHandler: CategoryCategoryComboLinkHandler,
private val categoryOptionCleaner: CategoryOptionComboOrphanCleaner,
) : IdentifiableHandlerImpl<CategoryCombo>(store) {

override fun afterObjectHandled(o: CategoryCombo, action: HandleAction) {
optionComboHandler.handleMany(
CategoryComboInternalAccessor.accessCategoryOptionCombos(o),
) { optionCombo: CategoryOptionCombo ->
optionCombo.toBuilder()
.categoryCombo(ObjectWithUid.create(o.uid()))
.build()
}
optionComboHandler.handleMany(CategoryComboInternalAccessor.accessCategoryOptionCombos(o))
categoryCategoryComboLinkHandler.handleMany(
o.uid(),
o.categories(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.hisp.dhis.android.core.category.internal

import org.hisp.dhis.android.core.arch.api.fields.internal.Fields
import org.hisp.dhis.android.core.arch.api.filters.internal.Filter
import org.hisp.dhis.android.core.arch.api.payload.internal.Payload
import org.hisp.dhis.android.core.category.CategoryCombo

internal fun interface CategoryComboNetworkHandler {
suspend fun getCategoryCombos(
fields: Fields<CategoryCombo>,
uids: Filter<CategoryCombo>,
paging: Boolean,
): Payload<CategoryCombo>
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,18 @@ package org.hisp.dhis.android.core.legendset.internal

import org.hisp.dhis.android.core.arch.handlers.internal.HandleAction
import org.hisp.dhis.android.core.arch.handlers.internal.IdentifiableHandlerImpl
import org.hisp.dhis.android.core.common.ObjectWithUid
import org.hisp.dhis.android.core.legendset.Legend
import org.hisp.dhis.android.core.legendset.LegendSet
import org.koin.core.annotation.Singleton

@Singleton
internal class LegendSetHandler constructor(
internal class LegendSetHandler(
legendSetStore: LegendSetStore,
private val legendHandler: LegendHandler,
private val legendCleaner: LegendSetLegendOrphanCleaner,
) : IdentifiableHandlerImpl<LegendSet>(legendSetStore) {

override fun afterObjectHandled(o: LegendSet, action: HandleAction) {
legendHandler.handleMany(
o.legends(),
) { legend: Legend ->
legend.toBuilder()
.legendSet(ObjectWithUid.create(o.uid()))
.build()
}
legendHandler.handleMany(o.legends())

if (action === HandleAction.Update) {
legendCleaner.deleteOrphan(o, o.legends())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class AttributeNetworkHandlerImpl(
uids: Filter<Attribute>,
paging: Boolean,
): PayloadJson<Attribute> {
val apiPayload = service.attributes(fields, uids, paging)
val apiPayload = service.getAttributes(fields, uids, paging)
return apiPayload.mapItems(::attributeDtoToDomainMapper)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.koin.core.annotation.Singleton

@Singleton
internal class AttributeService(private val client: HttpServiceClientKotlinx) {
suspend fun attributes(
suspend fun getAttributes(
fields: Fields<Attribute>,
uids: Filter<Attribute>,
paging: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.hisp.dhis.android.network.category

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.hisp.dhis.android.network.common.BaseIdentifiableObjectDTO
import org.hisp.dhis.android.network.common.ObjectWithUidDTO
import org.hisp.dhis.android.network.common.PagerDTO
import org.hisp.dhis.android.network.common.PayloadJson

@Serializable
internal data class CategoryComboDTO(
@SerialName("id") override val uid: String,
override val code: String? = BaseIdentifiableObjectDTO.CODE,
override val name: String? = BaseIdentifiableObjectDTO.NAME,
override val displayName: String? = BaseIdentifiableObjectDTO.DISPLAY_NAME,
override val created: String? = BaseIdentifiableObjectDTO.CREATED,
override val lastUpdated: String? = BaseIdentifiableObjectDTO.LAST_UPDATED,
override val deleted: Boolean? = BaseIdentifiableObjectDTO.DELETED,
val isDefault: Boolean? = null,
val categories: List<ObjectWithUidDTO> = emptyList(),
val categoryOptionCombos: List<CategoryOptionComboDTO> = emptyList(),
) : BaseIdentifiableObjectDTO

@Serializable
internal class CategoryComboPayload(
override val pager: PagerDTO? = null,
@SerialName("categoryCombos") override val items: List<CategoryComboDTO> = emptyList(),
) : PayloadJson<CategoryComboDTO>(pager, items)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* Copyright (c) 2004-2024, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -25,42 +25,28 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

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

import org.hisp.dhis.android.core.common.BaseIdentifiableObject;
import org.hisp.dhis.android.core.common.BaseObjectShould;
import org.hisp.dhis.android.core.common.ObjectShould;
import org.junit.Test;

import java.io.IOException;
import java.text.ParseException;

import static com.google.common.truth.Truth.assertThat;

public class CategoryComboShould extends BaseObjectShould implements ObjectShould {

public CategoryComboShould() {
super("category/category_combo.json");
}

@Override
@Test
public void map_from_json_string() throws IOException, ParseException {
CategoryCombo combo = objectMapper.readValue(jsonStream, CategoryCombo.class);

assertThat(combo.uid()).isEqualTo("m2jTvAj5kkm");
assertThat(combo.code()).isEqualTo("BIRTHS");
assertThat(combo.created()).isEqualTo(
BaseIdentifiableObject.DATE_FORMAT.parse("2011-12-24T12:24:25.203"));
assertThat(combo.lastUpdated()).isEqualTo(
BaseIdentifiableObject.DATE_FORMAT.parse("2016-04-18T16:04:34.745"));
assertThat(combo.name()).isEqualTo("Births");
assertThat(combo.displayName()).isEqualTo("Births");
assertThat(combo.isDefault()).isFalse();

// categories
assertThat(combo.categories().get(0).uid()).isEqualTo("KfdsGBcoiCa");
assertThat(combo.categories().get(1).uid()).isEqualTo("cX5k9anHEHd");
package org.hisp.dhis.android.network.category

import org.hisp.dhis.android.core.arch.api.fields.internal.Fields
import org.hisp.dhis.android.core.arch.api.filters.internal.Filter
import org.hisp.dhis.android.core.category.CategoryCombo
import org.hisp.dhis.android.core.category.internal.CategoryComboNetworkHandler
import org.hisp.dhis.android.network.common.HttpServiceClientKotlinx
import org.hisp.dhis.android.network.common.PayloadJson
import org.koin.core.annotation.Singleton

@Singleton
internal class CategoryComboNetworkHandlerImpl(
httpClient: HttpServiceClientKotlinx,
) : CategoryComboNetworkHandler {
private val service: CategoryComboService = CategoryComboService(httpClient)

override suspend fun getCategoryCombos(
fields: Fields<CategoryCombo>,
uids: Filter<CategoryCombo>,
paging: Boolean,
): PayloadJson<CategoryCombo> {
val apiPayload = service.getCategoryCombos(fields, uids, paging)
return apiPayload.mapItems(::categoryComboDtoToDomainMapper)
}
}
Loading