use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class ReportTable method getColumnName.
/**
* Generates a column name based on short-names of the argument objects.
* Null arguments are ignored in the name.
* <p/>
* The period column name must be static when on columns so it can be
* re-used in reports, hence the name property is used which will be formatted
* only when the period dimension is on rows.
*/
public static String getColumnName(List<DimensionalItemObject> objects) {
StringBuffer buffer = new StringBuffer();
for (DimensionalItemObject object : objects) {
if (object != null && object instanceof Period) {
buffer.append(object.getName()).append(SEPARATOR);
} else {
buffer.append(object != null ? (object.getShortName() + SEPARATOR) : EMPTY);
}
}
String column = columnEncode(buffer.toString());
return column.length() > 0 ? column.substring(0, column.lastIndexOf(SEPARATOR)) : TOTAL_COLUMN_NAME;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DefaultExpressionService method getDimensionalItemIdsInExpression.
@Override
public SetMap<Class<? extends DimensionalItemObject>, String> getDimensionalItemIdsInExpression(String expression) {
SetMap<Class<? extends DimensionalItemObject>, String> dimensionItemIdentifiers = new SetMap<>();
if (expression == null || expression.isEmpty()) {
return dimensionItemIdentifiers;
}
Matcher matcher = VARIABLE_PATTERN.matcher(expression);
while (matcher.find()) {
dimensionItemIdentifiers.putValue(VARIABLE_TYPES.get(matcher.group(1)), matcher.group(2));
}
return dimensionItemIdentifiers;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DefaultExpressionService method generateExpression.
/**
* Generates an expression based on the given data maps.
*
* @param expression the expression.
* @param valueMap the value map.
* @param constantMap the constant map.
* @param orgUnitCountMap the organisation unit count map.
* @param days the number of days.
* @param missingValueStrategy the missing value strategy.
* @param aggregateMap the aggregate map.
* @return an expression.
*/
private String generateExpression(String expression, Map<? extends DimensionalItemObject, Double> valueMap, Map<String, Double> constantMap, Map<String, Integer> orgUnitCountMap, Integer days, MissingValueStrategy missingValueStrategy, Map<String, List<Double>> aggregateMap) {
if (expression == null || expression.isEmpty()) {
return null;
}
expression = ExpressionUtils.normalizeExpression(expression);
Map<String, Double> dimensionItemValueMap = valueMap.entrySet().stream().filter(e -> e.getValue() != null).collect(Collectors.toMap(e -> e.getKey().getDimensionItem(), e -> e.getValue()));
missingValueStrategy = ObjectUtils.firstNonNull(missingValueStrategy, NEVER_SKIP);
// ---------------------------------------------------------------------
// Aggregates
// ---------------------------------------------------------------------
StringBuffer sb = new StringBuffer();
Pattern prefix = CustomFunctions.AGGREGATE_PATTERN_PREFIX;
Matcher matcher = prefix.matcher(expression);
int scan = 0, len = expression.length(), tail = 0;
while (scan < len && matcher.find(scan)) {
int start = matcher.end();
int end = Expression.matchExpression(expression, start);
if (end < 0) {
sb.append(expression.substring(scan, start));
scan = start + 1;
tail = start;
} else if (aggregateMap == null || expression.charAt(start) == '<') {
sb.append(expression.substring(scan, end));
scan = end + 1;
tail = end;
} else {
String subExpression = expression.substring(start, end);
List<Double> samples = aggregateMap.get(subExpression);
if (samples == null) {
if (SKIP_IF_ANY_VALUE_MISSING.equals(missingValueStrategy)) {
return null;
}
} else {
String literal = (samples == null) ? ("[]") : (samples.toString());
sb.append(expression.substring(scan, start));
sb.append(literal);
}
scan = end;
tail = end;
}
}
sb.append(expression.substring(tail));
expression = sb.toString();
// ---------------------------------------------------------------------
// DimensionalItemObjects
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = VARIABLE_PATTERN.matcher(expression);
int matchCount = 0;
int valueCount = 0;
while (matcher.find()) {
matchCount++;
String dimItem = matcher.group(GROUP_ID);
final Double value = dimensionItemValueMap.get(dimItem);
boolean missingValue = value == null;
if (missingValue && SKIP_IF_ANY_VALUE_MISSING.equals(missingValueStrategy)) {
return null;
}
if (!missingValue) {
valueCount++;
}
String replacement = value != null ? String.valueOf(value) : NULL_REPLACEMENT;
matcher.appendReplacement(sb, Matcher.quoteReplacement(replacement));
}
if (SKIP_IF_ALL_VALUES_MISSING.equals(missingValueStrategy) && matchCount > 0 && valueCount == 0) {
return null;
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = CONSTANT_PATTERN.matcher(expression);
while (matcher.find()) {
final Double constant = constantMap != null ? constantMap.get(matcher.group(GROUP_ID)) : null;
String replacement = constant != null ? String.valueOf(constant) : NULL_REPLACEMENT;
matcher.appendReplacement(sb, replacement);
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Org unit groups
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = OU_GROUP_PATTERN.matcher(expression);
while (matcher.find()) {
final Integer count = orgUnitCountMap != null ? orgUnitCountMap.get(matcher.group(GROUP_ID)) : null;
String replacement = count != null ? String.valueOf(count) : NULL_REPLACEMENT;
matcher.appendReplacement(sb, replacement);
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Days
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = DAYS_PATTERN.matcher(expression);
while (matcher.find()) {
String replacement = days != null ? String.valueOf(days) : NULL_REPLACEMENT;
matcher.appendReplacement(sb, replacement);
}
return TextUtils.appendTail(matcher, sb);
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DefaultExpressionService method getDimensionalItemObjectsInExpression.
@Override
public Set<DimensionalItemObject> getDimensionalItemObjectsInExpression(String expression) {
Set<DimensionalItemObject> dimensionItems = Sets.newHashSet();
if (expression == null || expression.isEmpty()) {
return dimensionItems;
}
Matcher matcher = VARIABLE_PATTERN.matcher(expression);
while (matcher.find()) {
String dimensionItem = matcher.group(GROUP_ID);
DimensionalItemObject dimensionItemObject = dimensionService.getDataDimensionalItemObject(dimensionItem);
if (dimensionItemObject != null) {
dimensionItems.add(dimensionItemObject);
}
}
return dimensionItems;
}
use of org.hisp.dhis.common.DimensionalItemObject in project dhis2-core by dhis2.
the class DimensionController method getItems.
@SuppressWarnings("unchecked")
@RequestMapping(value = "/{uid}/items", method = RequestMethod.GET)
@ResponseBody
public RootNode getItems(@PathVariable String uid, @RequestParam Map<String, String> parameters, Model model, HttpServletRequest request, HttpServletResponse response) throws QueryParserException {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
List<DimensionalItemObject> items = dimensionService.getCanReadDimensionItems(uid);
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>());
query.setObjects(items);
query.setDefaultOrder();
items = (List<DimensionalItemObject>) queryService.query(query);
RootNode rootNode = NodeUtils.createMetadata();
CollectionNode collectionNode = rootNode.addChild(fieldFilterService.filter(getEntityClass(), items, fields));
collectionNode.setName("items");
for (Node node : collectionNode.getChildren()) {
((AbstractNode) node).setName("item");
}
return rootNode;
}
Aggregations