use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.
the class DefaultQueryPlanner method groupByAggregationType.
/**
* Groups the given query in sub queries based on the aggregation type of
* its data elements. The aggregation type can be sum, average aggregation
* or average disaggregation. Sum means that the data elements have sum
* aggregation operator. Average aggregation means that the data elements
* have the average aggregation operator and that the period type of the
* data elements have higher or equal frequency than the aggregation period
* type. Average disaggregation means that the data elements have the
* average aggregation operator and that the period type of the data
* elements have lower frequency than the aggregation period type. Average
* bool means that the data elements have the average aggregation operator
* and the bool value type.
* <p>
* If no data elements are present, the aggregation type will be determined
* based on the first data element in the first data element group in the
* first data element group set in the query.
* <p>
* If the aggregation type is already set/overridden in the request, the
* query will be returned unchanged. If there are no data elements or data
* element group sets specified the aggregation type will fall back to sum.
*
* @param params the {@link DataQueryParams}.
* @return a list of {@link DataQueryParams}.
*/
private List<DataQueryParams> groupByAggregationType(DataQueryParams params) {
List<DataQueryParams> queries = new ArrayList<>();
if (!params.getDataElements().isEmpty()) {
ListMap<AnalyticsAggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params.getDataElements(), params.getAggregationType(), params.getPeriodType());
for (AnalyticsAggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(aggregationTypeDataElementMap.get(aggregationType)).withAggregationType(aggregationType).build();
queries.add(query);
}
} else if (!params.getDataElementGroupSets().isEmpty()) {
DataElementGroup deg = params.getFirstDataElementGroup();
AnalyticsAggregationType aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), AnalyticsAggregationType.SUM);
if (deg != null && !deg.getMembers().isEmpty()) {
PeriodType periodType = PeriodType.getPeriodTypeByName(params.getPeriodType());
AnalyticsAggregationType degAggType = AnalyticsAggregationType.fromAggregationType(deg.getAggregationType());
aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), degAggType, AnalyticsAggregationType.SUM);
aggregationType = QueryPlannerUtils.getAggregationType(aggregationType, deg.getValueType(), periodType, deg.getPeriodType());
}
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
queries.add(query);
} else if (filterHasDataElementsOfSameAggregationTypeAndValueType(params)) {
ListMap<AnalyticsAggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params.getFilterOptions(DATA_X_DIM_ID, DataDimensionItemType.DATA_ELEMENT), params.getAggregationType(), params.getPeriodType());
for (AnalyticsAggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
queries.add(query);
}
} else {
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(ObjectUtils.firstNonNull(params.getAggregationType(), AnalyticsAggregationType.SUM)).build();
queries.add(query);
}
logQuerySplit(queries, "aggregation type");
return queries;
}
use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.
the class PreheatServiceTest method testCollectReferenceUidDEG2.
@Test
void testCollectReferenceUidDEG2() {
DataElementGroup deg1 = createDataElementGroup('A');
DataElementGroup deg2 = createDataElementGroup('B');
DataElement de1 = createDataElement('A');
DataElement de2 = createDataElement('B');
DataElement de3 = createDataElement('C');
deg1.addDataElement(de1);
deg1.addDataElement(de2);
deg2.addDataElement(de3);
Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(Lists.newArrayList(deg1, deg2)).get(PreheatIdentifier.UID);
assertTrue(references.containsKey(DataElement.class));
assertEquals(3, references.get(DataElement.class).size());
assertTrue(references.get(DataElement.class).contains(de1.getUid()));
assertTrue(references.get(DataElement.class).contains(de2.getUid()));
assertTrue(references.get(DataElement.class).contains(de3.getUid()));
}
use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.
the class PreheatServiceTest method testCollectNoObjectsDEG.
@Test
void testCollectNoObjectsDEG() {
DataElementGroup dataElementGroup = createDataElementGroup('A');
Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(dataElementGroup).get(PreheatIdentifier.UID);
assertFalse(references.containsKey(DataElement.class));
assertFalse(references.containsKey(User.class));
}
use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.
the class PreheatServiceTest method testCollectReferenceCodeDEG2.
@Test
void testCollectReferenceCodeDEG2() {
DataElementGroup deg1 = createDataElementGroup('A');
DataElementGroup deg2 = createDataElementGroup('B');
DataElement de1 = createDataElement('A');
DataElement de2 = createDataElement('B');
DataElement de3 = createDataElement('C');
deg1.addDataElement(de1);
deg1.addDataElement(de2);
deg2.addDataElement(de3);
Map<Class<? extends IdentifiableObject>, Set<String>> references = preheatService.collectReferences(Lists.newArrayList(deg1, deg2)).get(PreheatIdentifier.CODE);
assertTrue(references.containsKey(DataElement.class));
assertEquals(3, references.get(DataElement.class).size());
assertTrue(references.get(DataElement.class).contains(de1.getCode()));
assertTrue(references.get(DataElement.class).contains(de2.getCode()));
assertTrue(references.get(DataElement.class).contains(de3.getCode()));
}
use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.
the class PreheatServiceTest method testPreheatReferenceConnectUID.
@Test
void testPreheatReferenceConnectUID() {
DataElementGroup dataElementGroup = fromJson("preheat/degAUidRef.json", DataElementGroup.class);
defaultSetup();
List<User> allUsers = userService.getAllUsers();
PreheatParams params = new PreheatParams();
params.setPreheatMode(PreheatMode.REFERENCE);
params.getObjects().put(DataElementGroup.class, Lists.newArrayList(dataElementGroup));
preheatService.validate(params);
Preheat preheat = preheatService.preheat(params);
preheatService.connectReferences(dataElementGroup, preheat, PreheatIdentifier.UID);
List<DataElement> members = new ArrayList<>(dataElementGroup.getMembers());
assertContains(members, "DataElementA", "DataElementCodeA");
assertContains(members, "DataElementB", "DataElementCodeB");
assertContains(members, "DataElementC", "DataElementCodeC");
User createdBy = dataElementGroup.getCreatedBy();
assertEquals("FirstNameA", createdBy.getFirstName());
assertEquals("SurnameA", createdBy.getSurname());
assertEquals("UserCodeA", createdBy.getCode());
}
Aggregations