1 4 package com.openedit.store; 5 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.Collections ; 9 import java.util.Comparator ; 10 import java.util.HashMap ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.Set ; 15 import java.util.TreeSet ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.openedit.money.Money; 20 21 22 26 public class InventoryItem 27 { 28 protected int fieldQuantityInStock; 29 protected Map fieldProperties; 30 protected Set fieldOptions; 31 protected String fieldSku; 32 protected Product fieldProduct; 33 protected PriceSupport fieldPriceSupport; 34 protected String 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 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 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 getSku() 150 { 151 return fieldSku; 152 } 153 154 public void setSku( String 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 toString() 180 { 181 return "[Item: " + getSku() + " " + getSize() + " " + getColor() + "]"; 182 } 183 184 188 public boolean isExactMatch( Collection inSomeOptions ) 189 { 190 for (Iterator 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 continue; } 201 204 if( option.equals(found) ) 205 { 206 continue; 208 } 209 return false; 211 } 212 return true; 214 } 215 216 217 public boolean isCloseMatch( Collection inOptions ) 218 { 219 for (Iterator 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 inOptions) 239 { 240 for (Iterator 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 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 inId, Collection inOptions) 286 { 287 for (Iterator 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 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 inKey, String inValue ) 324 { 325 getProperties().put( inKey, inValue ); 326 } 327 328 public String get( String inkey ) 329 { 330 return (String ) getProperties().get( inkey ); 331 } 332 333 public Map getProperties() 334 { 335 if (fieldProperties == null) 336 { 337 fieldProperties = new HashMap (); 338 } 339 340 return fieldProperties; 341 } 342 343 public void addProperty(String inKey, String 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 inAttributes ) 356 { 357 fieldProperties = inAttributes; 358 } 359 360 public String 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 387 public List getTiers() 388 { 389 return getPriceSupport().getTiers(); 390 } 391 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 407 public boolean hasOwnPrice() 408 { 409 return fieldPriceSupport != null && fieldPriceSupport.getTiers().size()> 0; 410 } 411 412 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 getDescription() 427 { 428 return fieldDescription; 429 } 430 public void setDescription(String inDescription) 431 { 432 fieldDescription = inDescription; 433 } 434 public List getPropertiesStartingWith(String inKey) 435 { 436 List keys = new ArrayList (); 437 for (Iterator iter = getProperties().keySet().iterator(); iter.hasNext();) 438 { 439 String key = (String ) iter.next(); 440 if ( key.startsWith(inKey)) 441 { 442 keys.add(key); 443 } 444 } 445 Collections.sort(keys); 446 List values = new ArrayList (); 447 for (Iterator iter = keys.iterator(); iter.hasNext();) 448 { 449 String key = (String ) 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 getOptions() 481 { 482 if (fieldOptions == null) 483 { 484 fieldOptions = new TreeSet ( new Comparator () 485 { 486 public int compare(Object arg0, Object 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 getAllOptions() 499 { 500 Map optionsMap = new HashMap (); 501 List productOptions = getProduct().getAllOptions(); 502 503 for (Iterator iter = productOptions.iterator(); iter.hasNext();) 504 { 505 Option option = (Option) iter.next(); 506 optionsMap.put(option.getId(), option); 507 } 508 509 for (Iterator iter = getOptions().iterator(); iter.hasNext();) 510 { 511 Option option = (Option) iter.next(); 512 optionsMap.put(option.getId(), option); 513 } 514 515 List allOptions = new ArrayList (); 516 allOptions.addAll(new ArrayList (optionsMap.values())); 517 Collections.sort(allOptions, new Comparator () 518 { 519 public int compare(Object inO1, Object 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 inOptions) 531 { 532 fieldOptions = inOptions; 533 } 534 public Option getLocalOption( String inId) 535 { 536 for (Iterator 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 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 |