Search in sources :

Example 36 with UnexpectedEnumException

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

the class DocumentContentBuilder method createRec.

private void createRec(Entity entity, Attribute attr, XContentGenerator generator, int depth, int maxDepth) throws IOException {
    String attrName = attr.getName();
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            Boolean boolValue = entity.getBoolean(attrName);
            if (boolValue != null) {
                generator.writeBoolean(boolValue);
            } else {
                generator.writeNull();
            }
            break;
        case DECIMAL:
            Double doubleValue = entity.getDouble(attrName);
            if (doubleValue != null) {
                generator.writeNumber(doubleValue);
            } else {
                generator.writeNull();
            }
            break;
        case INT:
            Integer intValue = entity.getInt(attrName);
            if (intValue != null) {
                generator.writeNumber(intValue);
            } else {
                generator.writeNull();
            }
            break;
        case LONG:
            Long longValue = entity.getLong(attrName);
            if (longValue != null) {
                generator.writeNumber(longValue);
            } else {
                generator.writeNull();
            }
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            String strValue = entity.getString(attrName);
            if (strValue != null) {
                generator.writeString(strValue);
            } else {
                generator.writeNull();
            }
            break;
        case DATE:
            LocalDate date = entity.getLocalDate(attrName);
            if (date != null) {
                generator.writeString(date.toString());
            } else {
                generator.writeNull();
            }
            break;
        case DATE_TIME:
            Instant dateTime = entity.getInstant(attrName);
            if (dateTime != null) {
                generator.writeString(dateTime.toString());
            } else {
                generator.writeNull();
            }
            break;
        case CATEGORICAL:
        case XREF:
        case FILE:
            {
                Entity xrefEntity = entity.getEntity(attrName);
                if (xrefEntity != null) {
                    createRecReferenceAttribute(generator, depth, maxDepth, xrefEntity);
                } else {
                    generator.writeNull();
                }
                break;
            }
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            {
                Iterable<Entity> mrefEntities = entity.getEntities(attrName);
                if (!Iterables.isEmpty(mrefEntities)) {
                    generator.writeStartArray();
                    for (Entity mrefEntity : mrefEntities) {
                        createRecReferenceAttribute(generator, depth, maxDepth, mrefEntity);
                    }
                    generator.writeEndArray();
                } else {
                    generator.writeNull();
                }
                break;
            }
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) Instant(java.time.Instant) LocalDate(java.time.LocalDate)

Example 37 with UnexpectedEnumException

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

the class SortGenerator method getSortField.

