Search in sources :

Example 21 with DataExportParams

use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.

the class FacilityReportingServiceImpl method getDataSetValues.

@Override
public DataSetValueList getDataSetValues(OrganisationUnit unit, DataSetList dataSetList) throws NotAllowedException {
    DataSetValueList dataSetValueList = new DataSetValueList();
    List<DataSet> dataSets = dataSetList.getCurrentDataSets();
    for (DataSet dataSet : dataSets) {
        log.info("Getting DataSetValue for: " + dataSet.getName());
        org.hisp.dhis.dataset.DataSet apiDataSet = dataSetService.getDataSet(dataSet.getId());
        Vector<String> periods = PeriodUtil.generatePeriods(dataSet.getPeriodType());
        if (periods != null) {
            for (int i = 0; i < periods.size(); i++) {
                Period period = getPeriod(periods.elementAt(i), apiDataSet.getPeriodType());
                if (period != null) {
                    Set<org.hisp.dhis.dataelement.DataElement> dataElements = apiDataSet.getDataElements();
                    Collection<org.hisp.dhis.datavalue.DataValue> dataValues = dataValueService.getDataValues(new DataExportParams().setDataElements(dataElements).setPeriods(Sets.newHashSet(period)).setOrganisationUnits(Sets.newHashSet(unit)));
                    if (dataValues != null && !dataValues.isEmpty()) {
                        DataSetValue dataSetValue = new DataSetValue();
                        dataSetValue.setId(dataSet.getId());
                        dataSetValue.setName(dataSet.getName());
                        dataSetValue.setPeriodName(periods.elementAt(i));
                        dataSetValue.setCompleted(true);
                        for (org.hisp.dhis.datavalue.DataValue dataValue : dataValues) {
                            DataValue dv = new DataValue();
                            dv.setCategoryOptComboID(dataValue.getCategoryOptionCombo().getId());
                            dv.setClientVersion(dataSet.getClientVersion());
                            dv.setId(dataValue.getDataElement().getId());
                            dv.setValue(dataValue.getValue());
                            dataSetValue.getDataValues().add(dv);
                        }
                        dataSetValueList.getDataSetValues().add(dataSetValue);
                    }
                }
            }
        }
    }
    log.info("Retrieved Data value set: " + unit.getName() + ", " + dataSetList.getName());
    return dataSetValueList;
}
Also used : DataSet(org.hisp.dhis.api.mobile.model.DataSet) DataValue(org.hisp.dhis.api.mobile.model.DataValue) DataSetValue(org.hisp.dhis.api.mobile.model.DataSetValue) Period(org.hisp.dhis.period.Period) DataElement(org.hisp.dhis.api.mobile.model.DataElement) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) DataSetValueList(org.hisp.dhis.api.mobile.model.DataSetValueList)

Example 22 with DataExportParams

use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.

the class FormUtilsImpl method getDataValueMap.

@Override
public Map<String, String> getDataValueMap(OrganisationUnit organisationUnit, DataSet dataSet, Period period) {
    Map<String, String> dataValueMap = new HashMap<>();
    List<DataValue> values = dataValueService.getDataValues(new DataExportParams().setDataElements(dataSet.getDataElements()).setPeriods(Sets.newHashSet(period)).setOrganisationUnits(Sets.newHashSet(organisationUnit)));
    for (DataValue dataValue : values) {
        DataElement dataElement = dataValue.getDataElement();
        DataElementCategoryOptionCombo optionCombo = dataValue.getCategoryOptionCombo();
        String key = String.format("DE%dOC%d", dataElement.getId(), optionCombo.getId());
        String value = dataValue.getValue();
        dataValueMap.put(key, value);
    }
    return dataValueMap;
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataValue(org.hisp.dhis.datavalue.DataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 23 with DataExportParams

use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.

the class GetDataValuesForDataSetAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    // ---------------------------------------------------------------------
    // Validation
    // ---------------------------------------------------------------------
    User currentUser = currentUserService.getCurrentUser();
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = PeriodType.getPeriodFromIsoString(periodId);
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
    if (organisationUnit == null || period == null || dataSet == null) {
        log.warn("Illegal input, org unit: " + organisationUnit + ", period: " + period + ", data set: " + dataSet);
        return SUCCESS;
    }
    Set<OrganisationUnit> children = organisationUnit.getChildren();
    // ---------------------------------------------------------------------
    // Attributes
    // ---------------------------------------------------------------------
    CategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(cc, cp, false);
    // ---------------------------------------------------------------------
    // Data values & Min-max data elements
    // ---------------------------------------------------------------------
    minMaxDataElements.addAll(minMaxDataElementService.getMinMaxDataElements(organisationUnit, dataSet.getDataElements()));
    if (!multiOrganisationUnit) {
        dataValues.addAll(dataValueService.getDataValues(new DataExportParams().setDataElements(dataSet.getDataElements()).setPeriods(Sets.newHashSet(period)).setOrganisationUnits(Sets.newHashSet(organisationUnit)).setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo))));
    } else {
        for (OrganisationUnit ou : children) {
            if (ou.getDataSets().contains(dataSet)) {
                dataValues.addAll(dataValueService.getDataValues(new DataExportParams().setDataElements(dataSet.getDataElements()).setPeriods(Sets.newHashSet(period)).setOrganisationUnits(Sets.newHashSet(ou)).setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo))));
                minMaxDataElements.addAll(minMaxDataElementService.getMinMaxDataElements(ou, dataSet.getDataElements()));
            }
        }
    }
    // ---------------------------------------------------------------------
    // File resource meta-data
    // ---------------------------------------------------------------------
    List<String> fileResourceUids = dataValues.stream().filter(dv -> dv.getDataElement().isFileType()).map(DataValue::getValue).collect(Collectors.toList());
    dataValueFileResourceMap.putAll(fileResourceService.getFileResources(fileResourceUids).stream().collect(Collectors.toMap(BaseIdentifiableObject::getUid, f -> f)));
    if (!multiOrganisationUnit) {
        CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, organisationUnit, attributeOptionCombo);
        if (registration != null) {
            complete = registration.getCompleted();
            date = registration.getDate();
            storedBy = registration.getStoredBy();
            lastUpdatedBy = registration.getLastUpdatedBy();
        }
        locked = dataSetService.getLockStatus(currentUser, dataSet, period, organisationUnit, attributeOptionCombo, null);
    } else {
        for (OrganisationUnit ou : children) {
            if (ou.getDataSets().contains(dataSet)) {
                locked = dataSetService.getLockStatus(currentUser, dataSet, period, ou, attributeOptionCombo, null);
                if (!locked.isOpen()) {
                    break;
                }
                CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, ou, attributeOptionCombo);
                if (registration != null) {
                    complete = registration.getCompleted();
                    lastUpdatedBy = registration.getLastUpdatedBy();
                }
            }
        }
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DataSet(org.hisp.dhis.dataset.DataSet) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) Period(org.hisp.dhis.period.Period) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 24 with DataExportParams

