Re: Best way to use object properties from a List to create Select Items?

2005-09-09 Thread Richard Wallace
This is something I've been fighting with too.  I've just been doing the 
backingBean.getDomainObjectAsSelectItems() kind of method that returns 
either a SelectItem[] or a List.  The biggest thing I don't like about 
that is that the iteration over the collection is done twice, once to 
convert the domain objects into select items and then again to display 
the select items.  It's probably not too big a deal because using a 
 or radio buttons or whatever wouldn't be a good idea for too 
long a list of domain objects, but it's one area that should be more 
optimized.


I looked at using either the TransformedList or the LazyList from 
Commons Collections, but neither seems to do what I want.  
TransformedList only transforms things as they are added to the list not 
as they are gotten.  LazyList doesn't provide anyway to parameterize the 
creation of the elements as they are accessed.  Does anyone know of 
another package out there that provides for transforming elements in a 
List as they are accessed?


I wouldn't think it would be too tough to implement.  You're basically 
wrapping a List with another that has custom get() and iterator() 
methods.  You'd probably want to have something like a caching list that 
would cache modifications.  If sometihng is not in that list you check 
the wrapped list and do the transform and put it in the cache and return 
it.  The hardest part is the iterator(), I'm not at all sure what that 
would look like.  I guess it would basically wrap the two iterators from 
the wrapped list and the cache list.  When a i.next() is called check if 
the cached list iterator has a next, if not check the wrapped list 
iterator and if it has a next then get it, transform it, add it to the 
cached list and return it.  I'm pretty sure you can do this with a list, 
but I'm not sure about other collection types (like Sets, which is what 
Hibernate likes to use so much).  Also, you'd probably want to require 
that the wrapped list be unmodifiable.


Anyways, just some thoughts that I've been kicking around about 
converting collections of domain objects into a list or array of 
SelectItems.


Rich

Martin Marinschek wrote:


I think this is indeed a great idea, Rick!

if you want to provide a new tag, extend from the existing SelectItems
tag - it should be quite easy to get up to speed with this component.

regards,

Martin

On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
 


On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
   


However, as you say, there's always room for
improvement.
 


Yup. And I understand JSF/MyFaces is still relatively young.

I do think a tag to support this would be nice.

As an interim solution I'll probably create a method that returns a
SelectItems array...

