use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DefaultDataEntryFormService method prepareDataEntryFormForEdit.
@Override
public String prepareDataEntryFormForEdit(DataEntryForm dataEntryForm, DataSet dataSet, I18n i18n) {
if (dataEntryForm == null || !dataEntryForm.hasForm() || dataSet == null) {
return null;
}
CachingMap<String, DataElementCategoryOptionCombo> optionComboMap = new CachingMap<>();
optionComboMap.putAll(IdentifiableObjectUtils.getUidObjectMap(dataSet.getDataElementOptionCombos()));
StringBuffer sb = new StringBuffer();
Matcher inputMatcher = INPUT_PATTERN.matcher(dataEntryForm.getHtmlCode());
while (inputMatcher.find()) {
String inputHtml = inputMatcher.group();
Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher(inputHtml);
Matcher dataElementTotalMatcher = DATAELEMENT_TOTAL_PATTERN.matcher(inputHtml);
Matcher indicatorMatcher = INDICATOR_PATTERN.matcher(inputHtml);
String displayValue = null;
String displayTitle = null;
if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
String dataElementId = identifierMatcher.group(1);
DataElement dataElement = dataElementService.getDataElement(dataElementId);
String optionComboId = identifierMatcher.group(2);
DataElementCategoryOptionCombo categoryOptionCombo = optionComboMap.get(optionComboId, () -> idObjectManager.getObject(DataElementCategoryOptionCombo.class, IdScheme.UID, optionComboId));
String optionComboName = categoryOptionCombo != null ? escapeHtml3(categoryOptionCombo.getName()) : "[ " + i18n.getString("cat_option_combo_not_exist") + " ]";
StringBuilder title = dataElement != null ? new StringBuilder("title=\"").append(dataElementId).append(" - ").append(escapeHtml3(dataElement.getDisplayName())).append(" - ").append(optionComboId).append(" - ").append(optionComboName).append(" - ").append(dataElement.getValueType()).append("\"") : new StringBuilder();
displayValue = dataElement != null ? "value=\"[ " + escapeHtml3(dataElement.getDisplayName()) + " " + optionComboName + " ]\"" : "[ " + i18n.getString("data_element_not_exist") + " ]";
displayTitle = dataElement != null ? title.toString() : "[ " + i18n.getString("dataelement_not_exist") + " ]";
} else if (dataElementTotalMatcher.find() && dataElementTotalMatcher.groupCount() > 0) {
String dataElementId = dataElementTotalMatcher.group(1);
DataElement dataElement = dataElementService.getDataElement(dataElementId);
displayValue = dataElement != null ? "value=\"[ " + escapeHtml3(dataElement.getDisplayName()) + " ]\"" : "[ " + i18n.getString("data_element_not_exist") + " ]";
displayTitle = dataElement != null ? "title=\"" + escapeHtml3(dataElement.getDisplayName()) + "\"" : "[ " + i18n.getString("data_element_not_exist") + " ]";
} else if (indicatorMatcher.find() && indicatorMatcher.groupCount() > 0) {
String indicatorId = indicatorMatcher.group(1);
Indicator indicator = indicatorService.getIndicator(indicatorId);
displayValue = indicator != null ? "value=\"[ " + escapeHtml3(indicator.getDisplayName()) + " ]\"" : "[ " + i18n.getString("indicator_not_exist") + " ]";
displayTitle = indicator != null ? "title=\"" + escapeHtml3(indicator.getDisplayName()) + "\"" : "[ " + i18n.getString("indicator_not_exist") + " ]";
}
if (displayValue == null || displayTitle == null) {
log.warn("Ignoring invalid form markup: '" + inputHtml + "'");
continue;
}
inputHtml = inputHtml.contains(EMPTY_VALUE_TAG) ? inputHtml.replace(EMPTY_VALUE_TAG, displayValue) : inputHtml.replace(TAG_CLOSE, (displayValue + TAG_CLOSE));
inputHtml = inputHtml.contains(EMPTY_TITLE_TAG) ? inputHtml.replace(EMPTY_TITLE_TAG, displayTitle) : inputHtml.replace(TAG_CLOSE, (displayTitle + TAG_CLOSE));
inputMatcher.appendReplacement(sb, inputHtml);
}
inputMatcher.appendTail(sb);
return sb.toString();
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DefaultDataEntryFormService method prepareDataEntryFormForEntry.
@Override
public String prepareDataEntryFormForEntry(DataEntryForm dataEntryForm, DataSet dataSet, I18n i18n) {
if (dataEntryForm == null || !dataEntryForm.hasForm() || dataSet == null) {
return null;
}
// ---------------------------------------------------------------------
// Inline javascript/html to add to HTML before output
// ---------------------------------------------------------------------
Map<String, DataElement> dataElementMap = Maps.uniqueIndex(dataSet.getDataElements(), de -> de.getUid());
CachingMap<String, DataElementCategoryOptionCombo> optionComboMap = new CachingMap<>();
optionComboMap.putAll(IdentifiableObjectUtils.getUidObjectMap(dataSet.getDataElementOptionCombos()));
int i = 1;
StringBuffer sb = new StringBuffer();
Matcher inputMatcher = INPUT_PATTERN.matcher(dataEntryForm.getHtmlCode());
while (inputMatcher.find()) {
// -----------------------------------------------------------------
// Get HTML input field code
// -----------------------------------------------------------------
String inputHtml = inputMatcher.group();
Matcher identifierMatcher = IDENTIFIER_PATTERN.matcher(inputHtml);
Matcher dataElementTotalMatcher = DATAELEMENT_TOTAL_PATTERN.matcher(inputHtml);
Matcher indicatorMatcher = INDICATOR_PATTERN.matcher(inputHtml);
if (identifierMatcher.find() && identifierMatcher.groupCount() > 0) {
String dataElementId = identifierMatcher.group(1);
String optionComboId = identifierMatcher.group(2);
DataElement dataElement = dataElementMap.get(dataElementId);
if (dataElement == null) {
return i18n.getString("dataelement_with_id") + ": " + dataElementId + " " + i18n.getString("does_not_exist_in_data_set");
}
DataElementCategoryOptionCombo categoryOptionCombo = optionComboMap.get(optionComboId, () -> idObjectManager.getObject(DataElementCategoryOptionCombo.class, IdScheme.UID, optionComboId));
if (categoryOptionCombo == null) {
return i18n.getString("category_option_combo_with_id") + ": " + optionComboId + " " + i18n.getString("does_not_exist_in_data_set");
}
if (dataSet.isDataElementDecoration() && dataElement.hasDescription()) {
String titleTag = " title=\"" + escapeHtml3(dataElement.getDisplayDescription()) + "\" ";
inputHtml = inputHtml.replaceAll("title=\".*?\"", "").replace(TAG_CLOSE, titleTag + TAG_CLOSE);
}
String appendCode = "";
ValueType valueType = dataElement.getValueType();
if (ValueType.BOOLEAN == valueType) {
inputHtml = inputHtml.replaceAll(inputHtml, TAG_CLOSE);
appendCode += "<label>";
appendCode += "<input type=\"radio\" class=\"entryselect\" name=\"" + dataElementId + "-" + optionComboId + "-val\" id=\"" + dataElementId + "-" + optionComboId + "-val\" tabindex=\"" + i++ + "\" value=\"\">";
appendCode += i18n.getString("no_value");
appendCode += "</label>";
appendCode += "<label>";
appendCode += "<input type=\"radio\" class=\"entryselect\" name=\"" + dataElementId + "-" + optionComboId + "-val\" id=\"" + dataElementId + "-" + optionComboId + "-val\" tabindex=\"" + i++ + "\" value=\"true\">";
appendCode += i18n.getString("yes");
appendCode += "</label>";
appendCode += "<label>";
appendCode += "<input type=\"radio\" class=\"entryselect\" name=\"" + dataElementId + "-" + optionComboId + "-val\" id=\"" + dataElementId + "-" + optionComboId + "-val\" tabindex=\"" + i++ + "\" value=\"false\">";
appendCode += i18n.getString("no");
appendCode += "</label>";
} else if (ValueType.TRUE_ONLY == valueType) {
appendCode += " name=\"entrytrueonly\" class=\"entrytrueonly\" type=\"checkbox\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
} else if (dataElement.hasOptionSet()) {
appendCode += " name=\"entryoptionset\" class=\"entryoptionset\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
} else if (ValueType.LONG_TEXT == valueType) {
inputHtml = inputHtml.replace("input", "textarea");
appendCode += " name=\"entryfield\" class=\"entryfield entryarea\" tabindex=\"" + i++ + "\"" + "></textarea>";
} else if (ValueType.FILE_RESOURCE == valueType) {
inputHtml = inputHtml.replace("input", "div");
appendCode += " class=\"entryfileresource\" tabindex=\"" + i++ + "\">" + "<input class=\"entryfileresource-input\" id=\"input-" + dataElementId + "-" + optionComboId + "-val\">" + "<div class=\"upload-field\">" + "<div class=\"upload-fileinfo\">" + "<div class=\"upload-fileinfo-size\"></div>" + "<div class=\"upload-fileinfo-name\"></div>" + "</div>" + "<div class=\"upload-progress\">" + "<div class=\"upload-progress-bar\"></div>" + "<div class=\"upload-progress-info\"></div>" + "</div>" + "</div>" + "<div class=\"upload-button-group\">" + "<button class=\"upload-button\"></button>" + "</div>" + "<input type=\"file\" style=\"display: none;\">" + "</div>";
} else if (ValueType.TIME == valueType) {
appendCode += " type=\"text\" name=\"entrytime\" class=\"entrytime\" tabindex=\"" + i++ + "\" id=\"" + dataElementId + "-" + optionComboId + "\">";
} else if (ValueType.URL == valueType) {
appendCode += " type=\"url\" name=\"entryfield\" class=\"entryfield\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
} else {
appendCode += " type=\"text\" name=\"entryfield\" class=\"entryfield\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
}
inputHtml = inputHtml.replace(TAG_CLOSE, appendCode);
inputHtml += "<span id=\"" + dataElement.getUid() + "-dataelement\" style=\"display:none\">" + dataElement.getFormNameFallback() + "</span>";
inputHtml += "<span id=\"" + categoryOptionCombo.getUid() + "-optioncombo\" style=\"display:none\">" + categoryOptionCombo.getName() + "</span>";
} else if (dataElementTotalMatcher.find() && dataElementTotalMatcher.groupCount() > 0) {
inputHtml = inputHtml.replace(TAG_CLOSE, " type=\"text\" class=\"dataelementtotal\"" + TAG_CLOSE);
} else if (indicatorMatcher.find() && indicatorMatcher.groupCount() > 0) {
inputHtml = inputHtml.replace(TAG_CLOSE, " type=\"text\" class=\"indicator\"" + TAG_CLOSE);
}
inputMatcher.appendReplacement(sb, inputHtml);
}
inputMatcher.appendTail(sb);
return sb.toString();
}
Aggregations