use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class DynamicEntity method validateValueType.
/**
* 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(entityType, 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:
if (!(value instanceof LocalDate)) {
throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), LocalDate.class.getSimpleName(), attrName));
}
break;
case DATE_TIME:
if (!(value instanceof Instant)) {
throw new MolgenisDataException(format("Value [%s] is of type [%s] instead of [%s] for attribute: [%s]", value.toString(), value.getClass().getSimpleName(), Instant.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));
}
if (((Double) value).isNaN()) {
throw new MolgenisDataException(format("Value [%s] for type [%s] is not allowed for attribute: [%s]", value.toString(), 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 UnexpectedEnumException(dataType);
}
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class QueryUtils method containsComputedAttribute.
public static boolean containsComputedAttribute(Iterable<QueryRule> rules, EntityType entityType) {
for (QueryRule rule : rules) {
List<QueryRule> nestedRules = rule.getNestedRules();
if (!nestedRules.isEmpty() && containsComputedAttribute(nestedRules, entityType)) {
return true;
}
Attribute attribute = getQueryRuleAttribute(rule, entityType);
if (attribute != null && attribute.hasExpression()) {
return true;
}
}
return false;
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class LazyEntityTest method getAttributeNames.
@Test
public void getAttributeNames() {
Entity entity = new DynamicEntity(entityType);
Attribute attr0 = when(mock(Attribute.class).getName()).thenReturn("attr0").getMock();
Attribute attr1 = when(mock(Attribute.class).getName()).thenReturn("attr1").getMock();
when(entityType.getAtomicAttributes()).thenReturn(Arrays.asList(attr0, attr1));
assertEquals(Lists.newArrayList(entity.getAttributeNames()), Arrays.asList("attr0", "attr1"));
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class EntityUtilsTest method getTypedValueStringAttributeEntityManagerXref.
@Test
public void getTypedValueStringAttributeEntityManagerXref() {
String valueStr = "0";
Attribute attr = mock(Attribute.class);
EntityType refEntityType = mock(EntityType.class);
Attribute refIdAttr = mock(Attribute.class);
when(refIdAttr.getDataType()).thenReturn(STRING);
when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
when(attr.getRefEntity()).thenReturn(refEntityType);
when(attr.getDataType()).thenReturn(XREF);
Entity entity = mock(Entity.class);
EntityManager entityManager = mock(EntityManager.class);
when(entityManager.getReference(refEntityType, valueStr)).thenReturn(entity);
assertEquals(EntityUtils.getTypedValue(valueStr, attr, entityManager), entity);
}
use of org.molgenis.data.meta.model.Attribute in project molgenis by molgenis.
the class EntityUtilsTest method getIsNullValueMockAttribute.
private Attribute getIsNullValueMockAttribute(String attributeName, AttributeType attributeType) {
Attribute attribute = mock(Attribute.class);
when(attribute.getName()).thenReturn(attributeName);
when(attribute.getDataType()).thenReturn(attributeType);
when(attribute.toString()).thenReturn(attributeType.toString());
return attribute;
}
Aggregations