KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > Cart


1 /*
2  * Created on Mar 3, 2004
3  *
4  */

5 package com.openedit.store;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashSet JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11 import java.util.Set JavaDoc;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.openedit.money.Money;
16
17 import com.openedit.store.adjustments.Adjustment;
18 import com.openedit.store.customer.Customer;
19
20 /**
21  * @author dbrown
22  *
23  */

24 public class Cart
25 {
26     private static final Log log = LogFactory.getLog(Cart.class);
27     protected List JavaDoc fieldItems; // sku item map
28
protected Customer fieldCustomer;
29     protected ShippingMethod fieldShippingMethod;
30     protected Set JavaDoc fieldAdjustments;
31     protected Order fieldCurrentOrder;
32     protected Store fieldStore;
33     protected String JavaDoc fieldRegion;
34     protected Category fieldLastVisitedCatalog;
35     protected Category fieldLastLoadedCatalog;
36     
37     public Cart()
38     {
39     }
40     public Cart(Store inStore)
41     {
42         fieldStore = inStore;
43     }
44
45     public boolean hasRegion()
46     {
47         return fieldRegion != null;
48     }
49     public String JavaDoc getRegion()
50     {
51         if ( fieldRegion == null)
52         {
53             return ""; //for easy velocity work
54
}
55         return fieldRegion;
56     }
57     public void setRegion(String JavaDoc inRegion)
58     {
59         fieldRegion = inRegion;
60     }
61
62     public Order getCurrentOrder()
63     {
64         return fieldCurrentOrder;
65     }
66     public void setCurrentOrder(Order inCurrentOrder)
67     {
68         fieldCurrentOrder = inCurrentOrder;
69     }
70
71     public boolean hasZeroSubTotal()
72     {
73         return getSubTotal().doubleValue() < 0.01 ;
74     }
75     public boolean hasBackOrderedItems()
76     {
77         for (Iterator JavaDoc iter = getItemIterator(); iter.hasNext();)
78         {
79             CartItem item = (CartItem) iter.next();
80             if ( item.isBackOrdered() )
81             {
82                 return true;
83             }
84         }
85         return false;
86     }
87     public boolean hasItemWithOption(String JavaDoc inOptionId)
88     {
89         for (Iterator JavaDoc iter = getItemIterator(); iter.hasNext();)
90         {
91             CartItem item = (CartItem) iter.next();
92             if (item.hasOption(inOptionId))
93             {
94                 return true;
95             }
96         }
97         return false;
98     }
99     
100     public Set JavaDoc getAdjustments()
101     {
102         if (fieldAdjustments == null)
103         {
104             fieldAdjustments = new HashSet JavaDoc();
105         }
106         return fieldAdjustments;
107     }
108     
109     public void addAdjustment( Adjustment inAdjustment)
110     {
111         getAdjustments().add( inAdjustment );
112     }
113     public List JavaDoc getItems()
114     {
115         if (fieldItems == null){
116             fieldItems = new ArrayList JavaDoc();
117         }
118         return fieldItems;
119     }
120     /**
121      * These are CartItems ready for purchase
122      * @return
123      */

124     public Iterator JavaDoc getItemIterator()
125     {
126         return getItems().iterator();
127     }
128     public Iterator JavaDoc getInventoryItemsIterator()
129     {
130         return getInventoryItems().iterator();
131     }
132     /**
133      * This points to the original products in the database
134      * @return
135      */

136     public List JavaDoc getInventoryItems()
137     {
138         List JavaDoc items = getItems();
139         List JavaDoc listOfInventoryItems = new ArrayList JavaDoc();
140         for (Iterator JavaDoc iter = items.iterator(); iter.hasNext();)
141         {
142             CartItem cartItem = (CartItem) iter.next();
143             InventoryItem inventoryItem = cartItem.getProduct().getInventoryItemBySku(cartItem.getSku());
144             listOfInventoryItems.add(inventoryItem);
145         }
146         return listOfInventoryItems;
147     }
148     public int getNumItems()
149     {
150         return getItems().size();
151     }
152     public boolean isEmpty()
153     {
154         if (getItems().size() == 0)
155         {
156             return true;
157         }
158         else
159         {
160             boolean allZeroQuantity = true;
161             for (Iterator JavaDoc iter = getItemIterator(); iter.hasNext();)
162             {
163                 CartItem item = (CartItem) iter.next();
164                 if (item.getQuantity() > 0)
165                 {
166                     allZeroQuantity = false;
167                 }
168             }
169             return allZeroQuantity;
170         }
171     }
172     
173     public boolean isAdditionalShippingCosts()
174     {
175         return isAdditionalShippingCostsForMethod( getShippingMethod() );
176     }
177
178     public boolean isAdditionalShippingCostsForMethod( ShippingMethod inShippingMethod )
179     {
180         if( inShippingMethod != null)
181         {
182             for ( Iterator JavaDoc it = getItemIterator(); it.hasNext(); )
183             {
184                 CartItem item = (CartItem)it.next();
185                 String JavaDoc handlingChargeLevel = item.getProduct().getHandlingChargeLevel();
186                 if ( handlingChargeLevel != null )
187                 {
188                     HandlingCharge handlingCharge = inShippingMethod.getHandlingCharge( handlingChargeLevel );
189                     if ( handlingCharge != null && handlingCharge.isAdditionalCosts() )
190                     {
191                         return true;
192                     }
193                 }
194             }
195         }
196         return false;
197     }
198 /*
199     public boolean isEuro()
200     {
201          return Price.REGION_EU.equals(getRegion());
202     }
203     */

204     public Money getTotalShipping()
205     {
206         if ( getShippingMethod() == null)
207         {
208             return Money.ZERO;
209         }
210         Money cost = getShippingMethod().getCost(this);
211         return cost;
212     }
213     public Money getTotalTax()
214     {
215         Money totalTax = Money.ZERO;
216         if ( getCustomer() == null || getCustomer().getTaxRate() == null
217             || (getCustomer().getTaxExemptId() != null && getCustomer().getTaxExemptId().trim().length() > 0))
218         {
219             return totalTax;
220         }
221         for ( Iterator JavaDoc it = getItemIterator(); it.hasNext(); )
222         {
223             CartItem item = (CartItem) it.next();
224             if ( !item.getProduct().isTaxExempt() )
225             {
226                 totalTax = totalTax.add( calculateAdjustedPrice( item ).multiply( item.getQuantity() ).multiply( getCustomer().getTaxRate() ) );
227             }
228         }
229         return totalTax;
230     }
231     public Money getTotalPrice()
232     {
233         Money totalPrice = getSubTotal();
234         totalPrice = totalPrice.add( getTotalShipping() );
235         totalPrice = totalPrice.add( getTotalTax() );
236         return totalPrice;
237     }
238     public Money getTotalProductsAndShipping()
239     {
240         Money total = getSubTotal();
241         total = total.add( getTotalShipping() );
242         return total;
243     }
244     
245     public Money getSubTotal()
246     {
247         Money totalPrice = Money.ZERO;
248         
249         for ( Iterator JavaDoc it = getItemIterator(); it.hasNext(); )
250         {
251             CartItem item = (CartItem) it.next();
252             Money toadd = calculateAdjustedPrice( item );
253             if( toadd != null)
254             {
255                 toadd = toadd.multiply( item.getQuantity() );
256                 totalPrice = totalPrice.add( toadd );
257             }
258         }
259         return totalPrice;
260     }
261
262     protected Money calculateAdjustedPrice( CartItem inItem )
263     {
264         for ( Iterator JavaDoc iter = getAdjustments().iterator(); iter.hasNext(); )
265         {
266             Adjustment adjust = (Adjustment) iter.next();
267             Money money = adjust.adjust(this,inItem);
268             if( money != null)
269             {
270                 return money;
271             }
272         }
273         return inItem.getYourPrice();
274     }
275
276     public void addItem( CartItem inItem, int inLineNumber )
277     {
278         getItems().add(inLineNumber, inItem);
279     }
280     
281     public void addItem( CartItem inItem )
282     {
283         getItems().add(inItem);
284     }
285
286     public void removeAllItems()
287     {
288         getItems().clear();
289     }
290     public void removeProduct( Product inProduct )
291     {
292         CartItem toremoveitem = null;
293         for (Iterator JavaDoc iter = getItems().iterator(); iter.hasNext();)
294         {
295             CartItem item = (CartItem) iter.next();
296             if ( item.getProduct() == inProduct )
297             {
298                 toremoveitem = item;
299                 return;
300             }
301         }
302         if( toremoveitem != null)
303         {
304             removeItem(toremoveitem);
305         }
306     }
307
308     public void removeItem( CartItem inItem )
309     {
310         getItems().remove( inItem );
311     }
312
313     public Customer getCustomer()
314     {
315         return fieldCustomer;
316     }
317     public void setCustomer(Customer inCustomer)
318     {
319         fieldCustomer = inCustomer;
320     }
321     public ShippingMethod getShippingMethod() {
322         return fieldShippingMethod;
323     }
324     public void setShippingMethod(ShippingMethod inShippingMethod) {
325         fieldShippingMethod = inShippingMethod;
326     }
327
328     public List JavaDoc getAvailableShippingMethods()
329     {
330         List JavaDoc availableMethods = new ArrayList JavaDoc();
331         
332         for (Iterator JavaDoc iter = getItemIterator(); iter.hasNext();)
333         {
334             CartItem cartI = (CartItem) iter.next();
335             String JavaDoc method = cartI.getInventoryItem().getProduct().getShippingMethodId();
336             if ( method != null )
337             {
338                 ShippingMethod smethod = getStore().findShippingMethod(method);
339                 if ( smethod == null)
340                 {
341                     log.error("Specified an invalid shipping method " + method);
342                 }
343                 else
344                 {
345                     availableMethods.add(smethod);
346                     return availableMethods;
347                 }
348             }
349         }
350         //TODO: make a composite of product shipping option. Adds up the costs, description and API?
351

352         for ( Iterator JavaDoc it = getStore().getAllShippingMethods().iterator();
353             it.hasNext(); )
354         {
355             ShippingMethod method = (ShippingMethod)it.next();
356             if ( !method.isHidden() && method.applies(this))
357             {
358                     availableMethods.add(method);
359             }
360         }
361         return availableMethods;
362     }
363
364     public Store getStore()
365     {
366         return fieldStore;
367     }
368
369     public void setStore(Store inStore)
370     {
371         fieldStore = inStore;
372     }
373     /**
374      * @param inCartItem
375      * @return
376      */

377     public CartItem findCartItemWith(InventoryItem inInventoryItem)
378     {
379         for (Iterator JavaDoc iter = getItems().iterator(); iter.hasNext();)
380         {
381             CartItem item = (CartItem) iter.next();
382             if ( item.getInventoryItem().getSku().equals(inInventoryItem.getSku() ) )
383             {
384                 return item;
385             }
386         }
387         
388         return null;
389     }
390     public boolean containsProduct(String JavaDoc inId)
391     {
392         for (Iterator JavaDoc iter = getItems().iterator(); iter.hasNext();)
393         {
394             CartItem item = (CartItem) iter.next();
395             if ( item.getInventoryItem().getProduct().getId().equals( inId ) )
396             {
397                 return true;
398             }
399         }
400         return false;
401     }
402     
403     public Category getLastVisitedCatalog()
404     {
405         return fieldLastVisitedCatalog;
406     }
407     public void setLastVisitedCatalog( Category inLastVisitedCatalog )
408     {
409         fieldLastVisitedCatalog = inLastVisitedCatalog;
410         if( inLastVisitedCatalog != null)
411         {
412             fieldLastLoadedCatalog = inLastVisitedCatalog;
413         }
414     }
415     public Category getLastLoadedCatalog()
416     {
417         return fieldLastLoadedCatalog;
418     }
419     
420 }
421
Popular Tags