use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class ResourceTableServiceTest method setUpTest.
@Override
public void setUpTest() {
PeriodType pt = new MonthlyPeriodType();
DataElement deA = createDataElement('A');
DataElement deB = createDataElement('B');
idObjectManager.save(deA);
idObjectManager.save(deB);
DataElementGroup degA = createDataElementGroup('A');
DataElementGroup degB = createDataElementGroup('B');
degA.addDataElement(deA);
degB.addDataElement(deB);
idObjectManager.save(degA);
idObjectManager.save(degB);
DataElementGroupSet degsA = createDataElementGroupSet('A');
degsA.addDataElementGroup(degA);
degsA.addDataElementGroup(degB);
idObjectManager.save(degsA);
OrganisationUnit ouA = createOrganisationUnit('A');
OrganisationUnit ouB = createOrganisationUnit('B');
OrganisationUnit ouC = createOrganisationUnit('C');
ouB.setParent(ouA);
ouC.setParent(ouA);
ouA.getChildren().add(ouB);
ouA.getChildren().add(ouC);
idObjectManager.save(ouA);
idObjectManager.save(ouB);
idObjectManager.save(ouC);
DataSet dsA = createDataSet('A', pt);
DataSet dsB = createDataSet('B', pt);
dsA.addDataSetElement(deA);
dsB.addDataSetElement(deA);
dsA.addOrganisationUnit(ouA);
dsB.addOrganisationUnit(ouA);
dataSetService.addDataSet(dsA);
dataSetService.addDataSet(dsB);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultPredictorService method predict.
// -------------------------------------------------------------------------
// Predictor run
// -------------------------------------------------------------------------
@Override
public int predict(Predictor predictor, Date startDate, Date endDate) {
log.info("Predicting for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
Expression generator = predictor.getGenerator();
Expression skipTest = predictor.getSampleSkipTest();
DataElement outputDataElement = predictor.getOutput();
Set<String> aggregates = expressionService.getAggregatesInExpression(generator.getExpression());
Map<String, Double> constantMap = constantService.getConstantMap();
List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
Set<DataElementOperand> dataElementOperands = getDataElementOperands(aggregates, skipTest);
User currentUser = currentUserService.getCurrentUser();
DataElementCategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? categoryService.getDefaultDataElementCategoryOptionCombo() : predictor.getOutputCombo();
List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(predictor.getOrganisationUnitLevels(), currentUser.getOrganisationUnits());
int predictionCount = 0;
for (OrganisationUnit orgUnit : orgUnits) {
MapMapMap<Period, String, DimensionalItemObject, Double> dataMap = dataElementOperands.isEmpty() ? null : dataValueService.getDataElementOperandValues(dataElementOperands, allSamplePeriods, orgUnit);
applySkipTest(dataMap, skipTest, constantMap);
for (Period period : outputPeriods) {
ListMapMap<String, String, Double> aggregateSampleMap = getAggregateSamples(dataMap, aggregates, samplePeriodsMap.get(period), constantMap);
for (String aoc : aggregateSampleMap.keySet()) {
ListMap<String, Double> aggregateValueMap = aggregateSampleMap.get(aoc);
Double value = expressionService.getExpressionValue(generator, new HashMap<>(), constantMap, null, period.getDaysInPeriod(), aggregateValueMap);
if (value != null && !value.isNaN() && !value.isInfinite()) {
writeDataValue(outputDataElement, period, orgUnit, outputOptionCombo, categoryService.getDataElementCategoryOptionCombo(aoc), value.toString(), currentUser.getUsername());
predictionCount++;
}
}
}
}
log.info("Generated " + predictionCount + " predictions for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
return predictionCount;
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultProgramDataEntryService method prepareDataEntryFormForAdd.
@Override
public String prepareDataEntryFormForAdd(String htmlCode, I18n i18n, ProgramStage programStage) {
// ---------------------------------------------------------------------
// Inline Javascript to add to HTML before outputting
// ---------------------------------------------------------------------
final String jQueryCalendar = "<script>$DATAPICKER-METHOD(\"$PROGRAMSTAGEID-$DATAELEMENTID-val\", false);</script>";
StringBuffer sb = new StringBuffer();
// ---------------------------------------------------------------------
// Pattern to match data elements in the HTML code
// ---------------------------------------------------------------------
Matcher dataElementMatcher = INPUT_PATTERN.matcher(htmlCode);
int tabindex = 0;
// ---------------------------------------------------------------------
// Iterate through all matching data element fields
// ---------------------------------------------------------------------
Map<String, ProgramStageDataElement> programStageDataElementMap = getProgramStageDataElementMap(programStage);
while (dataElementMatcher.find()) {
// -----------------------------------------------------------------
// Get HTML input field code
// -----------------------------------------------------------------
String compulsory = "null";
boolean allowProvidedElsewhere = false;
String dateMethod = "datePickerValid";
String inputHTML = dataElementMatcher.group(1);
Matcher identifierMatcher = IDENTIFIER_PATTERN_FIELD.matcher(inputHTML);
if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
// -------------------------------------------------------------
// Get ID of data element
// -------------------------------------------------------------
String programStageUid = identifierMatcher.group(1);
String dataElementUid = identifierMatcher.group(2);
DataElement dataElement = null;
String programStageName = programStage.getDisplayName();
if (!programStageUid.equals(programStage.getUid())) {
dataElement = dataElementService.getDataElement(dataElementUid);
ProgramStage otherProgramStage = programStageService.getProgramStage(programStageUid);
programStageName = otherProgramStage != null ? otherProgramStage.getDisplayName() : "N/A";
} else {
ProgramStageDataElement psde = programStageDataElementMap.get(dataElementUid);
if (psde == null) {
return i18n.getString("some_data_element_not_exist");
}
dataElement = psde.getDataElement();
compulsory = BooleanUtils.toStringTrueFalse(psde.isCompulsory());
allowProvidedElsewhere = psde.getAllowProvidedElsewhere();
if (psde.getAllowFutureDate()) {
dateMethod = "datePicker";
}
}
if (dataElement == null) {
continue;
}
// -------------------------------------------------------------
// Find existing value of data element in data set
// -------------------------------------------------------------
TrackedEntityDataValue entityInstanceDataValue = null;
String dataElementValue = EMPTY;
if (inputHTML.contains("title=\"\"")) {
inputHTML = inputHTML.replace("title=\"\"", "title=\"[ " + dataElement.getUid() + " - " + dataElement.getName() + " - " + dataElement.getValueType() + " ]\" ");
} else {
inputHTML += "title=\"[ " + dataElement.getUid() + " - " + dataElement.getName() + " - " + dataElement.getValueType() + " ]\" ";
}
// -------------------------------------------------------------
// Set field for dataElement
// -------------------------------------------------------------
tabindex++;
ValueType valueType = dataElement.getValueType();
if (valueType.isText() || valueType.isNumeric() || ValueType.USERNAME == valueType) {
inputHTML = populateCustomDataEntryForTextBox(dataElement, inputHTML, dataElementValue);
} else if (ValueType.DATE == valueType || ValueType.DATETIME == valueType) {
inputHTML = populateCustomDataEntryForDate(inputHTML, dataElementValue);
} else if (ValueType.TRUE_ONLY == valueType) {
inputHTML = populateCustomDataEntryForTrueOnly(dataElement, inputHTML, dataElementValue);
} else if (ValueType.BOOLEAN == valueType) {
inputHTML = populateCustomDataEntryForBoolean(dataElement, inputHTML, dataElementValue, i18n);
}
// -----------------------------------------------------------
// Check if this dataElement is from another programStage then
// disable
// If programStagsInstance is completed then disabled it
// -----------------------------------------------------------
String disabled = "";
if (!programStageUid.equals(programStage.getUid())) {
disabled = "disabled=\"\"";
} else {
if (ValueType.DATE == valueType || ValueType.DATETIME == valueType) {
inputHTML += jQueryCalendar;
}
if (allowProvidedElsewhere) {
// Add ProvidedByOtherFacility checkbox
inputHTML = addProvidedElsewherCheckbox(inputHTML, entityInstanceDataValue, programStage);
}
}
inputHTML = inputHTML.replace("$DATAELEMENTID", String.valueOf(dataElementUid));
inputHTML = inputHTML.replace("$VALUE", dataElementValue);
inputHTML = inputHTML.replace("$PROGRAMSTAGEID", String.valueOf(programStageUid));
inputHTML = inputHTML.replace("$PROGRAMSTAGENAME", programStageName);
inputHTML = inputHTML.replace("$DATAELEMENTNAME", dataElement.getName());
inputHTML = inputHTML.replace("$DATAELEMENTTYPE", dataElement.getValueType().toString());
inputHTML = inputHTML.replace("$DISABLED", disabled);
inputHTML = inputHTML.replace("$COMPULSORY", compulsory);
inputHTML = inputHTML.replace("$SAVEMODE", "false");
inputHTML = inputHTML.replace("$TABINDEX", tabindex + "");
inputHTML = inputHTML.replace("$DATAPICKER-METHOD", dateMethod);
inputHTML = inputHTML.replaceAll("\\$", "\\\\\\$");
dataElementMatcher.appendReplacement(sb, inputHTML);
}
}
dataElementMatcher.appendTail(sb);
return populateI18nStrings(sb.toString(), i18n);
}
use of org.hisp.dhis.dataelement.DataElement 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.dataelement.DataElement in project dhis2-core by dhis2.
the class DefaultProgramService method getGeneratedProgramDataElements.
// -------------------------------------------------------------------------
// ProgramDataElement
// -------------------------------------------------------------------------
@Override
public List<ProgramDataElementDimensionItem> getGeneratedProgramDataElements(String programUid) {
Program program = getProgram(programUid);
List<ProgramDataElementDimensionItem> programDataElements = Lists.newArrayList();
if (program == null) {
return programDataElements;
}
for (DataElement element : program.getDataElements()) {
programDataElements.add(new ProgramDataElementDimensionItem(program, element));
}
Collections.sort(programDataElements);
return programDataElements;
}
Aggregations