Search in sources :

Example 6 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 7 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 8 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 9 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)

Example 10 with Attribute

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

the class DimensionalObjectUtilsTest method testGetUidMapIsSchemeAttribute.

@Test
public void testGetUidMapIsSchemeAttribute() {
    DataElement deA = new DataElement("DataElementA");
    DataElement deB = new DataElement("DataElementB");
    DataElement deC = new DataElement("DataElementC");
    deA.setUid("A123456789A");
    deB.setUid("A123456789B");
    deC.setUid("A123456789C");
    Attribute atA = new Attribute("AttributeA", ValueType.INTEGER);
    atA.setUid("ATTR123456A");
    AttributeValue avA = new AttributeValue("AttributeValueA", atA);
    AttributeValue avB = new AttributeValue("AttributeValueB", atA);
    deA.getAttributeValues().add(avA);
    deB.getAttributeValues().add(avB);
    List<DataElement> elements = Lists.newArrayList(deA, deB, deC);
    String scheme = IdScheme.ATTR_ID_SCHEME_PREFIX + atA.getUid();
    IdScheme idScheme = IdScheme.from(scheme);
    Map<String, String> map = DimensionalObjectUtils.getDimensionItemIdSchemeMap(elements, idScheme);
    assertEquals(3, map.size());
    assertEquals("AttributeValueA", map.get("A123456789A"));
    assertEquals("AttributeValueB", map.get("A123456789B"));
    assertEquals(null, map.get("A123456789C"));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) Test(org.junit.Test)

Aggregations

Attribute (org.hisp.dhis.attribute.Attribute)16 AttributeValue (org.hisp.dhis.attribute.AttributeValue)12 Test (org.junit.Test)8 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 DataElement (org.hisp.dhis.dataelement.DataElement)4 User (org.hisp.dhis.user.User)3 ArrayList (java.util.ArrayList)2 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)2 Option (org.hisp.dhis.option.Option)2 OptionSet (org.hisp.dhis.option.OptionSet)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Session (org.hibernate.Session)1 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)1 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)1 DataSet (org.hisp.dhis.dataset.DataSet)1 ErrorReport (org.hisp.dhis.feedback.ErrorReport)1