You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Nicolas Bouillon <ni...@bouillon.net> on 2015/05/11 10:51:21 UTC

Grid mixin: grid datasource get instancied on each use

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

Re: Grid mixin: grid datasource get instancied on each use

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Wed, 20 May 2015 10:11:34 -0300, Lance Java <la...@googlemail.com>  
wrote:

> As you've discovered... Tapestry replaces field access with Binding.get()
> using plastic byte code magic. You can use a local variable to avoid
> multiple gets.

Or annotate the getter with @Cached. :)

>
> Eg:
>
> @BindParameter
> private GridDataSource source;
>
> ...
>
> GridDataSource sourceCopy = source;
> doStuff(sourceCopy);
> doMoreStuff(sourceCopy);


-- 
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Grid mixin: grid datasource get instancied on each use

Posted by Lance Java <la...@googlemail.com>.
As you've discovered... Tapestry replaces field access with Binding.get()
using plastic byte code magic. You can use a local variable to avoid
multiple gets.

Eg:

@BindParameter
private GridDataSource source;

...

GridDataSource sourceCopy = source;
doStuff(sourceCopy);
doMoreStuff(sourceCopy);

Re: Grid mixin: grid datasource get instancied on each use

Posted by Nicolas Bouillon <ni...@bouillon.net>.
2015-05-20 11:08 GMT+02:00 Lance Java <la...@googlemail.com>:

> The initial render of the grid occurs on a different request to the
> "export" event. Tapestry does not maintain state between requests (unless
> you explicitly @Persist which i wouldn't recommend).
>

Of course, that is well understood. The grid is not (and should not) be
rendered when my export event is called. Persisting would also make no
sense because the html grid display only a part of the grid data source
(pagninated), while the export event intent to fetch the whole
GridDataSource.


> Therefore it's perfectly reasonable for the GridDataSource to be invoked
> once for the initial html render and again for the export.
>

Let split the problem into 2 different issues i've got :

1.
The field "@BindParameter GridDataSource source" trigger a build of the
GridDataSource object each time I use the field "source". A method
generated by Tapestry named "conduit_get_source" is called, which in turns
call the getter for the GridDataSource in the template, which in my code
creates a new GridDataSource. That's probably my bad, because the method
"getDataSource" is not a pure getter, but constructs a new instance each
time. Fixing that (with @Cached) resolve the issue of my initial post
(without using a custom CachingDataSource)

2.
The field "@InjectContainer Grid grid", by the same mecanism, was calling
the getDataSource method of my page, which constructed the GridDataSource.
The @Cached annotation fixed that too.

So everything works fine now.

Its still not possible to use the "GridDataSource source" field of the Grid
in this case, because this field is not visible from outside, except if the
field "org.apache.tapestry5.corelib.components.Grid#cachingSource" is set.
And to my understanding of the Grid source code, there is no way to
initialize the "cachingSource" field, except by the "setupRender" event on
the Grid.

Thank you for your answer, you had making me think more about my issue and
digging more further into it to find the root cause.

Re: Grid mixin: grid datasource get instancied on each use

Posted by Lance Java <la...@googlemail.com>.
The initial render of the grid occurs on a different request to the
"export" event. Tapestry does not maintain state between requests (unless
you explicitly @Persist which i wouldn't recommend).

Therefore it's perfectly reasonable for the GridDataSource to be invoked
once for the initial html render and again for the export.

Re: Grid mixin: grid datasource get instancied on each use

Posted by Nicolas Bouillon <ni...@bouillon.net>.
Following  your advice, i've replicated the class CachingDataSource (from
Grid class) inside my mixin, and created an instance of it using the
BindParameter GridDataSource.

@BindParameter
private GridDataSource source;

@OnEvent(value = EXPORT_EVENT)
public StreamResponse export() {
    CachingDataSource cache = new CachingDataSource(source);
    int availableRows = cache.getAvailableRows();
    cache.prepare(0, availableRows, Collections.emptyList());
BeanModel gridDataModel = grid.getDataModel();

(...)


This way it works.

It's still quite odd : the grid.getDataModel() call triggers the
constructor of my GridDataSource. I can see in the debugger that
inside the "grid" field, there is a "source" field defined and not
null. But when I call the getSource() method, I get a null value,
because this getter returns the field "cachingSource" of the grid
which is null. This field seems to be initialized on the setupRender
phase, which is obviously not executed in my case (an event handler).
The Grid#setupRender method is package private, so I cannot call it to
fix that (or may I, dispatching an event to the grid ?)


Thanks.


2015-05-20 8:44 GMT+02:00 Nicolas Bouillon <ni...@bouillon.net>:

> Thank you for your response.
>
> But grid.getDataSource() returns null. I guess it's because I'm in a event
> handler method (grid.getDataSource() is not null in the AfterRenderTemplate
> method).
>
>
>
> 2015-05-15 10:49 GMT+02:00 Lance Java <la...@googlemail.com>:
>
>> I'm not entirely sure the problem but it's likely caused by @BindParameter
>> which is likely invoking the binding each access.
>>
>> Try
>>
>> @InjectContainer Grid grid;
>>
>> And grid.getDataSource().
>>
>> This will give you an instance of CachingDataSource which should have the
>> lazy behaviour you want.
>>
>
>

Re: Grid mixin: grid datasource get instancied on each use

Posted by Nicolas Bouillon <ni...@bouillon.net>.
Thank you for your response.

But grid.getDataSource() returns null. I guess it's because I'm in a event
handler method (grid.getDataSource() is not null in the AfterRenderTemplate
method).



2015-05-15 10:49 GMT+02:00 Lance Java <la...@googlemail.com>:

> I'm not entirely sure the problem but it's likely caused by @BindParameter
> which is likely invoking the binding each access.
>
> Try
>
> @InjectContainer Grid grid;
>
> And grid.getDataSource().
>
> This will give you an instance of CachingDataSource which should have the
> lazy behaviour you want.
>

Re: Grid mixin: grid datasource get instancied on each use

Posted by Lance Java <la...@googlemail.com>.
I'm not entirely sure the problem but it's likely caused by @BindParameter
which is likely invoking the binding each access.

Try

@InjectContainer Grid grid;

And grid.getDataSource().

This will give you an instance of CachingDataSource which should have the
lazy behaviour you want.