Search in sources :

Example 56 with DataElement

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) ValueType(org.hisp.dhis.common.ValueType) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)

Example 57 with DataElement

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();
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Matcher(java.util.regex.Matcher) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Constant(org.hisp.dhis.constant.Constant)

Example 58 with DataElement

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;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement)

Example 59 with DataElement

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);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet)

Example 60 with DataElement

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));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DataElement (org.hisp.dhis.dataelement.DataElement)254 Test (org.junit.Test)137 DhisSpringTest (org.hisp.dhis.DhisSpringTest)99 User (org.hisp.dhis.user.User)51 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)47 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)44 DataSet (org.hisp.dhis.dataset.DataSet)39 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)36 ArrayList (java.util.ArrayList)31 List (java.util.List)31 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HashSet (java.util.HashSet)23 Period (org.hisp.dhis.period.Period)20 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)19 DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)18 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)16 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)15 DataValue (org.hisp.dhis.datavalue.DataValue)15 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)14 Matcher (java.util.regex.Matcher)13