Search in sources :

Example 11 with ConstraintViolation

use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlRepositoryTest method testMrefValueTooLong.

@Test(expectedExceptions = MolgenisValidationException.class, expectedExceptionsMessageRegExp = "One of the mref values in entity type \\[test_entity\\] attribute \\[mref_attr\\] is too long.")
public void testMrefValueTooLong() {
    Attribute attr = mock(Attribute.class);
    when(attr.getName()).thenReturn("mref_attr");
    Attribute idAttr = mock(Attribute.class);
    when(idAttr.getName()).thenReturn("id");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getId()).thenReturn("test_entity");
    MolgenisValidationException mve = new MolgenisValidationException(new ConstraintViolation(VALUE_TOO_LONG_MSG));
    when(jdbcTemplate.batchUpdate(any(), any(BatchPreparedStatementSetter.class))).thenThrow(mve);
    HashMap<String, Object> value = newHashMap();
    value.put("mref_attr", "TOOLONG");
    postgreSqlRepo.addMrefs(newArrayList(value), attr);
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) Test(org.testng.annotations.Test)

Example 12 with ConstraintViolation

use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method translateForeignKeyViolation.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateForeignKeyViolation(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String tableName = serverErrorMessage.getTable();
    String detailMessage = serverErrorMessage.getDetail();
    Matcher m = Pattern.compile("\\((.*?)\\)").matcher(detailMessage);
    if (!m.find()) {
        LOG.error("Error translating postgres exception: ", pSqlException);
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    String colName = m.group(1);
    if (!m.find()) {
        LOG.error("Error translating postgres exception: ", pSqlException);
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    String value = m.group(1);
    String constraintViolationMessageTemplate;
    String attrName;
    if (detailMessage.contains("still referenced from")) {
        // ERROR: update or delete on table "x" violates foreign key constraint "y" on table "z"
        // Detail: Key (k)=(v) is still referenced from table "x".
        constraintViolationMessageTemplate = "Value '%s' for attribute '%s' is referenced by entity '%s'.";
        String refTableName = getRefTableFromForeignKeyPsqlException(pSqlException);
        attrName = getAttributeName(refTableName, colName);
    } else {
        // ERROR: insert or update on table "x" violates foreign key constraint "y"
        // Detail: Key (k)=(v) is not present in table "z".
        constraintViolationMessageTemplate = "Unknown xref value '%s' for attribute '%s' of entity '%s'.";
        attrName = getAttributeName(tableName, colName);
    }
    ConstraintViolation constraintViolation = new ConstraintViolation(format(constraintViolationMessageTemplate, value, attrName, getEntityTypeName(tableName)), null);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : Matcher(java.util.regex.Matcher) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 13 with ConstraintViolation

use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method translateCheckConstraintViolation.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateCheckConstraintViolation(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String tableName = serverErrorMessage.getTable();
    String constraintName = serverErrorMessage.getConstraint();
    // constraint name: <tableName>_<columnName>_chk
    String columnName = constraintName.substring(tableName.length() + 1, constraintName.length() - 4);
    ConstraintViolation constraintViolation = new ConstraintViolation(format("Unknown enum value for attribute '%s' of entity '%s'.", getAttributeName(tableName, columnName), getEntityTypeName(tableName)), null);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 14 with ConstraintViolation

use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method translateInvalidIntegerException.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
static MolgenisValidationException translateInvalidIntegerException(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String message = serverErrorMessage.getMessage();
    Matcher matcher = Pattern.compile("invalid input syntax for \\b(?:type )?\\b(.+?): \"(.*?)\"").matcher(message);
    boolean matches = matcher.matches();
    if (!matches) {
        throw new RuntimeException("Error translating exception", pSqlException);
    }
    String postgreSqlType = matcher.group(1);
    // convert PostgreSQL data type to attribute type:
    String type;
    switch(postgreSqlType) {
        case "boolean":
            type = BOOL.toString();
            break;
        case "date":
            type = DATE.toString();
            break;
        case "timestamp with time zone":
            type = DATE_TIME.toString();
            break;
        case "double precision":
            type = DECIMAL.toString();
            break;
        case "integer":
            type = INT.toString() + " or " + LONG.toString();
            break;
        default:
            type = postgreSqlType;
            break;
    }
    String value = matcher.group(2);
    ConstraintViolation constraintViolation = new ConstraintViolation(format("Value [%s] of this entity attribute is not of type [%s].", value, type), null);
    return new MolgenisValidationException(singleton(constraintViolation));
}
Also used : Matcher(java.util.regex.Matcher) ServerErrorMessage(org.postgresql.util.ServerErrorMessage) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 15 with ConstraintViolation

use of org.molgenis.data.validation.ConstraintViolation 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)

Aggregations

ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)20 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)19 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)7 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 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1