PDA

View Full Version : [FIXED] GXT 1.2.3 Mixed subclasses on BeanModelReader bug



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");
}
}

sven
1 Apr 2009, 2:17 PM
I just commited an update. You can now force beanmodelreader to get a factory for each bean. Change is in SVN. This is disabled by default because of performance.

sdc
2 Apr 2009, 1:30 AM
I don't find it intuitive. Without reading the javadoc, I would expect that beans can be of different types (by default).

Anyway, the javadoc still states :

* Valid return types are a list of beans or a list load result containing a
* list of beans. Beans must be of the same type

It should says that beans can be of different types if and only if you set factoryForEachBean to true.

sven
2 Apr 2009, 3:41 AM
That is why there are javadocs. I modified them to show the new feature.

Making that the default action would be bad for the performance. In IE it is up to 70% slower. FF only 20%.