use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class DefaultProgramIndicatorService method getExpressionDescription.
@Override
@Transactional
public String getExpressionDescription(String expression) {
if (expression == null) {
return null;
}
I18n i18n = i18nManager.getI18n();
StringBuffer description = new StringBuffer();
Matcher matcher = ProgramIndicator.EXPRESSION_PATTERN.matcher(expression);
while (matcher.find()) {
String key = matcher.group(1);
String uid = matcher.group(2);
if (ProgramIndicator.KEY_DATAELEMENT.equals(key)) {
String de = matcher.group(3);
ProgramStage programStage = programStageService.getProgramStage(uid);
DataElement dataElement = dataElementService.getDataElement(de);
if (programStage != null && dataElement != null) {
String programStageName = programStage.getDisplayName();
String dataelementName = dataElement.getDisplayName();
matcher.appendReplacement(description, programStageName + ProgramIndicator.SEPARATOR_ID + dataelementName);
}
} else if (ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute(uid);
if (attribute != null) {
matcher.appendReplacement(description, attribute.getDisplayName());
}
} else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
Constant constant = constantService.getConstant(uid);
if (constant != null) {
matcher.appendReplacement(description, constant.getDisplayName());
}
} else if (ProgramIndicator.KEY_PROGRAM_VARIABLE.equals(key)) {
String varName = i18n.getString(uid);
if (varName != null) {
matcher.appendReplacement(description, varName);
}
}
}
matcher.appendTail(description);
return description.toString();
}
use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class DefaultExpressionService method getExpressionDescription.
@Override
@Transactional
public String getExpressionDescription(String expression) {
if (expression == null || expression.isEmpty()) {
return null;
}
// ---------------------------------------------------------------------
// Operands
// ---------------------------------------------------------------------
StringBuffer sb = new StringBuffer();
Matcher matcher = VARIABLE_PATTERN.matcher(expression);
while (matcher.find()) {
String dimensionItem = matcher.group(GROUP_ID);
DimensionalItemObject dimensionItemObject = dimensionService.getDataDimensionalItemObject(dimensionItem);
if (dimensionItemObject == null) {
throw new InvalidIdentifierReferenceException("Identifier does not reference a dimensional item object: " + dimensionItem);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(dimensionItemObject.getDisplayName()));
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = CONSTANT_PATTERN.matcher(expression);
while (matcher.find()) {
String co = matcher.group(GROUP_ID);
Constant constant = constantService.getConstant(co);
if (constant == null) {
throw new InvalidIdentifierReferenceException("Identifier does not reference a constant: " + co);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(constant.getDisplayName()));
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Org unit groups
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = OU_GROUP_PATTERN.matcher(expression);
while (matcher.find()) {
String oug = matcher.group(GROUP_ID);
OrganisationUnitGroup group = organisationUnitGroupService.getOrganisationUnitGroup(oug);
if (group == null) {
throw new InvalidIdentifierReferenceException("Identifier does not reference an organisation unit group: " + oug);
}
matcher.appendReplacement(sb, Matcher.quoteReplacement(group.getDisplayName()));
}
expression = TextUtils.appendTail(matcher, sb);
// ---------------------------------------------------------------------
// Days
// ---------------------------------------------------------------------
sb = new StringBuffer();
matcher = DAYS_PATTERN.matcher(expression);
while (matcher.find()) {
matcher.appendReplacement(sb, DAYS_DESCRIPTION);
}
expression = TextUtils.appendTail(matcher, sb);
return expression;
}
use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class DefaultExpressionService method substituteExpressions.
@Override
@Transactional
public void substituteExpressions(Collection<Indicator> indicators, Integer days) {
if (indicators != null && !indicators.isEmpty()) {
Map<String, Constant> constants = new CachingMap<String, Constant>().load(idObjectManager.getAllNoAcl(Constant.class), c -> c.getUid());
Map<String, OrganisationUnitGroup> orgUnitGroups = new CachingMap<String, OrganisationUnitGroup>().load(idObjectManager.getAllNoAcl(OrganisationUnitGroup.class), g -> g.getUid());
for (Indicator indicator : indicators) {
indicator.setExplodedNumerator(substituteExpression(indicator.getNumerator(), constants, orgUnitGroups, days));
indicator.setExplodedDenominator(substituteExpression(indicator.getDenominator(), constants, orgUnitGroups, days));
}
}
}
Aggregations