Search in sources :

Example 1 with EntityTypeDescription

use of org.molgenis.data.postgresql.identifier.EntityTypeDescription in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method translateNotNullViolation.

/**
 * Package private for testability
 *
 * @param pSqlException PostgreSQL exception
 * @return translated validation exception
 */
MolgenisValidationException translateNotNullViolation(PSQLException pSqlException) {
    ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
    String tableName = serverErrorMessage.getTable();
    String message = serverErrorMessage.getMessage();
    Matcher matcher = Pattern.compile("null value in column \"?(.*?)\"? violates not-null constraint").matcher(message);
    boolean matches = matcher.matches();
    if (matches) {
        // exception message when adding data that does not match constraint
        String columnName = matcher.group(1);
        EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName);
        entityTypeDescription.getAttributeDescriptionMap().get(columnName);
        ConstraintViolation constraintViolation = new ConstraintViolation(format("The attribute '%s' of entity '%s' can not be null.", getAttributeName(tableName, columnName), getEntityTypeName(tableName)), null);
        return new MolgenisValidationException(singleton(constraintViolation));
    } else {
        // exception message when applying constraint on existing data
        matcher = Pattern.compile("column \"(.*?)\" contains null values").matcher(message);
        matches = matcher.matches();
        if (!matches) {
            throw new RuntimeException("Error translating exception", pSqlException);
        }
        String columnName = matcher.group(1);
        ConstraintViolation constraintViolation = new ConstraintViolation(format("The attribute '%s' of entity '%s' contains null values.", getAttributeName(tableName, columnName), 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) EntityTypeDescription(org.molgenis.data.postgresql.identifier.EntityTypeDescription) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 2 with EntityTypeDescription

use of org.molgenis.data.postgresql.identifier.EntityTypeDescription in project molgenis by molgenis.

the class PostgreSqlExceptionTranslator method getAttributeName.

/**
 * Returns the attribute name for this table name
 *
 * @param tableName table name
 * @param colName   column name
 * @return attribute name
 */
private String getAttributeName(String tableName, String colName) {
    EntityTypeDescription entityTypeDescription = entityTypeRegistry.getEntityTypeDescription(tableName);
    if (entityTypeDescription == null) {
        throw new RuntimeException(format("Unknown entity for table name [%s]", tableName));
    }
    AttributeDescription attrDescription = entityTypeDescription.getAttributeDescriptionMap().get(colName);
    if (attrDescription == null) {
        throw new RuntimeException(format("Unknown attribute for column name [%s]", colName));
    }
    return attrDescription.getName();
}
Also used : EntityTypeDescription(org.molgenis.data.postgresql.identifier.EntityTypeDescription) AttributeDescription(org.molgenis.data.postgresql.identifier.AttributeDescription)

Example 3 with EntityTypeDescription

use of org.molgenis.data.postgresql.identifier.EntityTypeDescription in project molgenis by molgenis.

the class PostgreSqlExceptionTranslatorTest method setUpBeforeMethod.

@BeforeMethod
public void setUpBeforeMethod() {
    DataSource dataSource = mock(DataSource.class);
    EntityTypeRegistry entityTypeRegistry = mock(EntityTypeRegistry.class);
    EntityTypeDescription entityTypeDescription = EntityTypeDescription.create("myEntity", ImmutableMap.<String, AttributeDescription>builder().put("myColumn", AttributeDescription.create("myAttr")).build());
    EntityTypeDescription refEntityTypeDescription = EntityTypeDescription.create("myRefEntity", ImmutableMap.<String, AttributeDescription>builder().put("myColumn", AttributeDescription.create("myAttr")).build());
    EntityTypeDescription otherRefEntityTypeDescription = EntityTypeDescription.create("myOtherRefEntity", ImmutableMap.<String, AttributeDescription>builder().put("myColumn", AttributeDescription.create("myAttr")).build());
    when(entityTypeRegistry.getEntityTypeDescription("myTable")).thenReturn(entityTypeDescription);
    when(entityTypeRegistry.getEntityTypeDescription("myDependentTable")).thenReturn(refEntityTypeDescription);
    when(entityTypeRegistry.getEntityTypeDescription("myOtherDependentTable")).thenReturn(otherRefEntityTypeDescription);
    postgreSqlExceptionTranslator = new PostgreSqlExceptionTranslator(dataSource, entityTypeRegistry);
}
Also used : EntityTypeRegistry(org.molgenis.data.postgresql.identifier.EntityTypeRegistry) EntityTypeDescription(org.molgenis.data.postgresql.identifier.EntityTypeDescription) DataSource(javax.sql.DataSource) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

EntityTypeDescription (org.molgenis.data.postgresql.identifier.EntityTypeDescription)3 Matcher (java.util.regex.Matcher)1 DataSource (javax.sql.DataSource)1 AttributeDescription (org.molgenis.data.postgresql.identifier.AttributeDescription)1 EntityTypeRegistry (org.molgenis.data.postgresql.identifier.EntityTypeRegistry)1 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)1 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)1 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)1 BeforeMethod (org.testng.annotations.BeforeMethod)1