Hi,

I'm trying to write a export file mixin for the Grid component and I got a
strange behavior on the GridDataSource value.

My code is quite simple for now :

public class Exportable {

    public static final String EXPORT_EVENT = "EXPORT";

    @InjectContainer
    private Grid grid;

    @BindParameter
    private GridDataSource source;

    @Inject
    private ComponentResources componentResources;

    @AfterRenderTemplate
    public void before(MarkupWriter writer) {

        writer.element("div", "class", "row");
        {
            writer.element("div", "class", "pull-right");
            {
                String export =
componentResources.createEventLink(EXPORT_EVENT).toAbsoluteURI();
                writer.element("a", "href", export);
                {
                    writer.element("span", "class", "glyphicon
glyphicon-download");
                    {
                        writer.write(" ");
                    }
                    writer.end();
                }
                writer.end();
            }
            writer.end();
        }
        writer.end();
    }

    @OnEvent(value = EXPORT_EVENT)
    public StreamResponse export() {

        int availableRows = source.getAvailableRows();
        source.prepare(0, availableRows, Collections.emptyList());

        StringBuilder result = new StringBuilder();
        for (int row = 0; row < availableRows; row++) {
            Object rowObject = source.getRowValue(row);

            // get each column value and print it to the result
            BeanModel gridDataModel = grid.getDataModel();
            List<String> propertyNames = gridDataModel.getPropertyNames();
            for (String propertyName : propertyNames) {
                PropertyModel propertyModel = gridDataModel.get(propertyName);
                PropertyConduit conduit = propertyModel.getConduit();
                if (conduit != null) {
                    Object columnValue = conduit.get(rowObject);
                    result.append(columnValue == null ? "" :
columnValue.toString()).append(";");
                }
            }
            result.append("\n");

        }
        return new TextStreamResponse("text/plain", result.toString());
    }


}


The problem is on export code, each time I access the "source"
variable, the constructor of the underlying implementation of the
gridDataSource get called, and I get another instance. For a simple
CollectionGridDataSource it kinda works (because I've not sort, but
any sort set in "prepare" would be lost), but for my
"QueryDslGridDataSource", the prepare function is supposed to cache
the result, and this result is lost, because when calling
"source.getRowValue", Tapestry create a new instance of my
QueryDslGridDataSource".

Any hint on how to do it correctly?

Thanks

Reply via email to