Skip to content

Commit

Permalink
fix: Order of relative periods in analytics models [DHIS2-18020] (#19253
Browse files Browse the repository at this point in the history
)
  • Loading branch information
maikelarabori authored Nov 21, 2024
1 parent 06ad47a commit 226fa11
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSetDimension;
import org.hisp.dhis.period.ConfigurablePeriod;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.RelativePeriodEnum;
import org.hisp.dhis.period.RelativePeriods;
Expand Down Expand Up @@ -139,6 +138,8 @@ public abstract class BaseAnalyticalObject extends BaseNameableObject implements

protected RelativePeriods relatives;

protected List<String> rawRelativePeriods = new ArrayList<>();

protected List<DataElementGroupSetDimension> dataElementGroupSetDimensions = new ArrayList<>();

protected List<OrganisationUnitGroupSetDimension> organisationUnitGroupSetDimensions =
Expand Down Expand Up @@ -250,7 +251,7 @@ public boolean hasUserOrgUnit() {
}

public boolean hasRelativePeriods() {
return relatives != null && !relatives.isEmpty();
return rawRelativePeriods != null && !rawRelativePeriods.isEmpty();
}

public boolean hasOrganisationUnitLevels() {
Expand Down Expand Up @@ -515,15 +516,13 @@ protected DimensionalObject getDimensionalObject(
* @return the dimensional object related to the given dimension and attribute.
*/
protected DimensionalObject getDimensionalObject(
final EventAnalyticalObject eventAnalyticalObject,
final String dimension,
final Attribute parent) {
final Optional<DimensionalObject> optionalDimensionalObject = getDimensionalObject(dimension);
EventAnalyticalObject eventAnalyticalObject, String dimension, Attribute parent) {
Optional<DimensionalObject> optionalDimensionalObject = getDimensionalObject(dimension);

if (optionalDimensionalObject.isPresent()) {
return linkAssociations(eventAnalyticalObject, optionalDimensionalObject.get(), parent);
} else if (Type.contains(dimension)) {
final DimensionalObject dimensionalObject =
DimensionalObject dimensionalObject =
new SimpleDimensionHandler(eventAnalyticalObject).getDimensionalObject(dimension, parent);

return linkAssociations(eventAnalyticalObject, dimensionalObject, parent);
Expand All @@ -542,14 +541,14 @@ protected DimensionalObject getDimensionalObject(
* @return the dimensional object containing the correct associations.
*/
private DimensionalObject linkAssociations(
final EventAnalyticalObject eventAnalyticalObject,
final DimensionalObject dimensionalObject,
final Attribute parent) {
EventAnalyticalObject eventAnalyticalObject,
DimensionalObject dimensionalObject,
Attribute parent) {
// Associating event repetitions.
final List<EventRepetition> repetitions = eventAnalyticalObject.getEventRepetitions();
List<EventRepetition> repetitions = eventAnalyticalObject.getEventRepetitions();

if (isNotEmpty(repetitions)) {
for (final EventRepetition eventRepetition : repetitions) {
for (EventRepetition eventRepetition : repetitions) {
if (eventRepetition.getDimension() != null
&& eventRepetition.getDimension().equals(dimensionalObject.getDimension())
&& parent == eventRepetition.getParent()) {
Expand All @@ -568,11 +567,11 @@ private DimensionalObject linkAssociations(
* @param dimensionalObjects
*/
protected void populateDimensions(
final List<String> dimensions, final List<DimensionalObject> dimensionalObjects) {
List<String> dimensions, List<DimensionalObject> dimensionalObjects) {
if (isNotEmpty(dimensions)) {
for (final String dimension : dimensions) {
for (String dimension : dimensions) {
if (isNotBlank(dimension)) {
final Optional<DimensionalObject> dimensionalObject = getDimensionalObject(dimension);
Optional<DimensionalObject> dimensionalObject = getDimensionalObject(dimension);
if (dimensionalObject.isPresent()) {
dimensionalObjects.add(dimensionalObject.get());
}
Expand All @@ -589,12 +588,12 @@ protected void populateDimensions(
* @param dimensionalObjects
*/
protected void populateDimensions(
final List<String> dimensions,
final List<DimensionalObject> dimensionalObjects,
final Attribute attribute,
final EventAnalyticalObject eventAnalyticalObject) {
List<String> dimensions,
List<DimensionalObject> dimensionalObjects,
Attribute attribute,
EventAnalyticalObject eventAnalyticalObject) {
if (isNotEmpty(dimensions)) {
for (final String dimension : dimensions) {
for (String dimension : dimensions) {
if (isNotBlank(dimension)) {
dimensionalObjects.add(getDimensionalObject(eventAnalyticalObject, dimension, attribute));
}
Expand Down Expand Up @@ -625,10 +624,8 @@ protected Optional<DimensionalObject> getDimensionalObject(String dimension) {
List<Period> periodList = new ArrayList<>(periods);

if (hasRelativePeriods()) {
List<RelativePeriodEnum> list = relatives.getRelativePeriodEnums();

for (RelativePeriodEnum periodEnum : list) {
periodList.add(new ConfigurablePeriod(periodEnum.toString()));
for (String relPeriod : rawRelativePeriods) {
periodList.add(new Period(RelativePeriodEnum.valueOf(relPeriod)));
}
}

Expand Down Expand Up @@ -718,12 +715,13 @@ private Optional<DimensionalObject> getTrackedEntityDimension(String dimension)
.collect(toMap(TrackedEntityAttributeDimension::getUid, Function.identity()));

if (attributes.containsKey(dimension)) {
final TrackedEntityAttributeDimension tead = attributes.get(dimension);
TrackedEntityAttributeDimension tead = attributes.get(dimension);

if (tead != null) {
final ValueType valueType =
ValueType valueType =
tead.getAttribute() != null ? tead.getAttribute().getValueType() : null;
final OptionSet optionSet =

OptionSet optionSet =
tead.getAttribute() != null ? tead.getAttribute().getOptionSet() : null;

return Optional.of(
Expand Down Expand Up @@ -777,7 +775,7 @@ private Optional<DimensionalObject> getTrackedEntityDimension(String dimension)
.collect(toMap(TrackedEntityProgramIndicatorDimension::getUid, Function.identity()));

if (programIndicators.containsKey(dimension)) {
final TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);
TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);

return Optional.of(
new BaseDimensionalObject(
Expand Down Expand Up @@ -805,11 +803,11 @@ private Optional<DimensionalObject> getTrackedEntityDimension(String dimension)
private <T extends DimensionalEmbeddedObject>
Optional<DimensionalObject> getDimensionFromEmbeddedObjects(
String dimension, DimensionType dimensionType, List<T> embeddedObjects) {
final Map<String, T> dimensions =
Map<String, T> dimensions =
Maps.uniqueIndex(embeddedObjects, d -> d.getDimension().getDimension());

if (dimensions.containsKey(dimension)) {
final DimensionalEmbeddedObject object = dimensions.get(dimension);
DimensionalEmbeddedObject object = dimensions.get(dimension);

if (object != null) {
return Optional.of(
Expand All @@ -835,7 +833,7 @@ private void setPeriodNames(List<Period> periods, boolean dynamicNames, I18nForm
* dynamic dimensions.
*/
public Map<String, String> getMetaData() {
final Map<String, String> meta = new HashMap<>();
Map<String, String> meta = new HashMap<>();

// TODO use getDimension() instead of getUid() ?
dataElementGroupSetDimensions.forEach(
Expand Down Expand Up @@ -934,13 +932,39 @@ public void setEndDate(Date endDate) {
@JsonProperty(value = "relativePeriods")
@JacksonXmlProperty(localName = "relativePeriods", namespace = DxfNamespaces.DXF_2_0)
public RelativePeriods getRelatives() {
if (relatives == null) {
List<RelativePeriodEnum> enums = new ArrayList<>();

if (rawRelativePeriods != null) {
for (String relativePeriod : rawRelativePeriods) {
if (RelativePeriodEnum.contains(relativePeriod)) {
enums.add(RelativePeriodEnum.valueOf(relativePeriod));
}
}
}

return new RelativePeriods().setRelativePeriodsFromEnums(enums);
}

return relatives;
}

public void setRelatives(RelativePeriods relatives) {
this.relatives = relatives;
}

@JsonProperty
@JsonIgnore
@JacksonXmlElementWrapper(localName = "rawRelativePeriods", namespace = DxfNamespaces.DXF_2_0)
@JacksonXmlProperty(localName = "rawRelativePeriods", namespace = DxfNamespaces.DXF_2_0)
public List<String> getRawRelativePeriods() {
return rawRelativePeriods;
}

public void setRawRelativePeriods(List<String> rawRelativePeriods) {
this.rawRelativePeriods = rawRelativePeriods;
}

@JsonProperty
@JacksonXmlElementWrapper(
localName = "dataElementGroupSetDimensions",
Expand Down
14 changes: 14 additions & 0 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/Period.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ public Period(Period period) {
this.dateField = period.getDateField();
}

/**
* Creates a period that is not bound to the persistent layer. It represents a detached Period
* that is mainly used for displaying purposes.
*
* @param isoRelativePeriod the ISO relative period
*/
public Period(RelativePeriodEnum isoRelativePeriod) {
this.isoPeriod = isoRelativePeriod.toString();
this.name = isoRelativePeriod.toString();
this.code = isoRelativePeriod.toString();
this.setStartDate(new Date());
this.setEndDate(new Date());
}

protected Period(PeriodType periodType, Date startDate, Date endDate) {
this.periodType = periodType;
this.startDate = startDate;
Expand Down
46 changes: 45 additions & 1 deletion dhis-2/dhis-api/src/main/java/org/hisp/dhis/report/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.util.ArrayList;
import java.util.List;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.MetadataObject;
import org.hisp.dhis.common.cache.CacheStrategy;
import org.hisp.dhis.common.cache.Cacheable;
import org.hisp.dhis.period.RelativePeriodEnum;
import org.hisp.dhis.period.RelativePeriods;
import org.hisp.dhis.schema.annotation.Gist;
import org.hisp.dhis.schema.annotation.Gist.Include;
Expand All @@ -56,6 +59,8 @@ public class Report extends BaseIdentifiableObject implements Cacheable, Metadat

private RelativePeriods relatives;

private List<String> rawRelativePeriods = new ArrayList<>();

private ReportingParams reportingParams;

private CacheStrategy cacheStrategy = CacheStrategy.RESPECT_SYSTEM_SETTING;
Expand Down Expand Up @@ -155,11 +160,50 @@ public void setVisualization(Visualization visualization) {
@JsonProperty("relativePeriods")
@JacksonXmlProperty(namespace = DXF_2_0)
public RelativePeriods getRelatives() {
if (relatives == null) {
List<RelativePeriodEnum> enums = new ArrayList<>();

if (rawRelativePeriods != null) {
for (String relativePeriod : rawRelativePeriods) {
if (RelativePeriodEnum.contains(relativePeriod)) {
enums.add(RelativePeriodEnum.valueOf(relativePeriod));
}
}
}

return new RelativePeriods().setRelativePeriodsFromEnums(enums);
}

return relatives;
}

/**
* It overrides the rawRelativePeriods with the relative periods provided. This is done for
* backward compatibility reasons.
*
* @param relatives the {@link RelativePeriods}.
*/
public void setRelatives(RelativePeriods relatives) {
this.relatives = relatives;
if (relatives != null) {
List<RelativePeriodEnum> enums = relatives.getRelativePeriodEnums();

for (RelativePeriodEnum periodEnum : enums) {
String relativePeriod = periodEnum.name();
if (RelativePeriodEnum.contains(relativePeriod)) {
this.rawRelativePeriods.add(relativePeriod);
}
}

this.relatives = relatives;
}
}

public List<String> getRawRelativePeriods() {
return rawRelativePeriods;
}

public void setRawRelativePeriods(List<String> rawRelativePeriods) {
this.rawRelativePeriods = rawRelativePeriods;
}

@JsonProperty
Expand Down
95 changes: 95 additions & 0 deletions dhis-2/dhis-api/src/test/java/org/hisp/dhis/report/ReportTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.report;

import static org.hisp.dhis.period.RelativePeriodEnum.BIMONTHS_THIS_YEAR;
import static org.hisp.dhis.period.RelativePeriodEnum.LAST_14_DAYS;
import static org.hisp.dhis.period.RelativePeriodEnum.LAST_3_MONTHS;
import static org.hisp.dhis.period.RelativePeriodEnum.LAST_7_DAYS;
import static org.hisp.dhis.period.RelativePeriodEnum.THIS_BIWEEK;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import org.hisp.dhis.period.RelativePeriods;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link Report}. */
class ReportTest {
@Test
void testGetRelatives() {
// Given
List<String> rawRelativePeriods =
List.of(BIMONTHS_THIS_YEAR.name(), THIS_BIWEEK.name(), LAST_7_DAYS.name());
Report report = new Report();
report.setRawRelativePeriods(rawRelativePeriods);
// When
RelativePeriods relativePeriods = report.getRelatives();
// Then
assertTrue(relativePeriods.isBiMonthsThisYear());
assertTrue(relativePeriods.isThisBiWeek());
assertTrue(relativePeriods.isLast7Days());
}

@Test
void testGetRelativesWhenRawRelativePeriodsIsNull() {
// Given
Report report = new Report();
report.setRawRelativePeriods(null);
// When
RelativePeriods relativePeriods = report.getRelatives();
// Then
assertTrue(relativePeriods.isEmpty());
}

@Test
void testGetRelativesWhenRawRelativePeriodsIsEmpty() {
// Given
Report report = new Report();
report.setRawRelativePeriods(List.of());
// When
RelativePeriods relativePeriods = report.getRelatives();
// Then
assertTrue(relativePeriods.isEmpty());
}

@Test
void testSetRelatives() {
// Given
RelativePeriods relativePeriods = new RelativePeriods();
relativePeriods.setBiMonthsThisYear(true);
relativePeriods.setLast14Days(true);
relativePeriods.setLast3Months(true);
Report report = new Report();
// When
report.setRelatives(relativePeriods);
// Then
assertTrue(report.getRawRelativePeriods().contains(BIMONTHS_THIS_YEAR.name()));
assertTrue(report.getRawRelativePeriods().contains(LAST_14_DAYS.name()));
assertTrue(report.getRawRelativePeriods().contains(LAST_3_MONTHS.name()));
}
}
Loading

0 comments on commit 226fa11

Please sign in to comment.