Search in sources :

Example 1 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException 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 UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class EntityHydration method getValueBasedOnType.

private static Object getValueBasedOnType(Entity entity, String name, AttributeType type) {
    Object value;
    switch(type) {
        case CATEGORICAL:
        case FILE:
        case XREF:
            Entity xrefEntity = entity.getEntity(name);
            value = xrefEntity != null ? xrefEntity.getIdValue() : null;
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            List<Object> mrefIdentifiers = newArrayList();
            entity.getEntities(name).forEach(mrefEntity -> {
                if (mrefEntity != null)
                    mrefIdentifiers.add(mrefEntity.getIdValue());
            });
            value = mrefIdentifiers;
            break;
        case DATE:
            value = entity.getLocalDate(name);
            break;
        case DATE_TIME:
            value = entity.getInstant(name);
            break;
        case BOOL:
        case DECIMAL:
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case INT:
        case LONG:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = entity.get(name);
            break;
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", type.toString()));
        default:
            throw new UnexpectedEnumException(type);
    }
    LOG.trace("Dehydrating attribute '{}' of type [{}] resulted in value: {}", name, type.toString(), value);
    return value;
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException)

Example 3 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class ClientFacade method toDocWriteRequest.

private DocWriteRequest toDocWriteRequest(DocumentAction documentAction) {
    String indexName = documentAction.getIndex().getName();
    String documentId = documentAction.getDocument().getId();
    DocWriteRequest docWriteRequest;
    switch(documentAction.getOperation()) {
        case INDEX:
            XContentBuilder source = documentAction.getDocument().getContent();
            if (source == null) {
                throw new IndexException(format("Document action is missing document source '%s'", documentAction));
            }
            docWriteRequest = Requests.indexRequest(indexName).type(indexName).id(documentId).source(source).opType(INDEX);
            break;
        case DELETE:
            docWriteRequest = Requests.deleteRequest(indexName).type(indexName).id(documentId);
            break;
        default:
            throw new UnexpectedEnumException(documentAction.getOperation());
    }
    return docWriteRequest;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 4 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseEqualsNoValue.

private QueryBuilder createQueryClauseEqualsNoValue(QueryRule queryRule, EntityType entityType) {
    List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
    String fieldName = getQueryFieldName(attributePath);
    Attribute attr = attributePath.get(0);
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
        case DATE:
        case DATE_TIME:
        case DECIMAL:
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case INT:
        case LONG:
        case SCRIPT:
        case STRING:
        case TEXT:
            return QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(fieldName));
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case FILE:
        case MREF:
        case ONE_TO_MANY:
        case XREF:
            if (attributePath.size() > 1) {
                throw new MolgenisQueryException("Can not filter on references deeper than 1.");
            }
            Attribute refIdAttr = attr.getRefEntity().getIdAttribute();
            List<Attribute> refAttributePath = concat(attributePath.stream(), of(refIdAttr)).collect(toList());
            String indexFieldName = getQueryFieldName(refAttributePath);
            return QueryBuilders.boolQuery().mustNot(QueryBuilders.nestedQuery(fieldName, QueryBuilders.existsQuery(indexFieldName), ScoreMode.Avg));
        case COMPOUND:
            throw new MolgenisQueryException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 5 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseRangeOpen.

private QueryBuilder createQueryClauseRangeOpen(QueryRule queryRule, EntityType entityType) {
    List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
    Attribute attr = attributePath.get(attributePath.size() - 1);
    validateNumericalQueryField(attr);
    String fieldName = getQueryFieldName(attributePath);
    Object queryValue = getQueryValue(attr, queryRule.getValue());
    if (queryValue == null) {
        throw new MolgenisQueryException("Query value cannot be null");
    }
    RangeQueryBuilder filterBuilder = QueryBuilders.rangeQuery(fieldName);
    QueryRule.Operator operator = queryRule.getOperator();
    switch(operator) {
        case GREATER:
            filterBuilder = filterBuilder.gt(queryValue);
            break;
        case GREATER_EQUAL:
            filterBuilder = filterBuilder.gte(queryValue);
            break;
        case LESS:
            filterBuilder = filterBuilder.lt(queryValue);
            break;
        case LESS_EQUAL:
            filterBuilder = filterBuilder.lte(queryValue);
            break;
        case AND:
        case DIS_MAX:
        case EQUALS:
        case FUZZY_MATCH:
        case FUZZY_MATCH_NGRAM:
        case IN:
        case LIKE:
        case NESTED:
        case NOT:
        case OR:
        case RANGE:
        case SEARCH:
        case SHOULD:
            throw new MolgenisQueryException(format("Illegal query rule operator [%s]", operator.toString()));
        default:
            throw new UnexpectedEnumException(operator);
    }
    return QueryBuilders.constantScoreQuery(nestedQueryBuilder(attributePath, filterBuilder));
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)39 AttributeType (org.molgenis.data.meta.AttributeType)26 Attribute (org.molgenis.data.meta.model.Attribute)20 Entity (org.molgenis.data.Entity)13 Instant (java.time.Instant)10 LocalDate (java.time.LocalDate)10 String.format (java.lang.String.format)4 Collectors.toList (java.util.stream.Collectors.toList)4 MolgenisDataException (org.molgenis.data.MolgenisDataException)4 Iterator (java.util.Iterator)3 List (java.util.List)3 StreamSupport.stream (java.util.stream.StreamSupport.stream)3 EntityType (org.molgenis.data.meta.model.EntityType)3 UTC (java.time.ZoneOffset.UTC)2 ChronoUnit (java.time.temporal.ChronoUnit)2 Collection (java.util.Collection)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors.joining (java.util.stream.Collectors.joining)2 Stream (java.util.stream.Stream)2