use of org.hisp.dhis.datavalue.DeflatedDataValue 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;
}
use of org.hisp.dhis.datavalue.DeflatedDataValue in project dhis2-core by dhis2.
the class PredictionDataValueFetcher method getData.
/**
* In the main thread, gets prediction data for the next organisation unit.
* <p>
* Note that "inflating" the data values from their deflated form must be
* done in the main thread so as not to upset the DataValue's Hibernate
* properties.
*
* @return the prediction data
*/
public PredictionData getData() {
if (nextOrgUnit == null) {
return null;
}
List<DeflatedDataValue> deflatedDataValues = new ArrayList<>();
OrganisationUnit startingOrgUnit = nextOrgUnit;
do {
deflatedDataValues.add(nextDeflatedDataValue);
getNextDeflatedDataValue();
} while (startingOrgUnit.equals(nextOrgUnit));
return getPredictionData(startingOrgUnit, deflatedDataValues);
}
use of org.hisp.dhis.datavalue.DeflatedDataValue in project dhis2-core by dhis2.
the class MinMaxOutlierAnalysisServiceTest method testAnalyse.
// ----------------------------------------------------------------------
// Business logic tests
// ----------------------------------------------------------------------
@Test
void testAnalyse() {
dataValueA = createDataValue(dataElementA, periodI, organisationUnitA, "41", categoryOptionCombo);
dataValueB = createDataValue(dataElementA, periodJ, organisationUnitA, "-41", categoryOptionCombo);
dataValueService.addDataValue(createDataValue(dataElementA, periodA, organisationUnitA, "5", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodB, organisationUnitA, "-5", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodC, organisationUnitA, "5", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodD, organisationUnitA, "-5", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodE, organisationUnitA, "10", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodF, organisationUnitA, "-10", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodG, organisationUnitA, "13", categoryOptionCombo));
dataValueService.addDataValue(createDataValue(dataElementA, periodH, organisationUnitA, "-13", categoryOptionCombo));
dataValueService.addDataValue(dataValueA);
dataValueService.addDataValue(dataValueB);
minMaxDataElement = new MinMaxDataElement(dataElementA, organisationUnitA, categoryOptionCombo, -40, 40, false);
minMaxDataElementService.addMinMaxDataElement(minMaxDataElement);
List<Period> periods = new ArrayList<>();
periods.add(periodI);
periods.add(periodJ);
periods.add(periodA);
periods.add(periodE);
List<DeflatedDataValue> result = minMaxOutlierAnalysisService.analyse(Lists.newArrayList(organisationUnitA), dataElementsA, periods, null, from);
assertEquals(2, result.size());
}
use of org.hisp.dhis.datavalue.DeflatedDataValue in project dhis2-core by dhis2.
the class DeflatedDataValueNameMinMaxRowMapper method mapRow.
@Override
public DeflatedDataValue mapRow(ResultSet resultSet) throws SQLException {
final DeflatedDataValue value = new DeflatedDataValue();
value.setDataElementId(resultSet.getLong("dataelementid"));
value.setPeriodId(resultSet.getLong("periodid"));
value.setSourceId(resultSet.getLong("sourceid"));
value.setCategoryOptionComboId(resultSet.getLong("categoryoptioncomboid"));
value.setAttributeOptionComboId(resultSet.getLong("attributeoptioncomboid"));
value.setValue(resultSet.getString("value"));
value.setStoredBy(resultSet.getString("storedby"));
value.setCreated(resultSet.getDate("created"));
value.setLastUpdated(resultSet.getDate("lastupdated"));
value.setComment(resultSet.getString("comment"));
value.setFollowup(resultSet.getBoolean("followup"));
value.setMin(minMap != null ? minMap.get(value.getSourceId()) : resultSet.getInt("minimumvalue"));
value.setMax(maxMap != null ? maxMap.get(value.getSourceId()) : resultSet.getInt("maximumvalue"));
value.setDataElementName(resultSet.getString("dataelementname"));
value.setPeriod(resultSet.getString("periodtypename"), resultSet.getDate("startdate"), resultSet.getDate("enddate"));
value.setSourceName(resultSet.getString("sourcename"));
value.setCategoryOptionComboName(resultSet.getString("categoryoptioncomboname"));
return value;
}
use of org.hisp.dhis.datavalue.DeflatedDataValue in project dhis2-core by dhis2.
the class DataAnalysisController method getPdfReport.
@GetMapping("/report.pdf")
public void getPdfReport(HttpSession session, HttpServletResponse response) throws Exception {
@SuppressWarnings("unchecked") List<DeflatedDataValue> results = (List<DeflatedDataValue>) session.getAttribute(KEY_ANALYSIS_DATA_VALUES);
Grid grid = generateAnalysisReportGridFromResults(results, (OrganisationUnit) session.getAttribute(KEY_ORG_UNIT));
String filename = filenameEncode(grid.getTitle()) + ".pdf";
contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_PDF, CacheStrategy.RESPECT_SYSTEM_SETTING, filename, false);
GridUtils.toPdf(grid, response.getOutputStream());
}
Aggregations