For a generic solution (framework), it seems this wrapper class would
come in very handy.  Pretty cool how it creates a composition of a bean
instantly.  

If it is a user solution to one problem I would think you would want to
go with a potentially more maintainable solution of extending the Foo
class and adding the methods that bean utils will use. With something
used for struts and then use that on the form/jsp.

So basically add a class FooBarStruts extends FooBar
And there add the getBar(int index) method which you can lazily
instantiate the colletion.

Since it extends FooBar, you can use it to call your business methods
and they would be none the wiser.  I say more maintainable just because
it reduces the dependencies on external libs.

Just a thought.

Regards...djsuarez


-----Original Message-----
From: Niall Pemberton [mailto:[EMAIL PROTECTED] 
Sent: Thursday, September 16, 2004 2:16 AM
To: Struts Users Mailing List
Subject: Re: Think I'm stuck with using Session scope for this..unless a
better idea?

Sorry, should have read the whole thread - I missed the fact you don't
have
control of FooBar.

You're probably right, session scope is probably the easiest, although
you
could do this by creating a FooBarWrapper using the WrapDynaBean (needs
BeanUtils 1.7.0 to use the getInstance() method).

The following class should work:

   a) create warpper for existing FooBar
            FooBarWrapper wrapper = new FooBarWrapper(fooBar);

   b) Retrieve FooBar from populated wrapper and clean up
           FooBar fooBar = (FooBar)wrapper.getInstance();:
           wrapper.clear();



import java.util.List;
import java.util.Collection;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.commons.beanutils.WrapDynaBean;

public class FooBarWrapper extends WrapDynaBean {
  private List wrapperList = new ArrayList();
  public FooBarWrapper() {
    super(new FooBar());
  }
  public FooBarWrapper(FooBar fooBar) {
    super(fooBar);
    Collection fooBars = fooBar.getFooBars();
    if (fooBars != null) {
      Iterator iterator = fooBars.iterator();
      while (iterator.hasNext()) {
        wrapperList.add(new FooBarWrapper((FooBar)iterator.next()));
      }
    }
  }
  public Object get(String name) {
    if ("fooBars".equals(name)) {
      return wrapperList;
    }
    return super.get(name);
  }

  public Object get(String name, int index) {
    if ("fooBars".equals(name)) {
      if (wrapperList.size() <= index) {
        while (wrapperList.size() <= index) {
          FooBarWrapper childWrapper = new FooBarWrapper();
          wrapperList.add(childWrapper);
          FooBar childFooBar = (FooBar)childWrapper.getInstance();
          FooBar fooBar = (FooBar)getInstance();
          if (fooBar.getFooBars() == null) {
            fooBar.setFooBars(new ArrayList());
          }
          fooBar.getFooBars().add(childFooBar);
        }
      }
      return wrapperList.get(index);
    }
    return super.get(name, index);
  }

  public void clear() {
    for (int i = 0; i < wrapperList.size(); i++) {
      FooBarWrapper wrapper = (FooBarWrapper)wrapperList.get(i);
      wrapper.clear();
    }
    instance = null;
    wrapperList.clear();
    wrapperList = null;
  }
  public String toString() {
    return getInstance().toString();
  }
}
----- Original Message ----- 
From: "Rick Reumann" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <[EMAIL PROTECTED]>
Sent: Thursday, September 16, 2004 5:45 AM
Subject: Re: Think I'm stuck with using Session scope for this..unless a
better idea?


> Niall Pemberton wrote the following on 9/16/2004 12:41 AM:
>
> > "I don't see how this is going to work though. The problem is I'm
getting
> > back an Object (the FooBar one) from the back end that has the
nested
> > collections inside. This isn't a Struts object (not of type
ActionForm)."
>
> I guess what meant was the object I'm dealing with is "handed" to me.
> It's not an object that I have that much control of since it's coming
> from the business layer (not coded by me) so not sure I can say "start
> implmenting lazyList stuff in there.
>
> >
> > Hadn't really been following this thread, but isn't the trick to
getting
> > round the BeanUtils issue all in the getFooBar(index) method. I
don't
use
> > LazyList, but if it was an array - wouldn't something like the
following
> > work
> >
> > private FooBar[] fooBars;
> >
> > public FooBar getFooBar(int index) {
>
> The problem is there are not getXXX(int index) methods in the business
> objects. The initial post mentions I'm stuffing one object into my
form:
>
> class MyForm extends ActionForm {
>     private FooBarValueObject fooBar;
>     //get/set fooBar
> }
>
> FooBarValueObject has a few properties but the main one of interest
being:
> Collection fooBars;
>
> Which end up being Collections of other FooBarValueObjects.
>
> It's not a big deal. I'm just going to use Session scope. If
performance
> becomes an issue I'll work on some listner thing that Michael talked
> about that cleans it up. (I've actually yet run into a case where
> keepign stuff in a User's Session caused serious memory problems...
then
> again I'm not working on Amazon.com or anything:)
>
> >
> >     if (fooBars == null) {
> >         fooBars = new FooBar[index];
> >     }
> >
> >     if (fooBars.length <= index) {
> >         FooBar[] newFooBars = new FooBar[index + 1];
> >         System.arraycopy(foobars, 0, newFooBars, 0, fooBars.length);
> >         fooBars = newFooBars;
> >     }
> >
> >     if (fooBars[index] == null) {
> >         fooBars[index] = new FooBar();
> >     }
> >
> >     return fooBars[index];
> >
> > }
>
> -- 
> Rick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
>






---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to