[pal-cvs 2802] [546] added cart service

svnno****@sourc***** svnno****@sourc*****
2007年 9月 7日 (金) 17:46:19 JST


Revision: 546
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=pal&view=rev&rev=546
Author:   shinsuke
Date:     2007-09-07 17:46:19 +0900 (Fri, 07 Sep 2007)

Log Message:
-----------
added cart service

Modified Paths:
--------------
    pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/web/user/product/ProductDetailsAction.java

Added Paths:
-----------
    pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/CartService.java
    pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/impl/CartServiceImpl.java


-------------- next part --------------
Added: pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/CartService.java
===================================================================
--- pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/CartService.java	2007-09-07 08:45:49 UTC (rev 545)
+++ pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/CartService.java	2007-09-07 08:46:19 UTC (rev 546)
@@ -0,0 +1,21 @@
+package jp.sf.pal.pompei.service;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+import jp.sf.pal.pompei.exentity.CustomersBasket;
+
+public interface CartService extends Serializable {
+
+    public abstract CustomersBasket getCustomersBasket(BigDecimal customersId,
+            BigDecimal productsId);
+
+    public abstract void addCustomersBasket(CustomersBasket customersBasket);
+
+    public abstract void updateCustomersBasket(CustomersBasket customersBasket);
+
+    public abstract void deleteCustomersBasket(CustomersBasket customersBasket);
+
+    public abstract void cleanupCustomersBasket(BigDecimal customersId);
+
+}
\ No newline at end of file


Property changes on: pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/CartService.java
___________________________________________________________________
Name: svn:eol-style
   + native

Added: pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/impl/CartServiceImpl.java
===================================================================
--- pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/impl/CartServiceImpl.java	2007-09-07 08:45:49 UTC (rev 545)
+++ pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/impl/CartServiceImpl.java	2007-09-07 08:46:19 UTC (rev 546)
@@ -0,0 +1,77 @@
+package jp.sf.pal.pompei.service.impl;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.util.Calendar;
+import java.util.List;
+
+import jp.sf.pal.pompei.cbean.CustomersBasketCB;
+import jp.sf.pal.pompei.exbhv.CustomersBasketBhv;
+import jp.sf.pal.pompei.exentity.CustomersBasket;
+import jp.sf.pal.pompei.service.CartService;
+
+public class CartServiceImpl implements Serializable, CartService {
+
+    /**
+     * 
+     */
+    private static final long serialVersionUID = 2138228904280920698L;
+
+    private CustomersBasketBhv customersBasketBhv;
+
+    /**
+     * @return customersBasketBhv
+     */
+    public CustomersBasketBhv getCustomersBasketBhv() {
+        return customersBasketBhv;
+    }
+
+    /**
+     * @param customersBasketBhv 設定する customersBasketBhv
+     */
+    public void setCustomersBasketBhv(CustomersBasketBhv customersBasketBhv) {
+        this.customersBasketBhv = customersBasketBhv;
+    }
+
+    /* (non-Javadoc)
+     * @see jp.sf.pal.pompei.service.impl.CartService#getCustomersBasket(java.math.BigDecimal, java.math.BigDecimal)
+     */
+    public CustomersBasket getCustomersBasket(BigDecimal customersId,
+            BigDecimal productsId) {
+        CustomersBasketCB cb = new CustomersBasketCB();
+        cb.setupSelect_Customers();
+        cb.setupSelect_Products();
+        cb.query().setCustomersId_Equal(customersId);
+        cb.query().setProductsId_Equal(productsId);
+        return getCustomersBasketBhv().selectEntity(cb);
+    }
+
+    public void addCustomersBasket(CustomersBasket customersBasket) {
+        getCustomersBasketBhv().insert(customersBasket);
+    }
+
+    public void deleteCustomersBasket(CustomersBasket customersBasket) {
+        getCustomersBasketBhv().update(customersBasket);
+    }
+
+    public void updateCustomersBasket(CustomersBasket customersBasket) {
+        getCustomersBasketBhv().delete(customersBasket);
+    }
+
+    public void cleanupCustomersBasket(BigDecimal customersId) {
+        // TODO set expire time
+        Calendar yesterday = Calendar.getInstance();
+        yesterday.add(Calendar.DATE, -1);
+
+        CustomersBasketCB cb = new CustomersBasketCB();
+        cb.query().setCustomersId_Equal(customersId);
+        cb.query().setCustomersBasketDateAdded_LessEqual(
+                new Timestamp(yesterday.getTimeInMillis()));
+        List<CustomersBasket> customersBasketList = getCustomersBasketBhv()
+                .selectList(cb);
+        if (customersBasketList.size() > 0) {
+            getCustomersBasketBhv().deleteList(customersBasketList);
+        }
+    }
+}


Property changes on: pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/service/impl/CartServiceImpl.java
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/web/user/product/ProductDetailsAction.java
===================================================================
--- pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/web/user/product/ProductDetailsAction.java	2007-09-07 08:45:49 UTC (rev 545)
+++ pompei/portlets/pompei-core/trunk/src/main/java/jp/sf/pal/pompei/web/user/product/ProductDetailsAction.java	2007-09-07 08:46:19 UTC (rev 546)
@@ -1,17 +1,23 @@
 package jp.sf.pal.pompei.web.user.product;
 
 import java.io.Serializable;
