si_gee2000
27 Mar 2009, 10:13 AM
Hi -
If you use the BeanModelReader (often in conjunction with a Paging Loader) and you use a common class as the generic type, but then pass in several different subclasses in the list of returned results, the BeanModelReader will fail as it only looks at the class of the first bean in the collection. I have got round this by implementing a Modified subclass to override the behaviour. I suggest that this replaces the current one in a forward release:
public class ModifiedBeanModelReader<C> extends BeanModelReader<C> {
public ListLoadResult<ModelData> read(C loadConfig, Object data) {
if (data instanceof List) {
List<Object> beans = (List) data;
if (beans.size() > 0) {
List models= new ArrayList(beans.size());
for(Object o : beans) {
BeanModelFactory factory = BeanModelLookup.get().getFactory(o.getClass());
if (factory == null) {
throw new RuntimeException("No BeanModelFactory found for " + o.getClass());
}
models.add(factory.createModel(o));
}
return newLoadResult(loadConfig, models);
}
return newLoadResult(loadConfig, (List) beans);
} else if (data instanceof ListLoadResult) {
ListLoadResult result = (ListLoadResult) data;
List beans = result.getData();
if (beans.size() > 0) {
List models= new ArrayList(beans.size());
for(Object o : beans) {
BeanModelFactory factory = BeanModelLookup.get().getFactory(o.getClass());
if (factory == null) {
throw new RuntimeException("No BeanModelFactory found for " + o.getClass());
}
models.add(factory.createModel(o));
}
beans.clear();
beans.addAll(models);
}
return (ListLoadResult) data;
} else {
throw new RuntimeException("Error converting data");
}
}
If you use the BeanModelReader (often in conjunction with a Paging Loader) and you use a common class as the generic type, but then pass in several different subclasses in the list of returned results, the BeanModelReader will fail as it only looks at the class of the first bean in the collection. I have got round this by implementing a Modified subclass to override the behaviour. I suggest that this replaces the current one in a forward release:
public class ModifiedBeanModelReader<C> extends BeanModelReader<C> {
public ListLoadResult<ModelData> read(C loadConfig, Object data) {
if (data instanceof List) {
List<Object> beans = (List) data;
if (beans.size() > 0) {
List models= new ArrayList(beans.size());
for(Object o : beans) {
BeanModelFactory factory = BeanModelLookup.get().getFactory(o.getClass());
if (factory == null) {
throw new RuntimeException("No BeanModelFactory found for " + o.getClass());
}
models.add(factory.createModel(o));
}
return newLoadResult(loadConfig, models);
}
return newLoadResult(loadConfig, (List) beans);
} else if (data instanceof ListLoadResult) {
ListLoadResult result = (ListLoadResult) data;
List beans = result.getData();
if (beans.size() > 0) {
List models= new ArrayList(beans.size());
for(Object o : beans) {
BeanModelFactory factory = BeanModelLookup.get().getFactory(o.getClass());
if (factory == null) {
throw new RuntimeException("No BeanModelFactory found for " + o.getClass());
}
models.add(factory.createModel(o));
}
beans.clear();
beans.addAll(models);
}
return (ListLoadResult) data;
} else {
throw new RuntimeException("Error converting data");
}
}