KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.openedit.store;
5
6 import java.util.Collection JavaDoc;
7 import java.util.Comparator JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.TreeSet JavaDoc;
13
14 import org.apache.commons.collections.map.ListOrderedMap;
15 import org.openedit.money.Fraction;
16 import org.openedit.money.Money;
17
18
19 /**
20  * @author dbrown
21  *
22  */

23 public class CartItem
24 {
25     protected int fieldQuantity = 1;
26     protected Money fieldYourPrice;
27     protected InventoryItem fieldInventoryItem;
28     protected Boolean JavaDoc fieldBackOrdered;
29     protected Map JavaDoc fieldOptions;
30     protected Map JavaDoc fieldProperties;
31     protected Product fieldProduct;
32     
33     public CartItem()
34     {
35         super();
36     }
37
38     public void addOption( Option inOption )
39     {
40         getOptionsMap().put( inOption.getId(), inOption );
41     }
42     public void setOptions( Set JavaDoc inOptions )
43     {
44         getOptionsMap().clear();
45         for (Iterator JavaDoc iter = inOptions.iterator(); iter.hasNext();)
46         {
47             Option obj = (Option) iter.next();
48             addOption(obj);
49         }
50     }
51     public void removeOption( Option inOption )
52     {
53         getOptionsMap().remove( inOption.getId() );
54     }
55
56     public Money getTotalPrice()
57     {
58         return getYourPrice().multiply( getQuantity() );
59     }
60
61     public Money getYourPrice()
62     {
63         if ( fieldYourPrice != null)
64         {
65             return fieldYourPrice;
66         }
67         int quantity = getQuantity();
68         Money price = Money.ZERO;
69         Money priceByQuantity = getInventoryItem().getYourPriceByQuantity(quantity);
70         if (priceByQuantity != null)
71         {
72             price = price.add(priceByQuantity);
73         }
74         for ( Iterator JavaDoc it = getOptions().iterator(); it.hasNext();)
75         {
76             Option option = (Option)it.next();
77             PriceSupport prices = option.getPriceSupport();
78             if( prices != null)
79             {
80                 price = prices.getYourPriceByQuantity(quantity).add(price);
81             }
82         }
83         return price;
84     }
85     public void setYourPrice(Money inMoney)
86     {
87         fieldYourPrice = inMoney;
88     }
89     public int getQuantity()
90     {
91         return fieldQuantity;
92     }
93
94     public void setQuantity(int quantity) throws StoreException
95     {
96         fieldQuantity = quantity;
97         
98         /* Set the cart's backorder quantity. This is for
99         informational purposes only. No inventory quantities
100         are being adjusted (that is done at checkout). A cart
101         item's backorder quantity indicates the portion of the
102         cart item's quantity that is unavailable (not in stock),
103         at the time the cart item's quantity was last changed.*/

104         /*
105         InventoryItem inventoryItem = getInventoryItem();
106         if ( inventoryItem != null )
107         {
108             int quantityInStock = inventoryItem.getQuantityInStock();
109             int remainingStock = quantityInStock - quantity;
110             if (remainingStock < 0)
111             {
112                 setBackOrderQuantity(-remainingStock);
113             }
114             else
115             {
116                 setBackOrderQuantity(0);
117             }
118         }
119         */

120     }
121     public Map JavaDoc getOptionsMap()
122     {
123         if ( fieldOptions == null )
124         {
125             // Options are always sorted so that SKUs can be
126
// easily uniquely generated from a set of options.
127
fieldOptions = ListOrderedMap.decorate(new HashMap JavaDoc());
128         }
129         return fieldOptions;
130     }
131
132     public Collection JavaDoc getOptions()
133     {
134         return getOptionsMap().values();
135     }
136
137     public Set JavaDoc getOtherOptions()
138     {
139         //everything but size and color
140
Set JavaDoc rest = new TreeSet JavaDoc( new Comparator JavaDoc()
141         {
142             public int compare(Object JavaDoc arg0, Object JavaDoc arg1)
143             {
144                 Option opt0 = (Option)arg0;
145                 Option opt1 = (Option)arg1;
146                 return opt0.getId().compareTo( opt1.getId() );
147             }
148         }
149         );
150         
151         for (Iterator JavaDoc iter = getOptions().iterator(); iter.hasNext();)
152         {
153             Option option = (Option) iter.next();
154             if( !option.getId().equals("size") && !option.getId().equals("color") )
155             {
156                 rest.add(option);
157             }
158         }
159         return rest;
160     }
161     
162     /**
163      * @param inRate
164      * @return
165      */

166     public Money calculateTax(Fraction inRate)
167     {
168         if ( inRate == null)
169         {
170             return Money.ZERO;
171         }
172         return getYourPrice().multiply(inRate);
173     }
174     
175     public InventoryItem getInventoryItem()
176     {
177         return fieldInventoryItem;
178     }
179     
180     public boolean isBackOrdered()
181     {
182         if ( fieldBackOrdered != null)
183         {
184             return getBackOrdered().booleanValue();
185         }
186         if( getInventoryItem().isBackOrdered() )
187         {
188             return true;
189         }
190         return getQuantity() > getInventoryItem().getQuantityInStock();
191     }
192
193     public Option getOption(String JavaDoc inId)
194     {
195         Option option = (Option)getOptionsMap().get(inId);
196         if( option == null)
197         {
198             option = getInventoryItem().getOption(inId);
199             if( option != null)
200             {
201                 //make a copy?
202
}
203         }
204         return option;
205     }
206     
207     public boolean hasOption(String JavaDoc inId)
208     {
209         Option option = (Option)getOptionsMap().get(inId);
210         if (option == null)
211         {
212             return false;
213         }
214         else
215         {
216             return true;
217         }
218     }
219     
220     public boolean hasOption(Option inOption)
221     {
222         Option option = (Option)getOptionsMap().get(inOption.getId());
223         if (option == null)
224         {
225             return false;
226         }
227         else
228         {
229             return option.equals(inOption);
230         }
231     }
232
233     public void setInventoryItem(InventoryItem inInventoryItem)
234     {
235         fieldInventoryItem = inInventoryItem;
236     }
237     public String JavaDoc getSku()
238     {
239         String JavaDoc sku = getInventoryItem().getSku();
240         //add options?
241

242         return sku;
243     }
244     public boolean isSpecialRequest()
245     {
246         if( hasOption("specialrequest") )
247         {
248             return true;
249         }
250         return false;
251     }
252
253     public boolean isSize(String JavaDoc inSize)
254     {
255         Option option = getSize();
256         if( option != null)
257         {
258             if ( inSize != null && inSize.equals(option.getValue()))
259             {
260                 return true;
261             }
262         }
263         if( option == null && inSize == null)
264         {
265             return true;
266         }
267         return false;
268     }
269     public boolean isColor(String JavaDoc inColor)
270     {
271         Option option = getColor();
272         if( option != null)
273         {
274             if ( inColor != null && inColor.equals(option.getValue()))
275             {
276                 return true;
277             }
278         }
279         if( option == null && inColor == null)
280         {
281             return true;
282         }
283         return false;
284     }
285
286
287     public Option getSize()
288     {
289         return getOption("size");
290     }
291     public Option getColor()
292     {
293         return getOption("color");
294     }
295
296     public Product getProduct()
297     {
298         if( fieldProduct != null) //This is here in case someone wants to use product without inventory ie Archive
299
{
300             return fieldProduct;
301         }
302         return getInventoryItem().getProduct();
303     }
304
305     public void setProduct(Product inProduct)
306     {
307         fieldProduct = inProduct;
308     }
309     public Boolean JavaDoc getBackOrdered()
310     {
311         return fieldBackOrdered;
312     }
313
314     public void setBackOrdered(boolean inBackOrdered)
315     {
316         fieldBackOrdered = new Boolean JavaDoc(inBackOrdered);
317     }
318
319     public Map JavaDoc getProperties()
320     {
321         return fieldProperties;
322     }
323
324     public void setProperties(Map JavaDoc inProperties)
325     {
326         fieldProperties = inProperties;
327     }
328
329     public void putProperty(String JavaDoc inKey, String JavaDoc inValue)
330     {
331         if (fieldProperties == null)
332         {
333             fieldProperties = new HashMap JavaDoc();
334         }
335         
336         getProperties().put(inKey, inValue);
337     }
338
339     public double getWeight()
340     {
341         return getInventoryItem().getWeight();
342     }
343     public String JavaDoc getName()
344     {
345         if( fieldInventoryItem == null)
346         {
347             return getProduct().getName();
348         }
349         return getInventoryItem().getName();
350     }
351     public String JavaDoc get(String JavaDoc inString)
352     {
353         if( getProperties() == null)
354         {
355             return getInventoryItem().get(inString);
356         }
357         String JavaDoc val = (String JavaDoc)getProperties().get(inString);
358         if( val == null)
359         {
360             val = getInventoryItem().get(inString);
361         }
362         return val;
363     }
364     
365     public boolean hasSize()
366     {
367         Option size = getSize();
368         if( size == null || size.getValue() == null )
369         {
370             return false;
371         }
372         return true;
373     }
374
375     public boolean hasColor()
376     {
377         Option option = getColor();
378         if( option == null || option.getValue() == null )
379         {
380             return false;
381         }
382         return true;
383     }
384
385     
386 }
387
Popular Tags