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.packages.mongo.impl.util.MongoUtils; /** * */ public class ItemSessionDataCartItem extends BasicDBObject { // ------------------------------------------------------------------------ // c o n s t a n t s // ------------------------------------------------------------------------ private static final String DEF_CURRENCY = IAppConstants.DEF_CURRENCY; public static final String CREATION_DATE = IAppConstants.CREATION_DATE; // milliseconds public static final String PRODUCT_ID = IAppConstants.PRODUCT_ID; // code public static final String SELLER_ID = IAppConstants.SELLER_ID; public static final String UID = IAppConstants.UID; // code public static final String NAME = IAppConstants.NAME; public static final String DESCRIPTION = IAppConstants.DESCRIPTION; public static final String QTY = IAppConstants.QTY; public static final String PRICE = IAppConstants.PRICE; public static final String CURRENCY_CODE = IAppConstants.CURRENCY_CODE; public static final String ATTRIBUTES = IAppConstants.ATTRIBUTES; // configurable (key pair value) public static final String OUTPUTS = IAppConstants.OUTPUTS; // ------------------------------------------------------------------------ // c o n s t r u c t o r // ------------------------------------------------------------------------ public ItemSessionDataCartItem(final DBObject raw) { this.init(raw); } public ItemSessionDataCartItem() { this.init(null); } // ------------------------------------------------------------------------ // p u b l i c // ------------------------------------------------------------------------ public void setAttribute(final String key, final Object value) { final DBObject attributes = getAttributes(this); attributes.put(key, value); setAttributes(this, attributes); } public Object getAttribute(final String key) { final DBObject attributes = getAttributes(this); return attributes.get(key); } // ------------------------------------------------------------------------ // p r i v a t e // ------------------------------------------------------------------------ private void init(final DBObject raw) { final long now = DateUtils.now().getTime(); this.put(CREATION_DATE, now); this.put(QTY, 0.0); this.put(PRICE, 0.0); if (null != raw) { setCreationDate(this, getCreationDate(raw)); setSellerId(this, getSellerId(raw)); setProductId(this, getProductId(raw)); setUid(this, getUid(raw)); setName(this, getName(raw)); setDescription(this, getDescription(raw)); setQty(this, getQty(raw)); setPrice(this, getPrice(raw)); setCurrencyCode(this, getCurrencyCode(raw)); setAttributes(this, getAttributes(raw)); setOutputs(this, getOutputs(raw)); } } // ------------------------------------------------------------------------ // S T A T I C // ------------------------------------------------------------------------ public static long getCreationDate(final DBObject item) { return MongoUtils.getLong(item, CREATION_DATE); } public static void setCreationDate(final DBObject item, final long value) { if (value > 0) { MongoUtils.put(item, CREATION_DATE, value); } } public static String getSellerId(final DBObject item) { return MongoUtils.getString(item, SELLER_ID); } public static void setSellerId(final DBObject item, final String value) { MongoUtils.put(item, SELLER_ID, value); } public static String getProductId(final DBObject item) { return MongoUtils.getString(item, PRODUCT_ID); } public static void setProductId(final DBObject item, final String value) { MongoUtils.put(item, PRODUCT_ID, value); } public static String getUid(final DBObject item) { return MongoUtils.getString(item, UID); } public static void setUid(final DBObject item, final String value) { MongoUtils.put(item, UID, value); } public static String getName(final DBObject item) { return MongoUtils.getString(item, NAME); } public static void setName(final DBObject item, final String value) { MongoUtils.put(item, NAME, value); } public static String getDescription(final DBObject item) { return MongoUtils.getString(item, DESCRIPTION); } public static void setDescription(final DBObject item, final String value) { MongoUtils.put(item, DESCRIPTION, value); } public static double getQty(final DBObject item) { return MongoUtils.getDouble(item, QTY); } public static void setQty(final DBObject item, final double value) { MongoUtils.put(item, QTY, value); } public static void incQty(final DBObject item, final double value) { final double qty = getQty(item); MongoUtils.put(item, QTY, qty + value); } public static double getPrice(final DBObject item) { return MongoUtils.getDouble(item, PRICE); } public static void setPrice(final DBObject item, final double value) { MongoUtils.put(item, PRICE, value); } 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 DBObject getAttributes(final DBObject item) { return MongoUtils.getDBObject(item, ATTRIBUTES); } public static void setAttributes(final DBObject item, final DBObject value) { MongoUtils.put(item, ATTRIBUTES, value); } public static DBObject getOutputs(final DBObject item) { return MongoUtils.getDBObject(item, OUTPUTS); } public static void setOutputs(final DBObject item, final DBObject value) { MongoUtils.put(item, OUTPUTS, value); } }