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 RuntimeException(format("Unknown attribute type [%s]", attrType.toString())); } return value; }
/** * Validate is value is of the type defined by the attribute data type. * * @param attrName attribute name * @param value value (must be of the type defined by the attribute data type.) */ protected void validateValueType(String attrName, Object value) { if (value == null) { return; } Attribute attr = entityType.getAttribute(attrName); if (attr == null) { throw new UnknownAttributeException(format("Unknown attribute [%s]", attrName)); } AttributeType dataType = attr.getDataType(); switch (dataType) { case BOOL: if (!(value instanceof Boolean)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Boolean.class.getSimpleName(), attrName)); } break; case CATEGORICAL: // expected type is FileMeta. validation is not possible because molgenis-data does not // depend on molgenis-file case FILE: case XREF: if (!(value instanceof Entity)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Entity.class.getSimpleName(), attrName)); } break; case CATEGORICAL_MREF: case MREF: case ONE_TO_MANY: if (!(value instanceof Iterable)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Iterable.class.getSimpleName(), attrName)); } break; case COMPOUND: throw new IllegalArgumentException( format("Unexpected data type [%s] for attribute: [%s]", dataType.toString(), attrName)); case DATE: case DATE_TIME: if (!(value instanceof java.util.Date)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), java.util.Date.class.getSimpleName(), attrName)); } break; case DECIMAL: if (!(value instanceof Double)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Double.class.getSimpleName(), attrName)); } break; case EMAIL: case ENUM: case HTML: case HYPERLINK: case SCRIPT: case STRING: case TEXT: if (!(value instanceof String)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), String.class.getSimpleName(), attrName)); } break; case INT: if (!(value instanceof Integer)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Integer.class.getSimpleName(), attrName)); } break; case LONG: if (!(value instanceof Long)) { throw new MolgenisDataException( format( "Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Long.class.getSimpleName(), attrName)); } break; default: throw new RuntimeException(format("Unknown data type [%s]", dataType.toString())); } }