use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class ValidationResultServiceTest method assertIllegalRequest.
private <T> void assertIllegalRequest(ErrorCode expected, BiConsumer<ValidationResultsDeletionRequest, T> operation, T value) {
ValidationResultsDeletionRequest request = new ValidationResultsDeletionRequest();
operation.accept(request, value);
IllegalQueryException ex = assertThrows(IllegalQueryException.class, () -> service.deleteValidationResults(request));
assertError(ex, expected, getFaultyValue(value));
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class ValidationResultServiceTest method assertIllegalQuery.
private void assertIllegalQuery(ErrorCode expected, BiConsumer<ValidationResultQuery, List<String>> operation, String... values) {
ValidationResultQuery query = new ValidationResultQuery();
operation.accept(query, asList(values));
IllegalQueryException ex = assertThrows(IllegalQueryException.class, () -> service.getValidationResults(query));
String errorValue = values[values.length - 1];
assertError(ex, expected, errorValue);
ex = assertThrows(IllegalQueryException.class, () -> service.countValidationResults(query));
assertError(ex, expected, errorValue);
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class ZScoreOutlierDetectionManager method getOutlierValues.
/**
* Returns a list of outlier data values based on z-score for the given
* request.
*
* @param request the {@link OutlierDetectionRequest}.
* @return a list of {@link OutlierValue}.
*/
public List<OutlierValue> getOutlierValues(OutlierDetectionRequest request) {
final String ouPathClause = getOrgUnitPathClause(request.getOrgUnits());
final String dataStartDateClause = getDataStartDateClause(request.getDataStartDate());
final String dataEndDateClause = getDataEndDateClause(request.getDataEndDate());
final boolean modifiedZ = request.getAlgorithm() == OutlierDetectionAlgorithm.MOD_Z_SCORE;
final String middle_stats_calc = modifiedZ ? "percentile_cont(0.5) within group(order by dv.value::double precision)" : "avg(dv.value::double precision)";
String order = request.getOrderBy() == Order.MEAN_ABS_DEV ? "middle_value_abs_dev" : request.getOrderBy().getKey();
// @formatter:off
final String sql = "select dvs.de_uid, dvs.ou_uid, dvs.coc_uid, dvs.aoc_uid, " + "dvs.de_name, dvs.ou_name, dvs.coc_name, dvs.aoc_name, dvs.value, dvs.follow_up, " + "dvs.pe_start_date, dvs.pt_name, " + "stats.middle_value as middle_value, " + "stats.std_dev as std_dev, " + "abs(dvs.value::double precision - stats.middle_value) as middle_value_abs_dev, " + "abs(dvs.value::double precision - stats.middle_value) / stats.std_dev as z_score, " + "stats.middle_value - (stats.std_dev * :threshold) as lower_bound, " + "stats.middle_value + (stats.std_dev * :threshold) as upper_bound " + // Data value query
"from (" + "select dv.dataelementid, dv.sourceid, dv.periodid, " + "dv.categoryoptioncomboid, dv.attributeoptioncomboid, " + "de.uid as de_uid, ou.uid as ou_uid, coc.uid as coc_uid, aoc.uid as aoc_uid, " + "de.name as de_name, ou.name as ou_name, coc.name as coc_name, aoc.name as aoc_name, " + "pe.startdate as pe_start_date, pt.name as pt_name, " + "dv.value as value, dv.followup as follow_up " + "from datavalue dv " + "inner join dataelement de on dv.dataelementid = de.dataelementid " + "inner join categoryoptioncombo coc on dv.categoryoptioncomboid = coc.categoryoptioncomboid " + "inner join categoryoptioncombo aoc on dv.attributeoptioncomboid = aoc.categoryoptioncomboid " + "inner join period pe on dv.periodid = pe.periodid " + "inner join periodtype pt on pe.periodtypeid = pt.periodtypeid " + "inner join organisationunit ou on dv.sourceid = ou.organisationunitid " + "where dv.dataelementid in (:data_element_ids) " + "and pe.startdate >= :start_date " + "and pe.enddate <= :end_date " + "and " + ouPathClause + " " + "and dv.deleted is false" + ") as dvs " + // Mean or Median and std dev mapping query
"inner join (" + "select dv.dataelementid as dataelementid, dv.sourceid as sourceid, " + "dv.categoryoptioncomboid as categoryoptioncomboid, " + "dv.attributeoptioncomboid as attributeoptioncomboid, " + middle_stats_calc + " as middle_value, " + "stddev_pop(dv.value::double precision) as std_dev " + "from datavalue dv " + "inner join period pe on dv.periodid = pe.periodid " + "inner join organisationunit ou on dv.sourceid = ou.organisationunitid " + "where dv.dataelementid in (:data_element_ids) " + dataStartDateClause + dataEndDateClause + "and " + ouPathClause + " " + "and dv.deleted is false " + "group by dv.dataelementid, dv.sourceid, dv.categoryoptioncomboid, dv.attributeoptioncomboid" + ") as stats " + // Query join
"on dvs.dataelementid = stats.dataelementid " + "and dvs.sourceid = stats.sourceid " + "and dvs.categoryoptioncomboid = stats.categoryoptioncomboid " + "and dvs.attributeoptioncomboid = stats.attributeoptioncomboid " + "where stats.std_dev != 0.0 " + // Filter on z-score threshold
"and (abs(dvs.value::double precision - stats.middle_value) / stats.std_dev) >= :threshold " + // Order and limit
"order by " + order + " desc " + "limit :max_results;";
// @formatter:on
final SqlParameterSource params = new MapSqlParameterSource().addValue("threshold", request.getThreshold()).addValue("data_element_ids", request.getDataElementIds()).addValue("start_date", request.getStartDate()).addValue("end_date", request.getEndDate()).addValue("data_start_date", request.getDataStartDate()).addValue("data_end_date", request.getDataEndDate()).addValue("max_results", request.getMaxResults());
final Calendar calendar = PeriodType.getCalendar();
try {
return jdbcTemplate.query(sql, params, getRowMapper(calendar, modifiedZ));
} catch (DataIntegrityViolationException ex) {
// Casting non-numeric data to double, catching exception is faster
// than filtering
log.error(ErrorCode.E2208.getMessage(), ex);
throw new IllegalQueryException(ErrorCode.E2208);
}
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class ListGrid method repositionHeaders.
/**
* Re-order the GridHeaders of the given Grid based on the List headers. The
* final Grid will have the all its headers defined in the same order as the
* given List of headers.
*
* @param headers
* @return a Set of indexes that holds the holds the new order
*/
@Override
public Set<Integer> repositionHeaders(final Set<String> headers) {
verifyGridState();
final List<String> gridHeaders = getHeaders().stream().map(GridHeader::getName).collect(toList());
final List<GridHeader> orderedHeaders = new ArrayList<>();
final Set<Integer> newColumnIndexes = new LinkedHashSet<>();
for (final String header : headers) {
if (gridHeaders.contains(header)) {
final int gridHeaderIndex = getIndexOfHeader(header);
orderedHeaders.add(getHeaders().get(gridHeaderIndex));
newColumnIndexes.add(gridHeaderIndex);
} else {
throw new IllegalQueryException(new ErrorMessage(E7230, header));
}
}
replaceHeaders(orderedHeaders);
return newColumnIndexes;
}
use of org.hisp.dhis.common.IllegalQueryException in project dhis2-core by dhis2.
the class GridTest method testRepositionHeadersUsingInvalidHeader.
@Test
void testRepositionHeadersUsingInvalidHeader() {
// Given
final GridHeader headerA = new GridHeader("headerA", "Header A");
final GridHeader headerB = new GridHeader("headerB", "Header B");
final GridHeader headerC = new GridHeader("headerC", "Header C");
final Grid grid = new ListGrid();
grid.addHeader(headerA);
grid.addHeader(headerB);
grid.addHeader(headerC);
grid.addRow().addValue(1).addValue("a").addValue("a-1");
grid.addRow().addValue(2).addValue("b").addValue("b-1");
grid.addRow().addValue(3).addValue("c").addValue("c-1");
final Set<String> headers = new LinkedHashSet<>(List.of("invalidHeader", "headerB", "headerA"));
// When
final IllegalQueryException expectedException = assertThrows(IllegalQueryException.class, () -> grid.repositionHeaders(headers));
// Then
assertThat(expectedException.getMessage(), is(equalTo("Header param `invalidHeader` does not exist")));
assertThat(expectedException.getErrorCode(), is(equalTo(E7230)));
}
Aggregations