KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package com.openedit.store;
5
6 import java.text.ParseException JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Collection JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.HashSet JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.apache.commons.collections.map.ListOrderedMap;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.openedit.money.Money;
23
24 import com.openedit.OpenEditRuntimeException;
25 import com.openedit.page.PageProperty;
26
27 /**
28  * @author cburkey
29  *
30  */

31 public class Product
32 {
33     protected String JavaDoc fieldName;
34     protected String JavaDoc fieldDescription;
35     protected String JavaDoc fieldId;
36     protected String JavaDoc fieldHandlingChargeLevel;
37     protected Set JavaDoc fieldCatalogs;
38     protected List JavaDoc fieldInventoryItems;
39     protected Category fieldDefaultCatalog;
40     protected Map JavaDoc fieldProperties;
41     protected String JavaDoc fieldDepartment;
42     protected PriceSupport fieldPriceSupport;
43     protected String JavaDoc fieldKeywords;
44     protected boolean fieldAvailable = true;
45     protected List JavaDoc fieldOptions;
46     protected boolean fieldCustomPrice = false;
47     protected boolean fieldTaxExempt = false;
48     protected int fieldOrdering = -1; //the order that these product should be shown in a list
49
protected String JavaDoc fieldShippingMethodId;
50     protected List JavaDoc fieldRelatedProductIds;
51     protected String JavaDoc fieldDeliveryType;
52     
53     private static final Log log = LogFactory.getLog(Product.class);
54     public Product()
55     {
56     }
57     
58     public Product( String JavaDoc inName )
59     {
60         setName(inName);
61     }
62
63     public int getOrdering()
64     {
65         return fieldOrdering;
66     }
67     public void setOrdering(int inOrdering)
68     {
69         fieldOrdering = inOrdering;
70     }
71
72     public String JavaDoc getName()
73     {
74         return fieldName;
75     }
76
77     public void setName( String JavaDoc inName )
78     {
79         fieldName = inName;
80     }
81 /**
82  * This is an optional field
83  * @return
84  */

85     
86     public String JavaDoc getShortDescription()
87     {
88         return get("Short Description");
89     }
90
91     public void setShortDescription( String JavaDoc inDescription )
92     {
93         addProperty("Short Description",inDescription);
94     }
95
96     public String JavaDoc toString()
97     {
98         return fieldName;
99     }
100
101     public String JavaDoc getId()
102     {
103         return fieldId;
104     }
105
106     public void setId( String JavaDoc inString )
107     {
108         fieldId = inString;
109     }
110     public String JavaDoc get(String JavaDoc inAttribute)
111     {
112         Object JavaDoc value = getProperties().get(inAttribute);
113         if ( value instanceof PageProperty)
114         {
115             PageProperty prop = (PageProperty)value;
116             return prop.getValue();
117         }
118         if ( value == null)
119         {
120             return null;
121         }
122         return String.valueOf(value);
123     }
124     public void addProperty(String JavaDoc inKey, String JavaDoc inValue)
125     {
126         if ( inValue == null || inValue.length() == 0)
127         {
128             getProperties().remove(inKey);
129         }
130         else
131         {
132             getProperties().put( inKey , inValue);
133         }
134     }
135     
136     public void removeProperty(String JavaDoc inKey)
137     {
138         if (inKey != null && inKey.length() > 0)
139         {
140             getProperties().remove(inKey);
141         }
142     }
143
144     /**
145      * @param inCatid
146      */

147     public void addCatalog(Category inCatid)
148     {
149         if ( inCatid == null)
150         {
151             throw new IllegalArgumentException JavaDoc("Catalogs cannot be null");
152         }
153         if ( !isInCatalog( inCatid ) )
154         {
155             getCatalogs().add(inCatid);
156         }
157     }
158     
159     public void removeCatalog(Category inCatid)
160     {
161         Category found = null;
162         for (Iterator JavaDoc iter = getCatalogs().iterator(); iter.hasNext();)
163         {
164             Category element = (Category) iter.next();
165             if ( element.getId().equals(inCatid.getId()))
166             {
167                 found = element;
168                 break;
169             }
170         }
171         if ( found != null)
172         {
173             getCatalogs().remove( found );
174         }
175     }
176     public Set JavaDoc getCatalogs()
177     {
178         if (fieldCatalogs == null)
179         {
180             fieldCatalogs = new HashSet JavaDoc();
181         }
182         return fieldCatalogs;
183     }
184     public boolean isInCatalog( Category inCat)
185     {
186         for (Iterator JavaDoc iter = getCatalogs().iterator(); iter.hasNext();)
187         {
188             Category element = (Category) iter.next();
189             if ( element.getId().equals(inCat.getId()))
190             {
191                 return true;
192             }
193         }
194         return false;
195     }
196     public Option getDefaultColor()
197     {
198         if ( hasColor())
199         {
200             return (Option) getColors().iterator().next();
201         }
202         return null;
203     }
204     public Option getDefaultSize()
205     {
206         if ( hasSize())
207         {
208             return (Option) getSizesSorted().iterator().next();
209         }
210         return null;
211     }
212     
213     public List JavaDoc getSizes()
214     {
215         List JavaDoc sizes = new ArrayList JavaDoc();
216         for ( Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext(); )
217         {
218             InventoryItem item = (InventoryItem) iter.next();
219             if ( item.hasSize() )
220             {
221                 Option size = item.getSize();
222                 if ( !sizes.contains( size ) )
223                 {
224                     sizes.add( item.getSize() );
225                 }
226             }
227         }
228         return sizes;
229     }
230     public List JavaDoc getSizesSorted()
231     {
232         List JavaDoc sizes = getSizes();
233         Collections.sort(sizes);
234         return sizes;
235     }
236     
237     public List JavaDoc getColors()
238     {
239         List JavaDoc colors = new ArrayList JavaDoc();
240         for ( Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext(); )
241         {
242             InventoryItem item = (InventoryItem) iter.next();
243             if ( item.hasColor() && !colors.contains( item.getColor() ) )
244             {
245                 colors.add( item.getColor() );
246             }
247         }
248         return colors;
249     }
250     /**
251      * List all the colors available in this size
252      * @param inSize
253      * @return
254      */

255     public List JavaDoc colorsInSize(Option inSize)
256     {
257         if ( inSize == null || "na".equals(inSize.getValue() ) )
258         {
259             return getColors();
260         }
261         List JavaDoc colors = new ArrayList JavaDoc();
262         for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
263         {
264             InventoryItem item = (InventoryItem) iter.next();
265             //if( item.isInStock())
266
//{
267
boolean add = false;
268                 if ( item.getSize() == null)
269                 {
270                     add = true;
271                 }
272                 else if ( inSize.equals(item.getSize()) )
273                 {
274                     add = true;
275                 }
276                 if ( item.getColor() == null || colors.contains(item.getColor() ) )
277                 {
278                     add = false;
279                 }
280                 if ( add )
281                 {
282                     colors.add(item.getColor());
283                 }
284             //}
285
}
286         return colors;
287     }
288     public List JavaDoc getInventoryItems()
289     {
290         if (fieldInventoryItems == null)
291         {
292             fieldInventoryItems = new ArrayList JavaDoc();
293         }
294         return fieldInventoryItems;
295     }
296     public void setInventoryItems(List JavaDoc inItems)
297     {
298         fieldInventoryItems = inItems;
299     }
300     public boolean hasSizes()
301     {
302         return getSizes().size() > 1;
303     }
304     public boolean hasColors()
305     {
306         return getColors().size() > 1;
307     }
308     
309     public boolean hasSize()
310     {
311         if ( getSizes().size() == 0)
312         {
313             return false;
314         }
315         return true;
316     }
317     
318     public boolean hasColor()
319     {
320         if ( getColors().size() == 0)
321         {
322             return false;
323         }
324         return true;
325     }
326     public Category getDefaultCatalog()
327     {
328         if ( fieldDefaultCatalog == null && getCatalogs().size() >= 1)
329         {
330             //grab the one
331
return (Category)getCatalogs().iterator().next();
332         }
333         return fieldDefaultCatalog;
334     }
335     public Collection JavaDoc getRelatedCatalogs()
336     {
337         Category cat = getDefaultCatalog();
338         if ( cat.getParentCatalog() != null )
339         {
340             return cat.getParentCatalog().getChildren();
341         }
342         else
343         {
344             List JavaDoc list = new ArrayList JavaDoc();
345             list.add( cat );
346             return list;
347         }
348     }
349     public void setDefaultCatalog(Category inDefaultCatalog)
350     {
351         fieldDefaultCatalog = inDefaultCatalog;
352     }
353     public Map JavaDoc getProperties()
354     {
355         if ( fieldProperties == null)
356         {
357             fieldProperties = ListOrderedMap.decorate(new HashMap JavaDoc());
358         }
359         return fieldProperties;
360     }
361     public String JavaDoc getProperty( String JavaDoc inKey )
362     {
363         return (String JavaDoc)getProperties().get( inKey );
364     }
365     public void setProperties(Map JavaDoc inAttributes)
366     {
367         fieldProperties = inAttributes;
368     }
369     public void putAttribute(String JavaDoc inKey, String JavaDoc inValue)
370     {
371         putProperty(inKey, inValue);
372     }
373     public void putProperty(String JavaDoc inKey, String JavaDoc inValue)
374     {
375         if (inValue != null)
376         {
377             getProperties().put(inKey, inValue);
378         }
379         else
380         {
381             getProperties().remove(inKey);
382         }
383     }
384     
385     /**
386      *
387      */

388     public void clearUserCatalogs()
389     {
390         Category[] copy = (Category[])getCatalogs().toArray(new Category[getCatalogs().size()]);
391         for (int i = 0; i < copy.length; i++)
392         {
393             if ( copy[i].isUserSelected() )
394             {
395                 removeCatalog(copy[i]);
396             }
397         }
398     }
399     public void clearItems()
400     {
401         if ( fieldInventoryItems != null)
402         {
403             getInventoryItems().clear();
404         }
405     }
406     public String JavaDoc getDepartment() {
407         return fieldDepartment;
408     }
409     public void setDepartment(String JavaDoc fieldDepartment) {
410         this.fieldDepartment = fieldDepartment;
411     }
412     
413     public boolean isInStock()
414     {
415         for ( Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext(); )
416         {
417             InventoryItem item = (InventoryItem) iter.next();
418             if ( item.isInStock() )
419             {
420                 return true;
421             }
422         }
423         return false;
424     }
425     public boolean isPartlyOutOfStock()
426     {
427         for ( Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext(); )
428         {
429             InventoryItem item = (InventoryItem) iter.next();
430             if ( !item.isInStock() )
431             {
432                 return true;
433             }
434         }
435         return false;
436     }
437
438     public boolean isOnSale()
439     {
440         if ( hasProductLevelPricing() )
441         {
442             return getPriceSupport().isOnSale();
443         }
444         InventoryItem item = getInventoryItem(0);
445         if ( item != null)
446         {
447             return item.isOnSale();
448         }
449 /* for ( Iterator iter = getInventoryItems().iterator(); iter.hasNext(); )
450         {
451             Item item = (Item) iter.next();
452             if ( item.isOnSale() )
453             {
454                 return true;
455             }
456         }
457 */
return false;
458     }
459     public void addInventoryItem( InventoryItem inItem )
460     {
461         inItem.setProduct( this );
462         getInventoryItems().add( inItem );
463     }
464
465     
466     /**
467      * @param inI
468      * @return
469      */

470     public InventoryItem getInventoryItem(int inI)
471     {
472         if( getInventoryItems().size() > inI)
473         {
474             return (InventoryItem)getInventoryItems().get(inI);
475         }
476         return null;
477     }
478     public InventoryItem getInventoryItemBySku( String JavaDoc inSku)
479     {
480         for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
481         {
482             InventoryItem element = (InventoryItem) iter.next();
483             if ( element.getSku().equals(inSku))
484             {
485                 return element;
486             }
487         }
488         return null;
489     }
490     public InventoryItem getInventoryItemByOptions(Collection JavaDoc inOptions)
491     {
492         for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
493         {
494             InventoryItem element = (InventoryItem) iter.next();
495             if ( element.isExactMatch(inOptions))
496             {
497                 return element;
498             }
499         }
500         return null;
501     }
502
503     public InventoryItem getCloseInventoryItemByOptions(Set JavaDoc inOptions)
504     {
505         //First check for exact match. Then size color only, then size only then color only
506
for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
507         {
508             InventoryItem element = (InventoryItem) iter.next();
509             if ( element.isExactMatch((inOptions) ) )
510             {
511                 return element;
512             }
513         }
514         //size color
515
for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
516         {
517             InventoryItem element = (InventoryItem) iter.next();
518             if ( element.isCloseMatch((inOptions) ) )
519             {
520                 return element;
521             }
522         }
523         for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
524         {
525             InventoryItem element = (InventoryItem) iter.next();
526             if ( element.isAnyMatch( inOptions))
527             {
528                 return element;
529             }
530         }
531         if( getInventoryItemCount() > 0)
532         {
533             return (InventoryItem)getInventoryItems().get(0);
534         }
535         return null;
536     }
537
538     public boolean hasProductLevelPricing()
539     {
540         return fieldPriceSupport != null;
541     }
542     public PriceSupport getPriceSupport()
543     {
544         return fieldPriceSupport;
545     }
546     public void setPriceSupport( PriceSupport priceSupport )
547     {
548         fieldPriceSupport = priceSupport;
549     }
550     public Money getRetailUnitPrice()
551     {
552         //return getPriceSupport().getPrice();
553
return getPriceSupport().getRetailPrice();
554         
555     }
556     
557     //** this is the lowest price avialable
558
public Money getYourPrice()
559     {
560         if( hasProductLevelPricing() )
561         {
562             Money price = getPriceSupport().getYourPriceByQuantity( 1 );
563             return price;
564         }
565         else if (getInventoryItemCount() > 0)
566         {
567             // ask cb
568
Money price = getInventoryItem(0).getYourPrice();
569             if (price != null)
570             {
571                 return price;
572             }
573             else
574             {
575                 return Money.ZERO;
576             }
577         }
578         else
579         {
580             return Money.ZERO;
581         }
582     }
583     
584     public Money getRetailPrice()
585     {
586         Money price = null;
587         if( hasProductLevelPricing() )
588         {
589             price = getPriceSupport().getRetailPrice();
590         }
591         else if (getInventoryItemCount() > 0)
592         {
593             price = getInventoryItem(0).getRetailPrice();
594         }
595         else
596         {
597             price = Money.ZERO;
598         }
599         return price;
600     }
601     
602     
603     /**
604      * @param inQuantity
605      * @param money
606      */

607     public void addTierPrice( int inQuantity, Price inPrice )
608     {
609         if (getPriceSupport() == null)
610         {
611             setPriceSupport(new PriceSupport());
612         }
613         getPriceSupport().addTierPrice( inQuantity, inPrice );
614     }
615     public String JavaDoc getKeywords()
616     {
617         return fieldKeywords;
618     }
619     public void setKeywords( String JavaDoc keywords )
620     {
621         fieldKeywords = keywords;
622     }
623     public boolean isAvailable()
624     {
625         return fieldAvailable;
626     }
627     public void setAvailable(boolean inAvailable)
628     {
629         fieldAvailable = inAvailable;
630     }
631
632     /**
633      * @return
634      */

635     public int getInventoryItemCount()
636     {
637         return getInventoryItems().size();
638     }
639     /**
640      *
641      */

642     public void clearCatalogs()
643     {
644         getCatalogs().clear();
645     }
646
647     public List JavaDoc getOptions()
648     {
649         if (fieldOptions == null)
650         {
651             fieldOptions = new ArrayList JavaDoc();
652         }
653         return fieldOptions;
654     }
655     
656     public List JavaDoc getAllOptions()
657     {
658         Map JavaDoc optionsMap = new HashMap JavaDoc();
659
660         if( getDefaultCatalog() != null)
661         {
662             List JavaDoc catalogOptions = getDefaultCatalog().getAllOptions();
663     
664             for (Iterator JavaDoc iter = catalogOptions.iterator(); iter.hasNext();)
665             {
666                 Option option = (Option) iter.next();
667                 optionsMap.put(option.getId(), option);
668             }
669         }
670         for (Iterator JavaDoc iter = getOptions().iterator(); iter.hasNext();)
671         {
672             Option option = (Option) iter.next();
673             optionsMap.put(option.getId(), option);
674         }
675         
676         return new ArrayList JavaDoc(optionsMap.values());
677     }
678
679     public void setOptions(List JavaDoc inOptions)
680     {
681         fieldOptions = inOptions;
682     }
683
684     public void addOption(Option inOption)
685     {
686         getOptions().add(inOption);
687     }
688     
689     public void removeOption(String JavaDoc id)
690     {
691         List JavaDoc options = getOptions();
692         for (int i = 0; i < options.size(); i++)
693         {
694             Option option = (Option)options.get(i);
695             if (option.getId().equals( id ) )
696             {
697                 getOptions().remove(i);
698             }
699         }
700     }
701
702     public void clearOptions()
703     {
704         getOptions().clear();
705     }
706
707     public Option getOption(String JavaDoc inOptionId)
708     {
709         for (Iterator JavaDoc it = getOptions().iterator(); it.hasNext();)
710         {
711             Option option = (Option)it.next();
712             if (inOptionId.equals(option.getId()))
713             {
714                 return option;
715             }
716         }
717         
718         if (getDefaultCatalog() != null)
719         {
720             return getDefaultCatalog().getOption(inOptionId);
721         }
722
723         
724         return null;
725     }
726
727     public String JavaDoc getHandlingChargeLevel()
728     {
729         return fieldHandlingChargeLevel;
730     }
731
732     public void setHandlingChargeLevel(String JavaDoc inHandlingChargeLevel)
733     {
734         fieldHandlingChargeLevel = inHandlingChargeLevel;
735     }
736     
737     public boolean isCustomPrice()
738     {
739         return fieldCustomPrice;
740     }
741
742     public void setCustomPrice(boolean inCustomPrice)
743     {
744         fieldCustomPrice = inCustomPrice;
745     }
746
747     public boolean isTaxExempt()
748     {
749         return fieldTaxExempt;
750     }
751     public void setTaxExempt(boolean inTaxExempt)
752     {
753         fieldTaxExempt = inTaxExempt;
754     }
755     public boolean hasProperty(String JavaDoc inKey)
756     {
757         String JavaDoc value = get(inKey);
758         if ( value != null)
759         {
760             return true;
761         }
762         for (Iterator JavaDoc iter = getInventoryItems().iterator(); iter.hasNext();)
763         {
764             InventoryItem item = (InventoryItem) iter.next();
765             value = item.get(inKey);
766             if ( value != null)
767             {
768                 return true;
769             }
770             List JavaDoc prop = item.getPropertiesStartingWith(inKey);
771             if ( prop.size() > 0)
772             {
773                 return true;
774             }
775         }
776         return false;
777     }
778     //this is a fixed method, this is optional since the cart has a range of methods available
779
public String JavaDoc getShippingMethodId()
780     {
781         return fieldShippingMethodId;
782     }
783     public void setShippingMethodId(String JavaDoc inShippingMethodId)
784     {
785         fieldShippingMethodId = inShippingMethodId;
786     }
787
788     public List JavaDoc getRelatedProductIds()
789     {
790         return fieldRelatedProductIds;
791     }
792     public String JavaDoc getRelatedProductId()
793     {
794         if ( fieldRelatedProductIds == null || getRelatedProductIds().size() == 0)
795         {
796             return null;
797         }
798         return (String JavaDoc)getRelatedProductIds().get(0); //grab the first one
799
}
800     public void addRelatedProductId( String JavaDoc inProductId )
801     {
802         if ( fieldRelatedProductIds == null)
803         {
804             fieldRelatedProductIds = new ArrayList JavaDoc();
805         }
806         getRelatedProductIds().add( inProductId );
807     }
808     
809     public void clearRelatedProductIds()
810     {
811         if ( fieldRelatedProductIds != null)
812         {
813             getRelatedProductIds().clear();
814         }
815     }
816     
817     public void removeRelatedProductId( String JavaDoc inProductId )
818     {
819         getRelatedProductIds().remove( inProductId );
820     }
821
822     public String JavaDoc getDescription()
823     {
824         return fieldDescription;
825     }
826
827     public void setDescription(String JavaDoc inDescription)
828     {
829         fieldDescription = inDescription;
830     }
831
832     public void addKeyword(String JavaDoc inString)
833     {
834         if ( fieldKeywords == null)
835         {
836             setKeywords(inString);
837         }
838         else
839         {
840             setKeywords( getKeywords() + " , " + inString);
841         }
842     }
843
844     public String JavaDoc getDeliveryType()
845     {
846         return fieldDeliveryType;
847     }
848
849     public void setDeliveryType(String JavaDoc inDeliveryType)
850     {
851         fieldDeliveryType = inDeliveryType;
852     }
853
854     public Date JavaDoc getDate(String JavaDoc inField, String JavaDoc inDateFormat)
855     {
856         String JavaDoc date = get(inField);
857         if( date != null)
858         {
859             SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc(inDateFormat);
860             try
861             {
862                 return format.parse(date);
863             } catch (ParseException JavaDoc e)
864             {
865                 throw new OpenEditRuntimeException(e);
866             }
867         }
868         return null;
869     }
870
871
872 }
Popular Tags