Search in sources :

Example 6 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class DocumentContentBuilderTest method createDocumentStringProvider.

@DataProvider(name = "createDocumentString")
public static Iterator<Object[]> createDocumentStringProvider() {
    List<Object[]> dataItems = new ArrayList<>();
    for (AttributeType attributeType : EnumSet.of(EMAIL, ENUM, HTML, HYPERLINK, SCRIPT, STRING, TEXT)) {
        dataItems.add(new Object[] { attributeType, null, "{\"attr\":null}" });
        dataItems.add(new Object[] { attributeType, "abc", "{\"attr\":\"abc\"}" });
        dataItems.add(new Object[] { attributeType, "", "{\"attr\":\"\"}" });
    }
    return dataItems.iterator();
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) ArrayList(java.util.ArrayList) DataProvider(org.testng.annotations.DataProvider)

Example 7 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class EntityHydration method dehydrate.

/**
 * Creates a Map containing the values required to rebuild this entity.
 * For references to other entities only stores the ids.
 *
 * @param entity the {@link Entity} to dehydrate
 * @return Map representation of the entity
 */
public Map<String, Object> dehydrate(Entity entity) {
    LOG.trace("Dehydrating entity {}", entity);
    Map<String, Object> dehydratedEntity = newHashMap();
    EntityType entityType = entity.getEntityType();
    entityType.getAtomicAttributes().forEach(attribute -> {
        // Only dehydrate if the attribute is NOT computed
        if (!attribute.hasExpression()) {
            String name = attribute.getName();
            AttributeType type = attribute.getDataType();
            dehydratedEntity.put(name, getValueBasedOnType(entity, name, type));
        }
    });
    return dehydratedEntity;
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) AttributeType(org.molgenis.data.meta.AttributeType)

Example 8 with AttributeType

use of org.molgenis.data.meta.AttributeType 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 9 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseSearchAttribute.

private QueryBuilder createQueryClauseSearchAttribute(QueryRule queryRule, EntityType entityType) {
    List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
    Attribute attr = attributePath.get(attributePath.size() - 1);
    Object queryValue = getQueryValue(attr, queryRule.getValue());
    String fieldName = getQueryFieldName(attributePath);
    AttributeType dataType = attr.getDataType();
    switch(dataType) {
        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 nestedQueryBuilder(attributePath, QueryBuilders.matchQuery(fieldName, queryValue));
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
        case XREF:
        case FILE:
            if (attributePath.size() > 1) {
                throw new UnsupportedOperationException("Can not filter on references deeper than 1.");
            }
            return QueryBuilders.nestedQuery(fieldName, QueryBuilders.matchQuery(fieldName + '.' + "_all", queryValue), ScoreMode.Avg);
        case BOOL:
            throw new MolgenisQueryException("Cannot execute search query on [" + dataType + "] attribute");
        case COMPOUND:
            throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.SEARCH + "]");
        default:
            throw new UnexpectedEnumException(dataType);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 10 with AttributeType

use of org.molgenis.data.meta.AttributeType in project molgenis by molgenis.

the class QueryGenerator method createQueryClauseIn.

private QueryBuilder createQueryClauseIn(QueryRule queryRule, EntityType entityType) {
    List<Attribute> attributePath = getAttributePath(queryRule.getField(), entityType);
    Attribute attr = attributePath.get(attributePath.size() - 1);
    Object queryRuleValue = queryRule.getValue();
    if (queryRuleValue == null) {
        throw new MolgenisQueryException("Query value cannot be null");
    }
    if (!(queryRuleValue instanceof Iterable<?>)) {
        throw new MolgenisQueryException("Query value must be a Iterable instead of [" + queryRuleValue.getClass().getSimpleName() + "]");
    }
    Object[] queryValues = StreamSupport.stream(((Iterable<?>) queryRuleValue).spliterator(), false).map(aQueryRuleValue -> getQueryValue(attr, aQueryRuleValue)).toArray();
    QueryBuilder queryBuilder;
    String fieldName = getQueryFieldName(attr);
    AttributeType dataType = attr.getDataType();
    switch(dataType) {
        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:
            if (useNotAnalyzedField(attr)) {
                fieldName = fieldName + '.' + FIELD_NOT_ANALYZED;
            }
            // note: inFilter expects array, not iterable
            queryBuilder = QueryBuilders.termsQuery(fieldName, queryValues);
            queryBuilder = nestedQueryBuilder(attributePath, queryBuilder);
            break;
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case MREF:
        case XREF:
        case FILE:
        case ONE_TO_MANY:
            if (attributePath.size() > 1) {
                throw new UnsupportedOperationException("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);
            if (useNotAnalyzedField(refIdAttr)) {
                indexFieldName = indexFieldName + '.' + FIELD_NOT_ANALYZED;
            }
            queryBuilder = QueryBuilders.termsQuery(indexFieldName, queryValues);
            queryBuilder = QueryBuilders.nestedQuery(fieldName, queryBuilder, ScoreMode.Avg);
            break;
        case COMPOUND:
            throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.IN + "]");
        default:
            throw new UnexpectedEnumException(dataType);
    }
    return QueryBuilders.constantScoreQuery(queryBuilder);
}
Also used : AttributeType(org.molgenis.data.meta.AttributeType) org.molgenis.data(org.molgenis.data) Iterator(java.util.Iterator) Stream.of(java.util.stream.Stream.of) LIKE(org.molgenis.data.QueryRule.Operator.LIKE) ScoreMode(org.apache.lucene.search.join.ScoreMode) Instant(java.time.Instant) Attribute(org.molgenis.data.meta.model.Attribute) EntityType(org.molgenis.data.meta.model.EntityType) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) FIELD_NOT_ANALYZED(org.molgenis.data.elasticsearch.FieldConstants.FIELD_NOT_ANALYZED) Component(org.springframework.stereotype.Component) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) org.elasticsearch.index.query(org.elasticsearch.index.query) LocalDate(java.time.LocalDate) Objects.requireNonNull(java.util.Objects.requireNonNull) StreamSupport(java.util.stream.StreamSupport) Stream.concat(java.util.stream.Stream.concat) DEFAULT_ANALYZER(org.molgenis.data.elasticsearch.FieldConstants.DEFAULT_ANALYZER) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Aggregations

AttributeType (org.molgenis.data.meta.AttributeType)46 Attribute (org.molgenis.data.meta.model.Attribute)24 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)24 LocalDate (java.time.LocalDate)11 Instant (java.time.Instant)10 Entity (org.molgenis.data.Entity)10 EntityType (org.molgenis.data.meta.model.EntityType)7 DataProvider (org.testng.annotations.DataProvider)7 ArrayList (java.util.ArrayList)5 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)2 String.format (java.lang.String.format)2 Collectors.toList (java.util.stream.Collectors.toList)2 Test (org.testng.annotations.Test)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 UTC (java.time.ZoneOffset.UTC)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1