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;
}
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;
}
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());
}
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()));
}
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()));
}
Aggregations