Search in sources :

Example 1 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class DefaultAttributeService method validateAttributeValues.

@Override
public <T extends IdentifiableObject> List<ErrorReport> validateAttributeValues(T object, Set<AttributeValue> attributeValues) {
    List<ErrorReport> errorReports = new ArrayList<>();
    if (attributeValues.isEmpty()) {
        return errorReports;
    }
    Map<String, AttributeValue> attributeValueMap = attributeValues.stream().filter(av -> av.getAttribute() != null).collect(Collectors.toMap(av -> av.getAttribute().getUid(), av -> av));
    Iterator<AttributeValue> iterator = object.getAttributeValues().iterator();
    List<Attribute> mandatoryAttributes = getMandatoryAttributes(object.getClass());
    while (iterator.hasNext()) {
        AttributeValue attributeValue = iterator.next();
        if (attributeValue.getAttribute() != null && attributeValueMap.containsKey(attributeValue.getAttribute().getUid())) {
            AttributeValue av = attributeValueMap.get(attributeValue.getAttribute().getUid());
            if (attributeValue.isUnique()) {
                if (!manager.isAttributeValueUnique(object.getClass(), object, attributeValue.getAttribute(), av.getValue())) {
                    errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4009, attributeValue.getAttribute().getUid(), av.getValue()).setMainId(attributeValue.getAttribute().getUid()).setErrorProperty("value"));
                }
            }
            attributeValueMap.remove(attributeValue.getAttribute().getUid());
            mandatoryAttributes.remove(attributeValue.getAttribute());
        }
    }
    for (String uid : attributeValueMap.keySet()) {
        AttributeValue attributeValue = attributeValueMap.get(uid);
        if (attributeValue.getAttribute() != null && !attributeValue.getAttribute().getSupportedClasses().contains(object.getClass())) {
            errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4010, attributeValue.getAttribute().getUid(), object.getClass().getSimpleName()).setMainId(uid).setErrorProperty("id"));
        } else {
            mandatoryAttributes.remove(attributeValue.getAttribute());
        }
    }
    mandatoryAttributes.forEach(att -> errorReports.add(new ErrorReport(Attribute.class, ErrorCode.E4011, att.getUid()).setMainId(att.getUid())));
    return errorReports;
}
Also used : ErrorReport(org.hisp.dhis.feedback.ErrorReport) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) NonUniqueAttributeValueException(org.hisp.dhis.attribute.exception.NonUniqueAttributeValueException) Iterator(java.util.Iterator) ErrorReport(org.hisp.dhis.feedback.ErrorReport) ValueType(org.hisp.dhis.common.ValueType) Predicate(java.util.function.Predicate) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) MissingMandatoryAttributeValueException(org.hisp.dhis.attribute.exception.MissingMandatoryAttributeValueException) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) Maps(com.google.api.client.util.Maps) ErrorCode(org.hisp.dhis.feedback.ErrorCode) Transactional(org.springframework.transaction.annotation.Transactional) ArrayList(java.util.ArrayList)

Example 2 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class DefaultRenderService method fromMetadata.

@Override
@SuppressWarnings("unchecked")
public Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> fromMetadata(InputStream inputStream, RenderFormat format) throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> map = new HashMap<>();
    ObjectMapper mapper;
    if (RenderFormat.JSON == format) {
        mapper = jsonMapper;
    } else if (RenderFormat.XML == format) {
        throw new IllegalArgumentException("XML format is not supported.");
    } else {
        return map;
    }
    JsonNode rootNode = mapper.readTree(inputStream);
    Iterator<String> fieldNames = rootNode.fieldNames();
    while (fieldNames.hasNext()) {
        String fieldName = fieldNames.next();
        JsonNode node = rootNode.get(fieldName);
        Schema schema = schemaService.getSchemaByPluralName(fieldName);
        if (schema == null || !schema.isIdentifiableObject()) {
            log.info("Skipping unknown property '" + fieldName + "'.");
            continue;
        }
        if (!schema.isMetadata()) {
            log.debug("Skipping non-metadata property `" + fieldName + "`.");
            continue;
        }
        List<IdentifiableObject> collection = new ArrayList<>();
        for (JsonNode item : node) {
            IdentifiableObject value = mapper.treeToValue(item, (Class<? extends IdentifiableObject>) schema.getKlass());
            if (value != null)
                collection.add(value);
        }
        map.put((Class<? extends IdentifiableObject>) schema.getKlass(), collection);
    }
    return map;
}
Also used : HashMap(java.util.HashMap) Schema(org.hisp.dhis.schema.Schema) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) ArrayList(java.util.ArrayList) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class PreheatServiceTest method testPreheatWithDataSetElements.

