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));
}
}
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();
}
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);
}
Aggregations