Search in sources :

Example 41 with MolgenisValidationException

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));
}
Also used : Matcher(java.util.regex.Matcher) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 42 with MolgenisValidationException

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)));
    }
}
Also used : RepositoryCollection(org.molgenis.data.RepositoryCollection) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Example 43 with MolgenisValidationException

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)));
                }
            }
        }
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) AttributeType(org.molgenis.data.meta.AttributeType) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException)

Aggregations

MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)43 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)31 Test (org.testng.annotations.Test)25 PSQLException (org.postgresql.util.PSQLException)24 ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)19 Matcher (java.util.regex.Matcher)6 Attribute (org.molgenis.data.meta.model.Attribute)4 String.format (java.lang.String.format)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 MolgenisDataException (org.molgenis.data.MolgenisDataException)2 RepositoryCollection (org.molgenis.data.RepositoryCollection)2 AttributeType (org.molgenis.data.meta.AttributeType)2 EntityType (org.molgenis.data.meta.model.EntityType)2 EntityTypeDescription (org.molgenis.data.postgresql.identifier.EntityTypeDescription)2 Component (org.springframework.stereotype.Component)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 Multimap (com.google.common.collect.Multimap)1 File (java.io.File)1