aspose file tools*
The moose likes JSF and the fly likes EL: Iterate over Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "EL: Iterate over "sub" entities (Hibernate, Facelets)?" Watch "EL: Iterate over "sub" entities (Hibernate, Facelets)?" New topic
Author

EL: Iterate over "sub" entities (Hibernate, Facelets)?

Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Hello,

I have a Game class which represents a (sports) game DB table:
A game has two associated scores, one for the home team and another for the away team. Each score has a boolean isHome, denoting the type.

Now I'm looking for a way to pseudo-iterate over both scores to display their individual player stats and team totals. I was wondering if it was possible to retrieve both scores from the game entity/object, iterate over both scores and display only the data for home or away. Example for home:
Note, that I'm using Seam, Hibernate, and Facelets. The EL expression gameHome.instance returns the game instance of the game at hand (current page). However, I get an exception when iterating over the two scores:


Obviously JSF EL can't iterate over an instance of PersistentSet, but how do I do it without an extra query to get the two associated scores? Is it possible at all to use the scores collection?

Thanks
Karsten


OCJP JavaSE 6 (86%)
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

That's basically putting logic into the View and since JSF is about as pure an MVC implementation as you can get in an HTTP environment, not the preferred way of doing things. Also, I don't recommend raw HTML in a JSF View definition either.

A better approach is usually to simply use the JSF dataTable in conjunction with a suitable dataModel. Which comes out more like this:



You'll notice I didn't put the "isHome" render qualification there, since I would have presumably built my dataModel to include only those rows that were home games. Like I said, keep the logic out of the View.

Incidentally, the "isHome" failed because the "is" is implicit in EL JavaBean access of boolean properties. So it's just "scr,.home".


Customer surveys are for companies who didn't pay proper attention to begin with.
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Thanks so much for the comments made. They helped me a lot.

I indeed tried to get the same query (data model) to work for the home and the away teams separately, but I somehow failed how I would pass the true/false values to the Seam EntityQuery component prior to execution. Maybe I should just use a simple setter which then adjusts the JPQL appropriately. Is that the way to do it, that is set isHome = true once and then set isHome = false to get the stat lines? Is it possible at all? Or should I hardwire home and away into separate query objects (components)?

Can you recommend anything here out-of-the-box?

Karsten

PS: it's my first time using JSF/Facelets (and Seam) so my questions might seem a little naive
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

I'm afraid my Seam book is still sitting on the shelf, mostly unread. Some day...

In general (non-Seam) terms, you have 2 options:

1. Make the original database query with filtering done as part of the query. "home/away" can be a query parameter if that works for you.

2. Make a general query and filter the results when you build the dataModel, selecting only the home (or away) rows from the query results collection and adding only them to the datamodel's wrapped data list, ignoring the others. For small result sets, this may work just as well. And for cases where you want all the data, but in different locations, you can actually be more efficient, since only one query has to be made and the rest of the work is done locally.

As far as I know, this approach should work just as well in Seam as it would in non-Seam apps.
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Thanks, I got it to work with dataTable now in real JSF style. I like it.

The only problem I'm facing now is:

How do I add custom markup to certain cells? For example, the last line of the query is the "total" line, so it may contain only the sum if the columns are numeric. Names, jersey numbers, etc. must not be displayed for the total line. Also, I want to show certain names in bold or italic (strong or em tag) or use a special class="hatch" to show diagonal lines for n.a.

What's the technique in JSF? (it'll be my last question)

Karsten
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

One of the easiest ways to have custom markup for a total line is to put the total in the dataTable footer and use footer markups. For more complex displays, such as tables with sub-totals, there are extended datatable tags available, such as the richFaces tag named (originally enough ) extendedDataTable.

conditional Markup per-cell means basically defining CSS for the cell element itself. One way to do this is to realize that the dataModel doesn't have to be directly bound to the domain model. You can create a decorator collection which adds methods to return the desired style classnames in addition to the domain data.
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Tim Holloway wrote:One of the easiest ways to have custom markup for a total line is to put the total in the dataTable footer and use footer markups.


How do I add custom markup to the footer facet? My f:facet name="footer" will always create a table row (tr tag) plus an automatic table data cell (td tag) with a colspan="# of columns", so any tags I add are placed outside that colspan cell. Is there a way to replace/suppress the td colspan tag?

Karsten
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16250
    
  21

You can't set style info on header or footer facets. You set it on the dataTable using the headerClass and footerClass attributes.

There are 2 types of footers. Column footers and table footers. Column footers are facets defined within the column definition. Table footers are defined outside of the column definitions within the table definition. The table footer (and header) spans the table, whereas the column header and footer facets apply to a single column.

DataTables have no explicit cell definition elements. So you have to set the cell style based on the intersection of the row and column classes. About the closest thing to a true cell style would be if you defined nested a 1x1 element panelGrid for a cell element and specified its width to be 100%. That actually generates a table within a table in HTML terms.

Yes, this stuff can be a bit awkward, but it does work. Although for the less patient, there are also extension tagsets that make things a bit more like the original HTML table definition.
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Tim Holloway wrote:You can't set style info on header or footer facets. You set it on the dataTable using the headerClass and footerClass attributes.

There are 2 types of footers. Column footers and table footers. Column footers are facets defined within the column definition. Table footers are defined outside of the column definitions within the table definition. The table footer (and header) spans the table, whereas the column header and footer facets apply to a single column.


Thanks, I had found that out in the meantime. I wonder now, because it is possible to declare these two footer types in table and column scope, how to set their style properties individually. Can it be done?

Per-column footers actually implement what I was looking for, but for whatever reason the styles for columnClasses="..." aren't applied to column footers. I need to right-align most (but not all) of them and there's nothing I can do about it but to right align all column footers by using h:dataTable footerClass="right". This currently just suffices as a solution, but I can foresee a situation where I will need individual column footer styles, so this could be a dataTable request for enhancement: columnFooterClasses="..." and columnHeaderClasses="..."?

Maybe I need to have a look at RichFaces or similar.

Thanks again
Karsten
 
jQuery in Action, 2nd edition
 
subject: EL: Iterate over "sub" entities (Hibernate, Facelets)?