001: /*
002: * Created on Nov 4, 2004
003: */
004: package org.openedit.store;
005:
006: import java.util.Collection;
007: import java.util.Comparator;
008: import java.util.HashMap;
009: import java.util.Iterator;
010: import java.util.Map;
011: import java.util.Set;
012: import java.util.TreeSet;
013:
014: import org.apache.commons.collections.map.ListOrderedMap;
015: import org.openedit.money.Fraction;
016: import org.openedit.money.Money;
017:
018: /**
019: * @author dbrown
020: *
021: */
022: public class CartItem {
023: protected int fieldQuantity = 1;
024: protected Money fieldYourPrice;
025: protected InventoryItem fieldInventoryItem;
026: protected Boolean fieldBackOrdered;
027: protected Map fieldOptions;
028: protected Map fieldProperties;
029: protected Product fieldProduct;
030: protected String fieldShippingPrefix = "shipping";
031:
032: public CartItem() {
033: super ();
034: }
035:
036: public void addOption(Option inOption) {
037: getOptionsMap().put(inOption.getId(), inOption);
038: }
039:
040: public void setOptions(Set inOptions) {
041: getOptionsMap().clear();
042: for (Iterator iter = inOptions.iterator(); iter.hasNext();) {
043: Option obj = (Option) iter.next();
044: addOption(obj);
045: }
046: }
047:
048: public void removeOption(Option inOption) {
049: getOptionsMap().remove(inOption.getId());
050: }
051:
052: public Money getTotalPrice() {
053: return getYourPrice().multiply(getQuantity());
054: }
055:
056: public Money getYourPrice() {
057: if (fieldYourPrice != null) {
058: return fieldYourPrice;
059: }
060: if (getInventoryItem() == null) {
061: return null;
062: }
063: int quantity = getQuantity();
064: Money price = Money.ZERO;
065: Money priceByQuantity = getInventoryItem()
066: .getYourPriceByQuantity(quantity);
067: if (priceByQuantity != null) {
068: price = price.add(priceByQuantity);
069: }
070: for (Iterator it = getOptions().iterator(); it.hasNext();) {
071: Option option = (Option) it.next();
072: PriceSupport prices = option.getPriceSupport();
073: if (prices != null) {
074: price = prices.getYourPriceByQuantity(quantity).add(
075: price);
076: }
077: }
078: return price;
079: }
080:
081: public void setYourPrice(Money inMoney) {
082: fieldYourPrice = inMoney;
083: }
084:
085: public int getQuantity() {
086: return fieldQuantity;
087: }
088:
089: public void setQuantity(int quantity) throws StoreException {
090: fieldQuantity = quantity;
091:
092: /* Set the cart's backorder quantity. This is for
093: informational purposes only. No inventory quantities
094: are being adjusted (that is done at checkout). A cart
095: item's backorder quantity indicates the portion of the
096: cart item's quantity that is unavailable (not in stock),
097: at the time the cart item's quantity was last changed.*/
098: /*
099: InventoryItem inventoryItem = getInventoryItem();
100: if ( inventoryItem != null )
101: {
102: int quantityInStock = inventoryItem.getQuantityInStock();
103: int remainingStock = quantityInStock - quantity;
104: if (remainingStock < 0)
105: {
106: setBackOrderQuantity(-remainingStock);
107: }
108: else
109: {
110: setBackOrderQuantity(0);
111: }
112: }
113: */
114: }
115:
116: public Map getOptionsMap() {
117: if (fieldOptions == null) {
118: // Options are always sorted so that SKUs can be
119: // easily uniquely generated from a set of options.
120: fieldOptions = ListOrderedMap.decorate(new HashMap());
121: }
122: return fieldOptions;
123: }
124:
125: public Collection getOptions() {
126: return getOptionsMap().values();
127: }
128:
129: public Set getOtherOptions() {
130: //everything but size and color
131: Set rest = new TreeSet(new Comparator() {
132: public int compare(Object arg0, Object arg1) {
133: Option opt0 = (Option) arg0;
134: Option opt1 = (Option) arg1;
135: return opt0.getId().compareTo(opt1.getId());
136: }
137: });
138:
139: for (Iterator iter = getOptions().iterator(); iter.hasNext();) {
140: Option option = (Option) iter.next();
141: if (!option.getId().equals("size")
142: && !option.getId().equals("color")) {
143: rest.add(option);
144: }
145: }
146: return rest;
147: }
148:
149: /**
150: * @param inRate
151: * @return
152: */
153: public Money calculateTax(Fraction inRate) {
154: if (inRate == null) {
155: return Money.ZERO;
156: }
157: return getYourPrice().multiply(inRate);
158: }
159:
160: public InventoryItem getInventoryItem() {
161: if (fieldInventoryItem == null && fieldProduct != null
162: && getProduct().getInventoryItemCount() > 0) {
163: return getProduct().getInventoryItem(0);
164: }
165: return fieldInventoryItem;
166: }
167:
168: public boolean isBackOrdered() {
169: if (fieldBackOrdered != null) {
170: return getBackOrdered().booleanValue();
171: }
172: if (getInventoryItem().isBackOrdered()) {
173: return true;
174: }
175: return getQuantity() > getInventoryItem().getQuantityInStock();
176: }
177:
178: public Option getOption(String inId) {
179: Option option = (Option) getOptionsMap().get(inId);
180: if (option == null) {
181: option = getInventoryItem().getOption(inId);
182: if (option != null) {
183: //make a copy?
184: }
185: }
186: return option;
187: }
188:
189: public boolean hasOption(String inId) {
190: Option option = (Option) getOptionsMap().get(inId);
191: if (option == null) {
192: return false;
193: } else {
194: return true;
195: }
196: }
197:
198: public boolean hasOption(Option inOption) {
199: Option option = (Option) getOptionsMap().get(inOption.getId());
200: if (option == null) {
201: return false;
202: } else {
203: return option.equals(inOption);
204: }
205: }
206:
207: public void setInventoryItem(InventoryItem inInventoryItem) {
208: fieldInventoryItem = inInventoryItem;
209: }
210:
211: public String getSku() {
212: String sku = getInventoryItem().getSku();
213: //add options?
214:
215: return sku;
216: }
217:
218: public boolean isSpecialRequest() {
219: if (hasOption("specialrequest")) {
220: return true;
221: }
222: return false;
223: }
224:
225: public boolean isSize(String inSize) {
226: Option option = getSize();
227: if (option != null) {
228: if (inSize != null && inSize.equals(option.getValue())) {
229: return true;
230: }
231: }
232: if (option == null && inSize == null) {
233: return true;
234: }
235: return false;
236: }
237:
238: public boolean isColor(String inColor) {
239: Option option = getColor();
240: if (option != null) {
241: if (inColor != null && inColor.equals(option.getValue())) {
242: return true;
243: }
244: }
245: if (option == null && inColor == null) {
246: return true;
247: }
248: return false;
249: }
250:
251: public Option getSize() {
252: return getOption("size");
253: }
254:
255: public Option getColor() {
256: return getOption("color");
257: }
258:
259: public Product getProduct() {
260: if (fieldProduct != null) //This is here in case someone wants to use product without inventory ie Archive
261: {
262: return fieldProduct;
263: }
264: if (getInventoryItem() != null) {
265: return getInventoryItem().getProduct();
266: }
267: return null;
268: }
269:
270: public void setProduct(Product inProduct) {
271: fieldProduct = inProduct;
272: }
273:
274: public Boolean getBackOrdered() {
275: return fieldBackOrdered;
276: }
277:
278: public void setBackOrdered(boolean inBackOrdered) {
279: fieldBackOrdered = new Boolean(inBackOrdered);
280: }
281:
282: public Map getProperties() {
283: return fieldProperties;
284: }
285:
286: protected void setProperties(Map inProperties) {
287: fieldProperties = inProperties;
288: }
289:
290: public void putProperty(String inKey, String inValue) {
291: if (fieldProperties == null) {
292: fieldProperties = new HashMap();
293: }
294: if (inValue == null || inValue.length() == 0) {
295: getProperties().remove(inKey);
296: } else {
297: getProperties().put(inKey, inValue);
298: }
299: }
300:
301: public double getWeight() {
302: return getInventoryItem().getWeight();
303: }
304:
305: public String getName() {
306: if (fieldInventoryItem == null) {
307: return getProduct().getName();
308: }
309: return getInventoryItem().getName();
310: }
311:
312: public String get(String inString) {
313: if (getProperties() == null) {
314: return getInventoryItem().get(inString);
315: }
316: String val = (String) getProperties().get(inString);
317: if (val == null) {
318: val = getInventoryItem().get(inString);
319: }
320: return val;
321: }
322:
323: public boolean hasSize() {
324: Option size = getSize();
325: if (size == null || size.getValue() == null) {
326: return false;
327: }
328: return true;
329: }
330:
331: public boolean hasColor() {
332: Option option = getColor();
333: if (option == null || option.getValue() == null) {
334: return false;
335: }
336: return true;
337: }
338:
339: public String getShippingPrefix() {
340: return fieldShippingPrefix;
341: }
342:
343: public void setShippingPrefix(String inShippingPrefix) {
344: fieldShippingPrefix = inShippingPrefix;
345: }
346:
347: }
|