private String getSortField(Attribute attr) {
    String sortField;
    String fieldName = documentIdGenerator.generateId(attr);
    AttributeType dataType = attr.getDataType();
    switch(dataType) {
        case BOOL:
        case DATE:
        case DATE_TIME:
        case DECIMAL:
        case INT:
        case LONG:
            // use indexed field for sorting
            sortField = fieldName;
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            // use raw field for sorting
            sortField = fieldName + '.' + FIELD_NOT_ANALYZED;
            break;
        case CATEGORICAL:
        case CATEGORICAL_MREF:
        case FILE:
        case MREF:
        case ONE_TO_MANY:
        case XREF:
            // use nested field for sorting
            String refSortField = getSortField(attr.getRefEntity().getLabelAttribute());
            sortField = fieldName + '.' + refSortField;
            break;
        case COMPOUND:
            throw new UnsupportedOperationException();
        default:
            throw new UnexpectedEnumException(dataType);
    }
    return sortField;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 38 with UnexpectedEnumException

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

the class RestControllerV2 method createEntityValuesResponseRec.

private void createEntityValuesResponseRec(Entity entity, Iterable<Attribute> attrs, Fetch fetch, Map<String, Object> responseData) {
    responseData.put("_href", Href.concatEntityHref(BASE_URI, entity.getEntityType().getId(), entity.getIdValue()));
    for (// TODO performance use fetch instead of attrs
    Attribute attr : // TODO performance use fetch instead of attrs
    attrs) {
        String attrName = attr.getName();
        if (fetch == null || fetch.hasField(attr)) {
            AttributeType dataType = attr.getDataType();
            switch(dataType) {
                case BOOL:
                    responseData.put(attrName, entity.getBoolean(attrName));
                    break;
                case CATEGORICAL:
                case XREF:
                case FILE:
                    Entity refEntity = entity.getEntity(attrName);
                    Map<String, Object> refEntityResponse;
                    if (refEntity != null) {
                        Fetch refAttrFetch = fetch != null ? fetch.getFetch(attr) : createDefaultAttributeFetch(attr, LanguageService.getCurrentUserLanguageCode());
                        refEntityResponse = createEntityResponse(refEntity, refAttrFetch, false);
                    } else {
                        refEntityResponse = null;
                    }
                    responseData.put(attrName, refEntityResponse);
                    break;
                case CATEGORICAL_MREF:
                case MREF:
                case ONE_TO_MANY:
                    Iterable<Entity> refEntities = entity.getEntities(attrName);
                    List<Map<String, Object>> refEntityResponses;
                    if (refEntities != null) {
                        refEntityResponses = new ArrayList<>();
                        Fetch refAttrFetch = fetch != null ? fetch.getFetch(attrName) : createDefaultAttributeFetch(attr, LanguageService.getCurrentUserLanguageCode());
                        for (Entity refEntitiesEntity : refEntities) {
                            refEntityResponses.add(createEntityResponse(refEntitiesEntity, refAttrFetch, false));
                        }
                    } else {
                        refEntityResponses = null;
                    }
                    responseData.put(attrName, refEntityResponses);
                    break;
                case COMPOUND:
                    throw new RuntimeException("Invalid data type [" + dataType + "]");
                case DATE:
                    LocalDate dateValue = entity.getLocalDate(attrName);
                    responseData.put(attrName, dateValue != null ? dateValue.toString() : null);
                    break;
                case DATE_TIME:
                    Instant dateTimeValue = entity.getInstant(attrName);
                    responseData.put(attrName, dateTimeValue != null ? dateTimeValue.toString() : null);
                    break;
                case DECIMAL:
                    responseData.put(attrName, entity.getDouble(attrName));
                    break;
                case EMAIL:
                case ENUM:
                case HTML:
                case HYPERLINK:
                case SCRIPT:
                case STRING:
                case TEXT:
                    responseData.put(attrName, entity.getString(attrName));
                    break;
                case INT:
                    responseData.put(attrName, entity.getInt(attrName));
                    break;
                case LONG:
                    responseData.put(attrName, entity.getLong(attrName));
                    break;
                default:
                    throw new UnexpectedEnumException(dataType);
            }
        }
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) Instant(java.time.Instant) LocalDate(java.time.LocalDate) AttributeFilterToFetchConverter.createDefaultAttributeFetch(org.molgenis.data.rest.v2.AttributeFilterToFetchConverter.createDefaultAttributeFetch) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 39 with UnexpectedEnumException

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

the class RestService method toEntityValue.

/**
 * Converts a HTTP request parameter to a entity value of which the type is defined by the attribute. For file
 * attributes persists the file in the file store and persist a file meta data entity.
 *
 * @param attr       attribute
 * @param paramValue HTTP parameter value
 * @return Object
 */
public Object toEntityValue(Attribute attr, Object paramValue, Object id) {
    // Treat empty strings as null
    if (paramValue != null && (paramValue instanceof String) && ((String) paramValue).isEmpty()) {
        paramValue = null;
    }
    Object value;
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            value = convertBool(attr, paramValue);
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = convertString(attr, paramValue);
            break;
        case CATEGORICAL:
        case XREF:
            value = convertRef(attr, paramValue);
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            value = convertMref(attr, paramValue);
            break;
        case DATE:
            value = convertDate(attr, paramValue);
            break;
        case DATE_TIME:
            value = convertDateTime(attr, paramValue);
            break;
        case DECIMAL:
            value = convertDecimal(attr, paramValue);
            break;
        case FILE:
            value = convertFile(attr, paramValue, id);
            break;
        case INT:
            value = convertInt(attr, paramValue);
            break;
        case LONG:
            value = convertLong(attr, paramValue);
            break;
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
    return value;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) 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