Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/hibernate-schema-validate-41' in…
Browse files Browse the repository at this point in the history
…to hibernate-schema-validate-41
  • Loading branch information
vietnguyen committed Nov 24, 2024
2 parents 31eb63a + 6a10d64 commit d17f207
Show file tree
Hide file tree
Showing 29 changed files with 809 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ public abstract class BaseAnalyticalObject extends BaseNameableObject implements

protected RelativePeriods relatives;

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

protected int sortOrder;

protected int topLimit;
Expand Down Expand Up @@ -329,7 +331,7 @@ public boolean hasUserOrgUnit() {
}

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

public boolean hasOrganisationUnitLevels() {
Expand Down Expand Up @@ -681,10 +683,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 Period(periodEnum));
for (String relPeriod : rawRelativePeriods) {
periodList.add(new Period(RelativePeriodEnum.valueOf(relPeriod)));
}
}

Expand Down Expand Up @@ -1262,13 +1262,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
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ public boolean isSingleProgramStage() {
return programStages != null && programStages.size() == 1;
}

public boolean hasMaxTeiCountToReturn() {
return maxTeiCountToReturn > 0;
}

@Override
public int increaseVersion() {
return ++version;
Expand Down
47 changes: 46 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,51 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2004-2022, 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.user;

/**
* @author Lars Helge Overland
*/
public enum UserOrgUnitProperty {
ORG_UNITS("organisationUnits"),
DATA_VIEW_ORG_UNITS("dataViewOrganisationUnits"),
TEI_SEARCH_ORG_UNITS("teiSearchOrganisationUnits");

private final String value;

UserOrgUnitProperty(String value) {
this.value = value;
}

public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class UserQueryParams {

@ToString.Include private UserInvitationStatus invitationStatus;

/** If empty, no matching condition will be generated */
private Set<OrganisationUnit> organisationUnits = new HashSet<>();

private Set<UserGroup> userGroups = new HashSet<>();
Expand Down
12 changes: 12 additions & 0 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.hisp.dhis.common.IdentifiableObject;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.feedback.ErrorReport;
import org.hisp.dhis.feedback.NotFoundException;
import org.hisp.dhis.organisationunit.OrganisationUnit;

/**
* @author Chau Thu Tran
Expand Down Expand Up @@ -861,4 +863,14 @@ boolean canCurrentUserCanModify(
boolean canDataRead(IdentifiableObject identifiableObject);

CurrentUserGroupInfo getCurrentUserGroupInfo(String userUID);

/**
* Method that retrieves all {@link User}s that have an entry for the {@link OrganisationUnit} in
* the given table
*
* @param orgUnitProperty {@link UserOrgUnitProperty} used to search
* @param uid {@link OrganisationUnit} {@link UID} to match on
* @return matching {@link User}s
*/
List<User> getUsersWithOrgUnit(@Nonnull UserOrgUnitProperty orgUnitProperty, @Nonnull UID uid);
}
12 changes: 12 additions & 0 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.hisp.dhis.common.IdentifiableObjectStore;
import org.hisp.dhis.common.UID;
import org.hisp.dhis.organisationunit.OrganisationUnit;

/**
* @author Nguyen Hong Duc
Expand Down Expand Up @@ -218,4 +220,14 @@ Map<String, Optional<Locale>> findNotifiableUsersWithPasswordLastUpdatedBetween(
* @param activeUsername the username of the user to set as active
*/
void setActiveLinkedAccounts(@Nonnull String actingUser, @Nonnull String activeUsername);

/**
* Retrieves all {@link User}s that have an entry for the {@link OrganisationUnit} in the given
* table
*
* @param orgUnitProperty {@link UserOrgUnitProperty} used to search
* @param uid {@link OrganisationUnit} {@link UID} to match on
* @return matching {@link User}s
*/
List<User> getUsersWithOrgUnit(@Nonnull UserOrgUnitProperty orgUnitProperty, @Nonnull UID uid);
}
105 changes: 105 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,105 @@
/*
* 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 d17f207

Please sign in to comment.