use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultPreheatService method handleUniqueProperties.
private Map<String, Map<Object, String>> handleUniqueProperties(Schema schema, List<IdentifiableObject> objects) {
List<Property> uniqueProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && p.isUnique() && p.isSimple()).collect(Collectors.toList());
Map<String, Map<Object, String>> map = new HashMap<>();
for (IdentifiableObject object : objects) {
uniqueProperties.forEach(property -> {
if (!map.containsKey(property.getName()))
map.put(property.getName(), new HashMap<>());
Object value = ReflectionUtils.invokeMethod(object, property.getGetterMethod());
if (value != null)
map.get(property.getName()).put(value, object.getUid());
});
}
return map;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultPreheatService method collectObjectReferences.
@SuppressWarnings("unchecked")
private Map<Class<?>, Map<String, Map<String, Object>>> collectObjectReferences(Map<Class<?>, List<?>> objects) {
Map<Class<?>, Map<String, Map<String, Object>>> map = new HashMap<>();
if (objects.isEmpty()) {
return map;
}
Map<Class<?>, List<?>> targets = new HashMap<>();
// clone objects list, we don't want to modify it
targets.putAll(objects);
collectScanTargets(targets);
for (Class<?> objectClass : targets.keySet()) {
Schema schema = schemaService.getDynamicSchema(objectClass);
if (!schema.isIdentifiableObject()) {
continue;
}
List<Property> properties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
List<IdentifiableObject> identifiableObjects = (List<IdentifiableObject>) targets.get(objectClass);
Map<String, Map<String, Object>> refMap = new HashMap<>();
map.put(objectClass, refMap);
for (IdentifiableObject object : identifiableObjects) {
refMap.put(object.getUid(), new HashMap<>());
properties.forEach(p -> {
if (!p.isCollection()) {
IdentifiableObject reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (reference != null) {
try {
IdentifiableObject identifiableObject = (IdentifiableObject) p.getKlass().newInstance();
mergeService.merge(new MergeParams<>(reference, identifiableObject));
refMap.get(object.getUid()).put(p.getName(), identifiableObject);
} catch (InstantiationException | IllegalAccessException ignored) {
}
}
} else {
Collection<IdentifiableObject> refObjects = ReflectionUtils.newCollectionInstance(p.getKlass());
Collection<IdentifiableObject> references = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (references != null) {
for (IdentifiableObject reference : references) {
try {
IdentifiableObject identifiableObject = (IdentifiableObject) p.getItemKlass().newInstance();
mergeService.merge(new MergeParams<>(reference, identifiableObject));
refObjects.add(identifiableObject);
} catch (InstantiationException | IllegalAccessException ignored) {
}
}
}
refMap.get(object.getUid()).put(p.getCollectionName(), refObjects);
}
});
}
}
return map;
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class DefaultPreheatService method handleAttributes.
private void handleAttributes(Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects, Preheat preheat) {
for (Class<? extends IdentifiableObject> klass : objects.keySet()) {
List<Attribute> mandatoryAttributes = attributeService.getMandatoryAttributes(klass);
if (!mandatoryAttributes.isEmpty()) {
preheat.getMandatoryAttributes().put(klass, new HashSet<>());
}
mandatoryAttributes.forEach(attribute -> preheat.getMandatoryAttributes().get(klass).add(attribute.getUid()));
List<Attribute> uniqueAttributes = attributeService.getUniqueAttributes(klass);
if (!uniqueAttributes.isEmpty()) {
preheat.getUniqueAttributes().put(klass, new HashSet<>());
}
uniqueAttributes.forEach(attribute -> preheat.getUniqueAttributes().get(klass).add(attribute.getUid()));
List<? extends IdentifiableObject> uniqueAttributeValues = manager.getAllByAttributes(klass, uniqueAttributes);
handleUniqueAttributeValues(klass, uniqueAttributeValues, preheat);
}
if (objects.containsKey(Attribute.class)) {
List<IdentifiableObject> attributes = objects.get(Attribute.class);
for (IdentifiableObject identifiableObject : attributes) {
Attribute attribute = (Attribute) identifiableObject;
if (attribute.isMandatory()) {
attribute.getSupportedClasses().forEach(klass -> {
if (!preheat.getMandatoryAttributes().containsKey(klass))
preheat.getMandatoryAttributes().put(klass, new HashSet<>());
preheat.getMandatoryAttributes().get(klass).add(attribute.getUid());
});
}
if (attribute.isUnique()) {
attribute.getSupportedClasses().forEach(klass -> {
if (!preheat.getUniqueAttributes().containsKey(klass))
preheat.getUniqueAttributes().put(klass, new HashSet<>());
preheat.getUniqueAttributes().get(klass).add(attribute.getUid());
});
}
}
}
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class QueryServiceTest method resultTransformerTest.
@Test
@SuppressWarnings("rawtypes")
public void resultTransformerTest() {
Query query = Query.from(schemaService.getDynamicSchema(DataElement.class));
List<? extends IdentifiableObject> objects = queryService.query(query, result1 -> new ArrayList());
assertEquals(0, objects.size());
objects = queryService.query(query, result1 -> result1);
assertEquals(6, objects.size());
}
use of org.hisp.dhis.common.IdentifiableObject in project dhis2-core by dhis2.
the class ObjectBundleServiceFavoritesTest method testDeleteLegendSet.
@Test
public void testDeleteLegendSet() throws IOException {
Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/legends.json").getInputStream(), RenderFormat.JSON);
ObjectBundleParams params = new ObjectBundleParams();
params.setObjectBundleMode(ObjectBundleMode.COMMIT);
params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
params.setObjects(metadata);
ObjectBundle bundle = objectBundleService.create(params);
ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
assertTrue(validate.getErrorReports().isEmpty());
objectBundleService.commit(bundle);
List<LegendSet> legendSets = manager.getAll(LegendSet.class);
assertEquals(1, legendSets.size());
LegendSet legendSet = legendSets.get(0);
assertEquals("fqs276KXCXi", legendSet.getUid());
assertEquals("ANC Coverage", legendSet.getName());
assertEquals(7, legendSet.getLegends().size());
manager.delete(legendSet);
legendSets = manager.getAll(LegendSet.class);
assertTrue(legendSets.isEmpty());
}
Aggregations