Search in sources :

Example 1 with DataExportParams

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

the class DataValueSetController method getDataValueSetXml.

// -------------------------------------------------------------------------
// Get
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET, produces = CONTENT_TYPE_XML)
public void getDataValueSetXml(@RequestParam(required = false) Set<String> dataSet, @RequestParam(required = false) Set<String> dataElementGroup, @RequestParam(required = false) Set<String> period, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Set<String> orgUnit, @RequestParam(required = false) boolean children, @RequestParam(required = false) Set<String> orgUnitGroup, @RequestParam(required = false) Set<String> attributeOptionCombo, @RequestParam(required = false) boolean includeDeleted, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) String lastUpdatedDuration, @RequestParam(required = false) Integer limit, IdSchemes idSchemes, HttpServletResponse response) throws IOException {
    response.setContentType(CONTENT_TYPE_XML);
    DataExportParams params = dataValueSetService.getFromUrl(dataSet, dataElementGroup, period, startDate, endDate, orgUnit, children, orgUnitGroup, attributeOptionCombo, includeDeleted, lastUpdated, lastUpdatedDuration, limit, idSchemes);
    dataValueSetService.writeDataValueSetXml(params, response.getOutputStream());
}
Also used : DataExportParams(org.hisp.dhis.datavalue.DataExportParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with DataExportParams

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

the class ExportDataValueAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    //TODO re-implement using Web API
    IdSchemes idSchemes = new IdSchemes();
    idSchemes.setDataElementIdScheme(StringUtils.trimToNull(dataElementIdScheme));
    idSchemes.setOrgUnitIdScheme(StringUtils.trimToNull(orgUnitIdScheme));
    idSchemes.setCategoryOptionComboIdScheme(StringUtils.trimToNull(categoryOptionComboIdScheme));
    Set<String> orgUnits = new HashSet<>(IdentifiableObjectUtils.getUids(selectionTreeManager.getSelectedOrganisationUnits()));
    HttpServletResponse response = ServletActionContext.getResponse();
    OutputStream out = response.getOutputStream();
    DataExportParams params = dataValueSetService.getFromUrl(selectedDataSets, null, null, getMediumDate(startDate), getMediumDate(endDate), orgUnits, true, null, null, false, null, null, null, idSchemes);
    boolean isCompression = compression == null || COMPRESSION_ZIP.equals(compression);
    if (FORMAT_CSV.equals(exportFormat)) {
        ContextUtils.configureResponse(response, CONTENT_TYPE_CSV, true, getFileName(EXTENSION_CSV_ZIP), true);
        dataValueSetService.writeDataValueSetCsv(params, new OutputStreamWriter(getZipOut(out, getFileName(EXTENSION_CSV))));
    } else if (FORMAT_JSON.equals(exportFormat)) {
        ContextUtils.configureResponse(response, CONTENT_TYPE_JSON, true, getFileName(EXTENSION_JSON_ZIP), isCompression);
        dataValueSetService.writeDataValueSetJson(params, isCompression ? getZipOut(out, getFileName(EXTENSION_JSON)) : out);
    } else {
        ContextUtils.configureResponse(response, CONTENT_TYPE_XML, true, getFileName(EXTENSION_XML_ZIP), isCompression);
        dataValueSetService.writeDataValueSetXml(params, isCompression ? getZipOut(out, getFileName(EXTENSION_XML)) : out);
    }
    return SUCCESS;
}
Also used : IdSchemes(org.hisp.dhis.common.IdSchemes) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) OutputStreamWriter(java.io.OutputStreamWriter) HashSet(java.util.HashSet)

Example 3 with DataExportParams

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

the class DataValueSetController method getDataValueSetXmlAdx.

@RequestMapping(method = RequestMethod.GET, produces = CONTENT_TYPE_XML_ADX)
public void getDataValueSetXmlAdx(@RequestParam Set<String> dataSet, @RequestParam(required = false) Set<String> period, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam Set<String> orgUnit, @RequestParam(required = false) boolean children, @RequestParam(required = false) boolean includeDeleted, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Integer limit, IdSchemes idSchemes, HttpServletResponse response) throws IOException, AdxException {
    response.setContentType(CONTENT_TYPE_XML_ADX);
    DataExportParams params = adxDataService.getFromUrl(dataSet, period, startDate, endDate, orgUnit, children, includeDeleted, lastUpdated, limit, idSchemes);
    adxDataService.writeDataValueSet(params, response.getOutputStream());
}
Also used : DataExportParams(org.hisp.dhis.datavalue.DataExportParams) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 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(Set<String> dataSets, Set<String> periods, Date startDate, Date endDate, Set<String> organisationUnits, boolean includeChildren, boolean includeDeleted, Date lastUpdated, Integer limit, IdSchemes outputIdSchemes) {
    DataExportParams params = new DataExportParams();
    if (dataSets != null) {
        params.getDataSets().addAll(identifiableObjectManager.getByCode(DataSet.class, dataSets));
    }
    if (periods != null && !periods.isEmpty()) {
        params.getPeriods().addAll(periodService.reloadIsoPeriods(new ArrayList<>(periods)));
    } else if (startDate != null && endDate != null) {
        params.setStartDate(startDate);
        params.setEndDate(endDate);
    }
    if (organisationUnits != null) {
        params.getOrganisationUnits().addAll(identifiableObjectManager.getByCode(OrganisationUnit.class, organisationUnits));
    }
    params.setIncludeChildren(includeChildren);
    params.setIncludeDeleted(includeDeleted);
    params.setLastUpdated(lastUpdated);
    params.setLimit(limit);
    params.setOutputIdSchemes(outputIdSchemes);
    return params;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) ArrayList(java.util.ArrayList)

