Search in sources :

Example 31 with UnexpectedEnumException

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

the class ExcelSheetWriter method writeAttributeHeaders.

/**
 * Write sheet column headers
 */
public void writeAttributeHeaders(Iterable<Attribute> attributes, AttributeWriteMode attributeWriteMode) {
    if (attributes == null)
        throw new IllegalArgumentException("Attributes cannot be null");
    if (attributeWriteMode == null)
        throw new IllegalArgumentException("AttributeWriteMode cannot be null");
    if (cachedAttributes == null) {
        Row poiRow = sheet.createRow(row++);
        // write header
        int i = 0;
        for (Attribute attribute : attributes) {
            Cell cell = poiRow.createCell(i++, CellType.STRING);
            switch(attributeWriteMode) {
                case ATTRIBUTE_LABELS:
                    cell.setCellValue(AbstractCellProcessor.processCell(attribute.getLabel(), true, cellProcessors));
                    break;
                case ATTRIBUTE_NAMES:
                    cell.setCellValue(AbstractCellProcessor.processCell(attribute.getName(), true, cellProcessors));
                    break;
                default:
                    throw new UnexpectedEnumException(attributeWriteMode);
            }
        }
        // store header
        this.cachedAttributes = attributes;
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

Example 32 with UnexpectedEnumException

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

the class CsvWriter method toValue.

private String toValue(Object obj) {
    String value;
    if (obj == null) {
        value = null;
    } else if (obj instanceof Entity) {
        if (getEntityWriteMode() != null) {
            switch(getEntityWriteMode()) {
                case ENTITY_IDS:
                    value = ((Entity) obj).getIdValue().toString();
                    break;
                case ENTITY_LABELS:
                    Object labelValue = ((Entity) obj).getLabelValue();
                    value = labelValue != null ? labelValue.toString() : null;
                    break;
                default:
                    throw new UnexpectedEnumException(getEntityWriteMode());
            }
        } else {
            Object labelValue = ((Entity) obj).getLabelValue();
            value = labelValue != null ? labelValue.toString() : null;
        }
    } else if (obj instanceof Iterable<?>) {
        StringBuilder strBuilder = new StringBuilder();
        for (Object listItem : (Iterable<?>) obj) {
            if (strBuilder.length() > 0)
                strBuilder.append(',');
            strBuilder.append(toValue(listItem));
        }
        // TODO apply cell processors to list elements?
        value = strBuilder.toString();
    } else {
        value = obj.toString();
    }
    return AbstractCellProcessor.processCell(value, false, this.cellProcessors);
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException)

Example 33 with UnexpectedEnumException

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

the class QueryGenerator method createQueryClauseFuzzyMatchNgram.

private QueryBuilder createQueryClauseFuzzyMatchNgram(QueryRule queryRule, EntityType entityType) {
    String queryField = queryRule.getField();
    Object queryValue = queryRule.getValue();
    QueryBuilder queryBuilder;
    if (queryValue == null)
        throw new MolgenisQueryException("Query value cannot be null");
    if (queryField == null) {
        queryBuilder = QueryBuilders.matchQuery("_all", queryValue);
    } else {
        Attribute attr = entityType.getAttribute(queryField);
        if (attr == null) {
            throw new UnknownAttributeException(entityType, queryField);
        }
        // construct query part
        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:
                queryField = getQueryFieldName(attr) + ".ngram";
                queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")");
                break;
            case MREF:
            case XREF:
                queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + ".ngram";
                queryBuilder = QueryBuilders.nestedQuery(getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max);
                break;
            default:
                throw new UnexpectedEnumException(dataType);
        }
    }
    return queryBuilder;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 34 with UnexpectedEnumException

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

the class QueryGenerator method createQueryClauseEqualsValue.

private QueryBuilder createQueryClauseEqualsValue(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 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:
            if (useNotAnalyzedField(attr)) {
                fieldName = fieldName + '.' + FIELD_NOT_ANALYZED;
            }
            return nestedQueryBuilder(attributePath, QueryBuilders.termQuery(fieldName, queryValue));
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case XREF:
        case MREF:
        case FILE:
        case ONE_TO_MANY:
            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);
            if (useNotAnalyzedField(refIdAttr)) {
                indexFieldName = indexFieldName + '.' + FIELD_NOT_ANALYZED;
            }
            return QueryBuilders.nestedQuery(fieldName, QueryBuilders.termQuery(indexFieldName, queryValue), 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 35 with UnexpectedEnumException

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

the class QueryGenerator method createQueryClauseFuzzyMatch.

private QueryBuilder createQueryClauseFuzzyMatch(QueryRule queryRule, EntityType entityType) {
    String queryField = queryRule.getField();
    Object queryValue = queryRule.getValue();
    QueryBuilder queryBuilder;
    if (queryValue == null)
        throw new MolgenisQueryException("Query value cannot be null");
    if (queryField == null) {
        queryBuilder = QueryBuilders.matchQuery("_all", queryValue);
    } else {
        Attribute attr = entityType.getAttribute(queryField);
        if (attr == null) {
            throw new UnknownAttributeException(entityType, queryField);
        }
        // construct query part
        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:
                queryBuilder = QueryBuilders.queryStringQuery(getQueryFieldName(attr) + ":(" + queryValue + ")");
                break;
            case MREF:
            case XREF:
            case CATEGORICAL:
            case CATEGORICAL_MREF:
            case ONE_TO_MANY:
            case FILE:
                queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute());
                queryBuilder = QueryBuilders.nestedQuery(getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max);
                break;
            case BOOL:
            case COMPOUND:
                throw new MolgenisQueryException("Illegal data type [" + dataType + "] for operator [" + QueryRule.Operator.FUZZY_MATCH + "]");
            default:
                throw new UnexpectedEnumException(dataType);
        }
    }
    return queryBuilder;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

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