use of org.hisp.dhis.common.AnalyticalObject in project dhis2-core by dhis2.
the class DefaultPreheatService method collectReferences.
@SuppressWarnings("unchecked")
private Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> collectReferences(Map<Class<?>, List<?>> objects) {
Map<PreheatIdentifier, Map<Class<? extends IdentifiableObject>, Set<String>>> map = new HashMap<>();
map.put(PreheatIdentifier.UID, new HashMap<>());
map.put(PreheatIdentifier.CODE, new HashMap<>());
Map<Class<? extends IdentifiableObject>, Set<String>> uidMap = map.get(PreheatIdentifier.UID);
Map<Class<? extends IdentifiableObject>, Set<String>> codeMap = map.get(PreheatIdentifier.CODE);
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<?> klass : targets.keySet()) {
Schema schema = schemaService.getDynamicSchema(klass);
List<Property> referenceProperties = schema.getProperties().stream().filter(p -> p.isPersisted() && p.isOwner() && (PropertyType.REFERENCE == p.getPropertyType() || PropertyType.REFERENCE == p.getItemPropertyType())).collect(Collectors.toList());
for (Object object : targets.get(klass)) {
if (schema.isIdentifiableObject()) {
IdentifiableObject identifiableObject = (IdentifiableObject) object;
identifiableObject.getAttributeValues().forEach(av -> addIdentifiers(map, av.getAttribute()));
identifiableObject.getUserGroupAccesses().forEach(uga -> addIdentifiers(map, uga.getUserGroup()));
identifiableObject.getUserAccesses().forEach(ua -> addIdentifiers(map, ua.getUser()));
addIdentifiers(map, identifiableObject);
}
referenceProperties.forEach(p -> {
if (!p.isCollection()) {
Class<? extends IdentifiableObject> itemKlass = (Class<? extends IdentifiableObject>) p.getKlass();
if (!uidMap.containsKey(itemKlass))
uidMap.put(itemKlass, new HashSet<>());
if (!codeMap.containsKey(itemKlass))
codeMap.put(itemKlass, new HashSet<>());
Object reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
if (reference != null) {
IdentifiableObject identifiableObject = (IdentifiableObject) reference;
addIdentifiers(map, identifiableObject);
}
} else {
Collection<IdentifiableObject> reference = ReflectionUtils.invokeMethod(object, p.getGetterMethod());
reference.forEach(identifiableObject -> addIdentifiers(map, identifiableObject));
if (DataElementOperand.class.isAssignableFrom(p.getItemKlass())) {
CollectionUtils.nullSafeForEach(reference, identifiableObject -> {
DataElementOperand dataElementOperand = (DataElementOperand) identifiableObject;
addIdentifiers(map, dataElementOperand.getDataElement());
addIdentifiers(map, dataElementOperand.getCategoryOptionCombo());
});
}
}
});
if (AnalyticalObject.class.isInstance(object)) {
BaseAnalyticalObject analyticalObject = (BaseAnalyticalObject) object;
List<DataDimensionItem> dataDimensionItems = analyticalObject.getDataDimensionItems();
List<CategoryDimension> categoryDimensions = analyticalObject.getCategoryDimensions();
List<TrackedEntityDataElementDimension> trackedEntityDataElementDimensions = analyticalObject.getDataElementDimensions();
List<TrackedEntityAttributeDimension> attributeDimensions = analyticalObject.getAttributeDimensions();
List<TrackedEntityProgramIndicatorDimension> programIndicatorDimensions = analyticalObject.getProgramIndicatorDimensions();
CollectionUtils.nullSafeForEach(dataDimensionItems, dataDimensionItem -> {
addIdentifiers(map, dataDimensionItem.getDimensionalItemObject());
if (dataDimensionItem.getDataElementOperand() != null) {
addIdentifiers(map, dataDimensionItem.getDataElementOperand().getDataElement());
addIdentifiers(map, dataDimensionItem.getDataElementOperand().getCategoryOptionCombo());
}
if (dataDimensionItem.getReportingRate() != null) {
addIdentifiers(map, dataDimensionItem.getReportingRate().getDataSet());
}
});
CollectionUtils.nullSafeForEach(categoryDimensions, categoryDimension -> {
addIdentifiers(map, categoryDimension.getDimension());
categoryDimension.getItems().forEach(item -> addIdentifiers(map, item));
});
CollectionUtils.nullSafeForEach(trackedEntityDataElementDimensions, trackedEntityDataElementDimension -> {
addIdentifiers(map, trackedEntityDataElementDimension.getDataElement());
addIdentifiers(map, trackedEntityDataElementDimension.getLegendSet());
});
CollectionUtils.nullSafeForEach(attributeDimensions, trackedEntityAttributeDimension -> {
addIdentifiers(map, trackedEntityAttributeDimension.getAttribute());
addIdentifiers(map, trackedEntityAttributeDimension.getLegendSet());
});
CollectionUtils.nullSafeForEach(programIndicatorDimensions, programIndicatorDimension -> {
addIdentifiers(map, programIndicatorDimension.getProgramIndicator());
addIdentifiers(map, programIndicatorDimension.getLegendSet());
});
}
}
}
cleanEmptyEntries(uidMap);
cleanEmptyEntries(codeMap);
return map;
}
use of org.hisp.dhis.common.AnalyticalObject in project dhis2-core by dhis2.
the class AnalyticalObjectEmbeddedDimensionUpgrader method upgradeGrupSetDimensions.
@SuppressWarnings("unchecked")
private void upgradeGrupSetDimensions(String favorite, String dimension, String item, Class<? extends AnalyticalObject> favoriteClazz, Class<? extends DimensionalObject> dimensionClass, Class<? extends DimensionalItemObject> itemClass, BiConsumer<BaseDimensionalEmbeddedObject, AnalyticalObject> consumer) {
String groupSetSqlPattern = "select distinct d.{favorite}id, gsm.{dimension}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id";
String groupSetSql = TextUtils.replace(groupSetSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item);
log.debug(String.format("Group set SQL: %s", groupSetSql));
String groupSqlPattern = "select d.{item}id " + "from {favorite}_{item}s d " + "inner join {dimension}members gsm on d.{item}id=gsm.{item}id " + "where d.{favorite}id={favoriteId} " + "and gsm.{dimension}id={dimensionId} " + "order by d.sort_order";
SqlRowSet groupSetRs = jdbcTemplate.queryForRowSet(groupSetSql);
while (groupSetRs.next()) {
int favoriteId = groupSetRs.getInt(1);
int dimensionId = groupSetRs.getInt(2);
AnalyticalObject analyticalObject = idObjectManager.get(favoriteClazz, favoriteId);
DimensionalObject groupSet = idObjectManager.get(dimensionClass, dimensionId);
Assert.notNull(analyticalObject, String.format("Analytical object not found: %s, class: %s", favoriteId, favoriteClazz));
Assert.notNull(groupSet, String.format("Group set not found: %s, class: %s", dimensionId, dimensionClass));
String groupSql = TextUtils.replace(groupSqlPattern, "{favorite}", favorite, "{dimension}", dimension, "{item}", item, "{favoriteId}", String.valueOf(favoriteId), "{dimensionId}", String.valueOf(dimensionId));
log.debug(String.format("Group SQL: %s", groupSql));
SqlRowSet groupRs = jdbcTemplate.queryForRowSet(groupSql);
List<Integer> groupIds = new ArrayList<>();
while (groupRs.next()) {
groupIds.add(groupRs.getInt(1));
}
List<DimensionalItemObject> groups = (List<DimensionalItemObject>) idObjectManager.getById(itemClass, groupIds);
Assert.notNull(groups, "Groups cannot be null");
BaseDimensionalEmbeddedObject embeddedDimension = new BaseDimensionalEmbeddedObject(groupSet, groups);
consumer.accept(embeddedDimension, analyticalObject);
idObjectManager.update(analyticalObject);
log.info(String.format("Added %s group set dimension: %s with groups: %d for favorite: %s", favorite, groupSet.getUid(), groups.size(), analyticalObject.getUid()));
}
String dropSql = TextUtils.replace("drop table {favorite}_{item}s", "{favorite}", favorite, "{item}", item);
jdbcTemplate.update(dropSql);
log.info(String.format("Dropped table, update done for favorite: %s and dimension: %s", favorite, dimension));
}
Aggregations