use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.
the class PostgreSqlRepository method createMrefMap.
private static Map<String, List<Map<String, Object>>> createMrefMap(Attribute idAttr, List<Attribute> junctionTableAttrs, List<? extends Entity> entitiesBatch) {
Map<String, List<Map<String, Object>>> mrefs = Maps.newHashMapWithExpectedSize(junctionTableAttrs.size());
AtomicInteger seqNr = new AtomicInteger();
for (Entity entity : entitiesBatch) {
for (Attribute attr : junctionTableAttrs) {
Iterable<Entity> refEntities = entity.getEntities(attr.getName());
// so validate manually.
if (!attr.isNillable() && Iterables.isEmpty(refEntities)) {
throw new MolgenisValidationException(new ConstraintViolation(format("The attribute [%s] of entity [%s] with id [%s] can not be null.", attr.getName(), attr.getEntity().getId(), entity.getIdValue().toString())));
}
mrefs.putIfAbsent(attr.getName(), new ArrayList<>());
seqNr.set(0);
for (Entity val : refEntities) {
Map<String, Object> mref = createJunctionTableRowData(seqNr.getAndIncrement(), idAttr, val, attr, entity);
mrefs.get(attr.getName()).add(mref);
}
}
}
return mrefs;
}
use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.
the class PostgreSqlRepository method addMrefs.
void addMrefs(final List<Map<String, Object>> mrefs, final Attribute attr) {
// so validate it ourselves
if (!attr.isNillable() && mrefs.isEmpty()) {
throw new MolgenisValidationException(new ConstraintViolation(format("Entity [%s] attribute [%s] value cannot be null", entityType.getId(), attr.getName())));
}
final Attribute idAttr = entityType.getIdAttribute();
String insertMrefSql = getSqlInsertJunction(entityType, attr);
if (LOG.isDebugEnabled()) {
LOG.debug("Adding junction table entries for entity [{}] attribute [{}]", getName(), attr.getName());
if (LOG.isTraceEnabled()) {
LOG.trace("SQL: {}", insertMrefSql);
}
}
try {
jdbcTemplate.batchUpdate(insertMrefSql, new BatchJunctionTableAddPreparedStatementSetter(mrefs, attr, idAttr));
} catch (MolgenisValidationException mve) {
if (mve.getMessage().equals(VALUE_TOO_LONG_MSG)) {
mve = new MolgenisValidationException(new ConstraintViolation(format("One of the mref values in entity type [%s] attribute [%s] is too long.", getEntityType().getId(), attr.getName())));
}
throw mve;
}
}
use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes.
@Test
public void translateUniqueKeyViolationKeyIsDuplicatedDoubleQuotes() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getSQLState()).thenReturn("23505");
when(serverErrorMessage.getTable()).thenReturn("myTable");
when(serverErrorMessage.getDetail()).thenReturn("Key (\"myColumn\")=(myValue) is duplicated.");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "The attribute 'myAttr' of entity 'myEntity' contains duplicate value 'myValue'.");
}
use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateInvalidIntegerExceptionBoolean.
@Test
public void translateInvalidIntegerExceptionBoolean() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getMessage()).thenReturn("invalid input syntax for type boolean: \"str1\"");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = PostgreSqlExceptionTranslator.translateInvalidIntegerException(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "Value [str1] of this entity attribute is not of type [BOOL].");
}
use of org.molgenis.data.validation.MolgenisValidationException in project molgenis by molgenis.
the class PostgreSqlExceptionTranslatorTest method translateUniqueKeyViolationCompositeKey.
@Test
public void translateUniqueKeyViolationCompositeKey() {
ServerErrorMessage serverErrorMessage = mock(ServerErrorMessage.class);
when(serverErrorMessage.getSQLState()).thenReturn("23505");
when(serverErrorMessage.getTable()).thenReturn("myTable");
when(serverErrorMessage.getDetail()).thenReturn("Key (myIdColumn, myColumn)=(myIdValue, myValue) already exists.");
// noinspection ThrowableResultOfMethodCallIgnored
MolgenisValidationException e = postgreSqlExceptionTranslator.translateUniqueKeyViolation(new PSQLException(serverErrorMessage));
assertEquals(e.getMessage(), "Duplicate list value 'myValue' for attribute 'myAttr' from entity 'myEntity' with id 'myIdValue'.");
}
Aggregations