1 4 package com.openedit.store; 5 6 import java.util.Collection ; 7 import java.util.Comparator ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 import java.util.Set ; 12 import java.util.TreeSet ; 13 14 import org.apache.commons.collections.map.ListOrderedMap; 15 import org.openedit.money.Fraction; 16 import org.openedit.money.Money; 17 18 19 23 public class CartItem 24 { 25 protected int fieldQuantity = 1; 26 protected Money fieldYourPrice; 27 protected InventoryItem fieldInventoryItem; 28 protected Boolean fieldBackOrdered; 29 protected Map fieldOptions; 30 protected Map 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 inOptions ) 43 { 44 getOptionsMap().clear(); 45 for (Iterator 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 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 104 120 } 121 public Map getOptionsMap() 122 { 123 if ( fieldOptions == null ) 124 { 125 fieldOptions = ListOrderedMap.decorate(new HashMap ()); 128 } 129 return fieldOptions; 130 } 131 132 public Collection getOptions() 133 { 134 return getOptionsMap().values(); 135 } 136 137 public Set getOtherOptions() 138 { 139 Set rest = new TreeSet ( new Comparator () 141 { 142 public int compare(Object arg0, Object 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 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 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 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 } 203 } 204 return option; 205 } 206 207 public boolean hasOption(String 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 getSku() 238 { 239 String sku = getInventoryItem().getSku(); 240 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 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 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) { 300 return fieldProduct; 301 } 302 return getInventoryItem().getProduct(); 303 } 304 305 public void setProduct(Product inProduct) 306 { 307 fieldProduct = inProduct; 308 } 309 public Boolean getBackOrdered() 310 { 311 return fieldBackOrdered; 312 } 313 314 public void setBackOrdered(boolean inBackOrdered) 315 { 316 fieldBackOrdered = new Boolean (inBackOrdered); 317 } 318 319 public Map getProperties() 320 { 321 return fieldProperties; 322 } 323 324 public void setProperties(Map inProperties) 325 { 326 fieldProperties = inProperties; 327 } 328 329 public void putProperty(String inKey, String inValue) 330 { 331 if (fieldProperties == null) 332 { 333 fieldProperties = new HashMap (); 334 } 335 336 getProperties().put(inKey, inValue); 337 } 338 339 public double getWeight() 340 { 341 return getInventoryItem().getWeight(); 342 } 343 public String getName() 344 { 345 if( fieldInventoryItem == null) 346 { 347 return getProduct().getName(); 348 } 349 return getInventoryItem().getName(); 350 } 351 public String get(String inString) 352 { 353 if( getProperties() == null) 354 { 355 return getInventoryItem().get(inString); 356 } 357 String val = (String )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 |