@Test
public void testPreheatWithDataSetElements() {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = new HashMap<>();
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    manager.save(de1);
    manager.save(de2);
    manager.save(de3);
    DataSet dataSet = createDataSet('A');
    dataSet.setAutoFields();
    dataSet.getDataSetElements().add(new DataSetElement(dataSet, de1));
    dataSet.getDataSetElements().add(new DataSetElement(dataSet, de2));
    dataSet.getDataSetElements().add(new DataSetElement(dataSet, de3));
    metadata.put(DataSet.class, new ArrayList<>());
    metadata.get(DataSet.class).add(dataSet);
    PreheatParams params = new PreheatParams();
    params.setPreheatIdentifier(PreheatIdentifier.UID);
    params.setPreheatMode(PreheatMode.REFERENCE);
    params.setObjects(metadata);
    preheatService.validate(params);
    Preheat preheat = preheatService.preheat(params);
    Map<String, IdentifiableObject> map = preheat.getMap().get(PreheatIdentifier.UID).get(DataElement.class);
    assertEquals(3, map.size());
}
Also used : HashMap(java.util.HashMap) DataSet(org.hisp.dhis.dataset.DataSet) DataSetElement(org.hisp.dhis.dataset.DataSetElement) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 4 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class PreheatServiceTest method testCollectReferenceUidDEG2.

@Test
public void testCollectReferenceUidDEG2() {
    DataElementGroup deg1 = createDataElementGroup('A');
    DataElementGroup deg2 = createDataElementGroup('B');
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    deg1.addDataElement(de1);
    deg1.addDataElement(de2);
    deg2.addDataElement(de3);
    Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(Lists.newArrayList(deg1, deg2)).get(PreheatIdentifier.UID);
    assertTrue(references.containsKey(DataElement.class));
    assertEquals(3, references.get(DataElement.class).size());
    assertTrue(references.get(DataElement.class).contains(de1.getUid()));
    assertTrue(references.get(DataElement.class).contains(de2.getUid()));
    assertTrue(references.get(DataElement.class).contains(de3.getUid()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataSet(org.hisp.dhis.dataset.DataSet) LegendSet(org.hisp.dhis.legend.LegendSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 5 with IdentifiableObject

use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.

the class PreheatServiceTest method testCollectReferenceCodeDEG1.

@Test
public void testCollectReferenceCodeDEG1() {
    DataElementGroup dataElementGroup = createDataElementGroup('A');
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    User user = createUser('A');
    dataElementGroup.addDataElement(de1);
    dataElementGroup.addDataElement(de2);
    dataElementGroup.addDataElement(de3);
    dataElementGroup.setUser(user);
    Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(dataElementGroup).get(PreheatIdentifier.CODE);
    assertTrue(references.containsKey(DataElement.class));
    assertTrue(references.containsKey(User.class));
    assertEquals(3, references.get(DataElement.class).size());
    assertEquals(1, references.get(User.class).size());
    assertTrue(references.get(DataElement.class).contains(de1.getCode()));
    assertTrue(references.get(DataElement.class).contains(de2.getCode()));
    assertTrue(references.get(DataElement.class).contains(de3.getCode()));
    assertTrue(references.get(User.class).contains(user.getCode()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) LegendSet(org.hisp.dhis.legend.LegendSet) Set(java.util.Set) OptionSet(org.hisp.dhis.option.OptionSet) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)124 List (java.util.List)76 Test (org.junit.Test)67 DhisSpringTest (org.hisp.dhis.DhisSpringTest)64 ClassPathResource (org.springframework.core.io.ClassPathResource)54 DataElement (org.hisp.dhis.dataelement.DataElement)44 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)39 User (org.hisp.dhis.user.User)37 DataSet (org.hisp.dhis.dataset.DataSet)24 ArrayList (java.util.ArrayList)22 ObjectReport (org.hisp.dhis.feedback.ObjectReport)22 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)22 Schema (org.hisp.dhis.schema.Schema)19 HashMap (java.util.HashMap)18 TypeReport (org.hisp.dhis.feedback.TypeReport)18 Set (java.util.Set)15 ErrorReport (org.hisp.dhis.feedback.ErrorReport)15 PreheatErrorReport (org.hisp.dhis.preheat.PreheatErrorReport)15 Map (java.util.Map)14 Property (org.hisp.dhis.schema.Property)13