use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.
the class JdbcEventAnalyticsManagerTest method testHandlingWhenExceptionIsNull.
@Test
void testHandlingWhenExceptionIsNull() {
// Given
final DataIntegrityViolationException aNullException = null;
// When
assertThrows(QueryRuntimeException.class, () -> handle(aNullException), E7133.getMessage());
}
use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.
the class JdbcEventAnalyticsManagerTest method testHandlingWhenExceptionCauseIsNotPSQLException.
@Test
void testHandlingWhenExceptionCauseIsNotPSQLException() {
// Given
final ArrayIndexOutOfBoundsException aRandomCause = new ArrayIndexOutOfBoundsException();
final DataIntegrityViolationException aNonPSQLExceptionCause = new DataIntegrityViolationException("not caused by PSQLException", aRandomCause);
// When
assertThrows(QueryRuntimeException.class, () -> handle(aNonPSQLExceptionCause), E7133.getMessage());
}
use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.
the class MinMaxOutlierDetectionManager method getOutlierValues.
/**
* Returns a list of outlier data values based on min-max values 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());
// @formatter:off
final String sql = "select 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::double precision as value, dv.followup as follow_up, " + "least(abs(dv.value::double precision - mm.minimumvalue), " + "abs(dv.value::double precision - mm.maximumvalue)) as bound_abs_dev, " + "mm.minimumvalue as lower_bound, " + "mm.maximumvalue as upper_bound " + "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 " + // Min-max value join
"inner join minmaxdataelement mm on (dv.dataelementid = mm.dataelementid " + "and dv.sourceid = mm.sourceid and dv.categoryoptioncomboid = mm.categoryoptioncomboid) " + "where dv.dataelementid in (:data_element_ids) " + "and pe.startdate >= :start_date " + "and pe.enddate <= :end_date " + "and " + ouPathClause + " " + "and dv.deleted is false " + // Filter for values outside the min-max range
"and (dv.value::double precision < mm.minimumvalue or dv.value::double precision > mm.maximumvalue) " + // Order and limit
"order by bound_abs_dev desc " + "limit :max_results;";
// @formatter:on
final SqlParameterSource params = new MapSqlParameterSource().addValue("data_element_ids", request.getDataElementIds()).addValue("start_date", request.getStartDate()).addValue("end_date", request.getEndDate()).addValue("max_results", request.getMaxResults());
final Calendar calendar = PeriodType.getCalendar();
try {
return jdbcTemplate.query(sql, params, getRowMapper(calendar));
} 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.springframework.dao.DataIntegrityViolationException in project spring-framework by spring-projects.
the class SQLErrorCodeSQLExceptionTranslatorTests method customExceptionTranslation.
@Test
public void customExceptionTranslation() {
final String TASK = "TASK";
final String SQL = "SQL SELECT *";
final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation();
customErrorCodes.setBadSqlGrammarCodes("1", "2");
customErrorCodes.setDataIntegrityViolationCodes("3", "4");
customTranslation.setErrorCodes("1");
customTranslation.setExceptionClass(CustomErrorCodeException.class);
customErrorCodes.setCustomTranslations(customTranslation);
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(customErrorCodes);
// Should custom translate this
SQLException badSqlEx = new SQLException("", "", 1);
assertThat(sext.translate(TASK, SQL, badSqlEx).getClass()).isEqualTo(CustomErrorCodeException.class);
assertThat(sext.translate(TASK, SQL, badSqlEx).getCause()).isEqualTo(badSqlEx);
// Shouldn't custom translate this
SQLException invResEx = new SQLException("", "", 3);
DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx);
assertThat(diex.getCause()).isEqualTo(invResEx);
// Shouldn't custom translate this - invalid class
assertThatIllegalArgumentException().isThrownBy(() -> customTranslation.setExceptionClass(String.class));
}
use of org.springframework.dao.DataIntegrityViolationException in project spring-framework by spring-projects.
the class SQLErrorCodeSQLExceptionTranslatorTests method customTranslateMethodTranslation.
@SuppressWarnings("serial")
@Test
public void customTranslateMethodTranslation() {
final String TASK = "TASK";
final String SQL = "SQL SELECT *";
final DataAccessException customDex = new DataAccessException("") {
};
final SQLException badSqlEx = new SQLException("", "", 1);
SQLException intVioEx = new SQLException("", "", 6);
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() {
@Override
@Nullable
protected DataAccessException customTranslate(String task, @Nullable String sql, SQLException sqlex) {
assertThat(task).isEqualTo(TASK);
assertThat(sql).isEqualTo(SQL);
return (sqlex == badSqlEx) ? customDex : null;
}
};
sext.setSqlErrorCodes(ERROR_CODES);
// Shouldn't custom translate this
assertThat(sext.translate(TASK, SQL, badSqlEx)).isEqualTo(customDex);
DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx);
assertThat(diex.getCause()).isEqualTo(intVioEx);
}
Aggregations