Re: signed int
Re: signed int
- Subject: Re: signed int
- From: Lachlan Deck <email@hidden>
- Date: Sat, 24 Dec 2005 09:58:24 +1100
Hi there,
On 23/12/2005, at 2:53 AM, Jerry W. Walker wrote:
Lachlan Deck has provided your answer (in a separate post), I
think, in his comments:
// I think you'll want to add the available count rather than
subtract
// so that whether the availability is positive or negative
// it will provide the status with the correct number
Substituting Lachlan's formula would get you closer, but I still
have problems.
I don't see your virtualStockStatus as a meaningful number, unless
I'm badly misunderstanding the problem.
I think what Xavier wants is an array of stockStatus' per item rather
than an overall out of stock count. The latter (if still desired)
would be easily displayed via the @sum directive (to value for key).
I would have thought that the 'status' ought to be initialised with
the current quantity available, per item; then adjusted with the
quantity ordered or indeed refunded. i.e., whether quantity is +ve or
-ve.
To start getting this right, my tip would be to lessen the verbose
code ;-)
i.e., this is simple and easy to read... at least to me ;-)
virtualStockStatus -= aCustomerOrderItem.quantityAvailable().intValue
() - aCustomerOrderItem.quantity().intValue();
The following, for me, is verbose; too many unnecessary brackets and
operands. The names are long enough as it is. You don't want to write
them twice unnecessarily :-)
virtualStockStatus = virtualStockStatus -
((aCustomerOrderItem.quantityAvailable().intValue()) -
(aCustomerOrderItem.quantity().intValue()));
Second, use the principles of OOP. That'll make it easier to read/
debug/adjust. i.e., principles of OO: Abstraction, Encapsulation, ...
Now, let's wrap it up with a full example of something along the
lines of what you're after...
import com.webobjects.appserver.*;
import com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import java.util.*;
public class XAOrderableItemsComponent extends WOComponent {
public final static NSKeyValueCoding.Null Nil =
NSKeyValueCoding.NullValue;
public final static NSSelector SelEqual =
EOQualifier.QualifierOperatorEqual;
public final static NSSelector SelNotEqual =
EOQualifier.QualifierOperatorNotEqual;
protected final static EOQualifier _CustomerOrderQualifier = new
EOAndQualifier( new NSArray( new Object[] {
new EOKeyValueQualifier( "customerOrder.orderCancledOn", SelEqual,
Nil ),
new EOKeyValueQualifier( "customerOrder.paymentValidatedOn",
SelNotEqual, Nil ),
new EOKeyValueQualifier( "customerOrder.shippedOrDeliveredOn",
SelEqual, Nil ),
new EOKeyComparisonQualifier( "quantity",
EOQualifier.QualifierOperatorNotEqual, "quantityAvailable" )
} ) );
// iterator
public CustomerOrderItem anOrderItem;
public XAOrderableItemsComponent( WOContext aContext ) {
super( aContext );
anOrderItem = null;
}
public NSArray customerOrderItems() {
NSArray orderItems;
orderItems = ( ( Session )session() ).customerOrderItems();
if ( orderItems == null )
return NSArray.EmptyArray;
return EOQualifier.filteredArrayWithQualifier( orderItems,
_CustomerOrderQualifier );
}
public Number statusForOrderItem() {
return statusForOrderItem( anOrderItem );
}
public Number statusForOrderItem( CustomerOrderItem anItem ) {
// if quantityAvailable is null maybe it's not a stocked item
// anyway we'll return zero.
// Otherwise, we'll check to see if quantity is null
// to determine if we offset the value or not
if ( anItem.quantityAvailable() != null ) {
int status = anItem.quantityAvailable().intValue();
status = anItem.quantityAvailable().intValue();
// we'll multiply by -1 and add the result
// seeing as a positive quantity ordered will
// reduce the availability.
// Naturally upon checkout, the availability will need
// to be adjusted with this same returned value.
if ( anItem.quantity() != null )
status += anItem.quantity().intValue() * -1;
return new Integer( status );
}
return new Integer( 0 );
}
// -------------------------------------------------------
// the above is enough to give you a WORepetition of items
// but if you want more (such as totals) then...
// -------------------------------------------------------
public NSDictionary availabilityStatusPerOrderedItem() {
return _availabilityStatusPerItem( customerOrderItems() );
}
public NSDictionary availabilityStatusPerSaleableItem() {
return _availabilityStatusPerItem( super.saleableItems() );
}
protected NSDictionary _availabilityStatusPerItem( NSArray
orderableItems ) {
NSMutableDictionary statusPerItem;
statusPerItem = new NSMutableDictionary();
if ( orderableItems != null && orderableItems.count() > 0 ) {
Enumerator en;
en = orderableItems .objectEnumerator();
while( en.hasMoreElements() ) {
CustomerOrderItem anItem;
anItem = ( CustomerOrderItem )en.nextElement();
statusPerItem.setObjectForKey( statusForOrderItem( anItem ),
anItem );
}
}
return statusPerItem;
}
}
To get the total count (if it means anything):
TotalCountString: WOString {
value = availabilityStatusPerOrderedItem.allValues.@sum;
}
with regards,
--
Lachlan Deck
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden