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

refactor: [ANDROSDK-1949] Move LegendSet api logic to network package #2258

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
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 @@ -44,23 +41,18 @@
import org.hisp.dhis.android.core.common.ObjectWithUid;

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

@Nullable
@JsonProperty()
public abstract Double startValue();

@Nullable
@JsonProperty()
public abstract Double endValue();

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

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

Expand All @@ -75,7 +67,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 @@ -45,15 +42,12 @@
import java.util.List;

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

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

@Nullable
@JsonProperty()
@ColumnAdapter(IgnoreLegendListColumnAdapter.class)
public abstract List<Legend> legends();

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 @@ -35,13 +35,13 @@ import org.koin.core.annotation.Singleton

@Singleton
internal class LegendSetCall(
private val service: LegendSetService,
private val networkHandler: LegendSetNetworkHandler,
private val handler: LegendSetHandler,
private val apiDownloader: APIDownloader,
) : UidsCallCoroutines<LegendSet> {
override suspend fun download(uids: Set<String>): List<LegendSet> {
return apiDownloader.downloadPartitioned(uids, MAX_UID_LIST_SIZE, handler) { partitionUids ->
service.getLegendSets(LegendSetFields.allFields, LegendSetFields.uid.`in`(partitionUids), false)
networkHandler.getLegendSets(LegendSetFields.allFields, LegendSetFields.uid.`in`(partitionUids), false)
}
}

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.legendset.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.legendset.LegendSet

internal fun interface LegendSetNetworkHandler {
suspend fun getLegendSets(
fields: Fields<LegendSet>,
uids: Filter<LegendSet>,
paging: Boolean,
): Payload<LegendSet>
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ class OptionCall internal constructor(
uids,
MAX_UID_LIST_SIZE,
handler,
{ partitionUids: Set<String> ->
val optionSetUidsFilterStr = "optionSet." + ObjectWithUid.uid.`in`(partitionUids).generateString()
) { partitionUids: Set<String> ->
val optionSetUidsFilterStr = "optionSet." + ObjectWithUid.uid.`in`(partitionUids).generateString()

apiDownloader.downloadPagedPayload(PAGE_SIZE) { page, pageSize ->
networkHandler.getOptions(OptionFields.allFields, optionSetUidsFilterStr, true, page, pageSize)
}
},
)
apiDownloader.downloadPagedPayload(PAGE_SIZE) { page, pageSize ->
networkHandler.getOptions(OptionFields.allFields, optionSetUidsFilterStr, true, page, pageSize)
}
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

package org.hisp.dhis.android.network.common

import org.hisp.dhis.android.core.common.BaseIdentifiableObject

// @Serializable
internal interface BaseIdentifiableObjectDTO {
val uid: String
Expand All @@ -42,8 +44,20 @@ internal interface BaseIdentifiableObjectDTO {
val CODE = null
val NAME = null
val DISPLAY_NAME = null
const val CREATED = ""
const val LAST_UPDATED = ""
val CREATED = null
val LAST_UPDATED = null
val DELETED = null
}
}

internal fun <T> T.applyBaseIdentifiableFields(item: BaseIdentifiableObjectDTO): T where
T : BaseIdentifiableObject.Builder<T> {
uid(item.uid)
code(item.code)
name(item.name)
displayName(item.displayName)
item.created?.let { created(it) } ?: { created(null) }
item.lastUpdated?.let { lastUpdated(it) } ?: { lastUpdated(null) }
deleted(item.deleted)
return this
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package org.hisp.dhis.android.network.common
import kotlinx.serialization.Serializable

@Serializable
internal data class ObjectWithStyle(
internal data class ObjectWithStyleDTO(
val color: String? = null,
val icon: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package org.hisp.dhis.android.network.common
import kotlinx.serialization.Serializable

@Serializable
internal data class Pager(
internal data class PagerDTO(
val page: Int,
val pageCount: Int,
val pageSize: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import org.hisp.dhis.android.core.arch.api.payload.internal.Payload

@Serializable
internal open class PayloadJson<T>(
@Transient override val pager: Pager? = null,
@Transient override val pager: PagerDTO? = null,
@Transient override val items: List<T> = emptyList(),
) : Payload<T> {

Expand All @@ -44,7 +44,7 @@ internal open class PayloadJson<T>(
return items
}

override fun pager(): Pager? {
override fun pager(): PagerDTO? {
return pager
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ package org.hisp.dhis.android.network.constant
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import org.hisp.dhis.android.network.common.BaseIdentifiableObjectDTO
import org.hisp.dhis.android.network.common.Pager
import org.hisp.dhis.android.network.common.PagerDTO
import org.hisp.dhis.android.network.common.PayloadJson

@Serializable
internal class ConstantPayload(
override val pager: Pager? = null,
@SerialName("constants") override val items: List<ConstantDTO> = emptyList(),
) : PayloadJson<ConstantDTO>(pager, items)

@Serializable
internal data class ConstantDTO(
@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 created: String? = BaseIdentifiableObjectDTO.CREATED,
override val lastUpdated: String? = BaseIdentifiableObjectDTO.LAST_UPDATED,
override val deleted: Boolean? = BaseIdentifiableObjectDTO.DELETED,
val value: Double? = null,
) : BaseIdentifiableObjectDTO

@Serializable
internal class ConstantPayload(
override val pager: PagerDTO? = null,
@SerialName("constants") override val items: List<ConstantDTO> = emptyList(),
) : PayloadJson<ConstantDTO>(pager, items)
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,11 @@
package org.hisp.dhis.android.network.constant

import org.hisp.dhis.android.core.constant.Constant
import org.hisp.dhis.android.network.common.applyBaseIdentifiableFields

internal fun constantApiToDomainMapper(item: ConstantDTO): Constant {
return Constant.builder()
.uid(item.uid)
.code(item.code)
.name(item.name)
.displayName(item.displayName)
.created(item.created)
.lastUpdated(item.lastUpdated)
.deleted(item.deleted)
.applyBaseIdentifiableFields(item)
.value(item.value)
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.legendset

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

@Serializable
internal data class LegendDTO(
@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 startValue: Double? = null,
val endValue: Double? = null,
val color: String? = null,
) : BaseIdentifiableObjectDTO
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.legendset

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

@Serializable
internal data class LegendSetDTO(
@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 symbolizer: String? = null,
val legends: List<LegendDTO> = emptyList(),
) : BaseIdentifiableObjectDTO

@Serializable
internal class LegendSetPayload(
override val pager: PagerDTO? = null,
@SerialName("legendSets") override val items: List<LegendSetDTO> = emptyList(),
) : PayloadJson<LegendSetDTO>(pager, items)
Loading