Search in sources :

Example 16 with AttributeValue

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

the class DataElementStoreTest method testAttributeValueFromAttribute.

@Test
public void testAttributeValueFromAttribute() 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.getAttributeValueByAttribute(attribute);
    assertEquals(3, 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 17 with AttributeValue

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

the class IdentifiableObjectBundleHook method handleAttributeValues.

private void handleAttributeValues(IdentifiableObject identifiableObject, ObjectBundle bundle, Schema schema) {
    Session session = sessionFactory.getCurrentSession();
    if (!schema.havePersistedProperty("attributeValues"))
        return;
    Iterator<AttributeValue> iterator = identifiableObject.getAttributeValues().iterator();
    while (iterator.hasNext()) {
        AttributeValue attributeValue = iterator.next();
        // if value null or empty, just skip it
        if (StringUtils.isEmpty(attributeValue.getValue())) {
            iterator.remove();
            continue;
        }
        Attribute attribute = bundle.getPreheat().get(bundle.getPreheatIdentifier(), attributeValue.getAttribute());
        if (attribute == null) {
            iterator.remove();
            continue;
        }
        attributeValue.setAttribute(attribute);
        session.save(attributeValue);
        session.flush();
    }
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute) Session(org.hibernate.Session)

Aggregations

AttributeValue (org.hisp.dhis.attribute.AttributeValue)17 Attribute (org.hisp.dhis.attribute.Attribute)13 Test (org.junit.Test)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)6 ArrayList (java.util.ArrayList)5 DataElement (org.hisp.dhis.dataelement.DataElement)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)3 User (org.hisp.dhis.user.User)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ErrorReport (org.hisp.dhis.feedback.ErrorReport)2 OrganisationUnitGroup (org.hisp.dhis.organisationunit.OrganisationUnitGroup)2 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1