SelectItems[] createSelectItemsFromCollection( Collection col, String
valueProp, String labelProp ) {

then using the reflection API, I should be able to return an array of
SelectItems. This method could then be used for *any* Collection
within the application.

   




 





Re: Best way to use object properties from a List to create Select Items?

2005-09-09 Thread Martin Marinschek
I think this is indeed a great idea, Rick!

if you want to provide a new tag, extend from the existing SelectItems
tag - it should be quite easy to get up to speed with this component.

regards,

Martin

On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
> On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
> >  However, as you say, there's always room for
> > improvement.
> 
> Yup. And I understand JSF/MyFaces is still relatively young.
> 
> I do think a tag to support this would be nice.
> 
> As an interim solution I'll probably create a method that returns a
> SelectItems array...
> 
> SelectItems[] createSelectItemsFromCollection( Collection col, String
> valueProp, String labelProp ) {
> 
> then using the reflection API, I should be able to return an array of
> SelectItems. This method could then be used for *any* Collection
> within the application.
> 


-- 

http://www.irian.at
Your JSF powerhouse - 
JSF Trainings in English and German


Re: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Rick Reumann
On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
>  However, as you say, there's always room for
> improvement.

Yup. And I understand JSF/MyFaces is still relatively young.

I do think a tag to support this would be nice.

As an interim solution I'll probably create a method that returns a
SelectItems array...

SelectItems[] createSelectItemsFromCollection( Collection col, String
valueProp, String labelProp ) {

then using the reflection API, I should be able to return an array of
SelectItems. This method could then be used for *any* Collection
within the application.


RE: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread CONNER, BRENDAN \(SBCSI\)
I agree with you that it would be easier if one didn't have to create a
new set of objects.  I guess I have a higher threshold for pain than
some; it never really bothered me.  What I like is the fact that we got
rid of any kind of JSTL logic in all of our pages, so our JSF pages are
much easier to follow than our old Struts pages were (as is our
"behind-the-scenes" code).  However, as you say, there's always room for
improvement.

- Brendan

-Original Message-
From: Rick Reumann [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 08, 2005 1:32 PM
To: MyFaces Discussion; [EMAIL PROTECTED]
Subject: Re: Best way to use object properties from a List to create
Select Items?


Your below is correct (at least from my understanding). My only point
is a developer shouldn't need to do this. EIther a) A tag should exist
that already supports this (similar to using html:options in Struts or
b) It should be easy to create you own selectItems while iterating
over the collection on the front end, similar to what you would do
with JSTL
 
On 9/8/05, Mike Kienenberger <[EMAIL PROTECTED]> wrote:
> I'm pretty sure this is rhetorical question, but in case it isn't:
> 
> public SelectItem[] getSecIndividualItems()
> {
> if (null == secIndividualList)
> {
> secIndividualList =
adminDbDataStore.getSecIndividualList();
> }
> 
> SelectItem secIndividualItems[] = new
> SelectItem[secIndividualList.size()];
> for (int index = 0; index < secIndividualList.size(); ++index)
> {
> SecIndividual secIndividual = (SecIndividual)
> secIndividualList.get(index);
> secIndividualItems[index] = new SelectItem(secIndividual,
> secIndividual.getName());
> }
> 
> return secIndividualItems;
> }
> 
> A better way to do it would be to create an EL function
> 
> public void getSelectItemsForLabelValueList(List list)
> // where list contains object that implement a LabelValue interface
> 
> and register the function.
> 
> 
> On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
> > On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
> > > It's actually pretty easy,
> >
> > What's the easy part? Creating a tag to do it? I agree it's probably
> > easy for a tag to do this, I was just surprised that it's not
already
> > included in MyFaces - it actually should be part of JSF in the
> > selectItems tag imo.
> >
> > > and you don't want to have your back-end
> > > objects tightly coupled to your front-end objects.  Your back-end
> > > objects typically should not have any concept of "selections."
> >
> > How are they coupled? I'm just wanting to use a Collection of
objects
> > for a drop down? Even struts in its earliest stages had a tag to
> > support this. How is my backend coupled with my front end when I
want
> > to make select options? JSF should make things more simple not more
> > difficult. If a method exists to bring me back a Collection of
> > "Regional Vice Presidents" and I want to use that for my select
> > options the famework should be able to handle this. It seems like
with
> > JSF (and MyFaces) there is nothing "out of the box" that deals with
> > this.
> >
> > So how would you handle this scenario...
> >
> > A perfectly good method exists that returns you a Collection of
> > "Persons." Each person has a Name and an ID and possibly a few other
> > fields you don't care about it. How do you go about using this
> > Collection to create your select options? (value is id, and label is
> > name)
> >
> 


-- 
Rick


Re: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Rick Reumann
Your below is correct (at least from my understanding). My only point
is a developer shouldn't need to do this. EIther a) A tag should exist
that already supports this (similar to using html:options in Struts or
b) It should be easy to create you own selectItems while iterating
over the collection on the front end, similar to what you would do
with JSTL
 
On 9/8/05, Mike Kienenberger <[EMAIL PROTECTED]> wrote:
> I'm pretty sure this is rhetorical question, but in case it isn't:
> 
> public SelectItem[] getSecIndividualItems()
> {
> if (null == secIndividualList)
> {
> secIndividualList = adminDbDataStore.getSecIndividualList();
> }
> 
> SelectItem secIndividualItems[] = new
> SelectItem[secIndividualList.size()];
> for (int index = 0; index < secIndividualList.size(); ++index)
> {
> SecIndividual secIndividual = (SecIndividual)
> secIndividualList.get(index);
> secIndividualItems[index] = new SelectItem(secIndividual,
> secIndividual.getName());
> }
> 
> return secIndividualItems;
> }
> 
> A better way to do it would be to create an EL function
> 
> public void getSelectItemsForLabelValueList(List list)
> // where list contains object that implement a LabelValue interface
> 
> and register the function.
> 
> 
> On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
> > On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
> > > It's actually pretty easy,
> >
> > What's the easy part? Creating a tag to do it? I agree it's probably
> > easy for a tag to do this, I was just surprised that it's not already
> > included in MyFaces - it actually should be part of JSF in the
> > selectItems tag imo.
> >
> > > and you don't want to have your back-end
> > > objects tightly coupled to your front-end objects.  Your back-end
> > > objects typically should not have any concept of "selections."
> >
> > How are they coupled? I'm just wanting to use a Collection of objects
> > for a drop down? Even struts in its earliest stages had a tag to
> > support this. How is my backend coupled with my front end when I want
> > to make select options? JSF should make things more simple not more
> > difficult. If a method exists to bring me back a Collection of
> > "Regional Vice Presidents" and I want to use that for my select
> > options the famework should be able to handle this. It seems like with
> > JSF (and MyFaces) there is nothing "out of the box" that deals with
> > this.
> >
> > So how would you handle this scenario...
> >
> > A perfectly good method exists that returns you a Collection of
> > "Persons." Each person has a Name and an ID and possibly a few other
> > fields you don't care about it. How do you go about using this
> > Collection to create your select options? (value is id, and label is
> > name)
> >
> 


-- 
Rick


Re: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Mike Kienenberger
I'm pretty sure this is rhetorical question, but in case it isn't:

public SelectItem[] getSecIndividualItems()
{
if (null == secIndividualList)
{
secIndividualList = adminDbDataStore.getSecIndividualList();
}

SelectItem secIndividualItems[] = new
SelectItem[secIndividualList.size()];
for (int index = 0; index < secIndividualList.size(); ++index)
{
SecIndividual secIndividual = (SecIndividual)
secIndividualList.get(index);
secIndividualItems[index] = new SelectItem(secIndividual,
secIndividual.getName());
}

return secIndividualItems;
}

A better way to do it would be to create an EL function

public void getSelectItemsForLabelValueList(List list)
// where list contains object that implement a LabelValue interface

and register the function.


On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
> On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
> > It's actually pretty easy,
> 
> What's the easy part? Creating a tag to do it? I agree it's probably
> easy for a tag to do this, I was just surprised that it's not already
> included in MyFaces - it actually should be part of JSF in the
> selectItems tag imo.
> 
> > and you don't want to have your back-end
> > objects tightly coupled to your front-end objects.  Your back-end
> > objects typically should not have any concept of "selections."
> 
> How are they coupled? I'm just wanting to use a Collection of objects
> for a drop down? Even struts in its earliest stages had a tag to
> support this. How is my backend coupled with my front end when I want
> to make select options? JSF should make things more simple not more
> difficult. If a method exists to bring me back a Collection of
> "Regional Vice Presidents" and I want to use that for my select
> options the famework should be able to handle this. It seems like with
> JSF (and MyFaces) there is nothing "out of the box" that deals with
> this.
> 
> So how would you handle this scenario...
> 
> A perfectly good method exists that returns you a Collection of
> "Persons." Each person has a Name and an ID and possibly a few other
> fields you don't care about it. How do you go about using this
> Collection to create your select options? (value is id, and label is
> name)
>


Re: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Rick Reumann
On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:
> It's actually pretty easy, 

What's the easy part? Creating a tag to do it? I agree it's probably
easy for a tag to do this, I was just surprised that it's not already
included in MyFaces - it actually should be part of JSF in the
selectItems tag imo.

> and you don't want to have your back-end
> objects tightly coupled to your front-end objects.  Your back-end
> objects typically should not have any concept of "selections."

How are they coupled? I'm just wanting to use a Collection of objects
for a drop down? Even struts in its earliest stages had a tag to
support this. How is my backend coupled with my front end when I want
to make select options? JSF should make things more simple not more
difficult. If a method exists to bring me back a Collection of
"Regional Vice Presidents" and I want to use that for my select
options the famework should be able to handle this. It seems like with
JSF (and MyFaces) there is nothing "out of the box" that deals with
this.

So how would you handle this scenario...

A perfectly good method exists that returns you a Collection of
"Persons." Each person has a Name and an ID and possibly a few other
fields you don't care about it. How do you go about using this
Collection to create your select options? (value is id, and label is
name)


RE: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread CONNER, BRENDAN \(SBCSI\)
It's actually pretty easy, and you don't want to have your back-end
objects tightly coupled to your front-end objects.  Your back-end
objects typically should not have any concept of "selections."

- Brendan

-Original Message-
From: Rick Reumann [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 08, 2005 12:43 PM
To: MyFaces Discussion
Subject: Re: Best way to use object properties from a List to create
Select Items?


On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:

> BTW, the List (or, more generally, the Collection) is assumed to
contain
> SelectItem instances.

Ok, I guess I'll have to make methods to covert my Collections to new
Collections/Maps or probably just convert them to a SelectItem[]
array.

This is one area that I believe will/should need to be addressed
moving forward. Often times Collections are brought back from backend
methods that are already in place and the developers are forced to
use. It's now pretty a much a pain to have to call
dackend.getAnimals(),  backend.getEmployees(),
backend.getCollectionOfFooBars(), only to have to then convert that
Collection into something that can be used nicely by the front end for
select options.

When I finally get up to speed with this stuff, I'll see if I can help
contribute a tag to help.


Re: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Rick Reumann
On 9/8/05, CONNER, BRENDAN (SBCSI) <[EMAIL PROTECTED]> wrote:

> BTW, the List (or, more generally, the Collection) is assumed to contain
> SelectItem instances.

Ok, I guess I'll have to make methods to covert my Collections to new
Collections/Maps or probably just convert them to a SelectItem[]
array.

This is one area that I believe will/should need to be addressed
moving forward. Often times Collections are brought back from backend
methods that are already in place and the developers are forced to
use. It's now pretty a much a pain to have to call
dackend.getAnimals(),  backend.getEmployees(),
backend.getCollectionOfFooBars(), only to have to then convert that
Collection into something that can be used nicely by the front end for
select options.

When I finally get up to speed with this stuff, I'll see if I can help
contribute a tag to help.


RE: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread CONNER, BRENDAN \(SBCSI\)
Actually, a List has one advantage over a Map in that one has control
over the order in which the elements are displayed.

BTW, the List (or, more generally, the Collection) is assumed to contain
SelectItem instances.

- Brendan

-Original Message-
From: Rick Reumann [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 08, 2005 12:21 PM
To: MyFaces Discussion
Subject: Best way to use object properties from a List to create Select
Items?


What's the best way in JSF/MyFaces to use a List to create a select
options list? I know the SelectItems can be built from a Map, but it
doesn't seem to support a list of objects too well.  Can I at least
set them easily looping over my collection manually?

For example in JSTL.. I can creat my options ...


${emp.name}
 

Can I do this with a dataList inbetween   tags?

It would be cool if the selectItems tag supported:



(above would loop over your collection of employees pull out Employee
and call appropriate getters - getName getEmpID )

-- 
Rick


Re: Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Mike Kienenberger
Probably the cleanest solution would be to define an EL function that
converted your Lists into maps.

Creating EL expressions is still ugly under pure Myfaces, however.

On 9/8/05, Rick Reumann <[EMAIL PROTECTED]> wrote:
> What's the best way in JSF/MyFaces to use a List to create a select
> options list? I know the SelectItems can be built from a Map, but it
> doesn't seem to support a list of objects too well.  Can I at least
> set them easily looping over my collection manually?
> 
> For example in JSTL.. I can creat my options ...
> 
> 
> ${emp.name}
> 
> 
> Can I do this with a dataList inbetween   tags?
> 
> It would be cool if the selectItems tag supported:
> 
> 
> 
> (above would loop over your collection of employees pull out Employee
> and call appropriate getters - getName getEmpID )
> 
> --
> Rick
>


Best way to use object properties from a List to create Select Items?

2005-09-08 Thread Rick Reumann
What's the best way in JSF/MyFaces to use a List to create a select
options list? I know the SelectItems can be built from a Map, but it
doesn't seem to support a list of objects too well.  Can I at least
set them easily looping over my collection manually?

For example in JSTL.. I can creat my options ...


${emp.name}
 

Can I do this with a dataList inbetween   tags?

It would be cool if the selectItems tag supported:



(above would loop over your collection of employees pull out Employee
and call appropriate getters - getName getEmpID )

-- 
Rick