use of org.hisp.dhis.dataitem.query.shared.QueryParam.VALUE_TYPES in project dhis2-core by dhis2.
the class DataElementQuery method getStatement.
@Override
public String getStatement(final MapSqlParameterSource paramsMap) {
final StringBuilder sql = new StringBuilder();
sql.append("(");
// Creating a temp translated table to be queried.
sql.append(SPACED_SELECT + "* from (");
if (hasNonBlankStringPresence(paramsMap, LOCALE)) {
// Selecting translated names.
sql.append(selectRowsContainingTranslatedName());
} else {
// Retrieving all rows ignoring translation as no locale is defined.
sql.append(selectAllRowsIgnoringAnyTranslation());
}
sql.append(" group by item_name, item_uid, item_valuetype, item_code, item_domaintype, item_sharing, item_shortname," + " i18n_first_name, i18n_first_shortname, i18n_second_name, i18n_second_shortname");
// Closing the temp table.
sql.append(" ) t");
sql.append(SPACED_WHERE);
// Applying filters, ordering and limits.
// Mandatory filters. They do not respect the root junction filtering.
sql.append(always(sharingConditions("t.item_sharing", READ_ACCESS, paramsMap)));
sql.append(" and ");
// ONLY aggregates
sql.append(always("t.item_domaintype = 'AGGREGATE'"));
// Optional filters, based on the current root junction.
final OptionalFilterBuilder optionalFilters = new OptionalFilterBuilder(paramsMap);
final Set<String> aggregatableTypes = getAggregatables().stream().map(type -> type.name()).collect(toSet());
// consider the domainType = 'AGGREGATE'. Very specific to DataElements.
if (paramsMap != null && paramsMap.hasValue(VALUE_TYPES) && paramsMap.getValue(VALUE_TYPES) != null && !((Set) paramsMap.getValue(VALUE_TYPES)).containsAll(aggregatableTypes)) {
optionalFilters.append(ifSet(valueTypeFiltering("t.item_valuetype", paramsMap)));
}
optionalFilters.append(ifSet(displayNameFiltering("t.i18n_first_name", paramsMap)));
optionalFilters.append(ifSet(displayShortNameFiltering("t.i18n_first_shortname", paramsMap)));
optionalFilters.append(ifSet(nameFiltering("t.item_name", paramsMap)));
optionalFilters.append(ifSet(shortNameFiltering("t.item_shortname", paramsMap)));
optionalFilters.append(ifSet(uidFiltering("t.item_uid", paramsMap)));
sql.append(ifAny(optionalFilters.toString()));
final String identifiableStatement = identifiableTokenFiltering("t.item_uid", "t.item_code", "t.i18n_first_name", null, paramsMap);
if (isNotBlank(identifiableStatement)) {
sql.append(rootJunction(paramsMap));
sql.append(identifiableStatement);
}
sql.append(ifSet(ordering("t.i18n_first_name, t.i18n_second_name, t.item_uid", "t.item_name, t.item_uid", "t.i18n_first_shortname, t.i18n_second_shortname, t.item_uid", "t.item_shortname, t.item_uid", paramsMap)));
sql.append(ifSet(maxLimit(paramsMap)));
sql.append(")");
final String fullStatement = sql.toString();
log.trace("Full SQL: " + fullStatement);
return fullStatement;
}
use of org.hisp.dhis.dataitem.query.shared.QueryParam.VALUE_TYPES in project dhis2-core by dhis2.
the class FilteringHelper method setFilteringParams.
/**
* Sets the filtering defined by filters list into the paramsMap.
*
* @param filters the source of filtering params
* @param paramsMap the map that will receive the filtering params
* @param currentUser the current user logged
*/
public static void setFilteringParams(final Set<String> filters, final WebOptions options, final MapSqlParameterSource paramsMap, final User currentUser) {
final Locale currentLocale = defaultIfNull(getUserSetting(DB_LOCALE), getUserSetting(UI_LOCALE));
if (currentLocale != null && isNotBlank(currentLocale.getLanguage())) {
paramsMap.addValue(LOCALE, trimToEmpty(currentLocale.getLanguage()));
}
final String ilikeName = extractValueFromFilter(filters, NAME_ILIKE);
if (StringUtils.isNotEmpty(ilikeName)) {
paramsMap.addValue(NAME, wrap(addIlikeReplacingCharacters(ilikeName), "%"));
}
final String ilikeDisplayName = extractValueFromFilter(filters, DISPLAY_NAME_ILIKE);
if (StringUtils.isNotEmpty(ilikeDisplayName)) {
paramsMap.addValue(DISPLAY_NAME, wrap(addIlikeReplacingCharacters(ilikeDisplayName), "%"));
}
final String ilikeShortName = extractValueFromFilter(filters, SHORT_NAME_ILIKE);
if (StringUtils.isNotEmpty(ilikeShortName)) {
paramsMap.addValue(SHORT_NAME, wrap(addIlikeReplacingCharacters(ilikeShortName), "%"));
}
final String ilikeDisplayShortName = extractValueFromFilter(filters, DISPLAY_SHORT_NAME_ILIKE);
if (StringUtils.isNotEmpty(ilikeDisplayShortName)) {
paramsMap.addValue(DISPLAY_SHORT_NAME, wrap(addIlikeReplacingCharacters(ilikeDisplayShortName), "%"));
}
final String equalId = extractValueFromFilter(filters, ID_EQUAL, true);
if (isNotBlank(equalId)) {
paramsMap.addValue(UID, equalId);
}
final String rootJunction = options.getRootJunction().name();
if (isNotBlank(rootJunction)) {
paramsMap.addValue(ROOT_JUNCTION, rootJunction);
}
final String identifiableToken = extractValueFromFilter(filters, IDENTIFIABLE_TOKEN);
if (identifiableToken != null) {
final List<String> wordsAsTokens = getTokens(identifiableToken);
if (CollectionUtils.isNotEmpty(wordsAsTokens)) {
paramsMap.addValue(IDENTIFIABLE_TOKEN_COMPARISON, StringUtils.join(wordsAsTokens, ","));
}
}
if (containsFilterWithAnyOfPrefixes(filters, VALUE_TYPE_EQUAL.getCombination(), VALUE_TYPE_IN.getCombination())) {
final Set<String> valueTypesFilter = extractAllValueTypesFromFilters(filters);
assertThatValueTypeFilterHasOnlyAggregatableTypes(valueTypesFilter, filters);
paramsMap.addValue(VALUE_TYPES, extractAllValueTypesFromFilters(filters));
} else {
// Includes all value types.
paramsMap.addValue(VALUE_TYPES, getAggregatables().stream().map(type -> type.name()).collect(toSet()));
}
final String programId = extractValueFromFilter(filters, PROGRAM_ID_EQUAL, true);
// Add program id filtering id, if present.
if (isNotBlank(programId)) {
paramsMap.addValue(PROGRAM_ID, programId);
}
// Add user group filtering, when present.
if (currentUser != null && CollectionUtils.isNotEmpty(currentUser.getGroups())) {
final Set<String> userGroupUids = currentUser.getGroups().stream().filter(group -> group != null).map(group -> trimToEmpty(group.getUid())).collect(toSet());
paramsMap.addValue(USER_GROUP_UIDS, "{" + join(",", userGroupUids) + "}");
}
}
Aggregations