Search in sources :

Example 36 with DataExportParams

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

the class HibernateDataValueStore method getDataValues.

// -------------------------------------------------------------------------
// Collections of DataValues
// -------------------------------------------------------------------------
@Override
public List<DataValue> getDataValues(DataExportParams params) {
    Set<DataElement> dataElements = params.getAllDataElements();
    Set<OrganisationUnit> organisationUnits = params.getAllOrganisationUnits();
    // ---------------------------------------------------------------------
    // HQL parameters
    // ---------------------------------------------------------------------
    String hql = "select dv from DataValue dv " + "inner join dv.dataElement de " + "inner join dv.period pe " + "inner join dv.source ou " + "inner join dv.categoryOptionCombo co " + "inner join dv.attributeOptionCombo ao " + "where de.id in (:dataElements) ";
    if (params.hasPeriods()) {
        hql += "and pe.id in (:periods) ";
    } else if (params.hasStartEndDate()) {
        hql += "and (pe.startDate >= :startDate and pe.endDate <= :endDate) ";
    }
    if (params.isIncludeDescendantsForOrganisationUnits()) {
        hql += "and (";
        for (OrganisationUnit unit : params.getOrganisationUnits()) {
            hql += "ou.path like '" + unit.getPath() + "%' or ";
        }
        hql = removeLastOr(hql);
        hql += ") ";
    } else if (!organisationUnits.isEmpty()) {
        hql += "and ou.id in (:orgUnits) ";
    }
    if (params.hasAttributeOptionCombos()) {
        hql += "and ao.id in (:attributeOptionCombos) ";
    }
    if (params.hasLastUpdated() || params.hasLastUpdatedDuration()) {
        hql += "and dv.lastUpdated >= :lastUpdated ";
    }
    if (!params.isIncludeDeleted()) {
        hql += "and dv.deleted is false ";
    }
    if (params.isOrderByOrgUnitPath()) {
        hql += "order by ou.path ";
    }
    if (params.isOrderByPeriod()) {
        hql += params.isOrderByOrgUnitPath() ? "," : "order by";
        hql += " pe.startDate, pe.endDate ";
    }
    // ---------------------------------------------------------------------
    // Query parameters
    // ---------------------------------------------------------------------
    Query<DataValue> query = getQuery(hql).setParameterList("dataElements", getIdentifiers(dataElements));
    if (params.hasPeriods()) {
        Set<Period> periods = params.getPeriods().stream().map(p -> periodStore.reloadPeriod(p)).collect(Collectors.toSet());
        query.setParameterList("periods", getIdentifiers(periods));
    } else if (params.hasStartEndDate()) {
        query.setParameter("startDate", params.getStartDate()).setParameter("endDate", params.getEndDate());
    }
    if (!params.isIncludeDescendantsForOrganisationUnits() && !organisationUnits.isEmpty()) {
        query.setParameterList("orgUnits", getIdentifiers(organisationUnits));
    }
    if (params.hasAttributeOptionCombos()) {
        query.setParameterList("attributeOptionCombos", getIdentifiers(params.getAttributeOptionCombos()));
    }
    if (params.hasLastUpdated()) {
        query.setParameter("lastUpdated", params.getLastUpdated());
    } else if (params.hasLastUpdatedDuration()) {
        query.setParameter("lastUpdated", DateUtils.nowMinusDuration(params.getLastUpdatedDuration()));
    }
    if (params.hasLimit()) {
        query.setMaxResults(params.getLimit());
    }
    return query.list();
}
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) DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataValue(org.hisp.dhis.datavalue.DataValue) Period(org.hisp.dhis.period.Period) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString)

Example 37 with DataExportParams

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

the class DefaultCompleteDataSetRegistrationService method getMissingCompulsoryFields.

@Override
@Transactional(readOnly = true)
public List<DataElementOperand> getMissingCompulsoryFields(DataSet dataSet, Period period, OrganisationUnit organisationUnit, CategoryOptionCombo attributeOptionCombo) {
    List<DataElementOperand> missingDataElementOperands = new ArrayList<>();
    if (!dataSet.getCompulsoryDataElementOperands().isEmpty()) {
        DataExportParams params = new DataExportParams();
        params.setDataElementOperands(dataSet.getCompulsoryDataElementOperands());
        params.setPeriods(Sets.newHashSet(period));
        params.setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo));
        params.setOrganisationUnits(Sets.newHashSet(organisationUnit));
        List<DeflatedDataValue> deflatedDataValues = dataValueService.getDeflatedDataValues(params);
        MapMapMap<Long, Long, Long, Boolean> dataPresent = new MapMapMap<>();
        for (DeflatedDataValue dv : deflatedDataValues) {
            dataPresent.putEntry(dv.getSourceId(), dv.getDataElementId(), dv.getCategoryOptionComboId(), true);
        }
        User currentUser = currentUserService.getCurrentUser();
        for (DataElementOperand deo : dataSet.getCompulsoryDataElementOperands()) {
            List<String> errors = accessManager.canWrite(currentUser, deo);
            if (!errors.isEmpty()) {
                continue;
            }
            MapMap<Long, Long, Boolean> ouDataPresent = dataPresent.get(organisationUnit.getId());
            if (ouDataPresent != null) {
                Map<Long, Boolean> deDataPresent = ouDataPresent.get(deo.getDataElement().getId());
                if (deDataPresent != null && (deo.getCategoryOptionCombo() == null || deDataPresent.get(deo.getCategoryOptionCombo().getId()) != null)) {
                    continue;
                }
            }
            missingDataElementOperands.add(deo);
        }
    }
    return missingDataElementOperands;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) User(org.hisp.dhis.user.User) ArrayList(java.util.ArrayList) MapMapMap(org.hisp.dhis.common.MapMapMap) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) Transactional(org.springframework.transaction.annotation.Transactional)

