use of org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension in project dhis2-core by dhis2.
the class BaseAnalyticalObject method getDimensionalObject.
/**
* Assembles a list of DimensionalObjects based on the concrete objects in
* this BaseAnalyticalObject.
* <p>
* Merges fixed and relative periods into the pe dimension, where the
* RelativePeriods object is represented by enums (e.g. LAST_MONTH). Merges
* fixed and user organisation units into the ou dimension, where user
* organisation units properties are represented by enums (e.g. USER_ORG_UNIT).
* <p>
* This method is useful when serializing the AnalyticalObject.
*
* @param dimension the dimension identifier.
* @return a list of DimensionalObjects.
*/
protected DimensionalObject getDimensionalObject(String dimension) {
if (DATA_X_DIM_ID.equals(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.DATA_X, getDataDimensionNameableObjects());
} else if (PERIOD_DIM_ID.equals(dimension)) {
List<Period> periodList = new ArrayList<>(periods);
if (hasRelativePeriods()) {
List<RelativePeriodEnum> list = relatives.getRelativePeriodEnums();
for (RelativePeriodEnum periodEnum : list) {
periodList.add(new ConfigurablePeriod(periodEnum.toString()));
}
}
Collections.sort(periodList, new AscendingPeriodComparator());
return new BaseDimensionalObject(dimension, DimensionType.PERIOD, periodList);
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> ouList = new ArrayList<>();
ouList.addAll(organisationUnits);
ouList.addAll(transientOrganisationUnits);
if (userOrganisationUnit) {
ouList.add(new BaseDimensionalItemObject(KEY_USER_ORGUNIT));
}
if (userOrganisationUnitChildren) {
ouList.add(new BaseDimensionalItemObject(KEY_USER_ORGUNIT_CHILDREN));
}
if (userOrganisationUnitGrandChildren) {
ouList.add(new BaseDimensionalItemObject(KEY_USER_ORGUNIT_GRANDCHILDREN));
}
if (organisationUnitLevels != null && !organisationUnitLevels.isEmpty()) {
for (Integer level : organisationUnitLevels) {
String id = KEY_LEVEL + level;
ouList.add(new BaseDimensionalItemObject(id));
}
}
if (itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty()) {
for (OrganisationUnitGroup group : itemOrganisationUnitGroups) {
String id = KEY_ORGUNIT_GROUP + group.getUid();
ouList.add(new BaseDimensionalItemObject(id));
}
}
return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, ouList);
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, new ArrayList<>());
} else if (DATA_COLLAPSED_DIM_ID.contains(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.DATA_COLLAPSED, new ArrayList<>());
} else if (STATIC_DIMS.contains(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.STATIC, new ArrayList<>());
} else {
// Embedded dimensions
Optional<DimensionalObject> object = Optional.empty();
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.DATA_ELEMENT_GROUP_SET, dataElementGroupSetDimensions)).isPresent()) {
return object.get();
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.ORGANISATION_UNIT_GROUP_SET, organisationUnitGroupSetDimensions)).isPresent()) {
return object.get();
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY, categoryDimensions)).isPresent()) {
return object.get();
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY_OPTION_GROUP_SET, categoryOptionGroupSetDimensions)).isPresent()) {
return object.get();
}
// Tracked entity attribute
Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex(attributeDimensions, TrackedEntityAttributeDimension::getUid);
if (attributes.containsKey(dimension)) {
TrackedEntityAttributeDimension tead = attributes.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_ATTRIBUTE, null, tead.getDisplayName(), tead.getLegendSet(), tead.getFilter());
}
// Tracked entity data element
Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex(dataElementDimensions, TrackedEntityDataElementDimension::getUid);
if (dataElements.containsKey(dimension)) {
TrackedEntityDataElementDimension tedd = dataElements.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_DATA_ELEMENT, null, tedd.getDisplayName(), tedd.getLegendSet(), tedd.getFilter());
}
// Tracked entity program indicator
Map<String, TrackedEntityProgramIndicatorDimension> programIndicators = Maps.uniqueIndex(programIndicatorDimensions, TrackedEntityProgramIndicatorDimension::getUid);
if (programIndicators.containsKey(dimension)) {
TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_INDICATOR, null, teid.getDisplayName(), teid.getLegendSet(), teid.getFilter());
}
}
throw new IllegalArgumentException("Not a valid dimension: " + dimension);
}
use of org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension in project dhis2-core by dhis2.
the class BaseAnalyticalObject method getDimensionalObject.
/**
* Assembles a DimensionalObject based on the persisted properties of this
* AnalyticalObject. Collapses indicators, data elements, data element
* operands and data sets into the dx dimension.
* <p>
* Collapses fixed and relative periods into the pe dimension. Collapses
* fixed and user organisation units into the ou dimension.
*
* @param dimension the dimension identifier.
* @param date the date used for generating relative periods.
* @param user the current user.
* @param dynamicNames whether to use dynamic or static names.
* @param format the I18nFormat.
* @return a DimensionalObject.
*/
protected DimensionalObject getDimensionalObject(String dimension, Date date, User user, boolean dynamicNames, List<OrganisationUnit> organisationUnitsAtLevel, List<OrganisationUnit> organisationUnitsInGroups, I18nFormat format) {
List<DimensionalItemObject> items = new ArrayList<>();
DimensionType type = null;
if (DATA_X_DIM_ID.equals(dimension)) {
items.addAll(getDataDimensionNameableObjects());
type = DimensionType.DATA_X;
} else if (PERIOD_DIM_ID.equals(dimension)) {
setPeriodNames(periods, dynamicNames, format);
items.addAll(periods);
if (hasRelativePeriods()) {
items.addAll(relatives.getRelativePeriods(date, format, dynamicNames));
}
type = DimensionType.PERIOD;
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
items.addAll(organisationUnits);
items.addAll(transientOrganisationUnits);
if (userOrganisationUnit && user != null && user.hasOrganisationUnit()) {
items.addAll(user.getOrganisationUnits());
}
if (userOrganisationUnitChildren && user != null && user.hasOrganisationUnit()) {
user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedChildren()));
}
if (userOrganisationUnitGrandChildren && user != null && user.hasOrganisationUnit()) {
user.getOrganisationUnits().forEach(ou -> items.addAll(ou.getSortedGrandChildren()));
}
if (organisationUnitLevels != null && !organisationUnitLevels.isEmpty() && organisationUnitsAtLevel != null) {
// Must be set externally
items.addAll(organisationUnitsAtLevel);
}
if (itemOrganisationUnitGroups != null && !itemOrganisationUnitGroups.isEmpty() && organisationUnitsInGroups != null) {
// Must be set externally
items.addAll(organisationUnitsInGroups);
}
type = DimensionType.ORGANISATION_UNIT;
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
items.addAll(transientCategoryOptionCombos);
type = DimensionType.CATEGORY_OPTION_COMBO;
} else if (STATIC_DIMS.contains(dimension)) {
type = DimensionType.STATIC;
} else {
// Embedded dimensions
Optional<DimensionalObject> object = Optional.empty();
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.DATA_ELEMENT_GROUP_SET, dataElementGroupSetDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.DATA_ELEMENT_GROUP_SET;
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.ORGANISATION_UNIT_GROUP_SET, organisationUnitGroupSetDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.ORGANISATION_UNIT_GROUP_SET;
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY, categoryDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.CATEGORY;
}
if ((object = getDimensionFromEmbeddedObjects(dimension, DimensionType.CATEGORY_OPTION_GROUP_SET, categoryOptionGroupSetDimensions)).isPresent()) {
items.addAll(object.get().getItems());
type = DimensionType.CATEGORY_OPTION_GROUP_SET;
}
// Tracked entity attribute
Map<String, TrackedEntityAttributeDimension> attributes = Maps.uniqueIndex(attributeDimensions, TrackedEntityAttributeDimension::getUid);
if (attributes.containsKey(dimension)) {
TrackedEntityAttributeDimension tead = attributes.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_ATTRIBUTE, null, tead.getDisplayName(), tead.getLegendSet(), tead.getFilter());
}
// Tracked entity data element
Map<String, TrackedEntityDataElementDimension> dataElements = Maps.uniqueIndex(dataElementDimensions, TrackedEntityDataElementDimension::getUid);
if (dataElements.containsKey(dimension)) {
TrackedEntityDataElementDimension tedd = dataElements.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_DATA_ELEMENT, null, tedd.getDisplayName(), tedd.getLegendSet(), tedd.getFilter());
}
// Tracked entity program indicator
Map<String, TrackedEntityProgramIndicatorDimension> programIndicators = Maps.uniqueIndex(programIndicatorDimensions, TrackedEntityProgramIndicatorDimension::getUid);
if (programIndicators.containsKey(dimension)) {
TrackedEntityProgramIndicatorDimension teid = programIndicators.get(dimension);
return new BaseDimensionalObject(dimension, DimensionType.PROGRAM_INDICATOR, null, teid.getDisplayName(), teid.getLegendSet(), teid.getFilter());
}
}
IdentifiableObjectUtils.removeDuplicates(items);
return new BaseDimensionalObject(dimension, type, items);
}
use of org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension 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.trackedentity.TrackedEntityProgramIndicatorDimension in project dhis2-core by dhis2.
the class AnalyticalObjectObjectBundleHook method handleProgramIndicatorDimensions.
private void handleProgramIndicatorDimensions(Session session, Schema schema, BaseAnalyticalObject analyticalObject, ObjectBundle bundle) {
if (!schema.havePersistedProperty("programIndicatorDimensions"))
return;
for (TrackedEntityProgramIndicatorDimension programIndicatorDimension : analyticalObject.getProgramIndicatorDimensions()) {
if (programIndicatorDimension == null) {
continue;
}
programIndicatorDimension.setProgramIndicator(bundle.getPreheat().get(bundle.getPreheatIdentifier(), programIndicatorDimension.getProgramIndicator()));
programIndicatorDimension.setLegendSet(bundle.getPreheat().get(bundle.getPreheatIdentifier(), programIndicatorDimension.getLegendSet()));
preheatService.connectReferences(programIndicatorDimension, bundle.getPreheat(), bundle.getPreheatIdentifier());
session.save(programIndicatorDimension);
}
}
use of org.hisp.dhis.trackedentity.TrackedEntityProgramIndicatorDimension 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