Search in sources :

Example 11 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException 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 12 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException 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 13 with UnexpectedEnumException

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

the class AttributeValidator method validate.

public void validate(Attribute attr, ValidationMode validationMode) {
    validateName(attr);
    validateDefaultValue(attr, validationMode == ADD || validationMode == UPDATE);
    validateParent(attr);
    validateChildren(attr);
    switch(validationMode) {
        case ADD:
        case ADD_SKIP_ENTITY_VALIDATION:
            validateAdd(attr);
            break;
        case UPDATE:
        case UPDATE_SKIP_ENTITY_VALIDATION:
            Attribute currentAttr = dataService.findOneById(ATTRIBUTE_META_DATA, attr.getIdentifier(), Attribute.class);
            validateUpdate(attr, currentAttr);
            break;
        default:
            throw new UnexpectedEnumException(validationMode);
    }
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Attribute(org.molgenis.data.meta.model.Attribute)

Example 14 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException 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 15 with UnexpectedEnumException

use of org.molgenis.util.UnexpectedEnumException 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

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