Search in sources :

Example 16 with ValueType

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;
}
Also used : ValueType(org.hisp.dhis.common.ValueType) ValidationCriteria(org.hisp.dhis.validation.ValidationCriteria) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Date(java.util.Date)

Example 17 with ValueType

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;
}
Also used : java.util(java.util) QueryItem(org.hisp.dhis.common.QueryItem) ValueType(org.hisp.dhis.common.ValueType) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) ProgramTrackedEntityAttributeStore(org.hisp.dhis.program.ProgramTrackedEntityAttributeStore) StringUtils(org.apache.commons.lang3.StringUtils) Program(org.hisp.dhis.program.Program) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Service(org.springframework.stereotype.Service) ImageIO(javax.imageio.ImageIO) User(org.hisp.dhis.user.User) UserService(org.hisp.dhis.user.UserService) ImmutableSet(com.google.common.collect.ImmutableSet) FileResource(org.hisp.dhis.fileresource.FileResource) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) QueryOperator(org.hisp.dhis.common.QueryOperator) Collectors(java.util.stream.Collectors) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) CurrentUserService(org.hisp.dhis.user.CurrentUserService) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) AclService(org.hisp.dhis.security.acl.AclService) ProgramService(org.hisp.dhis.program.ProgramService) MathUtils(org.hisp.dhis.system.util.MathUtils) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) ValueType(org.hisp.dhis.common.ValueType) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with ValueType

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()));
}
Also used : ValueType(org.hisp.dhis.common.ValueType) ProgramNotificationTemplate(org.hisp.dhis.program.notification.ProgramNotificationTemplate) ProgramNotificationRecipient(org.hisp.dhis.program.notification.ProgramNotificationRecipient)

Example 19 with ValueType

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);
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ValueType(org.hisp.dhis.common.ValueType) ProgramStage(org.hisp.dhis.program.ProgramStage)

Example 20 with ValueType

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++ + "\">&nbsp;";
                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();
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Matcher(java.util.regex.Matcher) ValueType(org.hisp.dhis.common.ValueType) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ValueType (org.hisp.dhis.common.ValueType)20 DataElement (org.hisp.dhis.dataelement.DataElement)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 Date (java.util.Date)4 QueryItem (org.hisp.dhis.common.QueryItem)4 Matcher (java.util.regex.Matcher)3 Collectors (java.util.stream.Collectors)3 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)3 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)3 PeriodType (org.hisp.dhis.period.PeriodType)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Lists (com.google.common.collect.Lists)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 StringUtils (org.apache.commons.lang3.StringUtils)2 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)2