Search in sources :

Example 36 with Attribute

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

the class QueryUtilsTest method setUp.

@BeforeEach
void setUp() {
    schema = new Schema(Attribute.class, "attribute", "attributes");
    Property property = new Property(String.class);
    property.setName("value1");
    property.setSimple(true);
    schema.addProperty(property);
    property = new Property(String.class);
    property.setName("value2");
    property.setSimple(false);
    schema.addProperty(property);
    property = new Property(String.class);
    property.setName("value3");
    property.setSimple(true);
    schema.addProperty(property);
    property = new Property(Integer.class);
    property.setName("value4");
    property.setSimple(true);
    schema.addProperty(property);
    property = new Property(String.class);
    property.setName("value5");
    property.setSimple(true);
    schema.addProperty(property);
    property = new Property(String.class);
    property.setName("value6");
    property.setSimple(true);
    schema.addProperty(property);
    property = new Property(String.class);
    property.setName("value7");
    property.setSimple(true);
    schema.addProperty(property);
}
Also used : Attribute(org.hisp.dhis.attribute.Attribute) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 37 with Attribute

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

the class DefaultQueryPlannerTest method verifyPlanQueryReturnsNonPersistedQueryWithCriterion.

/*
     * Verifies that when adding criteria on non-persisted fields and using OR
     * junction type the planner returns a "non Persisted Query" containing all
     * the criteria - since it will execute filter on the entire dataset from
     * the target table
     */
@Test
void verifyPlanQueryReturnsNonPersistedQueryWithCriterion() throws Exception {
    // Create schema with attributes
    final Attribute attribute = new Attribute();
    final Map<String, Property> propertyMap = new HashMap<>();
    addProperty(propertyMap, attribute, "id", true);
    addProperty(propertyMap, attribute, "uid", true);
    // note that this is a non-persisted attribute!
    addProperty(propertyMap, attribute, "name", false);
    Schema schema = new OrganisationUnitSchemaDescriptor().getSchema();
    schema.setPropertyMap(propertyMap);
    // Add restrictions on a non persisted field
    Query query = Query.from(schema, Junction.Type.OR);
    // adding a criterion on a non-persisted attribute
    query.add(Restrictions.eq("name", "test"));
    query.add(Restrictions.eq("id", 100));
    // method under test
    QueryPlan queryPlan = subject.planQuery(query, false);
    Query persistedQuery = queryPlan.getPersistedQuery();
    assertTrue(persistedQuery.isPlannedQuery());
    assertEquals(persistedQuery.getCriterions().size(), 0);
    assertEquals(persistedQuery.getFirstResult().intValue(), 0);
    assertEquals(persistedQuery.getMaxResults().intValue(), Integer.MAX_VALUE);
    assertEquals(persistedQuery.getRootJunctionType(), Junction.Type.AND);
    Query nonPersistedQuery = queryPlan.getNonPersistedQuery();
    assertEquals(nonPersistedQuery.getCriterions().size(), 2);
    assertTrue(nonPersistedQuery.isPlannedQuery());
    assertEquals(nonPersistedQuery.getRootJunctionType(), Junction.Type.OR);
}
Also used : OrganisationUnitSchemaDescriptor(org.hisp.dhis.schema.descriptors.OrganisationUnitSchemaDescriptor) Query(org.hisp.dhis.query.Query) Attribute(org.hisp.dhis.attribute.Attribute) HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) Property(org.hisp.dhis.schema.Property) Test(org.junit.jupiter.api.Test)

Example 38 with Attribute

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

the class PreheatServiceTest method defaultSetupWithAttributes.

private void defaultSetupWithAttributes() {
    Attribute attribute = new Attribute("AttributeA", ValueType.TEXT);
    attribute.setUnique(true);
    attribute.setMandatory(true);
    attribute.setDataElementAttribute(true);
    manager.save(attribute);
    AttributeValue attributeValue1 = new AttributeValue("Value1", attribute);
    AttributeValue attributeValue2 = new AttributeValue("Value2", attribute);
    AttributeValue attributeValue3 = new AttributeValue("Value3", attribute);
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    attributeService.addAttributeValue(de1, attributeValue1);
    attributeService.addAttributeValue(de2, attributeValue2);
    attributeService.addAttributeValue(de3, attributeValue3);
    manager.save(de1);
    manager.save(de2);
    manager.save(de3);
    User user = createUser('A');
    manager.save(user);
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) AttributeValue(org.hisp.dhis.attribute.AttributeValue) User(org.hisp.dhis.user.User) Attribute(org.hisp.dhis.attribute.Attribute)

Example 39 with Attribute

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

the class IdentifiableObjectBundleHook method handleAttributeValues.

private void handleAttributeValues(IdentifiableObject identifiableObject, ObjectBundle bundle, Schema schema) {
    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(), Attribute.class, attributeValue.getAttribute().getUid());
        if (attribute == null) {
            iterator.remove();
            continue;
        }
        attributeValue.setAttribute(attribute);
        attributeValue.setValue(attributeValue.getValue().replaceAll("\\s{2,}", StringUtils.SPACE));
    }
}
Also used : AttributeValue(org.hisp.dhis.attribute.AttributeValue) Attribute(org.hisp.dhis.attribute.Attribute)

Example 40 with Attribute

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

the class UniqueAttributesCheck method checkUniqueAttributes.

private List<ErrorReport> checkUniqueAttributes(Class<? extends IdentifiableObject> klass, IdentifiableObject object, Preheat preheat, PreheatIdentifier identifier) {
    if (object == null || preheat.isDefault(object) || !preheat.getUniqueAttributes().containsKey(klass)) {
        return emptyList();
    }
    if (preheat.getUniqueAttributes().get(klass).isEmpty()) {
        return emptyList();
    }
    Map<String, Map<String, String>> uniqueAttributeValues = preheat.getUniqueAttributeValues().computeIfAbsent(klass, key -> new HashMap<>());
    List<ErrorReport> errorReports = new ArrayList<>();
    object.getAttributeValues().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) 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