Skip to content

Commit

Permalink
Merge branch '2.40' into 2.40-DHIS2-18268
Browse files Browse the repository at this point in the history
  • Loading branch information
maikelarabori authored Dec 16, 2024
2 parents 229edc4 + 4945ce1 commit 5e83e7f
Show file tree
Hide file tree
Showing 21 changed files with 305 additions and 4,196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,13 @@ private InternalHibernateGenericStore<?> getStore(Class<? extends IdentifiableOb

private <Y> Predicate buildPredicates(CriteriaBuilder builder, Root<Y> root, Query query) {
Predicate junction = builder.conjunction();
query.getAliases().forEach(alias -> root.join(alias).alias(alias));
if (!query.getCriterions().isEmpty()) {
junction = getJpaJunction(builder, query.getRootJunctionType());
for (org.hisp.dhis.query.Criterion criterion : query.getCriterions()) {
addPredicate(builder, root, junction, criterion);
}
}

query.getAliases().forEach(alias -> root.get(alias).alias(alias));
return junction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Collection;
import java.util.Date;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.criterion.Criterion;
Expand Down Expand Up @@ -86,13 +85,6 @@ public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPa

return builder.equal(builder.size(root.get(queryPath.getPath())), value);
}
if (queryPath.haveAlias()) {
for (Join<Y, ?> join : root.getJoins()) {
if (join.getAlias().equals(queryPath.getAlias()[0])) {
return builder.equal(join.get(queryPath.getProperty().getFieldName()), args.get(0));
}
}
}
return builder.equal(root.get(queryPath.getPath()), args.get(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Collection;
import java.util.Date;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.criterion.Criterion;
Expand Down Expand Up @@ -80,13 +79,6 @@ public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPa
getCollectionArgs().get(0)));
}

