001package de.econda.droid;
002
003import org.json.JSONException;
004import org.json.JSONObject;
005
006import java.util.ArrayList;
007import java.util.List;
008
009
010/**
011 * BasketItem is the representation of a basket/shopping-cart
012 * item (article detail information). 
013 */
014public class BasketItem {
015
016    private String productId;
017    private String productName;
018    private String sku;
019    private String productGroup;
020    private List<String> variants;
021    private double price;
022    private int quantity;
023
024    /**
025     * Create new empty BasketItem
026     */
027    public BasketItem() {
028        this.productId = null;
029        this.productName = null;
030        this.sku = null;
031        this.productGroup = null;
032        this.variants = new ArrayList<>();
033        this.price = 0.0;
034        this.quantity = 0;
035    }
036
037    /**
038     * Create new BasketItem
039     *
040     * @param productId Id of product without variants, several variants of the product can have same productId
041     * @param productName product name
042     * @param sku stock keeping unit, id of product inclusive variants, each variant of a product has its own sku
043     * @param productGroup category of product
044     * @param variants List of variants
045     * @param price price of product
046     * @param quantity Count of this products in basket
047     */
048    public BasketItem(String productId,
049                      String productName,
050                      String sku,
051                      String productGroup,
052                      List<String> variants,
053                      double price,
054                      int quantity) {
055        this.productId = productId;
056        this.productName = productName;
057        this.sku = sku;
058        this.productGroup = productGroup;
059        this.variants = variants;
060        this.price = price;
061        this.quantity = quantity;
062    }
063
064    /**
065     * @param price price of product
066     * @return this Object for concatenating method calls
067     */
068    public BasketItem setPrice(double price) {
069        this.price = price;
070        return this;
071    }
072
073    /**
074     * @param productGroup category of product
075     * @return this Object for concatenating method calls
076     */
077    public BasketItem setProductGroup(String productGroup) {
078        this.productGroup = productGroup;
079        return this;
080    }
081
082    /**
083     * @param productName product name
084     * @return this Object for concatenating method calls
085     */
086    public BasketItem setProductName(String productName) {
087        this.productName = productName;
088        return this;
089    }
090
091    /**
092     * @param quantity Count of this products in basket
093     * @return this Object for concatenating method calls
094     */
095    public BasketItem setQuantity(int quantity) {
096        this.quantity = quantity;
097        return this;
098    }
099
100    /**
101     * @param productId Id of product without variants, several variants of the product can have same productId
102     * @return this Object for concatenating method calls
103     */
104    public BasketItem setProductId(String productId) {
105        this.productId = productId;
106        return this;
107    }
108
109    /**
110     * @param sku stock keeping unit, id of product inclusive variants, each variant of a product has its own sku
111     * @return this Object for concatenating method calls
112     */
113    public BasketItem setSku(String sku) {
114        this.sku = sku;
115        return this;
116    }
117
118    /**
119     * @param variants List of variants
120     * @return this Object for concatenating method calls
121     */
122    public BasketItem setVariants(List<String> variants) {
123        this.variants = variants;
124        return this;
125    }
126
127    void appendBasketItemProperties(JSONObject obj) throws JSONException {
128        addIfNotNull(obj, "pid", productId);
129        addIfNotNull(obj, "name", productName);
130        addIfNotNull(obj, "sku", sku);
131        obj.put("price", price);
132        addIfNotNull(obj, "group", productGroup);
133        obj.put("count", quantity);
134        if (variants!=null){
135            for (int i = 0; i < variants.size(); i++) {
136                String value = variants.get(i);
137                if (value!=null){
138                    obj.put("var" + Integer.toString(i + 1), value);
139                }
140            }
141        }
142    }
143
144    private void addIfNotNull(JSONObject obj, String key, String value) throws JSONException {
145        if (value!=null){
146            obj.put(key, value);
147        }
148    }
149
150}