+import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.util.Date;
 
+import javax.faces.context.FacesContext;
+
 import jp.sf.pal.common.util.FacesMessageUtil;
 import jp.sf.pal.pompei.dxo.ProductDxo;
-import jp.sf.pal.pompei.entity.Cart;
+import jp.sf.pal.pompei.exentity.Customers;
+import jp.sf.pal.pompei.exentity.CustomersBasket;
 import jp.sf.pal.pompei.exentity.Manufacturers;
+import jp.sf.pal.pompei.exentity.Products;
 import jp.sf.pal.pompei.exentity.ProductsDescription;
+import jp.sf.pal.pompei.service.CartService;
+import jp.sf.pal.pompei.service.CustomerService;
 import jp.sf.pal.pompei.service.ProductService;
-import jp.sf.pal.pompei.util.SessionUtil;
 
-import org.seasar.framework.log.Logger;
-
 public class ProductDetailsAction implements Serializable {
 
     /**
@@ -25,11 +31,13 @@
     /**	 */
     private ProductService productService;
 
+    private CustomerService customerService;
+
+    private CartService cartService;
+
     /**	 */
     private ProductDxo productDxo;
 
-    private Logger logger = Logger.getLogger(ProductDetailsAction.class);
-
     public Class<?> initialize() {
         return null;
     }
@@ -55,21 +63,66 @@
      * @return
      */
     public Class<?> doAddCart() {
-        Cart cart = (Cart) SessionUtil.getFromApplicationScope("cart");
+        // check login
+        String remoteUser = FacesContext.getCurrentInstance()
+                .getExternalContext().getRemoteUser();
+        if (remoteUser == null) {
+            // TODO put message
+            return null;
+        }
 
-        if (cart == null) {
-            cart = new Cart();
-            SessionUtil.putToApplicationScope("cart", cart);
+        Customers customers = getCustomerService().getCustomer(remoteUser);
+        if (customers == null) {
+            // TODO put message
+            return null;
         }
 
-        ProductsDescription description = productService
-                .getProdcutsDescription(productDetailsPage.getProductsId());
-        cart.addDescription(description);
-        logger.debug("カートに追加しました");
+        Products products = productService.getProdcuts(productDetailsPage
+                .getProductsId());
+        if (products == null) {
+            //TODO put message
+            return null;
+        }
+        getCartService().cleanupCustomersBasket(customers.getCustomersId());
+
+        CustomersBasket customersBasket = getCartService().getCustomersBasket(
+                customers.getCustomersId(), products.getProductsId());
+        if (customersBasket == null) {
+            //  new
+            customersBasket = new CustomersBasket();
+            customersBasket.setCustomersId(customers.getCustomersId());
+            customersBasket.setProductsId(products.getProductsId());
+            customersBasket.setCustomersBasketQuantity(new BigDecimal(1));
+            customersBasket.setCustomersBasketDateAdded(new Timestamp(
+                    new Date().getTime()));
+            customersBasket.setProducts(products);
+            customersBasket.setFinalPrice(calculateFinalPrice(customersBasket));
+            cartService.addCustomersBasket(customersBasket);
+            // TODO put message
+        } else {
+            //  add
+            customersBasket.setCustomersBasketQuantity(customersBasket
+                    .getCustomersBasketQuantity().add(new BigDecimal(1)));
+            customersBasket.setCustomersBasketDateAdded(new Timestamp(
+                    new Date().getTime()));
+            customersBasket.setFinalPrice(calculateFinalPrice(customersBasket));
+            cartService.updateCustomersBasket(customersBasket);
+            // TODO put message
+        }
+
         FacesMessageUtil.addInfoMessage("added.product.to.cart");
         return null;
     }
 
+    private BigDecimal calculateFinalPrice(CustomersBasket customersBasket) {
+        if (customersBasket != null) {
+            //TODO tax
+            return customersBasket.getProducts().getProductsPrice().pow(
+                    customersBasket.getCustomersBasketQuantity().intValue());
+        }
+        return null;
+    }
+
     /**
      * @return productDetailsPage
      */
@@ -100,12 +153,32 @@
         this.productService = productService;
     }
 
-    public Logger getLogger() {
-        return logger;
+    /**
+     * @return customerService
+     */
+    public CustomerService getCustomerService() {
+        return customerService;
     }
 
-    public void setLogger(Logger logger) {
-        this.logger = logger;
+    /**
+     * @param customerService 設定する customerService
+     */
+    public void setCustomerService(CustomerService customerService) {
+        this.customerService = customerService;
     }
 
+    /**
+     * @return cartService
+     */
+    public CartService getCartService() {
+        return cartService;
+    }
+
+    /**
+     * @param cartService 設定する cartService
+     */
+    public void setCartService(CartService cartService) {
+        this.cartService = cartService;
+    }
+
 }


pal-cvs メーリングリストの案内