1 4 package com.openedit.store; 5 6 import java.text.ParseException ; 7 import java.text.SimpleDateFormat ; 8 import java.util.ArrayList ; 9 import java.util.Collection ; 10 import java.util.Collections ; 11 import java.util.Date ; 12 import java.util.HashMap ; 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import java.util.Map ; 17 import java.util.Set ; 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 31 public class Product 32 { 33 protected String fieldName; 34 protected String fieldDescription; 35 protected String fieldId; 36 protected String fieldHandlingChargeLevel; 37 protected Set fieldCatalogs; 38 protected List fieldInventoryItems; 39 protected Category fieldDefaultCatalog; 40 protected Map fieldProperties; 41 protected String fieldDepartment; 42 protected PriceSupport fieldPriceSupport; 43 protected String fieldKeywords; 44 protected boolean fieldAvailable = true; 45 protected List fieldOptions; 46 protected boolean fieldCustomPrice = false; 47 protected boolean fieldTaxExempt = false; 48 protected int fieldOrdering = -1; protected String fieldShippingMethodId; 50 protected List fieldRelatedProductIds; 51 protected String fieldDeliveryType; 52 53 private static final Log log = LogFactory.getLog(Product.class); 54 public Product() 55 { 56 } 57 58 public Product( String 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 getName() 73 { 74 return fieldName; 75 } 76 77 public void setName( String inName ) 78 { 79 fieldName = inName; 80 } 81 85 86 public String getShortDescription() 87 { 88 return get("Short Description"); 89 } 90 91 public void setShortDescription( String inDescription ) 92 { 93 addProperty("Short Description",inDescription); 94 } 95 96 public String toString() 97 { 98 return fieldName; 99 } 100 101 public String getId() 102 { 103 return fieldId; 104 } 105 106 public void setId( String inString ) 107 { 108 fieldId = inString; 109 } 110 public String get(String inAttribute) 111 { 112 Object 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 inKey, String 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 inKey) 137 { 138 if (inKey != null && inKey.length() > 0) 139 { 140 getProperties().remove(inKey); 141 } 142 } 143 144 147 public void addCatalog(Category inCatid) 148 { 149 if ( inCatid == null) 150 { 151 throw new IllegalArgumentException ("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 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 getCatalogs() 177 { 178 if (fieldCatalogs == null) 179 { 180 fieldCatalogs = new HashSet (); 181 } 182 return fieldCatalogs; 183 } 184 public boolean isInCatalog( Category inCat) 185 { 186 for (Iterator 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 getSizes() 214 { 215 List sizes = new ArrayList (); 216 for ( Iterator 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 getSizesSorted() 231 { 232 List sizes = getSizes(); 233 Collections.sort(sizes); 234 return sizes; 235 } 236 237 public List getColors() 238 { 239 List colors = new ArrayList (); 240 for ( Iterator 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 255 public List colorsInSize(Option inSize) 256 { 257 if ( inSize == null || "na".equals(inSize.getValue() ) ) 258 { 259 return getColors(); 260 } 261 List colors = new ArrayList (); 262 for (Iterator iter = getInventoryItems().iterator(); iter.hasNext();) 263 { 264 InventoryItem item = (InventoryItem) iter.next(); 265 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 } 286 return colors; 287 } 288 public List getInventoryItems() 289 { 290 if (fieldInventoryItems == null) 291 { 292 fieldInventoryItems = new ArrayList (); 293 } 294 return fieldInventoryItems; 295 } 296 public void setInventoryItems(List 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 return (Category)getCatalogs().iterator().next(); 332 } 333 return fieldDefaultCatalog; 334 } 335 public Collection getRelatedCatalogs() 336 { 337 Category cat = getDefaultCatalog(); 338 if ( cat.getParentCatalog() != null ) 339 { 340 return cat.getParentCatalog().getChildren(); 341 } 342 else 343 { 344 List list = new ArrayList (); 345 list.add( cat ); 346 return list; 347 } 348 } 349 public void setDefaultCatalog(Category inDefaultCatalog) 350 { 351 fieldDefaultCatalog = inDefaultCatalog; 352 } 353 public Map getProperties() 354 { 355 if ( fieldProperties == null) 356 { 357 fieldProperties = ListOrderedMap.decorate(new HashMap ()); 358 } 359 return fieldProperties; 360 } 361 public String getProperty( String inKey ) 362 { 363 return (String )getProperties().get( inKey ); 364 } 365 public void setProperties(Map inAttributes) 366 { 367 fieldProperties = inAttributes; 368 } 369 public void putAttribute(String inKey, String inValue) 370 { 371 putProperty(inKey, inValue); 372 } 373 public void putProperty(String inKey, String inValue) 374 { 375 if (inValue != null) 376 { 377 getProperties().put(inKey, inValue); 378 } 379 else 380 { 381 getProperties().remove(inKey); 382 } 383 } 384 385 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 getDepartment() { 407 return fieldDepartment; 408 } 409 public void setDepartment(String fieldDepartment) { 410 this.fieldDepartment = fieldDepartment; 411 } 412 413 public boolean isInStock() 414 { 415 for ( Iterator 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 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 return false; 458 } 459 public void addInventoryItem( InventoryItem inItem ) 460 { 461 inItem.setProduct( this ); 462 getInventoryItems().add( inItem ); 463 } 464 465 466 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 inSku) 479 { 480 for (Iterator 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 inOptions) 491 { 492 for (Iterator 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 inOptions) 504 { 505 for (Iterator iter = getInventoryItems().iterator(); iter.hasNext();) 507 { 508 InventoryItem element = (InventoryItem) iter.next(); 509 if ( element.isExactMatch((inOptions) ) ) 510 { 511 return element; 512 } 513 } 514 for (Iterator 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 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().getRetailPrice(); 554 555 } 556 557 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 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 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 getKeywords() 616 { 617 return fieldKeywords; 618 } 619 public void setKeywords( String 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 635 public int getInventoryItemCount() 636 { 637 return getInventoryItems().size(); 638 } 639 642 public void clearCatalogs() 643 { 644 getCatalogs().clear(); 645 } 646 647 public List getOptions() 648 { 649 if (fieldOptions == null) 650 { 651 fieldOptions = new ArrayList (); 652 } 653 return fieldOptions; 654 } 655 656 public List getAllOptions() 657 { 658 Map optionsMap = new HashMap (); 659 660 if( getDefaultCatalog() != null) 661 { 662 List catalogOptions = getDefaultCatalog().getAllOptions(); 663 664 for (Iterator iter = catalogOptions.iterator(); iter.hasNext();) 665 { 666 Option option = (Option) iter.next(); 667 optionsMap.put(option.getId(), option); 668 } 669 } 670 for (Iterator iter = getOptions().iterator(); iter.hasNext();) 671 { 672 Option option = (Option) iter.next(); 673 optionsMap.put(option.getId(), option); 674 } 675 676 return new ArrayList (optionsMap.values()); 677 } 678 679 public void setOptions(List 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 id) 690 { 691 List 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 inOptionId) 708 { 709 for (Iterator 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 getHandlingChargeLevel() 728 { 729 return fieldHandlingChargeLevel; 730 } 731 732 public void setHandlingChargeLevel(String 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 inKey) 756 { 757 String value = get(inKey); 758 if ( value != null) 759 { 760 return true; 761 } 762 for (Iterator 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 prop = item.getPropertiesStartingWith(inKey); 771 if ( prop.size() > 0) 772 { 773 return true; 774 } 775 } 776 return false; 777 } 778 public String getShippingMethodId() 780 { 781 return fieldShippingMethodId; 782 } 783 public void setShippingMethodId(String inShippingMethodId) 784 { 785 fieldShippingMethodId = inShippingMethodId; 786 } 787 788 public List getRelatedProductIds() 789 { 790 return fieldRelatedProductIds; 791 } 792 public String getRelatedProductId() 793 { 794 if ( fieldRelatedProductIds == null || getRelatedProductIds().size() == 0) 795 { 796 return null; 797 } 798 return (String )getRelatedProductIds().get(0); } 800 public void addRelatedProductId( String inProductId ) 801 { 802 if ( fieldRelatedProductIds == null) 803 { 804 fieldRelatedProductIds = new ArrayList (); 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 inProductId ) 818 { 819 getRelatedProductIds().remove( inProductId ); 820 } 821 822 public String getDescription() 823 { 824 return fieldDescription; 825 } 826 827 public void setDescription(String inDescription) 828 { 829 fieldDescription = inDescription; 830 } 831 832 public void addKeyword(String inString) 833 { 834 if ( fieldKeywords == null) 835 { 836 setKeywords(inString); 837 } 838 else 839 { 840 setKeywords( getKeywords() + " , " + inString); 841 } 842 } 843 844 public String getDeliveryType() 845 { 846 return fieldDeliveryType; 847 } 848 849 public void setDeliveryType(String inDeliveryType) 850 { 851 fieldDeliveryType = inDeliveryType; 852 } 853 854 public Date getDate(String inField, String inDateFormat) 855 { 856 String date = get(inField); 857 if( date != null) 858 { 859 SimpleDateFormat format = new SimpleDateFormat (inDateFormat); 860 try 861 { 862 return format.parse(date); 863 } catch (ParseException e) 864 { 865 throw new OpenEditRuntimeException(e); 866 } 867 } 868 return null; 869 } 870 871 872 } | Popular Tags |