use of org.hisp.dhis.indicator.IndicatorGroup in project dhis2-core by dhis2.
the class AnalyticsServiceMetadataTest method metadataContainsIndicatorGroupMetadata.
@SuppressWarnings("unchecked")
@Test
void metadataContainsIndicatorGroupMetadata() {
List<DimensionalItemObject> periods = new ArrayList<>();
periods.add(new MonthlyPeriodType().createPeriod(new DateTime(2014, 4, 1, 0, 0).toDate()));
IndicatorGroup indicatorGroup = new IndicatorGroup("ANC");
indicatorGroup.setCode("COD_1000");
indicatorGroup.setUid("wjP19dkFeIk");
DataQueryParams params = DataQueryParams.newBuilder().withDimensions(Lists.newArrayList(new BaseDimensionalObject("pe", DimensionType.PERIOD, periods), new BaseDimensionalObject("dx", DimensionType.DATA_X, DISPLAY_NAME_DATA_X, "display name", Lists.newArrayList(new Indicator(), new Indicator(), createDataElement('A', new CategoryCombo()), createDataElement('B', new CategoryCombo())), new DimensionItemKeywords(Collections.singletonList(indicatorGroup))))).withFilters(Collections.singletonList(new BaseDimensionalObject("ou", DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, ImmutableList.of(new OrganisationUnit("aaa", "aaa", "OU_1", null, null, "c1"), new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2"))))).withIgnoreLimit(true).withSkipData(true).build();
initMock(params);
Grid grid = target.getAggregatedDataValueGrid(params);
Map<String, Object> items = (Map<String, Object>) grid.getMetaData().get("items");
assertThat(items.get(indicatorGroup.getUid()), allOf(hasProperty("name", is(indicatorGroup.getName())), hasProperty("uid", is(indicatorGroup.getUid())), hasProperty("code", is(indicatorGroup.getCode()))));
}
use of org.hisp.dhis.indicator.IndicatorGroup in project dhis2-core by dhis2.
the class DataQueryServiceDimensionItemKeywordTest method convertAnalyticsRequestWithDataElementGroupAndIndicatorGroup.
@Test
void convertAnalyticsRequestWithDataElementGroupAndIndicatorGroup() {
final String DATA_ELEMENT_GROUP_UID = "oehv9EO3vP7";
final String INDICATOR_GROUP_UID = "iezv4GO4vD9";
when(dimensionService.getDataDimensionalItemObject(UID, "cYeuwXTCPkU")).thenReturn(new DataElement());
DataElementGroup dataElementGroup = new DataElementGroup("dummyDG");
dataElementGroup.setUid(DATA_ELEMENT_GROUP_UID);
dataElementGroup.setCode("CODE_10");
dataElementGroup.setMembers(Sets.newHashSet(new DataElement(), new DataElement()));
IndicatorGroup indicatorGroup = new IndicatorGroup("dummyIG");
indicatorGroup.setUid(INDICATOR_GROUP_UID);
indicatorGroup.setCode("CODE_10");
indicatorGroup.setMembers(Sets.newHashSet(new Indicator(), new Indicator()));
when(idObjectManager.getObject(DataElementGroup.class, UID, DATA_ELEMENT_GROUP_UID)).thenReturn(dataElementGroup);
when(idObjectManager.getObject(IndicatorGroup.class, UID, INDICATOR_GROUP_UID)).thenReturn(indicatorGroup);
when(idObjectManager.getObject(OrganisationUnit.class, UID, "goRUwCHPg1M")).thenReturn(new OrganisationUnit("aaa"));
when(idObjectManager.getObject(OrganisationUnit.class, UID, "fdc6uOvgoji")).thenReturn(new OrganisationUnit("bbb"));
rb.addOuFilter("goRUwCHPg1M;fdc6uOvgoji");
rb.addDimension("DE_GROUP-" + DATA_ELEMENT_GROUP_UID + ";cYeuwXTCPkU;Jtf34kNZhz;IN_GROUP-" + INDICATOR_GROUP_UID);
rb.addPeDimension(PERIOD_DIMENSION);
DataQueryRequest request = DataQueryRequest.newBuilder().filter(rb.getFilterParams()).dimension(rb.getDimensionParams()).build();
DataQueryParams params = target.getFromRequest(request);
DimensionalObject dimension = params.getDimension("dx");
DimensionItemKeywords keywords = dimension.getDimensionItemKeywords();
assertEquals(2, keywords.getKeywords().size());
assertNotNull(keywords.getKeyword(dataElementGroup.getUid()));
assertEquals("dummyDG", keywords.getKeyword(dataElementGroup.getUid()).getMetadataItem().getName());
assertEquals("CODE_10", keywords.getKeyword(dataElementGroup.getUid()).getMetadataItem().getCode());
assertNotNull(keywords.getKeyword(indicatorGroup.getUid()));
assertEquals("dummyIG", keywords.getKeyword(indicatorGroup.getUid()).getMetadataItem().getName());
assertEquals("CODE_10", keywords.getKeyword(indicatorGroup.getUid()).getMetadataItem().getCode());
}
use of org.hisp.dhis.indicator.IndicatorGroup in project dhis2-core by dhis2.
the class DefaultDataQueryService method getDimension.
// TODO Optimize so that org unit levels + boundary are used in query
// instead of fetching all org units one by one.
@Override
public DimensionalObject getDimension(String dimension, List<String> items, Date relativePeriodDate, List<OrganisationUnit> userOrgUnits, I18nFormat format, boolean allowNull, boolean allowAllPeriodItems, IdScheme inputIdScheme) {
final boolean allItems = items.isEmpty();
User user = currentUserService.getCurrentUser();
if (DATA_X_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> dataDimensionItems = new ArrayList<>();
DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
for (String uid : items) {
if (// DATA ELEMENT GROUP
uid.startsWith(KEY_DE_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
DataElementGroup group = idObjectManager.getObject(DataElementGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
dimensionalKeywords.addKeyword(group);
}
} else if (// INDICATOR GROUP
uid.startsWith(KEY_IN_GROUP)) {
String groupUid = DimensionalObjectUtils.getUidFromGroupParam(uid);
IndicatorGroup group = idObjectManager.getObject(IndicatorGroup.class, inputIdScheme, groupUid);
if (group != null) {
dataDimensionItems.addAll(group.getMembers());
dimensionalKeywords.addKeyword(group);
}
} else {
DimensionalItemObject dimItemObject = dimensionService.getDataDimensionalItemObject(inputIdScheme, uid);
if (dimItemObject != null) {
dataDimensionItems.add(dimItemObject);
}
}
}
if (dataDimensionItems.isEmpty()) {
throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.DATA_X_DIM_ID);
}
return new BaseDimensionalObject(dimension, DimensionType.DATA_X, null, DISPLAY_NAME_DATA_X, dataDimensionItems, dimensionalKeywords);
} else if (CATEGORYOPTIONCOMBO_DIM_ID.equals(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.CATEGORY_OPTION_COMBO, null, DISPLAY_NAME_CATEGORYOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
} else if (ATTRIBUTEOPTIONCOMBO_DIM_ID.equals(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.ATTRIBUTE_OPTION_COMBO, null, DISPLAY_NAME_ATTRIBUTEOPTIONCOMBO, getCategoryOptionComboList(items, inputIdScheme));
} else if (PERIOD_DIM_ID.equals(dimension)) {
Calendar calendar = PeriodType.getCalendar();
I18n i18n = i18nManager.getI18n();
List<Period> periods = new ArrayList<>();
DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
AnalyticsFinancialYearStartKey financialYearStart = systemSettingManager.getSystemSetting(SettingKey.ANALYTICS_FINANCIAL_YEAR_START, AnalyticsFinancialYearStartKey.class);
boolean containsRelativePeriods = false;
for (String isoPeriod : items) {
// Contains isoPeriod and timeField
IsoPeriodHolder isoPeriodHolder = IsoPeriodHolder.of(isoPeriod);
if (RelativePeriodEnum.contains(isoPeriodHolder.getIsoPeriod())) {
containsRelativePeriods = true;
RelativePeriodEnum relativePeriod = RelativePeriodEnum.valueOf(isoPeriodHolder.getIsoPeriod());
dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), i18n.getString(isoPeriodHolder.getIsoPeriod()));
List<Period> relativePeriods = RelativePeriods.getRelativePeriodsFromEnum(relativePeriod, relativePeriodDate, format, true, financialYearStart);
// If custom time filter is specified, set it in periods
if (isoPeriodHolder.hasDateField()) {
relativePeriods.forEach(period -> period.setDateField(isoPeriodHolder.getDateField()));
}
periods.addAll(relativePeriods);
} else {
Period period = PeriodType.getPeriodFromIsoString(isoPeriodHolder.getIsoPeriod());
if (period != null) {
if (isoPeriodHolder.hasDateField()) {
period.setDescription(isoPeriodHolder.getIsoPeriod());
period.setDateField(isoPeriodHolder.getDateField());
}
dimensionalKeywords.addKeyword(isoPeriodHolder.getIsoPeriod(), format != null ? i18n.getString(format.formatPeriod(period)) : isoPeriodHolder.getIsoPeriod());
periods.add(period);
} else {
tryParseDateRange(isoPeriodHolder).ifPresent(periods::add);
}
}
}
// Remove duplicates
periods = periods.stream().distinct().collect(Collectors.toList());
if (containsRelativePeriods) {
periods.sort(new AscendingPeriodComparator());
}
for (Period period : periods) {
String name = format != null ? format.formatPeriod(period) : null;
if (!period.getPeriodType().getName().contains(WeeklyPeriodType.NAME)) {
period.setShortName(name);
}
period.setName(name);
if (!calendar.isIso8601()) {
period.setUid(getLocalPeriodIdentifier(period, calendar));
}
}
return new BaseDimensionalObject(dimension, DimensionType.PERIOD, null, DISPLAY_NAME_PERIOD, asList(periods), dimensionalKeywords);
} else if (ORGUNIT_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> ous = new ArrayList<>();
List<Integer> levels = new ArrayList<>();
List<OrganisationUnitGroup> groups = new ArrayList<>();
for (String ou : items) {
if (KEY_USER_ORGUNIT.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(userOrgUnits);
} else if (KEY_USER_ORGUNIT_CHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedChildren(userOrgUnits));
} else if (KEY_USER_ORGUNIT_GRANDCHILDREN.equals(ou) && userOrgUnits != null && !userOrgUnits.isEmpty()) {
ous.addAll(OrganisationUnit.getSortedGrandChildren(userOrgUnits));
} else if (ou != null && ou.startsWith(KEY_LEVEL)) {
String level = DimensionalObjectUtils.getValueFromKeywordParam(ou);
Integer orgUnitLevel = organisationUnitService.getOrganisationUnitLevelByLevelOrUid(level);
if (orgUnitLevel != null) {
levels.add(orgUnitLevel);
}
} else if (ou != null && ou.startsWith(KEY_ORGUNIT_GROUP)) {
String uid = DimensionalObjectUtils.getUidFromGroupParam(ou);
OrganisationUnitGroup group = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
if (group != null) {
groups.add(group);
}
} else if (!inputIdScheme.is(IdentifiableProperty.UID) || CodeGenerator.isValidUid(ou)) {
OrganisationUnit unit = idObjectManager.getObject(OrganisationUnit.class, inputIdScheme, ou);
if (unit != null) {
ous.add(unit);
}
}
}
// Remove duplicates
ous = ous.stream().distinct().collect(Collectors.toList());
List<DimensionalItemObject> orgUnits = new ArrayList<>();
List<OrganisationUnit> ousList = asTypedList(ous);
DimensionItemKeywords dimensionalKeywords = new DimensionItemKeywords();
if (!levels.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnitsAtLevels(levels, ousList)));
dimensionalKeywords.addKeywords(levels.stream().map(level -> organisationUnitService.getOrganisationUnitLevelByLevel(level)).filter(Objects::nonNull).collect(Collectors.toList()));
}
if (!groups.isEmpty()) {
orgUnits.addAll(sort(organisationUnitService.getOrganisationUnits(groups, ousList)));
dimensionalKeywords.addKeywords(groups.stream().map(group -> new BaseNameableObject(group.getUid(), group.getCode(), group.getName())).collect(Collectors.toList()));
}
if (levels.isEmpty() && groups.isEmpty()) {
orgUnits.addAll(ous);
}
if (!dimensionalKeywords.isEmpty()) {
dimensionalKeywords.addKeywords(ousList);
}
if (orgUnits.isEmpty()) {
throwIllegalQueryEx(ErrorCode.E7124, DimensionalObject.ORGUNIT_DIM_ID);
}
// Remove duplicates
orgUnits = orgUnits.stream().distinct().collect(Collectors.toList());
return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT, null, DISPLAY_NAME_ORGUNIT, orgUnits, dimensionalKeywords);
} else if (ORGUNIT_GROUP_DIM_ID.equals(dimension)) {
List<DimensionalItemObject> ougs = new ArrayList<>();
for (String uid : items) {
OrganisationUnitGroup organisationUnitGroup = idObjectManager.getObject(OrganisationUnitGroup.class, inputIdScheme, uid);
if (organisationUnitGroup != null) {
ougs.add(organisationUnitGroup);
}
}
return new BaseDimensionalObject(dimension, DimensionType.ORGANISATION_UNIT_GROUP, null, DISPLAY_NAME_ORGUNIT_GROUP, ougs);
} else if (LONGITUDE_DIM_ID.contains(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LONGITUDE, new ArrayList<>());
} else if (LATITUDE_DIM_ID.contains(dimension)) {
return new BaseDimensionalObject(dimension, DimensionType.STATIC, null, DISPLAY_NAME_LATITUDE, new ArrayList<>());
} else {
DimensionalObject dimObject = idObjectManager.get(DataQueryParams.DYNAMIC_DIM_CLASSES, inputIdScheme, dimension);
if (dimObject != null && dimObject.isDataDimension()) {
Class<?> dimClass = HibernateProxyUtils.getRealClass(dimObject);
Class<? extends DimensionalItemObject> itemClass = DimensionalObject.DIMENSION_CLASS_ITEM_CLASS_MAP.get(dimClass);
List<DimensionalItemObject> dimItems = !allItems ? asList(idObjectManager.getOrdered(itemClass, inputIdScheme, items)) : getCanReadItems(user, dimObject);
return new BaseDimensionalObject(dimObject.getDimension(), dimObject.getDimensionType(), null, dimObject.getName(), dimItems, allItems);
}
}
if (allowNull) {
return null;
}
throw new IllegalQueryException(new ErrorMessage(ErrorCode.E7125, dimension));
}
use of org.hisp.dhis.indicator.IndicatorGroup in project dhis2-core by dhis2.
the class DefaultCsvImportService method indicatorGroupMembersFromCsv.
private List<IndicatorGroup> indicatorGroupMembersFromCsv(CsvReader reader) throws IOException {
Map<String, IndicatorGroup> uidMap = new HashMap<>();
while (reader.readRecord()) {
String[] values = reader.getValues();
if (values != null && values.length > 0) {
String groupUid = values[0];
String memberUid = values[1];
IndicatorGroup persistedGroup = indicatorGroupService.getIndicatorGroupByUid(groupUid);
if (persistedGroup != null) {
IndicatorGroup group = uidMap.computeIfAbsent(groupUid, key -> {
IndicatorGroup nonPersistedGroup = new IndicatorGroup();
nonPersistedGroup.setUid(persistedGroup.getUid());
nonPersistedGroup.setName(persistedGroup.getName());
return nonPersistedGroup;
});
Indicator member = new Indicator();
member.setUid(memberUid);
group.addIndicator(member);
}
}
}
return new ArrayList<>(uidMap.values());
}
use of org.hisp.dhis.indicator.IndicatorGroup in project dhis2-core by dhis2.
the class GetIndicatorsAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
canReadType(Indicator.class);
if (id != null && id != ALL) {
IndicatorGroup indicatorGroup = indicatorService.getIndicatorGroup(id);
if (indicatorGroup != null) {
indicators = new ArrayList<>(indicatorGroup.getMembers());
}
} else if (dataSetId != null) {
DataSet dataset = dataSetService.getDataSet(dataSetId);
if (dataset != null) {
indicators = new ArrayList<>(dataset.getIndicators());
}
} else {
indicators = new ArrayList<>(indicatorService.getAllIndicators());
ContextUtils.clearIfNotModified(ServletActionContext.getRequest(), ServletActionContext.getResponse(), indicators);
}
if (key != null) {
indicators = IdentifiableObjectUtils.filterNameByKey(indicators, key, true);
}
if (indicators == null) {
indicators = new ArrayList<>();
}
Collections.sort(indicators);
User currentUser = currentUserService.getCurrentUser();
indicators.forEach(instance -> canReadInstance(instance, currentUser));
if (usePaging) {
this.paging = createPaging(indicators.size());
indicators = indicators.subList(paging.getStartPos(), paging.getEndPos());
}
return SUCCESS;
}
Aggregations