use of org.molgenis.data.Entity in project molgenis by molgenis.
the class EntityHydration method getValueBasedOnType.
private static Object getValueBasedOnType(Entity entity, String name, AttributeType type) {
Object value;
switch(type) {
case CATEGORICAL:
case FILE:
case XREF:
Entity xrefEntity = entity.getEntity(name);
value = xrefEntity != null ? xrefEntity.getIdValue() : null;
break;
case CATEGORICAL_MREF:
case MREF:
case ONE_TO_MANY:
List<Object> mrefIdentifiers = newArrayList();
entity.getEntities(name).forEach(mrefEntity -> {
if (mrefEntity != null)
mrefIdentifiers.add(mrefEntity.getIdValue());
});
value = mrefIdentifiers;
break;
case DATE:
value = entity.getLocalDate(name);
break;
case DATE_TIME:
value = entity.getInstant(name);
break;
case BOOL:
case DECIMAL:
case EMAIL:
case ENUM:
case HTML:
case HYPERLINK:
case INT:
case LONG:
case SCRIPT:
case STRING:
case TEXT:
value = entity.get(name);
break;
case COMPOUND:
throw new RuntimeException(format("Illegal attribute type [%s]", type.toString()));
default:
throw new UnexpectedEnumException(type);
}
LOG.trace("Dehydrating attribute '{}' of type [{}] resulted in value: {}", name, type.toString(), value);
return value;
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class L1CacheTest method testEvictStream.
@Test
public void testEvictStream() {
// Start transaction
l1Cache.transactionStarted(transactionID);
// Entity has been added to cache, return entity
l1Cache.put(repository, entity1);
l1Cache.put(repository, entity2);
Entity actualEntity = l1Cache.get(repository, entityID1, entityType).get();
assertTrue(EntityUtils.equals(actualEntity, entity1));
actualEntity = l1Cache.get(repository, entityID2, entityType).get();
assertTrue(EntityUtils.equals(actualEntity, entity2));
l1Cache.evict(Stream.of(EntityKey.create(entity1), EntityKey.create(entity2)));
Optional<Entity> result = l1Cache.get(repository, entityID1, entityType);
assertEquals(result, null);
result = l1Cache.get(repository, entityID2, entityType);
assertEquals(result, null);
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class L1CacheTest method testEvictStreamOfOneEntity.
@Test
public void testEvictStreamOfOneEntity() {
// Start transaction
l1Cache.transactionStarted(transactionID);
// Entity has been added to cache, return entity
l1Cache.put(repository, entity1);
l1Cache.put(repository, entity2);
Entity actualEntity = l1Cache.get(repository, entityID1, entityType).get();
assertTrue(EntityUtils.equals(actualEntity, entity1));
actualEntity = l1Cache.get(repository, entityID2, entityType).get();
assertTrue(EntityUtils.equals(actualEntity, entity2));
l1Cache.evict(Stream.of(EntityKey.create(entity2)));
actualEntity = l1Cache.get(repository, entityID1, entityType).get();
assertTrue(EntityUtils.equals(actualEntity, entity1));
Optional<Entity> result = l1Cache.get(repository, entityID2, entityType);
assertEquals(result, null);
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class CsvWriterTest method addCellProcessor_data.
@Test
public void addCellProcessor_data() throws IOException {
CellProcessor processor = when(mock(CellProcessor.class).processData()).thenReturn(true).getMock();
Entity entity = new DynamicEntity(entityType);
entity.set("col1", "val1");
entity.set("col2", "val2");
try (CsvWriter csvWriter = new CsvWriter(new StringWriter())) {
csvWriter.addCellProcessor(processor);
csvWriter.writeAttributeNames(Arrays.asList("col1", "col2"));
csvWriter.add(entity);
}
verify(processor).process("val1");
verify(processor).process("val2");
}
use of org.molgenis.data.Entity in project molgenis by molgenis.
the class CsvWriterTest method testLabels.
@Test
public void testLabels() throws IOException {
StringWriter strWriter = new StringWriter();
try (CsvWriter csvWriter = new CsvWriter(strWriter)) {
csvWriter.writeAttributes(Arrays.asList("col1", "col2"), Arrays.asList("label1", "label2"));
Entity entity = new DynamicEntity(entityType);
entity.set("col1", "val1");
entity.set("col2", "val2");
csvWriter.add(entity);
assertEquals(strWriter.toString(), "\"label1\",\"label2\"\n\"val1\",\"val2\"\n");
}
}
Aggregations