001    package data;
002    
003    /**
004     * A collection of convenience {@link BasketEntryValue BasketEntryValues}.
005     *
006     * @author Steffen Zschaler
007     * @version 2.0 18/08/1999
008     * @since v2.0
009     */
010    public final class BasketEntryValues {
011    
012        /**
013         * A BasketEntryValue returning the value of a DataBasketEntry under the assumption that this describes a
014         * StockItem movement. The value will be calculated by multiplying the number of items with the
015         * default value of the associated CatalogItem.
016         *
017         * <p>To obtain the associated CatalogItem, the BasketEntryValue first tries to use the DataBasketEntries
018         * destination and only if this is <code>null</code> it will try its source. If both are <code>null</code>
019         * or if an exception occurs, the BasketEntryValue will return 0.</p>
020         */
021        public static final BasketEntryValue ONLY_STOCK_ITEMS = new BasketEntryValue() {
022            public Value getEntryValue(DataBasketEntry dbe) {
023                Stock st = (Stock)dbe.getDestination();
024    
025                if (st == null) {
026                    st = (Stock)dbe.getSource();
027                }
028    
029                if (st != null) {
030                    try {
031                        int nCount = ((dbe.getValue()instanceof Number) ? (((Number)dbe.getValue()).intValue()) :
032                                (1));
033    
034                        return st.getCatalog(dbe.getOwner()).get(dbe.getSecondaryKey(), null,
035                                false).getValue().multiply(nCount);
036                    }
037                    catch (Throwable t) {
038                        return new IntegerValue(0);
039                    }
040                } else {
041                    return new IntegerValue(0);
042                }
043            }
044        };
045    
046        /**
047         * A BasketEntryValue returning the value of a DataBasketEntry under the assumption that this describes a
048         * CatalogItem movement. The value will be taken from the CatalogItem's {@link CatalogItem#getValue} method.
049         */
050        public static final BasketEntryValue ONLY_CATALOG_ITEMS = new BasketEntryValue() {
051            public Value getEntryValue(DataBasketEntry dbe) {
052                try {
053                    return ((CatalogItem)dbe.getValue()).getValue();
054                }
055                catch (Throwable t) {
056                    return new IntegerValue(0);
057                }
058            }
059        };
060    
061        /**
062         * A BasketEntryValue that returns 1 for each entry. You can use this BasketEntryValue to count entries that
063         * match a given condition.
064         */
065        public static final BasketEntryValue COUNT_ITEMS = new BasketEntryValue() {
066            private IntegerValue m_ivOne = new IntegerValue(1);
067    
068            public Value getEntryValue(DataBasketEntry dbe) {
069                return m_ivOne;
070            }
071        };
072    }