Skip to content

Commit

Permalink
check systemLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyen committed Nov 17, 2023
1 parent 8327350 commit bc80840
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,14 @@
*/
package org.hisp.dhis.query.operators;

import static org.hisp.dhis.user.UserSettingKey.DB_LOCALE;
import static org.hisp.dhis.user.UserSettingKey.UI_LOCALE;

import java.util.Locale;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.ObjectUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.hisp.dhis.hibernate.jsonb.type.JsonbFunctions;
import org.hisp.dhis.query.Typed;
import org.hisp.dhis.query.planner.QueryPath;
import org.hisp.dhis.user.CurrentUserUtil;

/**
* @author Henning Håkonsen
Expand Down Expand Up @@ -76,10 +70,8 @@ public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPa
root.get(queryPath.getPath()),
builder.literal(TokenUtils.createRegex(value).toString())),
true);
Locale currentLocale =
ObjectUtils.defaultIfNull(
CurrentUserUtil.getUserSetting(DB_LOCALE), CurrentUserUtil.getUserSetting(UI_LOCALE));
if (currentLocale == null

if (queryPath.getLocale() == null
|| !queryPath.getProperty().isTranslatable()
|| queryPath.getProperty().getTranslationKey() == null) {
return defaultSearch;
Expand All @@ -90,7 +82,7 @@ public <Y> Predicate getPredicate(CriteriaBuilder builder, Root<Y> root, QueryPa
Boolean.class,
root.get("translations"),
builder.literal("{" + queryPath.getProperty().getTranslationKey() + "}"),
builder.literal(currentLocale.getLanguage()),
builder.literal(queryPath.getLocale().getLanguage()),
builder.literal(TokenUtils.createRegex(value).toString())),
true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,27 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.attribute.Attribute;
import org.hisp.dhis.common.CodeGenerator;
import org.hisp.dhis.i18n.locale.LocaleManager;
import org.hisp.dhis.query.Conjunction;
import org.hisp.dhis.query.Criterion;
import org.hisp.dhis.query.Disjunction;
import org.hisp.dhis.query.Junction;
import org.hisp.dhis.query.Query;
import org.hisp.dhis.query.Restriction;
import org.hisp.dhis.query.operators.TokenOperator;
import org.hisp.dhis.schema.Property;
import org.hisp.dhis.schema.Schema;
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.setting.SettingKey;
import org.hisp.dhis.setting.SystemSettingManager;
import org.hisp.dhis.user.CurrentUserUtil;
import org.hisp.dhis.user.UserSettingKey;
import org.springframework.stereotype.Component;

/**
Expand All @@ -54,6 +61,7 @@
@RequiredArgsConstructor
public class DefaultQueryPlanner implements QueryPlanner {
private final SchemaService schemaService;
private final SystemSettingManager systemSettingManager;

@Override
public QueryPlan planQuery(Query query) {
Expand Down Expand Up @@ -202,6 +210,10 @@ private Query getQuery(Query query, boolean persistedOnly) {
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));

if (restriction.getOperator().getClass().isAssignableFrom(TokenOperator.class)) {
setQueryPathLocale(restriction);
}

if (restriction.getQueryPath().isPersisted()
&& !restriction.getQueryPath().haveAlias()
&& !Attribute.ObjectType.isValidType(restriction.getQueryPath().getPath())) {
Expand Down Expand Up @@ -247,6 +259,10 @@ private Junction handleJunction(Query query, Junction queryJunction, boolean per
Restriction restriction = (Restriction) criterion;
restriction.setQueryPath(getQueryPath(query.getSchema(), restriction.getPath()));

if (restriction.getOperator().getClass().isAssignableFrom(TokenOperator.class)) {
setQueryPathLocale(restriction);
}

if (restriction.getQueryPath().isPersisted()
&& !restriction.getQueryPath().haveAlias(1)
&& !Attribute.ObjectType.isValidType(restriction.getQueryPath().getPath())) {
Expand All @@ -270,4 +286,17 @@ private Junction handleJunction(Query query, Junction queryJunction, boolean per
private boolean isFilterByAttributeId(Property curProperty, String propertyName) {
return curProperty == null && CodeGenerator.isValidUid(propertyName);
}

private void setQueryPathLocale(Restriction restriction) {
Locale systemLocale =
systemSettingManager.getSystemSetting(SettingKey.DB_LOCALE, LocaleManager.DEFAULT_LOCALE);
Locale currentUserLocale = CurrentUserUtil.getUserSetting(UserSettingKey.DB_LOCALE);
if (currentUserLocale != null && !currentUserLocale.equals(systemLocale)) {
// Use translations jsonb column for querying with the current user locale.
restriction.getQueryPath().setLocale(currentUserLocale);
} else {
// Use default properties for querying. Don't use the translations jsonb column.
restriction.getQueryPath().setLocale(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@
import com.google.common.base.Joiner;
import com.google.common.base.MoreObjects;
import java.util.Arrays;
import lombok.AllArgsConstructor;
import java.util.Locale;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.schema.Property;

/**
* @author Morten Olav Hansen <[email protected]>
*/
@Getter
@AllArgsConstructor
@RequiredArgsConstructor
public class QueryPath {
private final Property property;

Expand All @@ -48,6 +49,12 @@ public class QueryPath {

private static final Joiner PATH_JOINER = Joiner.on(".");

/**
* If this locale is not null then the query must use the translations jsonb column instead of
* default properties.
*/
private Locale locale;

public QueryPath(Property property, boolean persisted) {
this(property, persisted, new String[0]);
}
Expand All @@ -70,6 +77,14 @@ public boolean haveAlias(int n) {
return alias != null && alias.length > n;
}

public void setLocale(Locale locale) {
this.locale = locale;
}

public Locale getLocale() {
return locale;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.hisp.dhis.query.planner.QueryPlanner;
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.schema.descriptors.OrganisationUnitSchemaDescriptor;
import org.hisp.dhis.setting.SystemSettingManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -65,9 +66,11 @@ class DefaultQueryServiceTest {

@Mock private SchemaService schemaService;

@Mock private SystemSettingManager systemSettingManager;

@BeforeEach
public void setUp() {
QueryPlanner queryPlanner = new DefaultQueryPlanner(schemaService);
QueryPlanner queryPlanner = new DefaultQueryPlanner(schemaService, systemSettingManager);
subject =
new DefaultQueryService(
queryParser, queryPlanner, criteriaQueryEngine, inMemoryQueryEngine);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.hisp.dhis.schema.SchemaService;
import org.hisp.dhis.schema.descriptors.DataElementSchemaDescriptor;
import org.hisp.dhis.schema.descriptors.OrganisationUnitSchemaDescriptor;
import org.hisp.dhis.setting.SystemSettingManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -60,9 +61,11 @@ class DefaultQueryPlannerTest {

@Mock private SchemaService schemaService;

@Mock private SystemSettingManager systemSettingManager;

@BeforeEach
public void setUp() {
this.subject = new DefaultQueryPlanner(schemaService);
this.subject = new DefaultQueryPlanner(schemaService, systemSettingManager);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,12 @@

import static org.hisp.dhis.utils.JavaToJson.toJson;
import static org.hisp.dhis.web.WebClientUtils.assertStatus;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.hisp.dhis.jsontree.JsonArray;
import org.hisp.dhis.user.User;
import org.hisp.dhis.user.UserSettingKey;
import org.hisp.dhis.user.UserSettingService;
import org.hisp.dhis.utils.JavaToJson;
import org.hisp.dhis.web.HttpStatus;
import org.hisp.dhis.webapi.DhisControllerConvenienceTest;
import org.hisp.dhis.webapi.DhisControllerIntegrationTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Base class for testing the {@link DatastoreController} providing helpers to set up entries in the
Expand Down Expand Up @@ -79,52 +68,4 @@ final void postPet(String key, String name, int age, List<String> eats) {
eats == null ? List.of() : eats.stream().map(food -> Map.of("name", food)));
postEntry("pets", key, toJson(obj));
}

static class CrudControllerIntegrationTest extends DhisControllerIntegrationTest {

@Autowired private UserSettingService userSettingService;

@Test
void testSearchByToken() {
String id =
assertStatus(
HttpStatus.CREATED,
POST(
"/dataSets/",
"{'name':'My data set', 'shortName': 'MDS', 'periodType':'Monthly'}"));

PUT(
"/dataSets/" + id + "/translations",
"{'translations': [{'locale':'fr', 'property':'NAME', 'value':'fr dataSet'}]}")
.content(HttpStatus.NO_CONTENT);

JsonArray translations =
GET("/dataSets/{id}/translations", id).content().getArray("translations");
assertEquals(1, translations.size());

assertTrue(
GET("/dataSets?filter=identifiable:token:fr", id)
.content()
.getArray("dataSets")
.isEmpty());
User userA = createAndAddUser("userA", null, "ALL");
userSettingService.saveUserSetting(UserSettingKey.DB_LOCALE, Locale.FRENCH, userA);
injectSecurityContext(userA);
assertTrue(
GET("/dataSets?filter=identifiable:token:bb", id)
.content()
.getArray("dataSets")
.isEmpty());
assertFalse(
GET("/dataSets?filter=identifiable:token:fr", id)
.content()
.getArray("dataSets")
.isEmpty());
assertFalse(
GET("/dataSets?filter=identifiable:token:dataSet", id)
.content()
.getArray("dataSets")
.isEmpty());
}
}
}
Loading

0 comments on commit bc80840

Please sign in to comment.