Search in sources :

Example 1 with EntityManager

use of org.molgenis.data.EntityManager 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 2 with EntityManager

use of org.molgenis.data.EntityManager in project molgenis by molgenis.

the class RestServiceTest method setUpBeforeMethod.

@BeforeMethod
public void setUpBeforeMethod() {
    dataService = mock(DataService.class);
    idGenerator = mock(IdGenerator.class);
    FileStore fileStore = mock(FileStore.class);
    fileMetaFactory = mock(FileMetaFactory.class);
    entityManager = mock(EntityManager.class);
    servletUriComponentsBuilderFactory = mock(ServletUriComponentsBuilderFactory.class);
    this.restService = new RestService(dataService, idGenerator, fileStore, fileMetaFactory, entityManager, servletUriComponentsBuilderFactory);
}
Also used : FileStore(org.molgenis.data.file.FileStore) EntityManager(org.molgenis.data.EntityManager) FileMetaFactory(org.molgenis.data.file.model.FileMetaFactory) IdGenerator(org.molgenis.data.populate.IdGenerator) DataService(org.molgenis.data.DataService) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with EntityManager

use of org.molgenis.data.EntityManager in project molgenis by molgenis.

the class QueryValidatorTest method setUpBeforeMethod.

@BeforeMethod
public void setUpBeforeMethod() {
    EntityManager entityManager = mock(EntityManager.class);
    this.queryValidator = new QueryValidator(entityManager);
}
Also used : EntityManager(org.molgenis.data.EntityManager) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 4 with EntityManager

use of org.molgenis.data.EntityManager in project molgenis by molgenis.

the class EntityUtils method getTypedValue.

/**
 * Convert a string value to a typed value based on the attribute data type.
 *
 * @param valueStr      string value
 * @param attr          attribute
 * @param entityManager entity manager used to convert referenced entity values
 * @return typed value
 */
public static Object getTypedValue(String valueStr, Attribute attr, EntityManager entityManager) {
    if (valueStr == null)
        return null;
    switch(attr.getDataType()) {
        case BOOL:
            return Boolean.valueOf(valueStr);
        case CATEGORICAL:
        case FILE:
        case XREF:
            EntityType xrefEntity = attr.getRefEntity();
            Object xrefIdValue = getTypedValue(valueStr, xrefEntity.getIdAttribute(), entityManager);
            return entityManager.getReference(xrefEntity, xrefIdValue);
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            EntityType mrefEntity = attr.getRefEntity();
            List<String> mrefIdStrValues = ListEscapeUtils.toList(valueStr);
            return mrefIdStrValues.stream().map(mrefIdStrValue -> getTypedValue(mrefIdStrValue, mrefEntity.getIdAttribute(), entityManager)).map(mrefIdValue -> entityManager.getReference(mrefEntity, mrefIdValue)).collect(toList());
        case COMPOUND:
            throw new IllegalArgumentException("Compound attribute has no value");
        case DATE:
            try {
                return parseLocalDate(valueStr);
            } catch (DateTimeParseException e) {
                throw new MolgenisDataException(format(FAILED_TO_PARSE_ATTRIBUTE_AS_DATE_MESSAGE, attr.getName(), valueStr), e);
            }
        case DATE_TIME:
            try {
                return parseInstant(valueStr);
            } catch (DateTimeParseException e) {
                throw new MolgenisDataException(format(FAILED_TO_PARSE_ATTRIBUTE_AS_DATETIME_MESSAGE, attr.getName(), valueStr), e);
            }
        case DECIMAL:
            return Double.valueOf(valueStr);
        case EMAIL:
        case ENUM:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            return valueStr;
        case INT:
            return Integer.valueOf(valueStr);
        case LONG:
            return Long.valueOf(valueStr);
        default:
            throw new UnexpectedEnumException(attr.getDataType());
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) ListEscapeUtils(org.molgenis.util.ListEscapeUtils) Iterables(com.google.common.collect.Iterables) Pair(org.molgenis.util.Pair) Attribute(org.molgenis.data.meta.model.Attribute) Tag(org.molgenis.data.meta.model.Tag) AttributeType(org.molgenis.data.meta.AttributeType) Iterator(java.util.Iterator) EntityTypeUtils(org.molgenis.data.support.EntityTypeUtils) MolgenisDateFormat(org.molgenis.data.util.MolgenisDateFormat) EntityType(org.molgenis.data.meta.model.EntityType) PACKAGE_SYSTEM(org.molgenis.data.system.model.RootSystemPackage.PACKAGE_SYSTEM) String.format(java.lang.String.format) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) Objects(java.util.Objects) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) StreamSupport.stream(java.util.stream.StreamSupport.stream) COMPOUND(org.molgenis.data.meta.AttributeType.COMPOUND) DataService(org.molgenis.data.DataService) Package(org.molgenis.data.meta.model.Package) MolgenisDataException(org.molgenis.data.MolgenisDataException) EntityManager(org.molgenis.data.EntityManager) Entity(org.molgenis.data.Entity) DateTimeParseException(java.time.format.DateTimeParseException) UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) MolgenisDataException(org.molgenis.data.MolgenisDataException)

Example 5 with EntityManager

use of org.molgenis.data.EntityManager in project molgenis by molgenis.

the class PartialEntityTest method setUpBeforeMethod.

@BeforeMethod
public void setUpBeforeMethod() {
    Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn("id").getMock();
    meta = when(mock(EntityType.class).getId()).thenReturn("entity").getMock();
    when(meta.getIdAttribute()).thenReturn(idAttr);
    originalEntity = mock(Entity.class);
    decoratedEntity = mock(Entity.class);
    when(decoratedEntity.getEntityType()).thenReturn(meta);
    when(decoratedEntity.getIdValue()).thenReturn("id");
    fetch = new Fetch().field("id");
    entityManager = mock(EntityManager.class);
    when(entityManager.getReference(meta, "id")).thenReturn(originalEntity);
    partialEntity = new PartialEntity(decoratedEntity, fetch, entityManager);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Fetch(org.molgenis.data.Fetch) Entity(org.molgenis.data.Entity) EntityManager(org.molgenis.data.EntityManager) Attribute(org.molgenis.data.meta.model.Attribute) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

EntityManager (org.molgenis.data.EntityManager)6 Entity (org.molgenis.data.Entity)4 Attribute (org.molgenis.data.meta.model.Attribute)4 EntityType (org.molgenis.data.meta.model.EntityType)4 BeforeMethod (org.testng.annotations.BeforeMethod)3 DataService (org.molgenis.data.DataService)2 DynamicEntity (org.molgenis.data.support.DynamicEntity)2 Test (org.testng.annotations.Test)2 Iterables (com.google.common.collect.Iterables)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 String.format (java.lang.String.format)1 DateTimeParseException (java.time.format.DateTimeParseException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Objects (java.util.Objects)1 Collectors.toList (java.util.stream.Collectors.toList)1 Stream (java.util.stream.Stream)1 StreamSupport.stream (java.util.stream.StreamSupport.stream)1 Fetch (org.molgenis.data.Fetch)1 MolgenisDataException (org.molgenis.data.MolgenisDataException)1