Search in sources :

Example 21 with DataElementCategoryOptionCombo

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Calendar(org.hisp.dhis.calendar.Calendar) Period(org.hisp.dhis.period.Period) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DataElement(org.hisp.dhis.dataelement.DataElement) DateTimeUnit(org.hisp.dhis.calendar.DateTimeUnit) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 22 with DataElementCategoryOptionCombo

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) ArrayList(java.util.ArrayList) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date)

Example 23 with DataElementCategoryOptionCombo

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;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Period(org.hisp.dhis.period.Period) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Expression(org.hisp.dhis.expression.Expression) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 24 with DataElementCategoryOptionCombo

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) RootNode(org.hisp.dhis.node.types.RootNode) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) CollectionNode(org.hisp.dhis.node.types.CollectionNode) DataElement(org.hisp.dhis.dataelement.DataElement) Pager(org.hisp.dhis.common.Pager) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) DataValueAudit(org.hisp.dhis.datavalue.DataValueAudit) TrackedEntityDataValueAudit(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAudit) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 25 with DataElementCategoryOptionCombo

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));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Test(org.junit.Test)

Aggregations

DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)97 DataElement (org.hisp.dhis.dataelement.DataElement)43 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)36 Period (org.hisp.dhis.period.Period)31 DataValue (org.hisp.dhis.datavalue.DataValue)19 ArrayList (java.util.ArrayList)18 Date (java.util.Date)18 DataSet (org.hisp.dhis.dataset.DataSet)15 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)15 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)13 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)12 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)12 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)10 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)9 HashSet (java.util.HashSet)8 Test (org.junit.Test)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 Matcher (java.util.regex.Matcher)5 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)5 HashMap (java.util.HashMap)4