Search in sources :

Example 6 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 7 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 8 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 9 with DataExportParams

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

the class HibernateDataValueStore method getDataValues.

// -------------------------------------------------------------------------
// Collections of DataValues
// -------------------------------------------------------------------------
@Override
@SuppressWarnings("unchecked")
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.isIncludeChildrenForOrganisationUnits()) {
        hql += "and (";
        for (OrganisationUnit unit : params.getOrganisationUnits()) {
            hql += "ou.path like '" + unit.getPath() + "%' or ";
        }
        hql = TextUtils.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()) {
        hql += "and dv.lastUpdated >= :lastUpdated ";
    }
    if (!params.isIncludeDeleted()) {
        hql += "and dv.deleted is false ";
    }
    // ---------------------------------------------------------------------
    // Query parameters
    // ---------------------------------------------------------------------
    Query query = sessionFactory.getCurrentSession().createQuery(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.setDate("startDate", params.getStartDate()).setDate("endDate", params.getEndDate());
    }
    if (!params.isIncludeChildrenForOrganisationUnits() && !organisationUnits.isEmpty()) {
        query.setParameterList("orgUnits", getIdentifiers(organisationUnits));
    }
    if (params.hasAttributeOptionCombos()) {
        query.setParameterList("attributeOptionCombos", getIdentifiers(params.getAttributeOptionCombos()));
    }
    if (params.hasLastUpdated()) {
        query.setDate("lastUpdated", params.getLastUpdated());
    }
    if (params.hasLimit()) {
        query.setMaxResults(params.getLimit());
    }
    return query.list();
}
Also used : Criteria(org.hibernate.Criteria) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) IdentifiableObjectUtils(org.hisp.dhis.common.IdentifiableObjectUtils) Restrictions(org.hibernate.criterion.Restrictions) Date(java.util.Date) IdentifiableObjectUtils.getIdentifiers(org.hisp.dhis.common.IdentifiableObjectUtils.getIdentifiers) Session(org.hibernate.Session) StringUtils(org.apache.commons.lang3.StringUtils) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet) MapMapMap(org.hisp.dhis.common.MapMapMap) Map(java.util.Map) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) Query(org.hibernate.Query) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) Period(org.hisp.dhis.period.Period) DataValueStore(org.hisp.dhis.datavalue.DataValueStore) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) Collection(java.util.Collection) SessionFactory(org.hibernate.SessionFactory) CategoryOptionGroup(org.hisp.dhis.dataelement.CategoryOptionGroup) Set(java.util.Set) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) DateUtils(org.hisp.dhis.system.util.DateUtils) Projections(org.hibernate.criterion.Projections) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) Collectors(java.util.stream.Collectors) SetMap(org.hisp.dhis.common.SetMap) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) PeriodStore(org.hisp.dhis.period.PeriodStore) DataValue(org.hisp.dhis.datavalue.DataValue) PeriodType(org.hisp.dhis.period.PeriodType) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) MathUtils(org.hisp.dhis.system.util.MathUtils) MapMap(org.hisp.dhis.common.MapMap) TextUtils(org.hisp.dhis.commons.util.TextUtils) DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Query(org.hibernate.Query) Period(org.hisp.dhis.period.Period)

Example 10 with DataExportParams

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

the class SpringDataValueSetStore method writeDataValueSetJson.

@Override
public void writeDataValueSetJson(Date lastUpdated, OutputStream outputStream, IdSchemes idSchemes) {
    String deScheme = idSchemes.getDataElementIdScheme().getIdentifiableString().toLowerCase();
    String ouScheme = idSchemes.getOrgUnitIdScheme().getIdentifiableString().toLowerCase();
    String ocScheme = idSchemes.getCategoryOptionComboIdScheme().getIdentifiableString().toLowerCase();
    DataValueSet dataValueSet = new StreamingJsonDataValueSet(outputStream);
    final String sql = "select de." + deScheme + " as deid, pe.startdate as pestart, pt.name as ptname, ou." + ouScheme + " as ouid, " + "coc." + ocScheme + " as cocid, aoc." + ocScheme + " as aocid, " + "dv.value, dv.storedby, dv.created, dv.lastupdated, dv.comment, dv.followup, dv.deleted " + "from datavalue dv " + "join dataelement de on (dv.dataelementid=de.dataelementid) " + "join period pe on (dv.periodid=pe.periodid) " + "join periodtype pt on (pe.periodtypeid=pt.periodtypeid) " + "join organisationunit ou on (dv.sourceid=ou.organisationunitid) " + "join categoryoptioncombo coc on (dv.categoryoptioncomboid=coc.categoryoptioncomboid) " + "join categoryoptioncombo aoc on (dv.attributeoptioncomboid=aoc.categoryoptioncomboid) " + "where dv.lastupdated >= '" + DateUtils.getLongDateString(lastUpdated) + "'";
    writeDataValueSet(sql, new DataExportParams(), null, dataValueSet);
}
Also used : DataExportParams(org.hisp.dhis.datavalue.DataExportParams) DateUtils.getMediumDateString(org.hisp.dhis.system.util.DateUtils.getMediumDateString) DateUtils.getLongGmtDateString(org.hisp.dhis.system.util.DateUtils.getLongGmtDateString) TextUtils.getCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getCommaDelimitedString)

Aggregations

DataExportParams (org.hisp.dhis.datavalue.DataExportParams)16 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 DhisSpringTest (org.hisp.dhis.DhisSpringTest)5 Test (org.junit.Test)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 IdSchemes (org.hisp.dhis.common.IdSchemes)3 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)3 DataValue (org.hisp.dhis.datavalue.DataValue)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 Period (org.hisp.dhis.period.Period)2 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1