KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.openedit.store;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Comparator JavaDoc;
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.Set JavaDoc;
15 import java.util.TreeSet JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.openedit.money.Money;
20
21
22 /**
23  * This is a specific item that is held in stock
24  *
25  */

26 public class InventoryItem
27 {
28     protected int fieldQuantityInStock;
29     protected Map JavaDoc fieldProperties;
30     protected Set JavaDoc fieldOptions;
31     protected String JavaDoc fieldSku;
32     protected Product fieldProduct;
33     protected PriceSupport fieldPriceSupport;
34     protected String JavaDoc fieldDescription;
35     protected double fieldWeight;
36     protected boolean fieldBackOrdered;
37     private static final Log log = LogFactory.getLog(InventoryItem.class);
38     
39     public InventoryItem()
40     {
41         super();
42     }
43
44     public Money getYourPrice()
45     {
46         return getYourPriceByQuantity(1);
47     }
48     public Money getYourPriceByQuantity(int i)
49     {
50         PriceSupport sup = getPriceSupport();
51         if ( sup == null )
52         {
53             sup = getProduct().getPriceSupport();
54         }
55         if ( sup == null)
56         {
57             return null;
58         }
59         return sup.getYourPriceByQuantity( i );
60     }
61     
62     public int getQuantityInStock()
63     {
64         return fieldQuantityInStock;
65     }
66
67     public void setQuantityInStock(int inQuantityInStock)
68     {
69         fieldQuantityInStock = inQuantityInStock;
70     }
71     
72     public boolean isInStock()
73     {
74         return fieldQuantityInStock == -1 || fieldQuantityInStock > 0;
75     }
76     
77     public void increaseQuantityInStock(int inIncrease)
78     {
79         fieldQuantityInStock = fieldQuantityInStock + inIncrease;
80     }
81     
82     public void decreaseQuantityInStock(int inDecrease)
83     {
84         if ( fieldQuantityInStock == -1)
85         {
86             return;
87         }
88         fieldQuantityInStock = fieldQuantityInStock - inDecrease;
89     }
90     
91     public Option getColor()
92     {
93         Option color = getOption("color");
94         return color;
95     }
96
97     public Option getSize()
98     {
99         Option size = getOption("size");
100         return size;
101     }
102
103     public void setSize(String JavaDoc inString)
104     {
105         Option opt = getLocalOption("size");
106         if( opt == null)
107         {
108             opt = new Option();
109             opt.setId("size");
110             opt.setName("Size");
111             Option parent = getOption("size");
112             if( parent != null)
113             {
114                 opt.setRequired(parent.isRequired());
115                 opt.setName(parent.getName());
116                 opt.setPriceSupport(parent.getPriceSupport());
117             }
118             
119             addOption(opt);
120         }
121         opt.setValue(inString);
122     }
123     public void setColor(String JavaDoc inString)
124     {
125         Option opt = getLocalOption("color");
126         if( opt == null)
127         {
128             opt = new Option();
129             opt.setId("color");
130             opt.setName("Color");
131             Option parent = getOption("color");
132             if( parent != null)
133             {
134                 opt.setRequired(parent.isRequired());
135                 opt.setName(parent.getName());
136                 opt.setPriceSupport(parent.getPriceSupport());
137             }
138             addOption(opt);
139         }
140         opt.setValue(inString);
141     }
142
143     
144     public void addOption(Option inOpt)
145     {
146         getOptions().add(inOpt);
147     }
148
149     public String JavaDoc getSku()
150     {
151         return fieldSku;
152     }
153
154     public void setSku( String JavaDoc sku )
155     {
156         fieldSku = sku;
157     }
158
159     public boolean hasSize()
160     {
161         Option size = getSize();
162         if( size == null || size.getValue() == null )
163         {
164             return false;
165         }
166         return true;
167     }
168
169     public boolean hasColor()
170     {
171         Option option = getColor();
172         if( option == null || option.getValue() == null )
173         {
174             return false;
175         }
176         return true;
177     }
178
179     public String JavaDoc toString()
180     {
181         return "[Item: " + getSku() + " " + getSize() + " " + getColor() + "]";
182     }
183
184     /**
185      * @param inCartItem
186      * @return
187      */

188     public boolean isExactMatch( Collection JavaDoc inSomeOptions )
189     {
190         //all options must match
191
for (Iterator JavaDoc iter = inSomeOptions.iterator(); iter.hasNext();)
192         {
193             Option option = (Option) iter.next();
194             Option found = getLocalOption(option.getId());
195
196             if( found == null)
197             {
198                 //log.info("No option was found locally with id: " + option.getId());
199
continue; //it is not specified
200
}
201             //log.info("Does " + option.getId() + ":" + option.getValue()
202
// + " match " + found.getId() + ":" + option.getValue() + "?");
203

204             if( option.equals(found) )
205             {
206                 //log.info("Yes.");
207
continue;
208             }
209             //log.info("No. Not a match");
210
return false;
211         }
212         //log.info("We found a match.");
213
return true;
214     }
215     
216         
217     public boolean isCloseMatch( Collection JavaDoc inOptions )
218     {
219         //look over all the required options and see if they all match
220
for (Iterator JavaDoc iter = inOptions.iterator(); iter.hasNext();)
221         {
222             Option inCheck = (Option) iter.next();
223             if( inCheck.isRequired() || inCheck.getId().equals("color") || inCheck.getId().equals("size"))
224             {
225                 Option has = getLocalOption(inCheck.getId());
226                 if( has == null)
227                 {
228                     return false;
229                 }
230                 if( has.equals(inCheck))
231                 {
232                     continue;
233                 }
234             }
235         }
236         return true;
237     }
238     public boolean isAnyMatch(Collection JavaDoc inOptions)
239     {
240         for (Iterator JavaDoc iter = inOptions.iterator(); iter.hasNext();)
241         {
242             Option inCheck = (Option) iter.next();
243             if( inCheck.getId().equals("color") || inCheck.getId().equals("size"))
244             {
245                 Option has = getLocalOption(inCheck.getId());
246                 if( has != null && has.equals(inCheck))
247                 {
248                     return true;
249                 }
250             }
251         }
252         for (Iterator JavaDoc iter = inOptions.iterator(); iter.hasNext();)
253         {
254             Option inCheck = (Option) iter.next();
255             Option has = getLocalOption(inCheck.getId());
256             if( has != null && has.equals(inCheck))
257             {
258                 return true;
259             }
260         }
261         return false;
262     }
263
264     protected boolean equals( Option inOption, Option inOther)
265     {
266         if( inOption == inOther)
267         {
268             return true;
269         }
270         if( inOption == null || inOther == null)
271         {
272             return false;
273         }
274         if( inOption.getValue() == inOther.getValue() )
275         {
276             return true;
277         }
278         if( inOption.getValue() == null || inOther.getValue() == null)
279         {
280             return false;
281         }
282         return inOption.getValue().equals(inOther.getValue());
283         
284     }
285     protected Option findOption( String JavaDoc inId, Collection JavaDoc inOptions)
286     {
287         for (Iterator JavaDoc iterator = inOptions.iterator(); iterator.hasNext();)
288         {
289             Option found = (Option) iterator.next();
290             if( found.getId().equals(inId))
291             {
292                 return found;
293             }
294         }
295         return null;
296
297     }
298     
299     public Money getRetailPrice()
300     {
301         //return getPriceSupport().getPrice();
302
if (getPriceSupport() != null)
303         {
304             return getPriceSupport().getRetailPrice();
305         }
306         else
307         {
308             return Money.ZERO;
309         }
310         
311     }
312
313     public boolean isOnSale()
314     {
315         if( getPriceSupport() == null)
316         {
317             return false;
318         }
319         return getPriceSupport().isOnSale();
320     }
321
322
323     public void put( String JavaDoc inKey, String JavaDoc inValue )
324     {
325         getProperties().put( inKey, inValue );
326     }
327
328     public String JavaDoc get( String JavaDoc inkey )
329     {
330         return (String JavaDoc) getProperties().get( inkey );
331     }
332
333     public Map JavaDoc getProperties()
334     {
335         if (fieldProperties == null)
336         {
337             fieldProperties = new HashMap JavaDoc();
338         }
339
340         return fieldProperties;
341     }
342     
343     public void addProperty(String JavaDoc inKey, String JavaDoc inValue)
344     {
345         if ( inValue == null || inValue.length() == 0)
346         {
347             getProperties().remove(inKey);
348         }
349         else
350         {
351             getProperties().put( inKey , inValue);
352         }
353     }
354
355     public void setProperties( Map JavaDoc inAttributes )
356     {
357         fieldProperties = inAttributes;
358     }
359
360     public String JavaDoc getName()
361     {
362         return getProduct().getName();
363     }
364
365     public Product getProduct()
366     {
367         return fieldProduct;
368     }
369
370     public void setProduct( Product product )
371     {
372         fieldProduct = product;
373     }
374
375     public PriceSupport getPriceSupport()
376     {
377         return fieldPriceSupport;
378     }
379
380     public void setPriceSupport( PriceSupport priceSupport )
381     {
382         fieldPriceSupport = priceSupport;
383     }
384     /**
385      * @return
386      */

387     public List JavaDoc getTiers()
388     {
389         return getPriceSupport().getTiers();
390     }
391     /**
392      * @param inQuantity
393      * @param money
394      */

395     public void addTierPrice( int inQuantity, Price inPrice )
396     {
397         if( fieldPriceSupport == null)
398         {
399             fieldPriceSupport = new PriceSupport();
400         }
401         getPriceSupport().addTierPrice( inQuantity, inPrice );
402     }
403
404     /**
405      * @return
406      */

407     public boolean hasOwnPrice()
408     {
409         return fieldPriceSupport != null && fieldPriceSupport.getTiers().size()> 0;
410     }
411
412     /**
413      * @param inI
414      * @return
415      */

416     public PriceTier getTier(int inI)
417     {
418         if ( fieldPriceSupport == null || getPriceSupport().getTiers() == null || getPriceSupport().getTiers().size() <= inI)
419         {
420             return null;
421         }
422         
423         return (PriceTier)getPriceSupport().getTiers().get(inI);
424     }
425
426     public String JavaDoc getDescription()
427     {
428         return fieldDescription;
429     }
430     public void setDescription(String JavaDoc inDescription)
431     {
432         fieldDescription = inDescription;
433     }
434     public List JavaDoc getPropertiesStartingWith(String JavaDoc inKey)
435     {
436         List JavaDoc keys = new ArrayList JavaDoc();
437         for (Iterator JavaDoc iter = getProperties().keySet().iterator(); iter.hasNext();)
438         {
439             String JavaDoc key = (String JavaDoc) iter.next();
440             if ( key.startsWith(inKey))
441             {
442                 keys.add(key);
443             }
444         }
445         Collections.sort(keys);
446         List JavaDoc values = new ArrayList JavaDoc();
447         for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();)
448         {
449             String JavaDoc key = (String JavaDoc) iter.next();
450             values.add(get(key));
451         }
452         return values;
453     }
454
455     public double getWeight()
456     {
457         return fieldWeight;
458     }
459
460     public void setWeight(double inWeight)
461     {
462         fieldWeight = inWeight;
463     }
464
465     public boolean isBackOrdered()
466     {
467         return fieldBackOrdered;
468     }
469
470     public void setBackOrdered(boolean inBackOrdered)
471     {
472         fieldBackOrdered = inBackOrdered;
473     }
474     
475     public void clearOptions()
476     {
477         getOptions().clear();
478     }
479     
480     public Set JavaDoc getOptions()
481     {
482         if (fieldOptions == null)
483         {
484             fieldOptions = new TreeSet JavaDoc( new Comparator JavaDoc()
485             {
486                 public int compare(Object JavaDoc arg0, Object JavaDoc arg1)
487                 {
488                     Option opt0 = (Option)arg0;
489                     Option opt1 = (Option)arg1;
490                     return opt0.getId().compareTo( opt1.getId() );
491                 }
492             }
493             );
494         }
495         return fieldOptions;
496     }
497     
498     public List JavaDoc getAllOptions()
499     {
500         Map JavaDoc optionsMap = new HashMap JavaDoc();
501         List JavaDoc productOptions = getProduct().getAllOptions();
502
503         for (Iterator JavaDoc iter = productOptions.iterator(); iter.hasNext();)
504         {
505             Option option = (Option) iter.next();
506             optionsMap.put(option.getId(), option);
507         }
508         
509         for (Iterator JavaDoc iter = getOptions().iterator(); iter.hasNext();)
510         {
511             Option option = (Option) iter.next();
512             optionsMap.put(option.getId(), option);
513         }
514         
515         List JavaDoc allOptions = new ArrayList JavaDoc();
516         allOptions.addAll(new ArrayList JavaDoc(optionsMap.values()));
517         Collections.sort(allOptions, new Comparator JavaDoc()
518         {
519             public int compare(Object JavaDoc inO1, Object JavaDoc inO2)
520             {
521                 Option one = (Option)inO1;
522                 Option two = (Option)inO2;
523                 return one.getName().compareTo(two.getName());
524             }
525         });
526         
527         return allOptions;
528     }
529
530     public void setOptions(Set JavaDoc inOptions)
531     {
532         fieldOptions = inOptions;
533     }
534     public Option getLocalOption( String JavaDoc inId)
535     {
536         for (Iterator JavaDoc iter = getOptions().iterator(); iter.hasNext();)
537         {
538             Option option = (Option ) iter.next();
539             if( option.getId().equals(inId))
540             {
541                 return option;
542             }
543         }
544         return null;
545     }
546     
547     public Option getOption(String JavaDoc inId)
548     {
549         Option option = getLocalOption(inId);
550         if( option == null)
551         {
552             if( fieldProduct == null)
553             {
554                 return null;
555             }
556             return getProduct().getOption(inId);
557         }
558         return option;
559     }
560
561
562 }
563
Popular Tags