Search in sources :

Example 31 with MolgenisValidationException

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;
}
Also used : Entity(org.molgenis.data.Entity) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) Collections.emptyList(java.util.Collections.emptyList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 32 with MolgenisValidationException

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;
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 33 with MolgenisValidationException

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'.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 34 with MolgenisValidationException

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].");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 35 with MolgenisValidationException

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'.");
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) PSQLException(org.postgresql.util.PSQLException) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Aggregations

MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)43 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)31 Test (org.testng.annotations.Test)25 PSQLException (org.postgresql.util.PSQLException)24 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)19 Matcher (java.util.regex.Matcher)6 Attribute (org.molgenis.data.meta.model.Attribute)4 String.format (java.lang.String.format)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2 RepositoryCollection (org.molgenis.data.RepositoryCollection)2 AttributeType (org.molgenis.data.meta.AttributeType)2 EntityType (org.molgenis.data.meta.model.EntityType)2 EntityTypeDescription (org.molgenis.data.postgresql.identifier.EntityTypeDescription)2 Component (org.springframework.stereotype.Component)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Multimap (com.google.common.collect.Multimap)1 File (java.io.File)1