use of org.molgenis.data.validation.ConstraintViolation 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.validation.ConstraintViolation in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateUniqueKeyViolation.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
MolgenisValidationException translateUniqueKeyViolation(PSQLException pSqlException) {
ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
String tableName = serverErrorMessage.getTable();
String detailMessage = serverErrorMessage.getDetail();
Matcher matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) already exists.").matcher(detailMessage);
boolean matches = matcher.matches();
if (matches) {
ConstraintViolation constraintViolation;
// exception message when adding data that does not match constraint
String[] columnNames = matcher.group(1).split(", ");
if (columnNames.length == 1) {
String columnName = columnNames[0];
String value = matcher.group(2);
constraintViolation = new ConstraintViolation(format("Duplicate value '%s' for unique attribute '%s' from entity '%s'.", value, getAttributeName(tableName, columnName), getEntityTypeName(tableName)), null);
} else {
String columnName = columnNames[columnNames.length - 1];
String[] values = matcher.group(2).split(", ");
String idValue = values[0];
String value = values[1];
constraintViolation = new ConstraintViolation(format("Duplicate list value '%s' for attribute '%s' from entity '%s' with id '%s'.", value, getAttributeName(tableName, columnName), getEntityTypeName(tableName), idValue), null);
}
return new MolgenisValidationException(singleton(constraintViolation));
} else {
// exception message when applying constraint on existing data
matcher = Pattern.compile("Key \\(\"?(.*?)\"?\\)=\\((.*?)\\) is duplicated.").matcher(detailMessage);
matches = matcher.matches();
if (matches) {
String columnName = matcher.group(1);
String value = matcher.group(2);
ConstraintViolation constraintViolation = new ConstraintViolation(format("The attribute '%s' of entity '%s' contains duplicate value '%s'.", getAttributeName(tableName, columnName), getEntityTypeName(tableName), value), null);
return new MolgenisValidationException(singleton(constraintViolation));
} else {
LOG.error("Error translating postgres exception: ", pSqlException);
throw new RuntimeException("Error translating exception", pSqlException);
}
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateUndefinedColumnException.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
static MolgenisValidationException translateUndefinedColumnException(PSQLException pSqlException) {
ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
// FIXME exposes internal message
String message = serverErrorMessage.getMessage();
ConstraintViolation constraintViolation = new ConstraintViolation(message);
return new MolgenisValidationException(singleton(constraintViolation));
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateDependentObjectsStillExist.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
MolgenisValidationException translateDependentObjectsStillExist(PSQLException pSqlException) {
ServerErrorMessage serverErrorMessage = pSqlException.getServerErrorMessage();
String detail = serverErrorMessage.getDetail();
Matcher matcher = Pattern.compile("constraint (.+) on table \"?([^\"]+)\"? depends on table \"?([^\"]+)\"?\n?").matcher(detail);
Map<String, Set<String>> entityTypeDependencyMap = new LinkedHashMap<>();
while (matcher.find()) {
String tableName = matcher.group(2);
String dependentTableName = matcher.group(3);
String entityTypeName = getEntityTypeName(tableName);
String dependentEntityTypeName = getEntityTypeName(dependentTableName);
Set<String> dependentTableNames = entityTypeDependencyMap.computeIfAbsent(dependentEntityTypeName, k -> new LinkedHashSet<>());
dependentTableNames.add(entityTypeName);
}
if (// no matches
entityTypeDependencyMap.isEmpty()) {
LOG.error("Error translating postgres exception: ", pSqlException);
throw new RuntimeException("Error translating exception", pSqlException);
}
Set<ConstraintViolation> constraintViolations = entityTypeDependencyMap.entrySet().stream().map(entry -> {
String message;
if (entry.getValue().size() == 1) {
message = format("Cannot delete entity '%s' because entity '%s' depends on it.", entry.getKey(), entry.getValue().iterator().next());
} else {
message = format("Cannot delete entity '%s' because entities '%s' depend on it.", entry.getKey(), entry.getValue().stream().collect(joining(", ")));
}
return new ConstraintViolation(message, null);
}).collect(toCollection(LinkedHashSet::new));
return new MolgenisValidationException(constraintViolations);
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class PostgreSqlRepository method verifyUpdate.
private void verifyUpdate(List<? extends Entity> entitiesBatch, int[] counts, Attribute idAttr) {
int nrUpdatedEntities = Arrays.stream(counts).sum();
if (nrUpdatedEntities < entitiesBatch.size()) {
Set<Object> existingEntityIds = findAll(entitiesBatch.stream().map(Entity::getIdValue), new Fetch().field(idAttr.getName())).map(Entity::getIdValue).collect(toSet());
Object nonExistingEntityId = entitiesBatch.stream().map(Entity::getIdValue).filter(entityId -> !existingEntityIds.contains(entityId)).findFirst().orElseThrow(() -> new IllegalStateException("Not all entities in batch were updated but all are present in the repository."));
throw new MolgenisValidationException(new ConstraintViolation(format("Cannot update [%s] with id [%s] because it does not exist", entityType.getId(), nonExistingEntityId.toString())));
}
}
Aggregations