Thanks....Skip.
I got this point after some expeimentation " Typically, the GenericValue
contains the name/value
pairs of some row in a table or view."
I changed my code accordingly... to use elim.get
(i).getDouble("grandTotal").doubleValue();
thanks for the support.
One point that i would like to share is about beanshell.
Beanshell localizes the scope of the variable used within the loop to that
loop.
So if one has to use the same variable elsewhere than one must declare it
outside the loop and refer to it elsewhere as super.variable to use the same
version of the varible.
This actually took up lot of my time and was also one the reasons for "void"
!!
On 9/26/07, Skip <[EMAIL PROTECTED]> wrote:
>
> vijay
>
> I don't understand some of the code in your example. But, if you look at
> the source for GenericEntity (which GenericValue is a subclass of), you
> will
> see that at its heart, there is a Map of name-value pairs, i.e. a String
> which is the field name mapped to (in this case) the value(s) retrieved by
> the GenericDelegator. Typically, the GenericValue contains the name/value
> pairs of some row in a table or view.
>
> But, here is some code that will do what you want (assuming that
> "grandTotal" is really a field in OrderHeader which I don't remember for
> sure.) I have not included the required try/catch error trapping for
> clarity.
>
> //Get a list of orders matching our conditions. In place of the ellipse,
> add your conditions
> List orders = delegator.findByAnd("OrderHeader", ...);
> Iterator i = orders.iterator();
>
> double total = 0.0;
> //Go through the list and add up all the "grandTotal" values.
> while (i.hasNext())
> {
> GenericValue orderHeader = (GenericValue) i.next();
> String orderId = orderHeader.getString("orderId");
> Double totalThisOrder = orderHeader.getDouble("grandTotal");
> System.out.println("Order id is " + orderId + "\t\tAmount is " +
> totalThisOrder);
> if(totalThisOrder != null)
> total += totalThisOrder.doubleValue();
> }
> System.out.println(" \t\t Grand Total " + total);
>
>
> In the above example, "orders" is a Java List of GenericValue objects
> returned by the delegator. We have to iterate through them and extract
> the
> "grandTotal" values to add up.
>
> This is such a common task that maybe I'll write a
>
> double GenericValue.sumDouble(List <GenericValue>values, String field)
> and
> long GenericValue.sumLong(List <GenericValue>values, String field)
>
> Give it a try and see if this is not what you want.
>
> Skip
>
> -----Original Message-----
> From: vijay Si [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, September 25, 2007 2:32 AM
> To: [email protected]
> Subject: Re: TypeCasting GenericValue object.
>
>
> Hello Skip,
>
> My piece of code is :
>
> itelim = elim.iterator(); //where elim =
> findByCond(........);
> elimList = new ArrayList();
>
> while(itelim.hasNext())
> {
> elimList = itelim.next();
> elimList.toString();
>
> String a1 = elimList.getString("grandTotal");
> a = Double.parseDouble(a1);
>
> }
> Now this does give me some result at the o/p : which is last entry in the
> grandTotal column(a double value) say for eg: 350.30
>
> I also used the piece of code that you provided.....it works for sure but
> gives "void" at the o/p.......
>
> Also wanted to ask as to how can i iterate over the grandTotal entries.
> for
> eg: if i have to get grandTotal for all products and sum it up.
>
> Thanks
>
> On 9/25/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > You can't type cast the GenericValue. You possibly can type cast one of
> > the
> > objects it holds depending on the type. Each GenericValue holds
> different
> > types if information depending on what it represents. For example,
> >
> > GenericValue existingOrderHeader = delegator.findByPrimaryKey
> > ("OrderHeader",
> > UtilMisc.toMap("orderId", orderId));
> > String customerId = externalOrderHeader.getString("customerPartyId");
> >
> > if OrderHeader had a Double value in it, say "grandTotal", you could
> >
> > double grandTotal =
> > externalOrderHeader.getDouble("grandTotal").doubleValue();
> >
> >
> > or
> >
> > double grandTotal =
> > ((Double)externalOrderHeader.get("grandTotal")).doubleValue();
> >
> > Both of these will throw a ClassCastException if "grandTotal" is not a
> > double.
> >
> > Everything in a GenericValue object is an object itself, i.e double is
> > wrapped in Double, etc.
> >
> > Hope that helps
> >
> > Skip
> >
> > -----Original Message-----
> > From: vijay Si [mailto:[EMAIL PROTECTED]
> > Sent: Monday, September 24, 2007 11:12 PM
> > To: [email protected]
> > Subject: TypeCasting GenericValue object.
> >
> >
> > Hi,
> > I have been trying to type cast a generic value object to double and
> > string,
> > but i get an error
> >
> > "java.lang.ClassCastException: Cannot cast
> org.ofbiz.entity.GenericValueto
> > java.lang.xx"
> >
> > Is there any particular way in ofbiz to work around this?
> >
> > Regards
> >
> >
>
>