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;
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class ProgramStageSectionStoreTest method setUpTest.
@Override
public void setUpTest() {
organisationUnit = createOrganisationUnit('A');
organisationUnitService.addOrganisationUnit(organisationUnit);
Program program = createProgram('A', new HashSet<>(), organisationUnit);
programService.addProgram(program);
stageA = createProgramStage('A', program);
programStageService.saveProgramStage(stageA);
DataElement dataElementA = createDataElement('A');
DataElement dataElementB = createDataElement('B');
dataElementService.addDataElement(dataElementA);
dataElementService.addDataElement(dataElementB);
ProgramStageDataElement stageDeA = createProgramStageDataElement(stageA, dataElementA, 1);
ProgramStageDataElement stageDeB = createProgramStageDataElement(stageA, dataElementB, 2);
programStageDataElementService.addProgramStageDataElement(stageDeA);
programStageDataElementService.addProgramStageDataElement(stageDeB);
dataElements = Lists.newArrayList();
dataElements.add(dataElementA);
dataElements.add(dataElementB);
stageB = new ProgramStage("B", program);
programStageService.saveProgramStage(stageB);
Set<ProgramStage> programStages = new HashSet<>();
programStages.add(stageA);
programStages.add(stageB);
program.setProgramStages(programStages);
programService.updateProgram(program);
sectionA = createProgramStageSection('A', 1);
sectionA.setDataElements(dataElements);
sectionB = createProgramStageSection('B', 2);
Set<ProgramStageSection> sections = new HashSet<>();
sections.add(sectionA);
sections.add(sectionB);
stageA.setProgramStageSections(sections);
}
use of org.hisp.dhis.dataelement.DataElement in project dhis2-core by dhis2.
the class AclServiceTest method testUpdateObjectWithPublicRWSuccessPublic.
@Test
public void testUpdateObjectWithPublicRWSuccessPublic() {
User user = createAdminUser("F_DATAELEMENT_PUBLIC_ADD");
DataElement dataElement = createDataElement('A');
dataElement.setPublicAccess(AccessStringHelper.READ_WRITE);
assertTrue(aclService.canUpdate(user, dataElement));
}
Aggregations