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 ExpressionUpgrader method upgradeExpression.
private String upgradeExpression(String expression) {
if (expression == null || expression.trim().isEmpty()) {
return null;
}
boolean changes = false;
StringBuffer sb = new StringBuffer();
try {
// -----------------------------------------------------------------
// Constants
// -----------------------------------------------------------------
Matcher matcher = OLD_CONSTANT_PATTERN.matcher(expression);
while (matcher.find()) {
Constant constant = constantService.getConstant(Integer.parseInt(matcher.group(1)));
String replacement = "C{" + constant.getUid() + "}";
matcher.appendReplacement(sb, replacement);
changes = true;
}
matcher.appendTail(sb);
expression = sb.toString();
// -----------------------------------------------------------------
// Operands
// -----------------------------------------------------------------
matcher = OLD_OPERAND_PATTERN.matcher(expression);
sb = new StringBuffer();
while (matcher.find()) {
DataElement de = dataElementService.getDataElement(Integer.parseInt(matcher.group(1)));
String replacement = "#{" + de.getUid();
if (matcher.groupCount() == 2 && matcher.group(2) != null && !matcher.group(2).trim().isEmpty()) {
DataElementCategoryOptionCombo coc = categoryService.getDataElementCategoryOptionCombo(Integer.parseInt(matcher.group(2)));
replacement += "." + coc.getUid();
}
replacement += "}";
matcher.appendReplacement(sb, replacement);
changes = true;
}
matcher.appendTail(sb);
expression = sb.toString();
} catch (Exception ex) {
log.error("Failed to upgrade expression: " + expression);
// Log and continue
log.error(ex);
}
if (changes) {
log.info("Upgraded expression: " + expression);
}
return changes ? expression : null;
}
Aggregations