use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.

the class DefaultAdxDataService method getFromUrl.

// -------------------------------------------------------------------------
// Public methods
// -------------------------------------------------------------------------
@Override
public DataExportParams getFromUrl(DataValueSetQueryParams urlParams) {
    IdSchemes outputIdSchemes = urlParams.getOutputIdSchemes();
    outputIdSchemes.setDefaultIdScheme(IdScheme.CODE);
    DataExportParams params = new DataExportParams();
    if (!isEmpty(urlParams.getDataSet())) {
        params.getDataSets().addAll(getByUidOrCode(DataSet.class, urlParams.getDataSet()));
    }
    if (!isEmpty(urlParams.getPeriod())) {
        params.getPeriods().addAll(periodService.reloadIsoPeriods(new ArrayList<>(urlParams.getPeriod())));
    } else if (urlParams.getStartDate() != null && urlParams.getEndDate() != null) {
        params.setStartDate(urlParams.getStartDate());
        params.setEndDate(urlParams.getEndDate());
    }
    if (!isEmpty(urlParams.getOrgUnit())) {
        params.getOrganisationUnits().addAll(getByUidOrCode(OrganisationUnit.class, urlParams.getOrgUnit()));
    }
    if (!isEmpty(urlParams.getOrgUnitGroup())) {
        params.getOrganisationUnitGroups().addAll(getByUidOrCode(OrganisationUnitGroup.class, urlParams.getOrgUnitGroup()));
    }
    if (!isEmpty(urlParams.getAttributeOptionCombo())) {
        params.getAttributeOptionCombos().addAll(getByUidOrCode(CategoryOptionCombo.class, urlParams.getAttributeOptionCombo()));
    }
    params.setIncludeDescendants(urlParams.isChildren());
    params.setIncludeDeleted(urlParams.isIncludeDeleted());
    params.setLastUpdated(urlParams.getLastUpdated());
    params.setLastUpdatedDuration(urlParams.getLastUpdatedDuration());
    params.setLimit(urlParams.getLimit());
    params.setOutputIdSchemes(outputIdSchemes);
    return params;
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IdSchemes(org.hisp.dhis.common.IdSchemes) DataSet(org.hisp.dhis.dataset.DataSet) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) ArrayList(java.util.ArrayList) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 25 with DataExportParams

use of org.hisp.dhis.datavalue.DataExportParams in project dhis2-core by dhis2.

the class DefaultAdxDataService method writeDataValueSet.

