use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class ProgramIndicatorServiceTest method setUpTest.
@Override
public void setUpTest() {
OrganisationUnit organisationUnit = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(organisationUnit);
// ---------------------------------------------------------------------
// Program
// ---------------------------------------------------------------------
programA = createProgram('A', new HashSet<>(), organisationUnit);
programService.addProgram(programA);
psA = new ProgramStage("StageA", programA);
psA.setSortOrder(1);
programStageService.saveProgramStage(psA);
psB = new ProgramStage("StageB", programA);
psB.setSortOrder(2);
programStageService.saveProgramStage(psB);
Set<ProgramStage> programStages = new HashSet<>();
programStages.add(psA);
programStages.add(psB);
programA.setProgramStages(programStages);
programService.updateProgram(programA);
programB = createProgram('B', new HashSet<>(), organisationUnit);
programService.addProgram(programB);
// ---------------------------------------------------------------------
// Program Stage DE
// ---------------------------------------------------------------------
deA = createDataElement('A');
deA.setDomainType(DataElementDomain.TRACKER);
deB = createDataElement('B');
deB.setDomainType(DataElementDomain.TRACKER);
dataElementService.addDataElement(deA);
dataElementService.addDataElement(deB);
ProgramStageDataElement stageDataElementA = new ProgramStageDataElement(psA, deA, false, 1);
ProgramStageDataElement stageDataElementB = new ProgramStageDataElement(psA, deB, false, 2);
ProgramStageDataElement stageDataElementC = new ProgramStageDataElement(psB, deA, false, 1);
ProgramStageDataElement stageDataElementD = new ProgramStageDataElement(psB, deB, false, 2);
programStageDataElementService.addProgramStageDataElement(stageDataElementA);
programStageDataElementService.addProgramStageDataElement(stageDataElementB);
programStageDataElementService.addProgramStageDataElement(stageDataElementC);
programStageDataElementService.addProgramStageDataElement(stageDataElementD);
// ---------------------------------------------------------------------
// TrackedEntityInstance & Enrollment
// ---------------------------------------------------------------------
TrackedEntityInstance entityInstance = createTrackedEntityInstance('A', organisationUnit);
entityInstanceService.addTrackedEntityInstance(entityInstance);
incidentDate = DateUtils.getMediumDate("2014-10-22");
enrollmentDate = DateUtils.getMediumDate("2014-12-31");
programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, programA, enrollmentDate, incidentDate, organisationUnit);
incidentDate = DateUtils.getMediumDate("2014-10-22");
enrollmentDate = DateUtils.getMediumDate("2014-12-31");
programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, programA, enrollmentDate, incidentDate, organisationUnit);
// TODO enroll twice?
// ---------------------------------------------------------------------
// TrackedEntityAttribute
// ---------------------------------------------------------------------
atA = createTrackedEntityAttribute('A', ValueType.NUMBER);
atB = createTrackedEntityAttribute('B', ValueType.NUMBER);
attributeService.addTrackedEntityAttribute(atA);
attributeService.addTrackedEntityAttribute(atB);
TrackedEntityAttributeValue attributeValueA = new TrackedEntityAttributeValue(atA, entityInstance, "1");
TrackedEntityAttributeValue attributeValueB = new TrackedEntityAttributeValue(atB, entityInstance, "2");
attributeValueService.addTrackedEntityAttributeValue(attributeValueA);
attributeValueService.addTrackedEntityAttributeValue(attributeValueB);
// ---------------------------------------------------------------------
// TrackedEntityDataValue
// ---------------------------------------------------------------------
ProgramStageInstance stageInstanceA = programStageInstanceService.createProgramStageInstance(programInstance, psA, enrollmentDate, incidentDate, organisationUnit);
ProgramStageInstance stageInstanceB = programStageInstanceService.createProgramStageInstance(programInstance, psB, enrollmentDate, incidentDate, organisationUnit);
Set<ProgramStageInstance> programStageInstances = new HashSet<>();
programStageInstances.add(stageInstanceA);
programStageInstances.add(stageInstanceB);
programInstance.setProgramStageInstances(programStageInstances);
programInstance.setProgram(programA);
TrackedEntityDataValue dataValueA = new TrackedEntityDataValue(stageInstanceA, deA, "3");
TrackedEntityDataValue dataValueB = new TrackedEntityDataValue(stageInstanceA, deB, "1");
TrackedEntityDataValue dataValueC = new TrackedEntityDataValue(stageInstanceB, deA, "5");
TrackedEntityDataValue dataValueD = new TrackedEntityDataValue(stageInstanceB, deB, "7");
dataValueService.saveTrackedEntityDataValue(dataValueA);
dataValueService.saveTrackedEntityDataValue(dataValueB);
dataValueService.saveTrackedEntityDataValue(dataValueC);
dataValueService.saveTrackedEntityDataValue(dataValueD);
// ---------------------------------------------------------------------
// Constant
// ---------------------------------------------------------------------
Constant constantA = createConstant('A', 7.0);
constantService.saveConstant(constantA);
// ---------------------------------------------------------------------
// ProgramIndicator
// ---------------------------------------------------------------------
String expressionA = "( d2:daysBetween(" + KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_ENROLLMENT_DATE + "}, " + KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_INCIDENT_DATE + "}) ) / " + ProgramIndicator.KEY_CONSTANT + "{" + constantA.getUid() + "}";
indicatorA = createProgramIndicator('A', programA, expressionA, null);
programA.getProgramIndicators().add(indicatorA);
indicatorB = createProgramIndicator('B', programA, "70", null);
programA.getProgramIndicators().add(indicatorB);
indicatorC = createProgramIndicator('C', programA, "0", null);
programA.getProgramIndicators().add(indicatorC);
String expressionD = "0 + A + 4 + " + ProgramIndicator.KEY_PROGRAM_VARIABLE + "{" + ProgramIndicator.VAR_INCIDENT_DATE + "}";
indicatorD = createProgramIndicator('D', programB, expressionD, null);
String expressionE = KEY_DATAELEMENT + "{" + psA.getUid() + "." + deA.getUid() + "} + " + KEY_DATAELEMENT + "{" + psB.getUid() + "." + deA.getUid() + "} - " + KEY_ATTRIBUTE + "{" + atA.getUid() + "} + " + KEY_ATTRIBUTE + "{" + atB.getUid() + "}";
String filterE = KEY_DATAELEMENT + "{" + psA.getUid() + "." + deA.getUid() + "} + " + KEY_ATTRIBUTE + "{" + atA.getUid() + "} > 10";
indicatorE = createProgramIndicator('E', programB, expressionE, filterE);
}
use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class DhisConvenienceTest method createConstant.
/**
* @param uniqueCharacter A unique character to identify the object.
* @param value The value for constant
* @return a constant instance
*/
protected static Constant createConstant(char uniqueCharacter, double value) {
Constant constant = new Constant();
constant.setAutoFields();
constant.setName("Constant" + uniqueCharacter);
constant.setValue(value);
return constant;
}
use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class DefaultProgramIndicatorService method getSubstitutedElementsAnalyticsSql.
private String getSubstitutedElementsAnalyticsSql(String expression, boolean ignoreMissingValues, AnalyticsType analyticsType) {
if (expression == null) {
return null;
}
StringBuffer buffer = new StringBuffer();
Matcher matcher = ProgramIndicator.EXPRESSION_EQUALSZEROOREMPTY_PATTERN.matcher(expression);
while (matcher.find()) {
String key = matcher.group(1);
String el1 = matcher.group(2);
String el2 = matcher.group(3);
boolean equalsZero = matcher.group(4) != null && matcher.group(4).matches(ProgramIndicator.EQUALSZERO);
boolean equalsEmpty = matcher.group(4) != null && matcher.group(4).matches(ProgramIndicator.EQUALSEMPTY);
if (ProgramIndicator.KEY_DATAELEMENT.equals(key) || ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
String columnName;
if (ProgramIndicator.KEY_DATAELEMENT.equals(key)) {
columnName = AnalyticsType.ENROLLMENT == analyticsType ? statementBuilder.columnQuote(el1 + ProgramIndicator.DB_SEPARATOR_ID + el2) : statementBuilder.columnQuote(el2);
} else // ProgramIndicator.KEY_ATTRIBUTE
{
columnName = statementBuilder.columnQuote(el1);
}
if (equalsZero) {
columnName = getNumericIgnoreNullSql(columnName) + " == 0 ";
} else if (equalsEmpty) {
columnName = getTextIgnoreNullSql(columnName) + " == '' ";
} else if (ignoreMissingValues) {
columnName = getNumericIgnoreNullSql(columnName);
}
matcher.appendReplacement(buffer, columnName);
} else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
Constant constant = constantService.getConstant(el1);
if (constant != null) {
matcher.appendReplacement(buffer, String.valueOf(constant.getValue()));
}
}
}
return TextUtils.appendTail(matcher, buffer);
}
use of org.hisp.dhis.constant.Constant in project dhis2-core by dhis2.
the class DefaultProgramIndicatorService method getSubstitutedExpression.
/**
* Generates an expression where all items are substituted with a sample value
* in order to maintain a valid expression syntax.
*
* @param expression the expression.
*/
private String getSubstitutedExpression(String expression) {
StringBuffer expr = 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 sample = ValidationUtils.getSubstitutionValue(dataElement.getValueType());
matcher.appendReplacement(expr, sample);
} else {
return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
}
} else if (ProgramIndicator.KEY_ATTRIBUTE.equals(key)) {
TrackedEntityAttribute attribute = attributeService.getTrackedEntityAttribute(uid);
if (attribute != null) {
String sample = ValidationUtils.getSubstitutionValue(attribute.getValueType());
matcher.appendReplacement(expr, sample);
} else {
return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
}
} else if (ProgramIndicator.KEY_CONSTANT.equals(key)) {
Constant constant = constantService.getConstant(uid);
if (constant != null) {
matcher.appendReplacement(expr, String.valueOf(constant.getValue()));
} else {
return ProgramIndicator.INVALID_IDENTIFIERS_IN_EXPRESSION;
}
} else if (ProgramIndicator.KEY_PROGRAM_VARIABLE.equals(key)) {
String sampleValue = VARIABLE_SAMPLE_VALUE_MAP.get(uid);
if (sampleValue != null) {
matcher.appendReplacement(expr, sampleValue);
} else {
return ProgramIndicator.UNKNOWN_VARIABLE;
}
}
}
matcher.appendTail(expr);
return expr.toString();
}
use of org.hisp.dhis.constant.Constant 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);
}
Aggregations