You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Bruce Alspaugh <co...@gmail.com> on 2006/03/20 00:02:02 UTC

[beanutils] Mixing static and dynamic properties in the same DynaBean

Newbie here.  Is it possible to define a DynaBean that has both dynamic
properties that are lazily added at runtime as well as static properties
that are defined at compile time?

For example, I would like to be able to do something like this:

    DynaBean order = new LazyDynaBean()  {
        public double getTotal() {
            double price = ((Double)get("price")).doubleValue();
            double quantity = ((Double)get("quantity")).doubleValue();
            return price * quantity;
        }
    };

And then access the properties this way:

    PropertyUtils.setSimpleProperty(order, "price", Double.valueOf(5.0));
    System.out.println("Price: " +
            PropertyUtils.getSimpleProperty(order, "price"));

    PropertyUtils.setSimpleProperty(order, "quantity", Double.valueOf(10.0
));
    System.out.println("Quantity: " +
            PropertyUtils.getSimpleProperty(order, "quantity"));

    System.out.println("Total: " +
            PropertyUtils.getSimpleProperty(order, "total"));

The output is:

Price:  5.0
Quantity:  10.0
Total: null

How do I get BeanUtils to use my getTotal() method to output the product of
price times quantity?

Ultimately, I would like to get the dynamic properties from a JDBC result
set, and use the statically defined properties to compute totals and the
like for the dynamic properties.

- Bruce

Re: [beanutils] Mixing static and dynamic properties in the same DynaBean

Posted by Bruce Alspaugh <co...@gmail.com>.
Could this be done using a custom DynaBean implementation?  The WrapDynaBean 
lets you call methods defined at compile time by wrapping a traditional 
JavaBean.  What I would need is a DynaBean that effectively combines the 
functionality of a LazyDynaBean for the dynamic properties and a 
WrapDynaBean around itself to find the "static" properties.  The get and set 
methods would search both sets of properties to find the one they need.

Bruce

On 3/20/2006, Niall Pemberton <ni...@gmail.com> wrote:
>
> On 3/19/06, Bruce Alspaugh <co...@gmail.com> wrote:
> > Newbie here.  Is it possible to define a DynaBean that has both dynamic
> > properties that are lazily added at runtime as well as static properties
> > that are defined at compile time?
>
> Its possible to define it that way, but the rest of BeanUtils won't
> recoginze the "static" properties. So effectively the answer is no. To
> achieve this using DynaBeans you would need something that caculates
> these values and stores them in the DynaBean as actual values.
>
> Niall
>
> > For example, I would like to be able to do something like this:
> >
> >    DynaBean order = new LazyDynaBean()  {
> >        public double getTotal() {
> >            double price = ((Double)get("price")).doubleValue();
> >            double quantity = ((Double)get("quantity")).doubleValue();
> >            return price * quantity;
> >        }
> >    };
> >
> > And then access the properties this way:
> >
> >    PropertyUtils.setSimpleProperty(order, "price", Double.valueOf(5.0));
> >    System.out.println("Price: " +
> >            PropertyUtils.getSimpleProperty(order, "price"));
> >
> >    PropertyUtils.setSimpleProperty(order, "quantity", 
> > Double.valueOf(10.0
> > ));
> >    System.out.println("Quantity: " +
> >            PropertyUtils.getSimpleProperty(order, "quantity"));
> >
> >    System.out.println("Total: " +
> >            PropertyUtils.getSimpleProperty(order, "total"));
> >
> > The output is:
> >
> > Price:  5.0
> Quantity:  10.0
> > Total: null
> >
> > How do I get BeanUtils to use my getTotal() method to output the product 
> > of
> > price times quantity?
> >
> > Ultimately, I would like to get the dynamic properties from a JDBC 
> > result
> > set, and use the statically defined properties to compute totals and the
> > like for the dynamic properties.
> >
> > - Bruce


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [beanutils] Mixing static and dynamic properties in the same DynaBean

Posted by Niall Pemberton <ni...@gmail.com>.
On 3/19/06, Bruce Alspaugh <co...@gmail.com> wrote:
> Newbie here.  Is it possible to define a DynaBean that has both dynamic
> properties that are lazily added at runtime as well as static properties
> that are defined at compile time?

Its possible to define it that way, but the rest of BeanUtils won't
recoginze the "static" properties. So effectively the answer is no. To
achieve this using DynaBeans you would need something that caculates
these values and stores them in the DynaBean as actual values.

Niall

> For example, I would like to be able to do something like this:
>
>    DynaBean order = new LazyDynaBean()  {
>        public double getTotal() {
>            double price = ((Double)get("price")).doubleValue();
>            double quantity = ((Double)get("quantity")).doubleValue();
>            return price * quantity;
>        }
>    };
>
> And then access the properties this way:
>
>    PropertyUtils.setSimpleProperty(order, "price", Double.valueOf(5.0));
>    System.out.println("Price: " +
>            PropertyUtils.getSimpleProperty(order, "price"));
>
>    PropertyUtils.setSimpleProperty(order, "quantity", Double.valueOf(10.0
> ));
>    System.out.println("Quantity: " +
>            PropertyUtils.getSimpleProperty(order, "quantity"));
>
>    System.out.println("Total: " +
>            PropertyUtils.getSimpleProperty(order, "total"));
>
> The output is:
>
> Price:  5.0
> Quantity:  10.0
> Total: null
>
> How do I get BeanUtils to use my getTotal() method to output the product of
> price times quantity?
>
> Ultimately, I would like to get the dynamic properties from a JDBC result
> set, and use the statically defined properties to compute totals and the
> like for the dynamic properties.
>
> - Bruce

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org