if (queryPath.haveAlias()) {
for (Join<Y, ?> join : root.getJoins()) {
if (join.getAlias().equals(queryPath.getAlias()[0])) {
return join.get(queryPath.getProperty().getFieldName()).in(getCollectionArgs().get(0));
}
}
}
return root.get(queryPath.getPath()).in(getCollectionArgs().get(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
package org.hisp.dhis.query.operators;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.hibernate.criterion.Criterion;
Expand Down Expand Up @@ -65,13 +64,6 @@ public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPa

return builder.notEqual(builder.size(root.get(queryPath.getPath())), value);
}
if (queryPath.haveAlias()) {
for (Join<Y, ?> join : root.getJoins()) {
if (join.getAlias().equals(queryPath.getAlias()[0])) {
return builder.equal(join.get(queryPath.getProperty().getFieldName()), args.get(0));
}
}
}
return builder.notEqual(root.get(queryPath.getPath()), args.get(0));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,35 @@ public Path<?> getQueryPath(Root<?> root, Schema schema, String path) {
*/
private Query getQuery(Query query, boolean persistedOnly) {
Query pQuery = Query.from(query.getSchema(), query.getRootJunctionType());
Iterator<Criterion> criterions = query.getCriterions().iterator();
Iterator<Criterion> iterator = query.getCriterions().iterator();

while (criterions.hasNext()) {
Criterion criterion = criterions.next();
if (addJunction(criterion, pQuery, persistedOnly)
|| addRestriction(query, criterion, pQuery)) {
criterions.remove();
while (iterator.hasNext()) {
Criterion criterion = iterator.next();

if (criterion instanceof Junction) {
Junction junction = handleJunction(pQuery, (Junction) criterion, persistedOnly);

if (!junction.getCriterions().isEmpty()) {
pQuery.getAliases().addAll(junction.getAliases());
pQuery.add(junction);
}

if (((Junction) criterion).getCriterions().isEmpty()) {
iterator.remove();
}
} else if (criterion instanceof Restriction) {
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));

if (restriction.getQueryPath().isPersisted()
&& !restriction.getQueryPath().haveAlias()
&& !Attribute.ObjectType.isValidType(restriction.getQueryPath().getPath())) {
pQuery
.getAliases()
.addAll(Arrays.asList(((Restriction) criterion).getQueryPath().getAlias()));
pQuery.getCriterions().add(criterion);
iterator.remove();
}
}
}

Expand Down Expand Up @@ -224,12 +246,12 @@ private Junction handleJunction(Query query, Junction queryJunction, boolean per
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));

if (restriction.getQueryPath().isPersisted() && !isAttributeFilter(query, restriction)) {
if (restriction.getQueryPath().haveAlias()) {
criteriaJunction
.getAliases()
.addAll(Arrays.asList(restriction.getQueryPath().getAlias()));
}
if (restriction.getQueryPath().isPersisted()
&& !restriction.getQueryPath().haveAlias(1)
&& !Attribute.ObjectType.isValidType(restriction.getQueryPath().getPath())) {
criteriaJunction
.getAliases()
.addAll(Arrays.asList(((Restriction) criterion).getQueryPath().getAlias()));
criteriaJunction.getCriterions().add(criterion);
iterator.remove();
} else if (persistedOnly) {
Expand All @@ -247,58 +269,4 @@ private Junction handleJunction(Query query, Junction queryJunction, boolean per
private boolean isFilterByAttributeId(Property curProperty, String propertyName) {
return curProperty == null && CodeGenerator.isValidUid(propertyName);
}

/**
* Handle attribute filter such as /attributes?fields=id,name&filter=userAttribute:eq:true
*
* @return true if attribute filter
*/
private boolean isAttributeFilter(Query query, Restriction restriction) {
return query.getSchema().getKlass().isAssignableFrom(Attribute.class)
&& Attribute.ObjectType.isValidType(restriction.getQueryPath().getPath());
}

/**
* Add given criterion which is an instance of {@link Junction} to given {@link Query} pQuery. If
* successfully added, return TRUE indicating that the given {@link Criterion} criterion should be
* removed.
*/
private boolean addJunction(Criterion criterion, Query pQuery, boolean persistedOnly) {
if (!(criterion instanceof Junction)) {
return false;
}
boolean shouldRemove = false;
Junction junction = handleJunction(pQuery, (Junction) criterion, persistedOnly);
if (!junction.getCriterions().isEmpty()) {
pQuery.getAliases().addAll(junction.getAliases());
pQuery.add(junction);
}
if (((Junction) criterion).getCriterions().isEmpty()) {
shouldRemove = true;
}
return shouldRemove;
}

/**
* Add given criterion which is an instance of {@link Restriction} to given {@link Query} pQuery.
* If successfully added, return TRUE indicating that the given {@link Criterion} criterion should
* be removed.
*/
private boolean addRestriction(Query query, Criterion criterion, Query pQuery) {
if (!(criterion instanceof Restriction)) {
return false;
}
boolean shouldRemove = false;
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));

if (restriction.getQueryPath().isPersisted() && !isAttributeFilter(query, restriction)) {
pQuery.getCriterions().add(criterion);
shouldRemove = true;
if (restriction.getQueryPath().haveAlias()) {
pQuery.getAliases().addAll(Arrays.asList(restriction.getQueryPath().getAlias()));
}
}
return shouldRemove;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.hisp.dhis.external.conf.ConfigurationKey.LINKED_ACCOUNTS_ENABLED;
import static org.hisp.dhis.external.conf.ConfigurationKey.LINKED_ACCOUNTS_LOGOUT_URL;
import static org.hisp.dhis.external.conf.ConfigurationKey.LINKED_ACCOUNTS_RELOGIN_URL;
import static org.hisp.dhis.external.conf.ConfigurationKey.OIDC_LOGOUT_REDIRECT_URL;
import static org.hisp.dhis.external.conf.ConfigurationKey.OIDC_OAUTH2_LOGIN_ENABLED;
Expand Down Expand Up @@ -101,13 +102,22 @@ private void handleLinkedAccountsLogout(
HttpServletRequest request, HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {

String currentUsername = request.getParameter("current");
String usernameToSwitchTo = request.getParameter("switch");
String linkedAccountsLogoutUrl = config.getProperty(LINKED_ACCOUNTS_LOGOUT_URL);
if (isNullOrEmpty(linkedAccountsLogoutUrl)) {
// Fallback if not defined in config
linkedAccountsLogoutUrl = "/";
}

if (isNullOrEmpty(currentUsername) || isNullOrEmpty(usernameToSwitchTo)) {
setOidcLogoutUrl();
if (isNullOrEmpty(usernameToSwitchTo)) {
// No switch parameter present: redirect to linked_accounts.logout_url
this.handler.setDefaultTargetUrl(linkedAccountsLogoutUrl);
} else {
userStore.setActiveLinkedAccounts(currentUsername, usernameToSwitchTo);
// switch parameter present: switch accounts and then redirect to re-login URL
String currentUsername = request.getParameter("current");
if (!isNullOrEmpty(currentUsername)) {
userStore.setActiveLinkedAccounts(currentUsername, usernameToSwitchTo);
}
this.handler.setDefaultTargetUrl(config.getProperty(LINKED_ACCOUNTS_RELOGIN_URL));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ public Event getEvent(
if (eventParams.isIncludeRelationships()) {
event.setRelationships(
programStageInstance.getRelationshipItems().stream()
.filter(Objects::nonNull)
.filter(ri -> Objects.nonNull(ri) && !ri.getRelationship().isDeleted())
.map(
r ->
relationshipService.findRelationship(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1287,25 +1287,13 @@ private StringBuilder getFromWhereClause(
if (params.getStartDate() != null) {
mapSqlParameterSource.addValue("startDate", params.getStartDate(), Types.TIMESTAMP);

fromBuilder
.append(hlp.whereAnd())
.append(" (psi.executiondate >= ")
.append(":startDate")
.append(" or (psi.executiondate is null and psi.duedate >= ")
.append(":startDate")
.append(" )) ");
fromBuilder.append(hlp.whereAnd()).append(" psi.executiondate >= :startDate ");
}

if (params.getEndDate() != null) {
mapSqlParameterSource.addValue("endDate", params.getEndDate(), Types.TIMESTAMP);

fromBuilder
.append(hlp.whereAnd())
.append(" (psi.executiondate < ")
.append(":endDate")
.append(" or (psi.executiondate is null and psi.duedate < ")
.append(":endDate")
.append(" )) ");
fromBuilder.append(hlp.whereAnd()).append(" psi.executiondate < :endDate ");
}

if (params.getProgramType() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public void process(Event event, WorkContext ctx) {
if (dataElement.isFileType()) {
FileResource fileResource = fileResourceService.getFileResource(dataValue.getValue());

if (!fileResource.isAssigned() || fileResource.getFileResourceOwner() == null) {
if (fileResource != null
&& (!fileResource.isAssigned() || fileResource.getFileResourceOwner() == null)) {
fileResource.setAssigned(true);
fileResource.setFileResourceOwner(event.getEvent());
fileResourceService.updateFileResource(fileResource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,6 @@ private ProgramStageInstance from(
assignedUser.ifPresent(programStageInstance::setAssignedUser);
}

if (program.isRegistration()
&& programStageInstance.getDueDate() == null
&& programStageInstance.getExecutionDate() != null) {
programStageInstance.setDueDate(programStageInstance.getExecutionDate());
}

for (DataValue dataValue : event.getDataValues()) {
EventDataValue eventDataValue = new EventDataValue();
eventDataValue.setValue(dataValue.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,18 @@ public void validate(Reporter reporter, TrackerBundle bundle, Event event) {
organisationUnit = bundle.getPreheat().getOrganisationUnit(event.getOrgUnit());
}

// If event is newly created, or going to be deleted, capture scope
// has to be checked
if (program.isWithoutRegistration() || strategy.isCreate() || strategy.isDelete()) {
if (organisationUnit == null) {
log.warn(ORG_UNIT_NO_USER_ASSIGNED, event.getEvent());
} else {
checkOrgUnitInCaptureScope(reporter, bundle, event, organisationUnit);
checkEventOrgUnitWriteAccess(
reporter,
event,
organisationUnit,
strategy.isCreate()
? event.isCreatableInSearchScope()
: bundle.getPreheat().getEvent(event.getUid()).isCreatableInSearchScope(),
bundle.getUser());
}
}
String teUid = getTeUidFromEvent(bundle, event, program);
Expand Down Expand Up @@ -253,18 +258,6 @@ public boolean needsToRun(TrackerImportStrategy strategy) {
return true;
}

private void checkOrgUnitInCaptureScope(
Reporter reporter, TrackerBundle bundle, TrackerDto dto, OrganisationUnit orgUnit) {
User user = bundle.getUser();

checkNotNull(user, USER_CANT_BE_NULL);
checkNotNull(orgUnit, ORGANISATION_UNIT_CANT_BE_NULL);

if (!organisationUnitService.isInUserHierarchyCached(user, orgUnit)) {
reporter.addError(dto, ValidationCode.E1000, user, orgUnit);
}
}

private void checkTeiTypeAndTeiProgramAccess(
Reporter reporter,
TrackerDto dto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ public enum ConfigurationKey {
LINKED_ACCOUNTS_ENABLED("linked_accounts.enabled", Constants.OFF, false),

LINKED_ACCOUNTS_RELOGIN_URL("linked_accounts.relogin_url", "", false),

LINKED_ACCOUNTS_LOGOUT_URL("linked_accounts.logout_url", "", false),

SWITCH_USER_FEATURE_ENABLED("switch_user_feature.enabled", Constants.OFF, false),
SWITCH_USER_ALLOW_LISTED_IPS(
"switch_user_allow_listed_ips", "localhost,127.0.0.1,[0:0:0:0:0:0:0:1]", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public void query4VisitsByFacilityTypeLastYear() throws JSONException {

// Assert metaData.
String expectedMetaData =
"{\"items\":{\"RXL3lPSK8oG\":{\"name\":\"Clinic\"},\"J5jldMd8OHv\":{\"name\":\"Facility Type\"},\"ou\":{\"name\":\"Organisation unit\"},\"CXw2yu5fodb\":{\"name\":\"CHC\"},\"THIS_YEAR\":{\"name\":\"This year\"},\"2022\":{\"name\":\"2022\"},\"uYxK4wmcPqA\":{\"name\":\"CHP\"},\"ImspTQPwCqd\":{\"name\":\"Sierra Leone\"},\"tDZVQ1WtwpA\":{\"name\":\"Hospital\"},\"hfdmMSPBgLG\":{\"name\":\"ANC 4th or more visits\"},\"dx\":{\"name\":\"Data\"},\"pe\":{\"name\":\"Period\"},\"pq2XI5kz2BY\":{\"name\":\"Fixed\"},\"PT59n8BQbqM\":{\"name\":\"Outreach\"},\"EYbopBOJWsW\":{\"name\":\"MCHP\"}},\"dimensions\":{\"dx\":[\"hfdmMSPBgLG\"],\"pe\":[\"2022\"],\"J5jldMd8OHv\":[\"uYxK4wmcPqA\",\"tDZVQ1WtwpA\",\"EYbopBOJWsW\",\"RXL3lPSK8oG\",\"CXw2yu5fodb\"],\"ou\":[\"ImspTQPwCqd\"],\"co\":[\"pq2XI5kz2BY\",\"PT59n8BQbqM\"]}}";
"{\"items\":{\"RXL3lPSK8oG\":{\"name\":\"Clinic\"},\"J5jldMd8OHv\":{\"name\":\"Facility Type\"},\"ou\":{},\"CXw2yu5fodb\":{\"name\":\"CHC\"},\"THIS_YEAR\":{\"name\":\"This year\"},\"2022\":{\"name\":\"2022\"},\"uYxK4wmcPqA\":{\"name\":\"CHP\"},\"ImspTQPwCqd\":{\"name\":\"Sierra Leone\"},\"tDZVQ1WtwpA\":{\"name\":\"Hospital\"},\"hfdmMSPBgLG\":{\"name\":\"ANC 4th or more visits\"},\"dx\":{\"name\":\"Data\"},\"pe\":{\"name\":\"Period\"},\"pq2XI5kz2BY\":{\"name\":\"Fixed\"},\"PT59n8BQbqM\":{\"name\":\"Outreach\"},\"EYbopBOJWsW\":{\"name\":\"MCHP\"}},\"dimensions\":{\"dx\":[\"hfdmMSPBgLG\"],\"pe\":[\"2022\"],\"J5jldMd8OHv\":[\"uYxK4wmcPqA\",\"tDZVQ1WtwpA\",\"EYbopBOJWsW\",\"RXL3lPSK8oG\",\"CXw2yu5fodb\"],\"ou\":[\"ImspTQPwCqd\"],\"co\":[\"pq2XI5kz2BY\",\"PT59n8BQbqM\"]}}";
String actualMetaData = new JSONObject((Map) response.extract("metaData")).toString();
assertEquals(expectedMetaData, actualMetaData, false);

Expand Down
Loading

0 comments on commit 5e83e7f

Please sign in to comment.