Search in sources :

Example 1 with UnknownAttributeException

use of org.molgenis.data.UnknownAttributeException in project molgenis by molgenis.

the class DynamicEntity method validateValueType.

/**
 * Validate is value is of the type defined by the attribute data type.
 *
 * @param attrName attribute name
 * @param value    value (must be of the type defined by the attribute data type.)
 */
protected void validateValueType(String attrName, Object value) {
    if (value == null) {
        return;
    }
    Attribute attr = entityType.getAttribute(attrName);
    if (attr == null) {
        throw new UnknownAttributeException(entityType, attrName);
    }
    AttributeType dataType = attr.getDataType();
    switch(dataType) {
        case BOOL:
            if (!(value instanceof Boolean)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Boolean.class.getSimpleName(), attrName));
            }
            break;
        case CATEGORICAL:
        // expected type is FileMeta. validation is not possible because molgenis-data does not depend on molgenis-file
        case FILE:
        case XREF:
            if (!(value instanceof Entity)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Entity.class.getSimpleName(), attrName));
            }
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            if (!(value instanceof Iterable)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Iterable.class.getSimpleName(), attrName));
            }
            break;
        case COMPOUND:
            throw new IllegalArgumentException(format("Unexpected data type [%s] for attribute: [%s]", dataType.toString(), attrName));
        case DATE:
            if (!(value instanceof LocalDate)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), LocalDate.class.getSimpleName(), attrName));
            }
            break;
        case DATE_TIME:
            if (!(value instanceof Instant)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Instant.class.getSimpleName(), attrName));
            }
            break;
        case DECIMAL:
            if (!(value instanceof Double)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Double.class.getSimpleName(), attrName));
            }
            if (((Double) value).isNaN()) {
                throw new MolgenisDataException(format("Value [%s] for type [%s] is not allowed for attribute: [%s]", value.toString(), Double.class.getSimpleName(), attrName));
            }
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            if (!(value instanceof String)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), String.class.getSimpleName(), attrName));
            }
            break;
        case INT:
            if (!(value instanceof Integer)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Integer.class.getSimpleName(), attrName));
            }
            break;
        case LONG:
            if (!(value instanceof Long)) {
                throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Long.class.getSimpleName(), attrName));
            }
            break;
        default:
            throw new UnexpectedEnumException(dataType);
    }
}
Also used : Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Instant(java.time.Instant) LocalDate(java.time.LocalDate) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeType(org.molgenis.data.meta.AttributeType) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Example 2 with UnknownAttributeException

use of org.molgenis.data.UnknownAttributeException in project molgenis by molgenis.

the class MolgenisRSQLVisitor method getAttribute.

private Attribute getAttribute(ComparisonNode node) {
    String attrName = node.getSelector();
    String[] attrTokens = attrName.split("\\.");
    Attribute attr = entityType.getAttribute(attrTokens[0]);
    if (attr == null) {
        throw new UnknownAttributeException(entityType, attrName);
    }
    EntityType entityTypeAtDepth;
    for (int i = 1; i < attrTokens.length; ++i) {
        entityTypeAtDepth = attr.getRefEntity();
        attr = entityTypeAtDepth.getAttribute(attrTokens[i]);
        if (attr == null) {
            throw new UnknownAttributeException(entityTypeAtDepth, attrName);
        }
    }
    return attr;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Attribute(org.molgenis.data.meta.model.Attribute) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Example 3 with UnknownAttributeException

use of org.molgenis.data.UnknownAttributeException in project molgenis by molgenis.

the class AggregateQueryRsqlVisitor method getAttribute.

private Attribute getAttribute(ComparisonNode node) {
    List<String> args = node.getArguments();
    if (args.size() != 1) {
        throw new MolgenisQueryException(String.format("RSQL query value must have exactly one value instead of [%s]", StringUtils.join(args, ',')));
    }
    String attrName = args.iterator().next();
    String[] attrTokens = attrName.split("\\.");
    Attribute attr = entityType.getAttribute(attrTokens[0]);
    if (attr == null) {
        throw new UnknownAttributeException(entityType, attrName);
    }
    EntityType entityTypeAtDepth;
    for (int i = 1; i < attrTokens.length; ++i) {
        entityTypeAtDepth = attr.getRefEntity();
        attr = entityTypeAtDepth.getAttribute(attrTokens[i]);
        if (attr == null) {
            throw new UnknownAttributeException(entityTypeAtDepth, attrName);
        }
    }
    return attr;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) MolgenisQueryException(org.molgenis.data.MolgenisQueryException) Attribute(org.molgenis.data.meta.model.Attribute) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Example 4 with UnknownAttributeException

use of org.molgenis.data.UnknownAttributeException in project molgenis by molgenis.

the class SortGenerator method toSortOrder.

private SortOrder toSortOrder(org.molgenis.data.Sort.Order order, EntityType entityType) {
    String attributeName = order.getAttr();
    if (attributeName == null) {
        throw new IllegalArgumentException("Sort property is null");
    }
    Attribute sortAttribute = entityType.getAttribute(attributeName);
    if (sortAttribute == null) {
        throw new UnknownAttributeException(entityType, attributeName);
    }
    String sortField = getSortField(sortAttribute);
    SortDirection sortDirection = getSortDirection(order.getDirection());
    return SortOrder.create(sortField, sortDirection);
}
Also used : SortDirection(org.molgenis.data.elasticsearch.generator.model.SortDirection) Attribute(org.molgenis.data.meta.model.Attribute) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Aggregations

UnknownAttributeException (org.molgenis.data.UnknownAttributeException)4 Attribute (org.molgenis.data.meta.model.Attribute)4 EntityType (org.molgenis.data.meta.model.EntityType)2 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 Entity (org.molgenis.data.Entity)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1 MolgenisQueryException (org.molgenis.data.MolgenisQueryException)1 SortDirection (org.molgenis.data.elasticsearch.generator.model.SortDirection)1 AttributeType (org.molgenis.data.meta.AttributeType)1 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)1