Search in sources :

Example 1 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class DataElementCategoryOptionComboServiceTest method testAddAttributeValue.

@Test
public void testAddAttributeValue() {
    categoryOptionComboA = new DataElementCategoryOptionCombo();
    Set<DataElementCategoryOption> categoryOptions = Sets.newHashSet(categoryOptionA, categoryOptionB);
    categoryOptionComboA.setCategoryCombo(categoryComboA);
    categoryOptionComboA.setCategoryOptions(categoryOptions);
    int id = categoryService.addDataElementCategoryOptionCombo(categoryOptionComboA);
    categoryOptionComboA = categoryService.getDataElementCategoryOptionCombo(id);
    Attribute attribute1 = new Attribute("attribute 1", ValueType.TEXT);
    attribute1.setCategoryOptionComboAttribute(true);
    attributeService.addAttribute(attribute1);
    AttributeValue avA = new AttributeValue("value 1");
    avA.setAttribute(attribute1);
    categoryOptionComboA.getAttributeValues().add(avA);
    categoryService.updateDataElementCategoryOptionCombo(categoryOptionComboA);
    categoryOptionComboA = categoryService.getDataElementCategoryOptionCombo(id);
    assertFalse(categoryOptionComboA.getAttributeValues().isEmpty());
    categoryOptionComboA.getAttributeValues().clear();
    categoryService.updateDataElementCategoryOptionCombo(categoryOptionComboA);
    categoryOptionComboA = categoryService.getDataElementCategoryOptionCombo(id);
    assertTrue(categoryOptionComboA.getAttributeValues().isEmpty());
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class DataElementStoreTest method testDataElementFromAttribute.

// Fails with expected:<null> but was:<{"class":"class org.hisp.dhis.dataelement.DataElement"
@Ignore
@Test
public void testDataElementFromAttribute() throws NonUniqueAttributeValueException {
    Attribute attribute = new Attribute("test", ValueType.TEXT);
    attribute.setDataElementAttribute(true);
    attributeService.addAttribute(attribute);
    DataElement dataElementA = createDataElement('A');
    DataElement dataElementB = createDataElement('B');
    dataElementStore.save(dataElementA);
    dataElementStore.save(dataElementB);
    AttributeValue attributeValue = new AttributeValue("SOME VALUE", attribute);
    attributeService.addAttributeValue(dataElementA, attributeValue);
    dataElementA.getAttributeValues().add(attributeValue);
    dataElementStore.update(dataElementA);
    DataElement dataElement = dataElementStore.getByAttribute(attribute);
    assertEquals(dataElement, dataElementA);
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) Ignore(org.junit.Ignore) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 3 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class DataElementStoreTest method testAttributeValueFromAttributeAndValue.

@Test
public void testAttributeValueFromAttributeAndValue() throws NonUniqueAttributeValueException {
    Attribute attribute = new Attribute("test", ValueType.TEXT);
    attribute.setDataElementAttribute(true);
    attributeService.addAttribute(attribute);
    DataElement dataElementA = createDataElement('A');
    DataElement dataElementB = createDataElement('B');
    DataElement dataElementC = createDataElement('C');
    dataElementStore.save(dataElementA);
    dataElementStore.save(dataElementB);
    dataElementStore.save(dataElementC);
    AttributeValue attributeValueA = new AttributeValue("SOME VALUE", attribute);
    AttributeValue attributeValueB = new AttributeValue("SOME VALUE", attribute);
    AttributeValue attributeValueC = new AttributeValue("ANOTHER VALUE", attribute);
    attributeService.addAttributeValue(dataElementA, attributeValueA);
    attributeService.addAttributeValue(dataElementB, attributeValueB);
    attributeService.addAttributeValue(dataElementC, attributeValueC);
    dataElementStore.update(dataElementA);
    dataElementStore.update(dataElementB);
    dataElementStore.update(dataElementC);
    List<AttributeValue> values = dataElementStore.getAttributeValueByAttributeAndValue(attribute, "SOME VALUE");
    assertEquals(2, values.size());
    values = dataElementStore.getAttributeValueByAttributeAndValue(attribute, "ANOTHER VALUE");
    assertEquals(1, values.size());
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class OrganisationUnitLocationController method getEntitiesWithinRange.

/**
     * Get Organisation Units within a distance from a location
     */
@RequestMapping(value = "/withinRange", method = RequestMethod.GET, produces = { "*/*", "application/json" })
public void getEntitiesWithinRange(@RequestParam Double longitude, @RequestParam Double latitude, @RequestParam Double distance, @RequestParam(required = false) String orgUnitGroupSetId, HttpServletResponse response) throws Exception {
    List<OrganisationUnit> entityList = new ArrayList<>(organisationUnitService.getOrganisationUnitWithinDistance(longitude, latitude, distance));
    for (OrganisationUnit organisationUnit : entityList) {
        Set<AttributeValue> attributeValues = organisationUnit.getAttributeValues();
        attributeValues.clear();
        if (orgUnitGroupSetId != null) {
            for (OrganisationUnitGroup organisationUnitGroup : organisationUnit.getGroups()) {
                for (OrganisationUnitGroupSet orgunitGroupSet : organisationUnitGroup.getGroupSets()) {
                    if (orgunitGroupSet.getUid().compareTo(orgUnitGroupSetId) == 0) {
                        AttributeValue attributeValue = new AttributeValue();
                        // attributeValue.setAttribute( new Attribute( ORGUNIGROUP_SYMBOL, ORGUNIGROUP_SYMBOL ) );
                        attributeValue.setAttribute(new Attribute(ORGUNIGROUP_SYMBOL, ValueType.TEXT));
                        attributeValue.setValue(organisationUnitGroup.getSymbol());
                        attributeValues.add(attributeValue);
                    }
                }
            }
        }
        organisationUnit.setAttributeValues(attributeValues);
        // Clear out all data not needed for this task
        organisationUnit.removeAllDataSets();
        organisationUnit.removeAllUsers();
        organisationUnit.removeAllOrganisationUnitGroups();
    }
    renderService.toJson(response.getOutputStream(), entityList);
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) OrganisationUnitGroupSet(org.hisp.dhis.organisationunit.OrganisationUnitGroupSet) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with Attribute

use of org.hisp.dhis.attribute.Attribute in project dhis2-core by dhis2.

the class DefaultObjectBundleValidationService method checkUniqueAttributes.

private List<ErrorReport> checkUniqueAttributes(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (object == null || Preheat.isDefault(object) || !preheat.getUniqueAttributes().containsKey(klass)) {
        return errorReports;
    }
    Set<AttributeValue> attributeValues = object.getAttributeValues();
    // make copy for modification
    List<String> uniqueAttributes = new ArrayList<>(preheat.getUniqueAttributes().get(klass));
    if (!preheat.getUniqueAttributeValues().containsKey(klass)) {
        preheat.getUniqueAttributeValues().put(klass, new HashMap<>());
    }
    Map<String, Map<String, String>> uniqueAttributeValues = preheat.getUniqueAttributeValues().get(klass);
    if (uniqueAttributes.isEmpty()) {
        return errorReports;
    }
    attributeValues.forEach(attributeValue -> {
        Attribute attribute = preheat.get(identifier, attributeValue.getAttribute());
        if (attribute == null || !attribute.isUnique() || StringUtils.isEmpty(attributeValue.getValue())) {
            return;
        }
        if (uniqueAttributeValues.containsKey(attribute.getUid())) {
            Map<String, String> values = uniqueAttributeValues.get(attribute.getUid());
            if (values.containsKey(attributeValue.getValue()) && !values.get(attributeValue.getValue()).equals(object.getUid())) {
                errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4009, IdentifiableObjectUtils.getDisplayName(attribute), attributeValue.getValue()).setMainId(attribute.getUid()).setErrorProperty("value"));
            } else {
                uniqueAttributeValues.get(attribute.getUid()).put(attributeValue.getValue(), object.getUid());
            }
        } else {
            uniqueAttributeValues.put(attribute.getUid(), new HashMap<>());
            uniqueAttributeValues.get(attribute.getUid()).put(attributeValue.getValue(), object.getUid());
        }
    });
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) PreheatErrorReport(org.hisp.dhis.preheat.PreheatErrorReport) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Attribute (org.hisp.dhis.attribute.Attribute)56 Test (org.junit.jupiter.api.Test)28 AttributeValue (org.hisp.dhis.attribute.AttributeValue)27 HashMap (java.util.HashMap)13 DhisSpringTest (org.hisp.dhis.DhisSpringTest)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 List (java.util.List)8 DataElement (org.hisp.dhis.dataelement.DataElement)8 ArrayList (java.util.ArrayList)7 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)7 Property (org.hisp.dhis.schema.Property)7 Schema (org.hisp.dhis.schema.Schema)7 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)6 OrganisationUnitGroupSet (org.hisp.dhis.organisationunit.OrganisationUnitGroupSet)6 Test (org.junit.Test)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Lists (com.google.common.collect.Lists)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4