use of org.molgenis.data.validation.MolgenisValidationException 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.MolgenisValidationException 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.MolgenisValidationException 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)));
}
}
}
}
}
Aggregations