use of org.hisp.dhis.common.ValueType in project dhis2-core by dhis2.
the class DefaultTrackedEntityInstanceService method validateEnrollment.
@Override
public ValidationCriteria validateEnrollment(TrackedEntityInstance instance, Program program) {
for (ValidationCriteria criteria : program.getValidationCriteria()) {
for (TrackedEntityAttributeValue attributeValue : instance.getTrackedEntityAttributeValues()) {
if (attributeValue.getAttribute().getUid().equals(criteria.getProperty())) {
String value = attributeValue.getValue();
ValueType valueType = attributeValue.getAttribute().getValueType();
if (valueType.isNumeric()) {
int value1 = Integer.parseInt(value);
int value2 = Integer.parseInt(criteria.getValue());
if ((criteria.getOperator() == ValidationCriteria.OPERATOR_LESS_THAN && value1 >= value2) || (criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO && value1 != value2) || (criteria.getOperator() == ValidationCriteria.OPERATOR_GREATER_THAN && value1 <= value2)) {
return criteria;
}
} else if (valueType.isDate()) {
Date value1 = DateUtils.parseDate(value);
Date value2 = DateUtils.parseDate(criteria.getValue());
int i = value1.compareTo(value2);
if (i != criteria.getOperator()) {
return criteria;
}
} else {
if (criteria.getOperator() == ValidationCriteria.OPERATOR_EQUAL_TO && !value.equals(criteria.getValue())) {
return criteria;
}
}
}
}
}
return null;
}
use of org.hisp.dhis.common.ValueType in project dhis2-core by dhis2.
the class DefaultTrackedEntityAttributeService method validateValueType.
@Override
@Transactional(readOnly = true)
public String validateValueType(TrackedEntityAttribute trackedEntityAttribute, String value) {
Assert.notNull(trackedEntityAttribute, "tracked entity attribute is required");
ValueType valueType = trackedEntityAttribute.getValueType();
String errorValue = StringUtils.substring(value, 0, 30);
if (value.length() > VALUE_MAX_LENGTH) {
return "Value length is greater than 50000 chars for attribute " + trackedEntityAttribute.getUid();
}
if (ValueType.NUMBER == valueType && !MathUtils.isNumeric(value)) {
return "Value '" + errorValue + "' is not a valid numeric type for attribute " + trackedEntityAttribute.getUid();
} else if (ValueType.BOOLEAN == valueType && !MathUtils.isBool(value)) {
return "Value '" + errorValue + "' is not a valid boolean type for attribute " + trackedEntityAttribute.getUid();
} else if (ValueType.DATE == valueType && DateUtils.parseDate(value) == null) {
return "Value '" + errorValue + "' is not a valid date type for attribute " + trackedEntityAttribute.getUid();
} else if (ValueType.TRUE_ONLY == valueType && !"true".equals(value)) {
return "Value '" + errorValue + "' is not true (true-only type) for attribute " + trackedEntityAttribute.getUid();
} else if (ValueType.USERNAME == valueType) {
if (userService.getUserByUsername(value) == null) {
return "Value '" + errorValue + "' is not a valid username for attribute " + trackedEntityAttribute.getUid();
}
} else if (ValueType.DATE == valueType && !DateUtils.dateIsValid(value)) {
return "Value '" + errorValue + "' is not a valid date for attribute " + trackedEntityAttribute.getUid();
} else if (ValueType.DATETIME == valueType && !DateUtils.dateTimeIsValid(value)) {
return "Value '" + errorValue + "' is not a valid datetime for attribute " + trackedEntityAttribute.getUid();
} else if (ValueType.IMAGE == valueType) {
return validateImage(value);
} else if (null != trackedEntityAttribute.getOptionSet() && trackedEntityAttribute.getOptionSet().getOptions().stream().filter(Objects::nonNull).noneMatch(o -> o.getCode().equalsIgnoreCase(value))) {
return "Value '" + errorValue + "' is not a valid option for attribute " + trackedEntityAttribute.getUid() + " and option set " + trackedEntityAttribute.getOptionSet().getUid();
} else if (ValueType.FILE_RESOURCE == valueType && fileResourceService.getFileResource(value) == null) {
return "Value '" + value + "' is not a valid file resource.";
} else if (ValueType.ORGANISATION_UNIT == valueType && organisationUnitService.getOrganisationUnit(value) == null) {
return "Value '" + value + "' is not a valid organisation unit.";
}
return null;
}
use of org.hisp.dhis.common.ValueType in project dhis2-core by dhis2.
the class ProgramNotificationTemplateObjectBundleHook method postProcess.
private void postProcess(ProgramNotificationTemplate template) {
ProgramNotificationRecipient pnr = template.getNotificationRecipient();
ValueType valueType = null;
if (RECIPIENT_TO_VALUETYPE_RESOLVER.containsKey(pnr)) {
Function<ProgramNotificationTemplate, ValueType> resolver = RECIPIENT_TO_VALUETYPE_RESOLVER.get(pnr);
valueType = resolver.apply(template);
}
template.setDeliveryChannels(CHANNEL_MAPPER.getOrDefault(valueType, Sets.newHashSet()));
}
use of org.hisp.dhis.common.ValueType in project dhis2-core by dhis2.
the class DefaultQueryItemLocator method getTrackedEntityAttribute.
private Optional<QueryItem> getTrackedEntityAttribute(String dimension, Program program, LegendSet legendSet) {
QueryItem qi = null;
TrackedEntityAttribute at = attributeService.getTrackedEntityAttribute(getSecondElement(dimension));
if (at != null && program.containsAttribute(at)) {
ValueType valueType = legendSet != null ? ValueType.TEXT : at.getValueType();
qi = new QueryItem(at, program, legendSet, valueType, at.getAggregationType(), at.getOptionSet());
ProgramStage programStage = getProgramStageOrFail(dimension);
if (programStage != null) {
qi.setProgramStage(programStage);
}
}
return Optional.ofNullable(qi);
}
use of org.hisp.dhis.common.ValueType in project dhis2-core by dhis2.
the class DefaultDataEntryFormService method prepareDataEntryFormForEntry.
@Override
@Transactional(readOnly = true)
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
// ---------------------------------------------------------------------
List<String> compulsoryDataElementOperands = dataSet.getCompulsoryDataElementOperands().stream().map(DataElementOperand::getDimensionItem).collect(Collectors.toList());
Map<String, DataElement> dataElementMap = Maps.uniqueIndex(dataSet.getDataElements(), de -> de.getUid());
CachingMap<String, CategoryOptionCombo> 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");
}
CategoryOptionCombo categoryOptionCombo = optionComboMap.get(optionComboId, () -> idObjectManager.getObject(CategoryOptionCombo.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 = "", inputFieldId = dataElementId + "-" + optionComboId;
ValueType valueType = dataElement.getValueType();
String required = compulsoryDataElementOperands.contains(dataElementId + "." + optionComboId) ? "required=\"required\"" : "";
if (ValueType.BOOLEAN == valueType) {
inputHtml = inputHtml.replaceAll(inputHtml, TAG_CLOSE);
appendCode += "<label>";
appendCode += "<input type=\"radio\" class=\"entryselect\"" + required + " name=\"" + inputFieldId + "-val\" id=\"" + inputFieldId + "-val\" tabindex=\"" + i++ + "\" value=\"true\">";
appendCode += i18n.getString("yes");
appendCode += "</label>";
appendCode += "<label>";
appendCode += "<input type=\"radio\" class=\"entryselect\"" + required + " name=\"" + inputFieldId + "-val\" " + " id=\"" + inputFieldId + "-val\" tabindex=\"" + i++ + "\" value=\"false\">";
appendCode += i18n.getString("no");
appendCode += "</label>";
appendCode += "<img class=\"commentlink\" id=\"" + inputFieldId + "-comment\" " + "src=\"../images/comment.png\" title=\"View " + "comment\" style=\"cursor: pointer;\"" + TAG_CLOSE;
} else if (ValueType.TRUE_ONLY == valueType) {
appendCode += " name=\"entrytrueonly\" class=\"entrytrueonly\"" + required + "type=\"checkbox\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
} else if (dataElement.hasOptionSet()) {
appendCode += " name=\"entryoptionset\"" + required + " class=\"entryoptionset\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
appendCode += "<img class=\"commentlink\" id=\"" + inputFieldId + "-comment\" " + "src=\"../images/comment.png\" title=\"View " + "comment\" style=\"cursor: pointer;\"" + TAG_CLOSE;
} else if (ValueType.LONG_TEXT == valueType) {
inputHtml = inputHtml.replace("input", "textarea");
appendCode += " name=\"entryfield\"" + required + " class=\"entryfield entryarea\" tabindex=\"" + i++ + "\"" + "></textarea>";
} else if (valueType.isFile()) {
inputHtml = inputHtml.replace("input", "div");
appendCode += " class=\"entryfileresource\" tabindex=\"" + i++ + "\">" + "<input " + required + " class=\"entryfileresource-input\" id=\"input-" + inputFieldId + "-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=\"time\" name=\"entrytime\"" + required + " class=\"entrytime\" tabindex=\"" + i++ + "\" id=\"" + inputFieldId + "\">";
} else if (ValueType.DATETIME == valueType) {
appendCode += " type=\"text\" name=\"entryfield\"" + required + " class=\"entryfield\" tabindex=\"" + i++ + "\"> ";
appendCode += "<input type=\"time\" name=\"entrytime\"" + required + " class=\"entrytime\" tabindex=\"" + i++ + "\" id=\"" + inputFieldId + "-time" + "\">";
} else if (ValueType.URL == valueType) {
appendCode += " type=\"url\" name=\"entryfield\"" + required + " class=\"entryfield\" tabindex=\"" + i++ + "\"" + TAG_CLOSE;
} else {
appendCode += " type=\"text\" name=\"entryfield\"" + required + " 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