use of org.molgenis.data.validation.ConstraintViolation 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;
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class PostgreSqlExceptionTranslator method translateReadonlyViolation.
/**
* Package private for testability
*
* @param pSqlException PostgreSQL exception
* @return translated validation exception
*/
MolgenisValidationException translateReadonlyViolation(PSQLException pSqlException) {
Matcher matcher = Pattern.compile("Updating read-only column \"?(.*?)\"? of table \"?(.*?)\"? with id \\[(.*?)] is not allowed").matcher(pSqlException.getServerErrorMessage().getMessage());
boolean matches = matcher.matches();
if (!matches) {
LOG.error("Error translating postgres exception: ", pSqlException);
throw new RuntimeException("Error translating exception", pSqlException);
}
String colName = matcher.group(1);
String tableName = matcher.group(2);
String id = matcher.group(3);
ConstraintViolation constraintViolation = new ConstraintViolation(format("Updating read-only attribute '%s' of entity '%s' with id '%s' is not allowed.", getAttributeName(tableName, colName), getEntityTypeName(tableName), id));
return new MolgenisValidationException(singleton(constraintViolation));
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityTypeValidator method validateBackend.
/**
* Validate that the entity meta data backend exists
*
* @param entityType entity meta data
* @throws MolgenisValidationException if the entity meta data backend does not exist
*/
private void validateBackend(EntityType entityType) {
// Validate backend exists
String backendName = entityType.getBackend();
RepositoryCollection repoCollection = dataService.getMeta().getBackend(backendName);
if (repoCollection == null) {
throw new MolgenisValidationException(new ConstraintViolation(format("Unknown backend [%s]", backendName)));
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class AttributeValidator method validateDefaultValue.
void validateDefaultValue(Attribute attr, boolean validateEntityReferences) {
String value = attr.getDefaultValue();
if (value != null) {
if (attr.isUnique()) {
throw new MolgenisDataException("Unique attribute " + attr.getName() + " cannot have default value");
}
if (attr.getExpression() != null) {
throw new MolgenisDataException("Computed attribute " + attr.getName() + " cannot have default value");
}
AttributeType fieldType = attr.getDataType();
if (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) {
throw new MolgenisDataException("Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name());
}
if (fieldType == AttributeType.EMAIL) {
checkEmail(value);
}
if (fieldType == AttributeType.HYPERLINK) {
checkHyperlink(value);
}
if (fieldType == AttributeType.ENUM) {
checkEnum(attr, value);
}
// Get typed value to check if the value is of the right type.
Object typedValue;
try {
typedValue = EntityUtils.getTypedValue(value, attr, entityManager);
} catch (NumberFormatException e) {
throw new MolgenisValidationException(new ConstraintViolation(format("Invalid default value [%s] for data type [%s]", value, attr.getDataType())));
}
if (validateEntityReferences) {
if (isSingleReferenceType(attr)) {
Entity refEntity = (Entity) typedValue;
EntityType refEntityType = attr.getRefEntity();
if (dataService.query(refEntityType.getId()).eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()).count() == 0) {
throw new MolgenisValidationException(new ConstraintViolation(format("Default value [%s] refers to an unknown entity", value)));
}
} else if (isMultipleReferenceType(attr)) {
Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue;
EntityType refEntityType = attr.getRefEntity();
if (dataService.query(refEntityType.getId()).in(refEntityType.getIdAttribute().getName(), StreamSupport.stream(refEntitiesValue.spliterator(), false).map(Entity::getIdValue).collect(toList())).count() < Iterables.size(refEntitiesValue)) {
throw new MolgenisValidationException(new ConstraintViolation(format("Default value [%s] refers to one or more unknown entities", value)));
}
}
}
}
}
use of org.molgenis.data.validation.ConstraintViolation in project molgenis by molgenis.
the class RestController method handleMolgenisValidationException.
@ExceptionHandler(MolgenisValidationException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorMessageResponse handleMolgenisValidationException(MolgenisValidationException e) {
LOG.info("", e);
List<ErrorMessage> messages = Lists.newArrayList();
for (ConstraintViolation violation : e.getViolations()) {
messages.add(new ErrorMessage(violation.getMessage()));
}
return new ErrorMessageResponse(messages);
}
Aggregations