Example 5 with DataExportParams

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

the class HibernateDataValueStore method getDeflatedDataValues.

@Override
public List<DeflatedDataValue> getDeflatedDataValues(DataExportParams params) {
    SqlHelper sqlHelper = new SqlHelper(true);
    boolean joinOrgUnit = params.isOrderByOrgUnitPath() || params.hasOrgUnitLevel() || params.getOuMode() == DESCENDANTS || params.isIncludeDescendants();
    String sql = "select dv.dataelementid, dv.periodid, dv.sourceid" + ", dv.categoryoptioncomboid, dv.attributeoptioncomboid, dv.value" + ", dv.storedby, dv.created, dv.lastupdated, dv.comment, dv.followup, dv.deleted" + (joinOrgUnit ? ", ou.path" : "") + " from datavalue dv";
    String where = "";
    List<DataElementOperand> queryDeos = getQueryDataElementOperands(params);
    if (queryDeos != null) {
        List<Long> deIdList = queryDeos.stream().map(de -> de.getDataElement().getId()).collect(Collectors.toList());
        List<Long> cocIdList = queryDeos.stream().map(de -> de.getCategoryOptionCombo() == null ? null : de.getCategoryOptionCombo().getId()).collect(Collectors.toList());
        sql += " join " + statementBuilder.literalLongLongTable(deIdList, cocIdList, "deo", "deid", "cocid") + " on deo.deid = dv.dataelementid and (deo.cocid is null or deo.cocid::bigint = dv.categoryoptioncomboid)";
    } else if (params.hasDataElements()) {
        String dataElementIdList = getCommaDelimitedString(getIdentifiers(params.getDataElements()));
        where += sqlHelper.whereAnd() + "dv.dataelementid in (" + dataElementIdList + ")";
    }
    if (params.hasPeriods()) {
        String periodIdList = getCommaDelimitedString(getIdentifiers(params.getPeriods()));
        where += sqlHelper.whereAnd() + "dv.periodid in (" + periodIdList + ")";
    } else if (params.hasPeriodTypes() || params.hasStartEndDate() || params.hasIncludedDate()) {
        sql += " join period p on p.periodid = dv.periodid";
        if (params.hasPeriodTypes()) {
            sql += " join periodtype pt on pt.periodtypeid = p.periodtypeid";
            String periodTypeIdList = getCommaDelimitedString(params.getPeriodTypes().stream().map(o -> o.getId()).collect(Collectors.toList()));
            where += sqlHelper.whereAnd() + "pt.periodtypeid in (" + periodTypeIdList + ")";
        }
        if (params.hasStartEndDate()) {
            where += sqlHelper.whereAnd() + "p.startdate >= '" + DateUtils.getMediumDateString(params.getStartDate()) + "'" + " and p.enddate <= '" + DateUtils.getMediumDateString(params.getStartDate()) + "'";
        } else if (params.hasIncludedDate()) {
            where += sqlHelper.whereAnd() + "p.startdate <= '" + DateUtils.getMediumDateString(params.getIncludedDate()) + "'" + " and p.enddate >= '" + DateUtils.getMediumDateString(params.getIncludedDate()) + "'";
        }
    }
    if (joinOrgUnit) {
        sql += " join organisationunit ou on ou.organisationunitid = dv.sourceid";
    }
    if (params.hasOrgUnitLevel()) {
        where += sqlHelper.whereAnd() + "ou.hierarchylevel " + (params.isIncludeDescendants() ? ">" : "") + "= " + params.getOrgUnitLevel();
    }
    if (params.hasOrganisationUnits()) {
        if (params.getOuMode() == DESCENDANTS) {
            where += sqlHelper.whereAnd() + "(";
            for (OrganisationUnit parent : params.getOrganisationUnits()) {
                where += sqlHelper.or() + "ou.path like '" + parent.getPath() + "%'";
            }
            where += " )";
        } else {
            String orgUnitIdList = getCommaDelimitedString(getIdentifiers(params.getOrganisationUnits()));
            where += sqlHelper.whereAnd() + "dv.sourceid in (" + orgUnitIdList + ")";
        }
    }
    if (params.hasAttributeOptionCombos()) {
        String aocIdList = getCommaDelimitedString(getIdentifiers(params.getAttributeOptionCombos()));
        where += sqlHelper.whereAnd() + "dv.attributeoptioncomboid in (" + aocIdList + ")";
    }
    if (params.hasCogDimensionConstraints() || params.hasCoDimensionConstraints()) {
        sql += " join categoryoptioncombos_categoryoptions cc on dv.attributeoptioncomboid = cc.categoryoptioncomboid";
        if (params.hasCoDimensionConstraints()) {
            String coDimConstraintsList = getCommaDelimitedString(getIdentifiers(params.getCoDimensionConstraints()));
            where += sqlHelper.whereAnd() + "cc.categoryoptionid in (" + coDimConstraintsList + ") ";
        }
        if (params.hasCogDimensionConstraints()) {
            String cogDimConstraintsList = getCommaDelimitedString(getIdentifiers(params.getCogDimensionConstraints()));
            sql += " join categoryoptiongroupmembers cogm on cc.categoryoptionid = cogm.categoryoptionid";
            where += sqlHelper.whereAnd() + "cogm.categoryoptiongroupid in (" + cogDimConstraintsList + ")";
        }
    }
    if (params.hasLastUpdated()) {
        where += sqlHelper.whereAnd() + "dv.lastupdated >= " + DateUtils.getMediumDateString(params.getLastUpdated());
    }
    if (!params.isIncludeDeleted()) {
        where += sqlHelper.whereAnd() + "dv.deleted is false";
    }
    sql += where;
    if (params.isOrderByOrgUnitPath()) {
        sql += " order by ou.path";
    }
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    List<DeflatedDataValue> result = new ArrayList<>();
    while (rowSet.next()) {
        Integer dataElementId = rowSet.getInt(1);
        Integer periodId = rowSet.getInt(2);
        Integer organisationUnitId = rowSet.getInt(3);
        Integer categoryOptionComboId = rowSet.getInt(4);
        Integer attributeOptionComboId = rowSet.getInt(5);
        String value = rowSet.getString(6);
        String storedBy = rowSet.getString(7);
        Date created = rowSet.getDate(8);
        Date lastUpdated = rowSet.getDate(9);
        String comment = rowSet.getString(10);
        boolean followup = rowSet.getBoolean(11);
        boolean deleted = rowSet.getBoolean(12);
        String sourcePath = joinOrgUnit ? rowSet.getString(13) : null;
        DeflatedDataValue ddv = new DeflatedDataValue(dataElementId, periodId, organisationUnitId, categoryOptionComboId, attributeOptionComboId, value, storedBy, created, lastUpdated, comment, followup, deleted);
        ddv.setSourcePath(sourcePath);
        if (params.hasBlockingQueue()) {
            if (!addToBlockingQueue(params.getBlockingQueue(), ddv)) {
                // Abort
                return result;
            }
        } else {
            result.add(ddv);
        }
    }
    if (params.hasBlockingQueue()) {
        addToBlockingQueue(params.getBlockingQueue(), END_OF_DDV_DATA);
    }
    log.debug(result.size() + " DeflatedDataValues returned from: " + sql);
    return result;
}
Also used : HibernateGenericStore(org.hisp.dhis.hibernate.HibernateGenericStore) IdentifiableObjectUtils.getIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers) Date(java.util.Date) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) Function(java.util.function.Function) ArrayList(java.util.ArrayList) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) DataElement(org.hisp.dhis.dataelement.DataElement) Predicate(javax.persistence.criteria.Predicate) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) Repository(org.springframework.stereotype.Repository) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) Root(javax.persistence.criteria.Root) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) Period(org.hisp.dhis.period.Period) DataValueStore(org.hisp.dhis.datavalue.DataValueStore) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) SessionFactory(org.hibernate.SessionFactory) DESCENDANTS(org.hisp.dhis.common.OrganisationUnitSelectionMode.DESCENDANTS) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) Collectors(java.util.stream.Collectors) TextUtils.removeLastOr(org.hisp.dhis.commons.util.TextUtils.removeLastOr) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) PeriodStore(org.hisp.dhis.period.PeriodStore) DataValue(org.hisp.dhis.datavalue.DataValue) DateUtils(org.hisp.dhis.util.DateUtils) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) ArrayList(java.util.ArrayList) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString) Date(java.util.Date) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue)

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