final DomainObjectContainer container) throws IOException, InvalidFormatException {
final List<T> viewModels = Lists.newArrayList();
final ObjectSpecification objectSpec = specificationLoader.loadSpecification(cls);
final ViewModelFacet viewModelFacet = objectSpec.getFacet(ViewModelFacet.class);
if(viewModelFacet == null) {
throw new IllegalArgumentException("Class '" + objectSpec.getCssClass() + "' is not a view model");
}
final ByteArrayInputStream bais = new ByteArrayInputStream(bs);
final Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(bais);
final CellMarshaller cellMarshaller = newCellMarshaller(wb);
final Sheet sheet = wb.getSheetAt(0);
boolean header = true;
final Map<Integer, Property> propertyByColumn = Maps.newHashMap();
for(final Row row: sheet) {
if(header) {
for(final Cell cell: row) {
int columnIndex = cell.getColumnIndex();
final String propertyName = cellMarshaller.getStringCellValue(cell);
final OneToOneAssociation property = getAssociation(objectSpec, propertyName);
if(property != null) {
final Class<?> propertyType = property.getSpecification().getCorrespondingClass();
propertyByColumn.put(columnIndex, new Property(propertyName, property, propertyType));
}
}
header = false;
} else {
// detail
// copy the row into the template object
final T template = container.newTransientInstance(cls);
final ObjectAdapter templateAdapter = adapterManager.adapterFor(template);
for(final Cell cell: row) {
int columnIndex = cell.getColumnIndex();
final Property property = propertyByColumn.get(columnIndex);
if(property != null) {
final OneToOneAssociation otoa = property.getOneToOneAssociation();
final Object value = cellMarshaller.getCellValue(cell, otoa);
if(value != null) {
final ObjectAdapter valueAdapter = adapterManager.adapterFor(value);
otoa.set(templateAdapter, valueAdapter);
}
} else {
// not expected; just ignore.
}
}
final String memento = viewModelFacet.memento(template);
final T viewModel = container.newViewModelInstance(cls, memento);
viewModels.add(viewModel);
}
}