Search in sources :

Example 6 with Attribute

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);
    }
}
Also used : Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Instant(java.time.Instant) LocalDate(java.time.LocalDate) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException) AttributeType(org.molgenis.data.meta.AttributeType) UnknownAttributeException(org.molgenis.data.UnknownAttributeException)

Example 7 with Attribute

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;
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute)

Example 8 with Attribute

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"));
}
Also used : Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) Test(org.testng.annotations.Test)

Example 9 with Attribute

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);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) EntityManager(org.molgenis.data.EntityManager) Attribute(org.molgenis.data.meta.model.Attribute) Test(org.testng.annotations.Test)

Example 10 with Attribute

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;
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute)

Aggregations

Attribute (org.molgenis.data.meta.model.Attribute)600 Test (org.testng.annotations.Test)351 EntityType (org.molgenis.data.meta.model.EntityType)321 Entity (org.molgenis.data.Entity)109 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)54 DynamicEntity (org.molgenis.data.support.DynamicEntity)53 AbstractMolgenisSpringTest (org.molgenis.data.AbstractMolgenisSpringTest)47 Stream (java.util.stream.Stream)39 AttributeType (org.molgenis.data.meta.AttributeType)34 QueryImpl (org.molgenis.data.support.QueryImpl)29 ExplainedAttribute (org.molgenis.semanticsearch.explain.bean.ExplainedAttribute)29 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)28 BeforeMethod (org.testng.annotations.BeforeMethod)20 EntityTypeIdentity (org.molgenis.data.security.EntityTypeIdentity)18 WithMockUser (org.springframework.security.test.context.support.WithMockUser)18 Collectors.toList (java.util.stream.Collectors.toList)17 Relation (org.molgenis.data.semantic.Relation)17 Objects.requireNonNull (java.util.Objects.requireNonNull)16 MolgenisDataException (org.molgenis.data.MolgenisDataException)16 Package (org.molgenis.data.meta.model.Package)16