Example 38 with DataExportParams

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

the class PredictionServiceTest method getDataValue.

private String getDataValue(DataElement dataElement, CategoryOptionCombo combo, CategoryOptionCombo attributeOptionCombo, OrganisationUnit source, Period period) {
    DataExportParams params = new DataExportParams().setDataElementOperands(Sets.newHashSet(new DataElementOperand(dataElement, combo))).setAttributeOptionCombos(Sets.newHashSet(attributeOptionCombo)).setOrganisationUnits(Sets.newHashSet(source)).setPeriods(Sets.newHashSet(periodService.reloadPeriod(period)));
    List<DeflatedDataValue> values = dataValueService.getDeflatedDataValues(params);
    if (values != null && values.size() > 0) {
        return values.get(0).getValue();
    }
    return null;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams)

Example 39 with DataExportParams

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

the class DataValidationTask method getDataValueMap.

/**
 * Gets data elements and data element operands from the datavalue table.
 */
private void getDataValueMap() {
    DataExportParams params = new DataExportParams();
    params.setDataElements(periodTypeX.getDataElements());
    params.setDataElementOperands(periodTypeX.getDataElementOperands());
    params.setIncludedDate(period.getStartDate());
    params.setOrganisationUnits(new HashSet<>(orgUnits));
    params.setPeriodTypes(periodTypeX.getAllowedPeriodTypes());
    params.setCoDimensionConstraints(context.getCoDimensionConstraints());
    params.setCogDimensionConstraints(context.getCogDimensionConstraints());
    if (context.getAttributeCombo() != null) {
        params.setAttributeOptionCombos(Sets.newHashSet(context.getAttributeCombo()));
    }
    List<DeflatedDataValue> dataValues = dataValueService.getDeflatedDataValues(params);
    dataMap = new MapMapMap<>();
    MapMapMap<Long, String, DimensionalItemObject, Long> duplicateCheck = new MapMapMap<>();
    for (DeflatedDataValue dv : dataValues) {
        DataElement dataElement = periodTypeX.getDataElementIdMap().get(dv.getDataElementId());
        String deoIdKey = periodTypeX.getDeoIds(dv.getDataElementId(), dv.getCategoryOptionComboId());
        DataElementOperand dataElementOperand = periodTypeX.getDataElementOperandIdMap().get(deoIdKey);
        Period p = getPeriod(dv.getPeriodId());
        long orgUnitId = dv.getSourceId();
        String attributeOptionComboUid = getAttributeOptionCombo(dv.getAttributeOptionComboId()).getUid();
        if (dataElement != null) {
            Object value = getObjectValue(dv.getValue(), dataElement.getValueType());
            addValueToDataMap(orgUnitId, attributeOptionComboUid, dataElement, value, p, duplicateCheck);
        }
        if (dataElementOperand != null) {
            Object value = getObjectValue(dv.getValue(), dataElementOperand.getDataElement().getValueType());
            addValueToDataMap(orgUnitId, attributeOptionComboUid, dataElementOperand, value, p, duplicateCheck);
        }
    }
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Period(org.hisp.dhis.period.Period) DataElement(org.hisp.dhis.dataelement.DataElement) DeflatedDataValue(org.hisp.dhis.datavalue.DeflatedDataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams)

Example 40 with DataExportParams

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

the class DataSetController method getForm.

private Form getForm(List<DataSet> dataSets, OrganisationUnit ou, Period pe, String categoryOptions, boolean metaData) throws IOException {
    DataSet dataSet = dataSets.get(0);
    Form form = FormUtils.fromDataSet(dataSets.get(0), metaData, null);
    Set<String> options = null;
    if (StringUtils.isNotEmpty(categoryOptions) && categoryOptions.startsWith("[") && categoryOptions.endsWith("]")) {
        String[] split = categoryOptions.substring(1, categoryOptions.length() - 1).split(",");
        options = new HashSet<>(Lists.newArrayList(split));
    }
    if (ou != null && pe != null) {
        Set<CategoryOptionCombo> attrOptionCombos = options == null || options.isEmpty() ? null : Sets.newHashSet(inputUtils.getAttributeOptionCombo(dataSet.getCategoryCombo(), options, IdScheme.UID));
        List<DataValue> dataValues = dataValueService.getDataValues(new DataExportParams().setDataElements(dataSets.get(0).getDataElements()).setPeriods(Sets.newHashSet(pe)).setOrganisationUnits(Sets.newHashSet(ou)).setAttributeOptionCombos(attrOptionCombos));
        FormUtils.fillWithDataValues(form, dataValues);
    }
    return form;
}
Also used : DataSet(org.hisp.dhis.dataset.DataSet) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) Form(org.hisp.dhis.webapi.webdomain.form.Form) DataValue(org.hisp.dhis.datavalue.DataValue) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) 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