Java tutorial
/* * EYWA.COM (Eywa Commerce) * This program is an integrated platform with E-Commerce and Configurator system. * Support: Please, contact the Author on http://www.smartfeeling.org. * Copyright (C) 2014 Gian Angelo Geminiani * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.eywa.impl.app.mongo.entities.items.session.cart; import com.eywa.impl.app.IAppConstants; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import org.ly.commons.util.DateUtils; import org.ly.commons.util.StringUtils; import org.ly.packages.mongo.impl.util.MongoUtils; import java.util.LinkedList; import java.util.List; /** * Item for Session DATA field. * Contains information about cart session */ public class ItemSessionDataCart extends BasicDBObject { // ------------------------------------------------------------------------ // c o n s t a n t s // ------------------------------------------------------------------------ private static final String DEF_CURRENCY = IAppConstants.DEF_CURRENCY; private static final String TYPE_CART = "data-cart"; // ------------------------------------------------------------------------ // f i e l d s // ------------------------------------------------------------------------ public static final String SESSION_ID = IAppConstants.SESSION_ID; // session ID public static final String TYPE = IAppConstants.TYPE; // data type public static final String CREATION_DATE = IAppConstants.CREATION_DATE; // milliseconds public static final String ITEMS_COUNT = IAppConstants.ITEMS_COUNT; public static final String ITEMS_QTY = IAppConstants.ITEMS_QTY; public static final String ITEMS_PRICE = IAppConstants.ITEMS_PRICE; public static final String CURRENCY_CODE = IAppConstants.CURRENCY_CODE; // currency of last product public static final String SELLER_ID = IAppConstants.SELLER_ID; // array of seller id public static final String SELLER_PRICE = IAppConstants.SELLER_PRICE; // map of seller and total price public static final String ITEMS = IAppConstants.ITEMS; // ------------------------------------------------------------------------ // c o n s t r u c t o r // ------------------------------------------------------------------------ public ItemSessionDataCart(final DBObject raw) { this.init(raw); } public ItemSessionDataCart() { this.init(null); } // ------------------------------------------------------------------------ // p u b l i c // ------------------------------------------------------------------------ public int getItemsCount() { return getItemsCount(this); } public void setItemsCount(final int value) { setItemsCount(this, value); } public List<DBObject> getItems() { return getItems(this); } public void setItems(final List<DBObject> value) { setItems(this, value); } public List<DBObject> addItem(final DBObject cart_item) { final List<DBObject> items = getItems(); /** CHECK IF ITEM ALREADY EXISTS (not possible with configurable products) boolean added = this.incItemQty(cart_item, items); if (!added) { items.add(cart_item); } **/ items.add(cart_item); setItems(items); return items; } // ------------------------------------------------------------------------ // p r i v a t e // ------------------------------------------------------------------------ private void init(final DBObject raw) { final long now = DateUtils.now().getTime(); this.put(TYPE, TYPE_CART); this.put(CREATION_DATE, now); if (null != raw) { this.put(ITEMS, getItems(raw)); this.put(ITEMS_COUNT, getItemsCount(raw)); this.put(CREATION_DATE, getCreationDate(raw)); } } private boolean incItemQty(final DBObject cart_item, final List<DBObject> items) { final String cart_item_product_id = ItemSessionDataCartItem.getProductId(cart_item); boolean added = false; for (final DBObject item : items) { final String product_id = ItemSessionDataCartItem.getProductId(item); if (cart_item_product_id.equalsIgnoreCase(product_id)) { added = true; ItemSessionDataCartItem.incQty(item, ItemSessionDataCartItem.getQty(cart_item)); // update description and name because language can be changed ItemSessionDataCartItem.setName(item, ItemSessionDataCartItem.getName(cart_item)); ItemSessionDataCartItem.setDescription(item, ItemSessionDataCartItem.getDescription(cart_item)); } } return added; } // ------------------------------------------------------------------------ // S T A T I C // ------------------------------------------------------------------------ public static boolean is(final DBObject item) { return is(getType(item)); } public static boolean is(final String type) { return StringUtils.hasText(type) && type.equalsIgnoreCase(TYPE_CART); } public static String getType(final DBObject item) { return MongoUtils.getString(item, TYPE); } public static String getSessionId(final DBObject item) { return MongoUtils.getString(item, SESSION_ID); } public static void setSessionId(final DBObject item, final String value) { MongoUtils.put(item, SESSION_ID, value); } public static long getCreationDate(final DBObject item) { return MongoUtils.getLong(item, CREATION_DATE); } public static void setCreationDate(final DBObject item, final long value) { MongoUtils.put(item, CREATION_DATE, value); } public static int getItemsCount(final DBObject item) { return MongoUtils.getInt(item, ITEMS_COUNT); } public static void setItemsCount(final DBObject item, final int value) { MongoUtils.put(item, ITEMS_COUNT, value); } public static double getItemsQty(final DBObject item) { return MongoUtils.getDouble(item, ITEMS_QTY); } public static void setItemsQty(final DBObject item, final double value) { MongoUtils.put(item, ITEMS_QTY, value); } public static double getItemsPrice(final DBObject item) { return MongoUtils.getDouble(item, ITEMS_PRICE); } public static void setItemsPrice(final DBObject item, final double value) { MongoUtils.put(item, ITEMS_PRICE, value); } public static List<String> getSellerId(final DBObject item) { return MongoUtils.getList(item, SELLER_ID); } public static void setSellerId(final DBObject item, final List<String> value) { MongoUtils.put(item, SELLER_ID, value); } public static DBObject getSellerPrice(final DBObject item) { return MongoUtils.getDBObject(item, SELLER_PRICE); } public static void setSellerPrice(final DBObject item, final DBObject value) { MongoUtils.put(item, SELLER_PRICE, value); } public static List<DBObject> getItems(final DBObject item) { return MongoUtils.getList(item, ITEMS); } public static String getCurrencyCode(final DBObject item) { return MongoUtils.getString(item, CURRENCY_CODE, DEF_CURRENCY); } public static void setCurrencyCode(final DBObject item, final String value) { MongoUtils.put(item, CURRENCY_CODE, value); } public static void setItems(final DBObject item, final List<DBObject> value) { MongoUtils.put(item, ITEMS, value); setItemsCount(item, getItems(item).size()); double total_qty = 0; double total_price = 0; final List<String> seller_id = new LinkedList<>(); final DBObject seller_price = new BasicDBObject(); String currency = DEF_CURRENCY; for (final DBObject cart_item : value) { final double price = ItemSessionDataCartItem.getPrice(cart_item); final double qty = ItemSessionDataCartItem.getQty(cart_item); final String seller = ItemSessionDataCartItem.getSellerId(cart_item); currency = ItemSessionDataCartItem.getCurrencyCode(cart_item); total_qty += qty; total_price += price * qty; if (!seller_id.contains(seller)) { seller_id.add(seller); } if (!seller_price.containsField(seller)) { seller_price.put(seller, 0.0); } seller_price.put(seller, MongoUtils.getDouble(seller_price, seller) + price * qty); } setItemsPrice(item, total_price); setItemsQty(item, total_qty); setSellerId(item, seller_id); setSellerPrice(item, seller_price); setCurrencyCode(item, currency); } }