use of org.hisp.dhis.dataelement.CategoryDimension in project dhis2-core by dhis2.
the class AnalyticalObjectObjectBundleHook method handleCategoryDimensions.
private void handleCategoryDimensions(Session session, Schema schema, BaseAnalyticalObject analyticalObject, ObjectBundle bundle) {
if (!schema.havePersistedProperty("categoryDimensions"))
return;
for (CategoryDimension categoryDimension : analyticalObject.getCategoryDimensions()) {
if (categoryDimension == null) {
continue;
}
categoryDimension.setDimension(bundle.getPreheat().get(bundle.getPreheatIdentifier(), categoryDimension.getDimension()));
List<DataElementCategoryOption> categoryOptions = new ArrayList<>(categoryDimension.getItems());
categoryDimension.getItems().clear();
categoryOptions.forEach(co -> {
DataElementCategoryOption categoryOption = bundle.getPreheat().get(bundle.getPreheatIdentifier(), co);
if (categoryOption != null)
categoryDimension.getItems().add(categoryOption);
});
preheatService.connectReferences(categoryDimension, bundle.getPreheat(), bundle.getPreheatIdentifier());
session.save(categoryDimension);
}
}
use of org.hisp.dhis.dataelement.CategoryDimension in project dhis2-core by dhis2.
the class DefaultDimensionService method mergeDimensionalObjects.
/**
* Sets persistent objects for dimensional associations on the given
* BaseAnalyticalObject based on the given list of transient DimensionalObjects.
* <p>
* Relative periods represented by enums are converted into a RelativePeriods
* object. User organisation units represented by enums are converted and
* represented by the user organisation unit persisted properties on the
* BaseAnalyticalObject.
*
* @param object the BaseAnalyticalObject to merge.
* @param dimensions the list of dimensions.
*/
private void mergeDimensionalObjects(BaseAnalyticalObject object, List<DimensionalObject> dimensions) {
if (object == null || dimensions == null) {
return;
}
for (DimensionalObject dimension : dimensions) {
DimensionType type = getDimensionType(dimension.getDimension());
String dimensionId = dimension.getDimension();
List<DimensionalItemObject> items = dimension.getItems();
if (items != null) {
List<String> uids = getUids(items);
if (DATA_X.equals(type)) {
for (String uid : uids) {
DimensionalItemObject dimItemObject = getDataDimensionalItemObject(IdScheme.UID, uid);
if (dimItemObject != null) {
DataDimensionItem item = DataDimensionItem.create(dimItemObject);
object.getDataDimensionItems().add(item);
}
}
} else if (PERIOD.equals(type)) {
List<RelativePeriodEnum> enums = new ArrayList<>();
List<Period> periods = new UniqueArrayList<>();
for (String isoPeriod : uids) {
if (RelativePeriodEnum.contains(isoPeriod)) {
enums.add(RelativePeriodEnum.valueOf(isoPeriod));
} else {
Period period = PeriodType.getPeriodFromIsoString(isoPeriod);
if (period != null) {
periods.add(period);
}
}
}
object.setRelatives(new RelativePeriods().setRelativePeriodsFromEnums(enums));
object.setPeriods(periodService.reloadPeriods(new ArrayList<>(periods)));
} else if (ORGANISATION_UNIT.equals(type)) {
for (String ou : uids) {
if (KEY_USER_ORGUNIT.equals(ou)) {
object.setUserOrganisationUnit(true);
} else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou)) {
object.setUserOrganisationUnitChildren(true);
} else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou)) {
object.setUserOrganisationUnitGrandChildren(true);
} else if (ou != null && ou.startsWith(KEY_LEVEL)) {
int level = DimensionalObjectUtils.getLevelFromLevelParam(ou);
if (level > 0) {
object.getOrganisationUnitLevels().add(level);
}
} else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
OrganisationUnitGroup group = idObjectManager.get(OrganisationUnitGroup.class, uid);
if (group != null) {
object.getItemOrganisationUnitGroups().add(group);
}
} else {
OrganisationUnit unit = idObjectManager.get(OrganisationUnit.class, ou);
if (unit != null) {
object.getOrganisationUnits().add(unit);
}
}
}
} else if (DATA_ELEMENT_GROUP_SET.equals(type)) {
DataElementGroupSetDimension groupSetDimension = new DataElementGroupSetDimension();
groupSetDimension.setDimension(idObjectManager.get(DataElementGroupSet.class, dimensionId));
groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(DataElementGroup.class, uids));
object.getDataElementGroupSetDimensions().add(groupSetDimension);
} else if (ORGANISATION_UNIT_GROUP_SET.equals(type)) {
OrganisationUnitGroupSetDimension groupSetDimension = new OrganisationUnitGroupSetDimension();
groupSetDimension.setDimension(idObjectManager.get(OrganisationUnitGroupSet.class, dimensionId));
groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(OrganisationUnitGroup.class, uids));
object.getOrganisationUnitGroupSetDimensions().add(groupSetDimension);
} else if (CATEGORY.equals(type)) {
CategoryDimension categoryDimension = new CategoryDimension();
categoryDimension.setDimension(idObjectManager.get(DataElementCategory.class, dimensionId));
categoryDimension.getItems().addAll(idObjectManager.getByUidOrdered(DataElementCategoryOption.class, uids));
object.getCategoryDimensions().add(categoryDimension);
} else if (CATEGORY_OPTION_GROUP_SET.equals(type)) {
CategoryOptionGroupSetDimension groupSetDimension = new CategoryOptionGroupSetDimension();
groupSetDimension.setDimension(idObjectManager.get(CategoryOptionGroupSet.class, dimensionId));
groupSetDimension.getItems().addAll(idObjectManager.getByUidOrdered(CategoryOptionGroup.class, uids));
object.getCategoryOptionGroupSetDimensions().add(groupSetDimension);
} else if (PROGRAM_ATTRIBUTE.equals(type)) {
TrackedEntityAttributeDimension attributeDimension = new TrackedEntityAttributeDimension();
attributeDimension.setAttribute(idObjectManager.get(TrackedEntityAttribute.class, dimensionId));
attributeDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
attributeDimension.setFilter(dimension.getFilter());
object.getAttributeDimensions().add(attributeDimension);
} else if (PROGRAM_DATA_ELEMENT.equals(type)) {
TrackedEntityDataElementDimension dataElementDimension = new TrackedEntityDataElementDimension();
dataElementDimension.setDataElement(idObjectManager.get(DataElement.class, dimensionId));
dataElementDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
dataElementDimension.setFilter(dimension.getFilter());
object.getDataElementDimensions().add(dataElementDimension);
} else if (PROGRAM_INDICATOR.equals(type)) {
TrackedEntityProgramIndicatorDimension programIndicatorDimension = new TrackedEntityProgramIndicatorDimension();
programIndicatorDimension.setProgramIndicator(idObjectManager.get(ProgramIndicator.class, dimensionId));
programIndicatorDimension.setLegendSet(dimension.hasLegendSet() ? idObjectManager.get(LegendSet.class, dimension.getLegendSet().getUid()) : null);
programIndicatorDimension.setFilter(dimension.getFilter());
object.getProgramIndicatorDimensions().add(programIndicatorDimension);
}
}
}
}
use of org.hisp.dhis.dataelement.CategoryDimension 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;
}
Aggregations