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

fix: [ANDROAPP-6093] crash when overriding cat combo in data sets #3712

Merged
merged 3 commits into from
Jul 5, 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 @@ -8,13 +8,13 @@ import io.reactivex.Single
import org.dhis2.bindings.decimalFormat
import org.dhis2.commons.bindings.dataValueConflicts
import org.dhis2.commons.data.tuples.Pair
import org.dhis2.commons.date.DateUtils
import org.dhis2.composetable.model.TableCell
import org.dhis2.data.dhislogic.AUTH_DATAVALUE_ADD
import org.dhis2.data.forms.dataentry.tablefields.FieldViewModel
import org.dhis2.data.forms.dataentry.tablefields.FieldViewModelFactoryImpl
import org.dhis2.data.forms.dataentry.tablefields.spinner.SpinnerViewModel
import org.dhis2.usescases.datasets.dataSetTable.DataSetTableModel
import org.dhis2.utils.DateUtils
import org.hisp.dhis.android.core.D2
import org.hisp.dhis.android.core.arch.helpers.UidsHelper
import org.hisp.dhis.android.core.arch.repositories.scope.RepositoryScope
Expand Down Expand Up @@ -61,6 +61,7 @@ class DataValueRepository(
?.categoryComboUid()
}?.distinct()
}