@Override
public void writeDataValueSet(DataExportParams params, OutputStream out) throws AdxException {
    dataValueSetService.decideAccess(params);
    dataValueSetService.validate(params);
    XMLWriter adxWriter = XMLFactory.getXMLWriter(out);
    adxWriter.openElement(AdxDataService.ROOT);
    adxWriter.writeAttribute("xmlns", AdxDataService.NAMESPACE);
    IdSchemes idSchemes = params.getOutputIdSchemes();
    IdScheme ouScheme = idSchemes.getOrgUnitIdScheme();
    IdScheme dsScheme = idSchemes.getDataSetIdScheme();
    IdScheme deScheme = idSchemes.getDataElementIdScheme();
    for (DataSet dataSet : params.getDataSets()) {
        AdxDataSetMetadata metadata = new AdxDataSetMetadata(dataSet, idSchemes);
        for (CategoryOptionCombo aoc : getAttribuetOptionCombos(dataSet, params)) {
            Map<String, String> attributeDimensions = metadata.getExplodedCategoryAttributes(aoc.getId());
            for (OrganisationUnit orgUnit : params.getAllOrganisationUnits()) {
                Period currentPeriod = null;
                OrganisationUnit currentOrgUnit = null;
                DataExportParams queryParams = new DataExportParams().setDataElements(dataSet.getDataElements()).setOrganisationUnits(Sets.newHashSet(orgUnit)).setIncludeDescendants(params.isIncludeDescendants()).setIncludeDeleted(params.isIncludeDeleted()).setLastUpdated(params.getLastUpdated()).setLastUpdatedDuration(params.getLastUpdatedDuration()).setPeriods(params.getPeriods()).setStartDate(params.getStartDate()).setEndDate(params.getEndDate()).setAttributeOptionCombos(Sets.newHashSet(aoc)).setOrderByOrgUnitPath(true).setOrderByPeriod(true);
                for (DataValue dv : dataValueService.getDataValues(queryParams)) {
                    if (!dv.getPeriod().equals(currentPeriod) || !dv.getSource().equals(currentOrgUnit)) {
                        if (currentPeriod != null) {
                            // GROUP
                            adxWriter.closeElement();
                        }
                        currentPeriod = dv.getPeriod();
                        currentOrgUnit = dv.getSource();
                        adxWriter.openElement(AdxDataService.GROUP);
                        adxWriter.writeAttribute(AdxDataService.DATASET, dataSet.getPropertyValue(dsScheme));
                        adxWriter.writeAttribute(AdxDataService.PERIOD, AdxPeriod.serialize(currentPeriod));
                        adxWriter.writeAttribute(AdxDataService.ORGUNIT, currentOrgUnit.getPropertyValue(ouScheme));
                        for (Map.Entry<String, String> e : attributeDimensions.entrySet()) {
                            adxWriter.writeAttribute(e.getKey(), e.getValue());
                        }
                    }
                    adxWriter.openElement(AdxDataService.DATAVALUE);
                    adxWriter.writeAttribute(AdxDataService.DATAELEMENT, dv.getDataElement().getPropertyValue(deScheme));
                    CategoryOptionCombo coc = dv.getCategoryOptionCombo();
                    Map<String, String> categoryDimensions = metadata.getExplodedCategoryAttributes(coc.getId());
                    for (Map.Entry<String, String> e : categoryDimensions.entrySet()) {
                        adxWriter.writeAttribute(e.getKey(), e.getValue());
                    }
                    if (dv.getDataElement().getValueType().isNumeric()) {
                        adxWriter.writeAttribute(AdxDataService.VALUE, dv.getValue());
                    } else {
                        adxWriter.writeAttribute(AdxDataService.VALUE, "0");
                        adxWriter.openElement(AdxDataService.ANNOTATION);
                        adxWriter.writeCharacters(dv.getValue());
                        // ANNOTATION
                        adxWriter.closeElement();
                    }
                    // DATAVALUE
                    adxWriter.closeElement();
                }
                if (currentPeriod != null) {
                    // GROUP
                    adxWriter.closeElement();
                }
            }
        }
    }
    // ADX
    adxWriter.closeElement();
    adxWriter.closeWriter();
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) IdSchemes(org.hisp.dhis.common.IdSchemes) DataSet(org.hisp.dhis.dataset.DataSet) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) IdScheme(org.hisp.dhis.common.IdScheme) XMLWriter(org.hisp.staxwax.writer.XMLWriter) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) Map(java.util.Map) CategoryComboMap(org.hisp.dhis.category.CategoryComboMap) HashMap(java.util.HashMap) CachingMap(org.hisp.dhis.commons.collection.CachingMap) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Aggregations

DataExportParams (org.hisp.dhis.datavalue.DataExportParams)40 Test (org.junit.jupiter.api.Test)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 IdSchemes (org.hisp.dhis.common.IdSchemes)8 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)8 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 ArrayList (java.util.ArrayList)6 DataSet (org.hisp.dhis.dataset.DataSet)6 DeflatedDataValue (org.hisp.dhis.datavalue.DeflatedDataValue)6 User (org.hisp.dhis.user.User)6 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)5 DataValue (org.hisp.dhis.datavalue.DataValue)5 Period (org.hisp.dhis.period.Period)5 Date (java.util.Date)4 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)4 TextUtils.getCommaDelimitedString (org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString)4 DataElement (org.hisp.dhis.dataelement.DataElement)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 List (java.util.List)2