Search in sources :

Example 16 with AttributeType

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

the class AlgorithmServiceImpl method convert.

@SuppressWarnings("unchecked")
private Object convert(Object value, Attribute attr) {
    Object convertedValue;
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            convertedValue = value != null ? toBoolean(value) : null;
            break;
        case CATEGORICAL:
        case XREF:
        case FILE:
            convertedValue = value != null ? entityManager.getReference(attr.getRefEntity(), convert(value, attr.getRefEntity().getIdAttribute())) : null;
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            Collection<Object> valueIds = (Collection<Object>) value;
            convertedValue = valueIds.stream().map(valueId -> entityManager.getReference(attr.getRefEntity(), convert(valueId, attr.getRefEntity().getIdAttribute()))).collect(toList());
            break;
        case DATE:
            convertedValue = convertToDate(value);
            break;
        case DATE_TIME:
            convertedValue = convertToDateTime(value);
            break;
        case DECIMAL:
            convertedValue = convertToDouble(value);
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            convertedValue = value != null ? value.toString() : null;
            break;
        case INT:
            convertedValue = convertToInteger(value);
            break;
        case LONG:
            convertedValue = convertToLong(value);
            break;
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
    return convertedValue;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) Collection(java.util.Collection)

Example 17 with AttributeType

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

the class EntityAttributesValidator method validate.

public Set<ConstraintViolation> validate(Entity entity, EntityType meta) {
    Set<ConstraintViolation> violations = checkNullableExpressions(entity, meta);
    violations.addAll(checkValidationExpressions(entity, meta));
    for (Attribute attr : meta.getAtomicAttributes()) {
        ConstraintViolation violation = null;
        AttributeType attrType = attr.getDataType();
        switch(attrType) {
            case EMAIL:
                violation = checkEmail(entity, attr, meta);
                break;
            case BOOL:
                violation = checkBoolean(entity, attr, meta);
                break;
            case DATE:
                violation = checkDate(entity, attr, meta);
                break;
            case DATE_TIME:
                violation = checkDateTime(entity, attr, meta);
                break;
            case DECIMAL:
                violation = checkDecimal(entity, attr, meta);
                break;
            case HYPERLINK:
                violation = checkHyperlink(entity, attr, meta);
                break;
            case INT:
                violation = checkInt(entity, attr, meta);
                if ((violation == null) && (attr.getRange() != null)) {
                    violation = checkRange(entity, attr, meta);
                }
                break;
            case LONG:
                violation = checkLong(entity, attr, meta);
                if ((violation == null) && (attr.getRange() != null)) {
                    violation = checkRange(entity, attr, meta);
                }
                break;
            case ENUM:
                violation = checkEnum(entity, attr, meta);
                break;
            case HTML:
                violation = checkText(entity, attr, meta, HTML);
                break;
            case SCRIPT:
                violation = checkText(entity, attr, meta, SCRIPT);
                break;
            case TEXT:
                violation = checkText(entity, attr, meta, TEXT);
                break;
            case STRING:
                violation = checkText(entity, attr, meta, STRING);
                break;
            case CATEGORICAL:
            case FILE:
            case XREF:
                violation = checkXref(entity, attr, meta);
                break;
            case CATEGORICAL_MREF:
            case MREF:
            case ONE_TO_MANY:
                violation = checkMref(entity, attr, meta);
                break;
            case COMPOUND:
                // no op
                break;
            default:
                throw new UnexpectedEnumException(attrType);
        }
        if (violation != null) {
            violations.add(violation);
        }
    }
    return violations;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute) AttributeType(org.molgenis.data.meta.AttributeType)

Example 18 with AttributeType

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

the class QueryValidator method toQueryRuleValue.

