Search in sources :

Example 46 with AttributeType

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

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