else -> {
val dataElementsSectionUid = d2.dataSetModule().sections().withDataElements()
.byDataSetUid().eq(dataSetUid)
Expand Down Expand Up @@ -157,29 +158,16 @@ class DataValueRepository(
dataElement: DataElement,
override: List<DataSetElement>?,
): DataElement {
return override
?.firstOrNull {
it.dataElement().uid() == dataElement.uid() && it.categoryCombo() != null
}?.let {
DataElement.builder()
.uid(dataElement.uid())
.code(dataElement.code())
.name(dataElement.name())
.displayName(dataElement.displayName())
.shortName(dataElement.shortName())
.displayShortName(dataElement.displayShortName())
.description(dataElement.description())
.displayDescription(dataElement.displayDescription())
.valueType(dataElement.valueType())
.zeroIsSignificant(dataElement.zeroIsSignificant())
.aggregationType(dataElement.aggregationType())
.formName(dataElement.formName())
.domainType(dataElement.domainType())
.displayFormName(dataElement.displayFormName())
.optionSet(dataElement.optionSet())
.categoryCombo(it.categoryCombo()).build()
}
?: dataElement
val dataSetElement = override?.firstOrNull {
it.dataElement().uid() == dataElement.uid() && it.categoryCombo() != null
}
return if (dataSetElement != null) {
dataElement.toBuilder()
.categoryCombo(dataSetElement.categoryCombo())
.build()
} else {
dataElement
}
}

private fun getDataValues(): Flowable<List<DataSetTableModel>> {
Expand Down Expand Up @@ -290,6 +278,7 @@ class DataValueRepository(
.get()
.map { section -> section.greyedFields() }
.toFlowable()

else -> Flowable.just(ArrayList())
}

Expand Down Expand Up @@ -319,45 +308,38 @@ class DataValueRepository(

private fun getDataElements(categoryCombo: CategoryCombo): Flowable<List<DataElement>> {
return if (sectionUid != "NO_SECTION") {
val listDataElements =
d2.dataSetModule().sections().withDataElements().byDataSetUid().eq(dataSetUid)
.uid(sectionUid).blockingGet()?.dataElements()
val dataElementsOverride: MutableList<DataElement> =
ArrayList()
val dataSetElements =
d2.dataSetModule().dataSets().withDataSetElements().uid(dataSetUid).blockingGet()
?.dataSetElements()
listDataElements
?.map { transformDataElement(it, dataSetElements) }
?.filter { it.categoryComboUid() == categoryCombo.uid() }
?.forEach { dataElementsOverride.add(it) }
val dataElementsInSection = d2.dataSetModule().sections().withDataElements()
.byDataSetUid().eq(dataSetUid)
.uid(sectionUid)
.blockingGet()
?.dataElements()

val dataSetElements = d2.dataSetModule().dataSets().withDataSetElements()
.uid(dataSetUid)
.blockingGet()
?.dataSetElements()

Flowable.just(
dataElementsOverride,
dataElementsInSection
?.map { transformDataElement(it, dataSetElements) }
?.filter { it.categoryComboUid() == categoryCombo.uid() },
)
} else {
val dataElementUids: MutableList<String> =
ArrayList()
val dataSetElements =
d2.dataSetModule().dataSets().withDataSetElements().byUid().eq(dataSetUid).one()
.blockingGet()?.dataSetElements()
for (dataSetElement in dataSetElements!!) {
if (dataSetElement.categoryCombo() != null &&
categoryCombo.uid() == dataSetElement.categoryCombo()!!.uid()
) {
dataElementUids.add(dataSetElement.dataElement().uid())
} else {
val uid = d2.dataElementModule().dataElements()
.uid(dataSetElement.dataElement().uid()).blockingGet()?.categoryComboUid()
if (categoryCombo.uid() == uid) {
dataElementUids.add(dataSetElement.dataElement().uid())
}
}
}
d2.dataElementModule().dataElements()
.byUid().`in`(dataElementUids)
val dataSetElementsInDataset =
d2.dataSetModule().dataSets().withDataSetElements()
.uid(dataSetUid)
.blockingGet()
?.dataSetElements()

val dataElements = d2.dataElementModule().dataElements()
.byUid().`in`(dataSetElementsInDataset?.map { it.dataElement().uid() })
.orderByName(RepositoryScope.OrderByDirection.ASC)
.get().toFlowable()
.blockingGet()

Flowable.just(
dataElements.map { transformDataElement(it, dataSetElementsInDataset) }
.filter { it.categoryComboUid() == categoryCombo.uid() },
)
}
}

Expand Down Expand Up @@ -403,6 +385,7 @@ class DataValueRepository(
.blockingIsEmpty()
hasDataValueAuthority && canWriteCatOption && canWriteOrgUnit
}

else -> Flowable.just(false)
}
}
Expand Down Expand Up @@ -490,35 +473,13 @@ class DataValueRepository(
}

fun getDataTableModel(categoryCombo: CategoryCombo): Observable<DataTableModel> {
return Flowable.zip<
List<DataElement>,
Map<String, List<List<Pair<CategoryOption, Category>>>>,
List<DataSetTableModel>,
List<DataElementOperand>,
List<DataElementOperand>,
DataTableModel,
>(
return Flowable.zip(
getDataElements(categoryCombo),
getCatOptions(categoryCombo.uid()),
getDataValues(),
getGreyFields(),
getCompulsoryDataElements(),
) { dataElements: List<DataElement>,
optionsWithCategory: Map<
String,
List<
List<
Pair<
CategoryOption,
Category,
>,
>,
>,
>,
dataValues: List<DataSetTableModel>,
disabledDataElements: List<DataElementOperand>,
compulsoryCells: List<DataElementOperand>,
->
) { dataElements, optionsWithCategory, dataValues, disabledDataElements, compulsoryCells ->
var options: List<List<String>> = ArrayList()
for ((_, value) in optionsWithCategory) {
options = getCatOptionCombos(value, 0, ArrayList(), null)
Expand Down Expand Up @@ -663,7 +624,7 @@ class DataValueRepository(
.byDataElementUid().eq(dataElement.uid())
.byCategoryOptionComboUid().eq(categoryOptionCombo.uid())
.blockingGet()
?.find { it.dataElement() == dataElement.uid() }
.find { it.dataElement() == dataElement.uid() }
?.syncState()

val conflictInField =
Expand All @@ -672,6 +633,7 @@ class DataValueRepository(
State.ERROR,
State.WARNING,
-> true

else -> false
}
}?.filter {
Expand All @@ -685,17 +647,21 @@ class DataValueRepository(
conflictInField != null &&
error != null ->
conflictInField + listOf(error)

valueStateSyncState == State.ERROR && conflictInField != null ->
conflictInField

error != null ->
listOf(error)

else -> null
}

val warningList = when {
valueStateSyncState == State.WARNING &&
conflictInField != null ->
conflictInField

else ->
null
}
Expand Down
19 changes: 19 additions & 0 deletions commons/src/main/java/org/dhis2/commons/date/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.hisp.dhis.android.core.dataset.DataInputPeriod;
import org.hisp.dhis.android.core.event.EventStatus;
import org.hisp.dhis.android.core.period.PeriodType;

Expand Down Expand Up @@ -769,4 +770,22 @@ public static int[] getDifference(Date startDate, Date endDate) {
org.joda.time.Period interval = new org.joda.time.Period(startDate.getTime(), endDate.getTime(), org.joda.time.PeriodType.yearMonthDayTime());
return new int[]{interval.getYears(), interval.getMonths(), interval.getDays()};
}

public Boolean isDataSetExpired(int expiredDays, Date periodInitialDate) {
return Calendar.getInstance().getTime().getTime() > periodInitialDate.getTime() + TimeUnit.DAYS.toMillis(expiredDays);
}

public Boolean isInsideInputPeriod(DataInputPeriod dataInputPeriodModel) {
if (dataInputPeriodModel.openingDate() == null && dataInputPeriodModel.closingDate() != null)
return Calendar.getInstance().getTime().getTime() < dataInputPeriodModel.closingDate().getTime();

if (dataInputPeriodModel.openingDate() != null && dataInputPeriodModel.closingDate() == null)
return dataInputPeriodModel.openingDate().getTime() < Calendar.getInstance().getTime().getTime();

if (dataInputPeriodModel.openingDate() == null && dataInputPeriodModel.closingDate() == null)
return true;

return dataInputPeriodModel.openingDate().getTime() < Calendar.getInstance().getTime().getTime()
&& Calendar.getInstance().getTime().getTime() < dataInputPeriodModel.closingDate().getTime();
}
}
Loading