private Object toQueryRuleValue(Object queryRuleValue, Attribute attr) {
    Object value;
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            value = convertBool(attr, queryRuleValue);
            break;
        case EMAIL:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = convertString(attr, queryRuleValue);
            break;
        case ENUM:
            value = convertEnum(attr, queryRuleValue);
            break;
        case CATEGORICAL:
        case XREF:
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            value = convertRef(attr, queryRuleValue);
            break;
        case DATE:
            value = convertDate(attr, queryRuleValue);
            break;
        case DATE_TIME:
            value = convertDateTime(attr, queryRuleValue);
            break;
        case DECIMAL:
            value = convertDecimal(attr, queryRuleValue);
            break;
        case FILE:
            value = convertFile(attr, queryRuleValue);
            break;
        case INT:
            value = convertInt(attr, queryRuleValue);
            break;
        case LONG:
            value = convertLong(attr, queryRuleValue);
            break;
        case COMPOUND:
            throw new MolgenisValidationException(new ConstraintViolation(format("Attribute [%s] type [%s] is not allowed", attr.getName(), attrType.toString())));
        default:
            throw new UnexpectedEnumException(attrType);
    }
    return value;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 19 with AttributeType

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

the class EntityUtils method isNullValue.

/**
 * Returns whether an entity attribute value is <tt>null</tt> or empty for attributes referencing multiple entities.
 */
public static boolean isNullValue(Entity entity, Attribute attribute) {
    boolean isNullValue;
    String attributeName = attribute.getName();
    AttributeType attributeType = attribute.getDataType();
    switch(attributeType) {
        case BOOL:
            isNullValue = entity.getBoolean(attributeName) == null;
            break;
        case CATEGORICAL:
        case FILE:
        case XREF:
            isNullValue = entity.getEntity(attributeName) == null;
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            Iterable<Entity> refEntities = entity.getEntities(attributeName);
            isNullValue = Iterables.isEmpty(refEntities);
            break;
        case COMPOUND:
            throw new RuntimeException(format("Invalid data type [%s]", attribute.getDataType()));
        case DATE:
            isNullValue = entity.getLocalDate(attributeName) == null;
            break;
        case DATE_TIME:
            isNullValue = entity.getInstant(attributeName) == null;
            break;
        case DECIMAL:
            isNullValue = entity.getDouble(attributeName) == null;
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            isNullValue = entity.getString(attributeName) == null;
            break;
        case INT:
            isNullValue = entity.getInt(attributeName) == null;
            break;
        case LONG:
            isNullValue = entity.getLong(attributeName) == null;
            break;
        default:
            throw new UnexpectedEnumException(attributeType);
    }
    return isNullValue;
}
Also used : Entity(org.molgenis.data.Entity) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType)

Example 20 with AttributeType

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

the class JsMagmaScriptEvaluator method toScriptEngineValue.

private Object toScriptEngineValue(Entity entity, Attribute attr, int depth) {
    Object value = null;
    String attrName = attr.getName();
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            value = entity.getBoolean(attrName);
            break;
        case CATEGORICAL:
        case FILE:
        case XREF:
            Entity xrefEntity = entity.getEntity(attrName);
            value = toScriptEngineValueMap(xrefEntity, depth - 1);
            break;
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            ScriptObjectMirror jsArray = null;
            try {
                jsArray = (ScriptObjectMirror) jsScriptEngine.eval("var arr = []; arr");
                @SuppressWarnings("unchecked") List<Object> mrefValues = jsArray.to(List.class);
                entity.getEntities(attrName).forEach(mrefEntity -> mrefValues.add(toScriptEngineValueMap(mrefEntity, depth - 1)));
            } catch (javax.script.ScriptException ex) {
            // Do not catch this error to allow
            // the mapping service to collect errors to show in the UI
            }
            value = jsArray;
            break;
        case DATE:
            LocalDate localDate = entity.getLocalDate(attrName);
            if (localDate != null) {
                value = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
            }
            break;
        case DATE_TIME:
            Instant instant = entity.getInstant(attrName);
            if (instant != null) {
                value = instant.toEpochMilli();
            }
            break;
        case DECIMAL:
            value = entity.getDouble(attrName);
            break;
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = entity.getString(attrName);
            break;
        case INT:
            value = entity.getInt(attrName);
            break;
        case LONG:
            value = entity.getLong(attrName);
            break;
        case COMPOUND:
            throw new RuntimeException(format("Illegal attribute type [%s]", attrType.toString()));
        default:
            throw new UnexpectedEnumException(attrType);
    }
    return value;
}
Also used : Entity(org.molgenis.data.Entity) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Instant(java.time.Instant) LocalDate(java.time.LocalDate) 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