use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class AnalyticsUtils method getDimensionMetadataItemMap.
/**
* Returns a mapping between identifiers and meta data items for the given query.
*
* @param params the data query parameters.
* @return a mapping between identifiers and meta data items.
*/
public static Map<String, MetadataItem> getDimensionMetadataItemMap(DataQueryParams params) {
List<DimensionalObject> dimensions = params.getDimensionsAndFilters();
Map<String, MetadataItem> map = new HashMap<>();
Calendar calendar = PeriodType.getCalendar();
for (DimensionalObject dimension : dimensions) {
for (DimensionalItemObject item : dimension.getItems()) {
if (DimensionType.PERIOD == dimension.getDimensionType() && !calendar.isIso8601()) {
Period period = (Period) item;
DateTimeUnit dateTimeUnit = calendar.fromIso(period.getStartDate());
map.put(period.getPeriodType().getIsoDate(dateTimeUnit), new MetadataItem(period.getDisplayName()));
} else {
String legendSet = item.hasLegendSet() ? item.getLegendSet().getUid() : null;
map.put(item.getDimensionItem(), new MetadataItem(item.getDisplayProperty(params.getDisplayProperty()), legendSet));
}
if (DimensionType.ORGANISATION_UNIT == dimension.getDimensionType() && params.isHierarchyMeta()) {
OrganisationUnit unit = (OrganisationUnit) item;
for (OrganisationUnit ancestor : unit.getAncestors()) {
map.put(ancestor.getUid(), new MetadataItem(ancestor.getDisplayProperty(params.getDisplayProperty())));
}
}
if (DimensionItemType.DATA_ELEMENT == item.getDimensionItemType()) {
DataElement dataElement = (DataElement) item;
for (DataElementCategoryOptionCombo coc : dataElement.getCategoryOptionCombos()) {
map.put(coc.getUid(), new MetadataItem(coc.getDisplayProperty(params.getDisplayProperty())));
}
}
}
map.put(dimension.getDimension(), new MetadataItem(dimension.getDisplayProperty(params.getDisplayProperty())));
}
return map;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DataSetOrganisationUnitCategoryResourceTable method getPopulateTempTableContent.
/**
* Iterate over data sets and associated organisation units. If data set
* has a category combination and the organisation unit has category options,
* find the intersection of the category option combinations linked to the
* organisation unit through its category options, and the category option
* combinations linked to the data set through its category combination. If
* not, use the default category option combo.
*/
@Override
public Optional<List<Object[]>> getPopulateTempTableContent() {
List<Object[]> batchArgs = new ArrayList<>();
for (DataSet dataSet : objects) {
DataElementCategoryCombo categoryCombo = dataSet.getCategoryCombo();
for (OrganisationUnit orgUnit : dataSet.getSources()) {
if (!categoryCombo.isDefault()) {
if (orgUnit.hasCategoryOptions()) {
Set<DataElementCategoryOption> orgUnitOptions = orgUnit.getCategoryOptions();
for (DataElementCategoryOptionCombo optionCombo : categoryCombo.getOptionCombos()) {
Set<DataElementCategoryOption> optionComboOptions = optionCombo.getCategoryOptions();
if (orgUnitOptions.containsAll(optionComboOptions)) {
Date startDate = DateUtils.min(optionComboOptions.stream().map(co -> co.getStartDate()).collect(Collectors.toSet()));
Date endDate = DateUtils.max(optionComboOptions.stream().map(co -> co.getEndDate()).collect(Collectors.toSet()));
List<Object> values = Lists.newArrayList(dataSet.getId(), orgUnit.getId(), optionCombo.getId(), startDate, endDate);
batchArgs.add(values.toArray());
}
}
}
} else {
List<Object> values = Lists.newArrayList(dataSet.getId(), orgUnit.getId(), defaultOptionCombo.getId(), null, null);
batchArgs.add(values.toArray());
}
}
}
return Optional.of(batchArgs);
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DefaultPredictorService method predict.
// -------------------------------------------------------------------------
// Predictor run
// -------------------------------------------------------------------------
@Override
public int predict(Predictor predictor, Date startDate, Date endDate) {
log.info("Predicting for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
Expression generator = predictor.getGenerator();
Expression skipTest = predictor.getSampleSkipTest();
DataElement outputDataElement = predictor.getOutput();
Set<String> aggregates = expressionService.getAggregatesInExpression(generator.getExpression());
Map<String, Double> constantMap = constantService.getConstantMap();
List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
Set<DataElementOperand> dataElementOperands = getDataElementOperands(aggregates, skipTest);
User currentUser = currentUserService.getCurrentUser();
DataElementCategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? categoryService.getDefaultDataElementCategoryOptionCombo() : predictor.getOutputCombo();
List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(predictor.getOrganisationUnitLevels(), currentUser.getOrganisationUnits());
int predictionCount = 0;
for (OrganisationUnit orgUnit : orgUnits) {
MapMapMap<Period, String, DimensionalItemObject, Double> dataMap = dataElementOperands.isEmpty() ? null : dataValueService.getDataElementOperandValues(dataElementOperands, allSamplePeriods, orgUnit);
applySkipTest(dataMap, skipTest, constantMap);
for (Period period : outputPeriods) {
ListMapMap<String, String, Double> aggregateSampleMap = getAggregateSamples(dataMap, aggregates, samplePeriodsMap.get(period), constantMap);
for (String aoc : aggregateSampleMap.keySet()) {
ListMap<String, Double> aggregateValueMap = aggregateSampleMap.get(aoc);
Double value = expressionService.getExpressionValue(generator, new HashMap<>(), constantMap, null, period.getDaysInPeriod(), aggregateValueMap);
if (value != null && !value.isNaN() && !value.isInfinite()) {
writeDataValue(outputDataElement, period, orgUnit, outputOptionCombo, categoryService.getDataElementCategoryOptionCombo(aoc), value.toString(), currentUser.getUsername());
predictionCount++;
}
}
}
}
log.info("Generated " + predictionCount + " predictions for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
return predictionCount;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class AuditController method getAggregateDataValueAudit.
@RequestMapping(value = "dataValue", method = RequestMethod.GET)
@ResponseBody
public RootNode getAggregateDataValueAudit(@RequestParam(required = false, defaultValue = "") List<String> ds, @RequestParam(required = false, defaultValue = "") List<String> de, @RequestParam(required = false, defaultValue = "") List<String> pe, @RequestParam(required = false, defaultValue = "") List<String> ou, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) AuditType auditType, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false, defaultValue = "50") int pageSize, @RequestParam(required = false, defaultValue = "1") int page) throws WebMessageException {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
List<DataElement> dataElements = new ArrayList<>();
dataElements.addAll(getDataElements(de));
dataElements.addAll(getDataElementsByDataSet(ds));
List<Period> periods = getPeriods(pe);
List<OrganisationUnit> organisationUnits = getOrganisationUnit(ou);
DataElementCategoryOptionCombo categoryOptionCombo = getCategoryOptionCombo(co);
DataElementCategoryOptionCombo attributeOptionCombo = getAttributeOptionCombo(cc);
List<DataValueAudit> dataValueAudits;
Pager pager = null;
if (skipPaging) {
dataValueAudits = dataValueAuditService.getDataValueAudits(dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType);
} else {
int total = dataValueAuditService.countDataValueAudits(dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType);
pager = new Pager(page, total, pageSize);
dataValueAudits = dataValueAuditService.getDataValueAudits(dataElements, periods, organisationUnits, categoryOptionCombo, attributeOptionCombo, auditType, pager.getOffset(), pager.getPageSize());
}
RootNode rootNode = NodeUtils.createMetadata();
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
CollectionNode trackedEntityAttributeValueAudits = rootNode.addChild(new CollectionNode("dataValueAudits", true));
trackedEntityAttributeValueAudits.addChildren(fieldFilterService.filter(DataValueAudit.class, dataValueAudits, fields).getChildren());
return rootNode;
}
use of org.hisp.dhis.dataelement.DataElementCategoryOptionCombo in project dhis2-core by dhis2.
the class DimensionalObjectUtilsTest method testReplaceOperandTotalsWithDataElements.
@Test
public void testReplaceOperandTotalsWithDataElements() {
DataElement deA = new DataElement("NameA");
DataElement deB = new DataElement("NameB");
deA.setAutoFields();
deB.setAutoFields();
DataElementCategoryOptionCombo cocA = new DataElementCategoryOptionCombo();
cocA.setAutoFields();
DataElementOperand opA = new DataElementOperand(deA);
DataElementOperand opB = new DataElementOperand(deA, cocA);
DataElementOperand opC = new DataElementOperand(deB, cocA);
List<DimensionalItemObject> items = Lists.newArrayList(deB, opA, opB, opC);
assertEquals(4, items.size());
assertTrue(items.contains(deB));
assertTrue(items.contains(opA));
assertTrue(items.contains(opB));
assertTrue(items.contains(opC));
assertFalse(items.contains(deA));
items = DimensionalObjectUtils.replaceOperandTotalsWithDataElements(items);
assertEquals(4, items.size());
assertTrue(items.contains(deB));
assertFalse(items.contains(opA));
assertTrue(items.contains(opB));
assertTrue(items.contains(opC));
assertTrue(items.contains(deA));
}
Aggregations