Search in sources :

Example 16 with ConstraintViolation

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

Example 17 with ConstraintViolation

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

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

Example 19 with ConstraintViolation

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

Example 20 with ConstraintViolation

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

Aggregations

ConstraintViolation (org.molgenis.data.validation.ConstraintViolation)20 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)19 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)7 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 BatchUpdateException (java.sql.BatchUpdateException)1 SQLException (java.sql.SQLException)1