Menu

Possible to ask JETT which beans are used by an excel template?

2014-02-14
2014-03-10
  • Mark Vejvoda

    Mark Vejvoda - 2014-02-14

    Is there a way to pass a list of empty beans to JETT as well as the excel template file and in return get a list of beans + fields in the beans that are used? This would be ideal since we could know before executing the transformation which beans we actually require (And fields) and thus know how much we really need to populate.

    Thanks

     
  • Etienne Canaud

    Etienne Canaud - 2014-03-10

    Hi Mark,

    One issue with this approach is that a template will only call the getter of a bean value if it's actually displayed in the spreadsheet currently rendered. So if you have some bean evaluation logic within a 'if' tag, beans will only get evaluated when the <if> condition is true.
    So in the end, Jett would only be able to reliably tell you which beans will be used... by rendering the template with some accurate data. Empty beans won't do.

    Moreover, the beans access occurs in JExl engine, and I don't think that Jett has much control over what happens in there.

    So I would see two ways to workaround your problem:

    1) Write your own bean wrapper with some code on a single generic getter that would keep track of which beans values are requested by Jett during template rendering (and then return a mocked value, but be careful about returning the right type of mocked data). This approach would be relatively simple, and I also think that if this feature were to be included in Jett it could be implemented this way. FYI, as far as I know, JExl doesn't make any difference between a java bean with a "getValue()" method and a Map containing an entry with key "value". So you don't have to know what's the structure of your beans, you can consider every bean as a Map<String, Object="">.

    2) If populating a bean value is expensive (which is the only reason why you would require such a feature), then I believe that the simplest and more reliable solution is to populate your beans in a lazy way: just populate them at runtime when their getters are invoked by Jett. With this approach you don't have to care about what Jett is using: You are sure that you only populate what's actually required by the template. This is a much safer approach as 1) since it doesn't depend on runtime value for 'if' blocks evaluation.

    Thanks,
    Etienne.

     

    Last edit: Etienne Canaud 2014-03-10

Log in to post a comment.