public void merge(Member member, Cart cart) { if ((member != null) && (cart != null) && (cart.getMember() == null)) { Cart localCart = member.getCart(); if (localCart != null) { Iterator<CartItem> iterator = cart.getCartItems().iterator(); while (iterator.hasNext()) { CartItem cartItem = (CartItem) iterator.next(); Product product = cartItem.getProduct(); if (localCart.contains(product)) { if ((Cart.MAX_PRODUCT_COUNT != null) && (localCart.getCartItems().size() > Cart.MAX_PRODUCT_COUNT.intValue())) continue; CartItem localCartItem = localCart.getCartItem(product); localCartItem.add(cartItem.getQuantity().intValue()); this.cartItemDao.merge(localCartItem); } else { if ((Cart.MAX_PRODUCT_COUNT != null) && (localCart.getCartItems().size() >= Cart.MAX_PRODUCT_COUNT.intValue())) continue; iterator.remove(); cartItem.setCart(localCart); localCart.getCartItems().add(cartItem); this.cartItemDao.merge(cartItem); } } this.cartDao.remove(cart); } else { member.setCart(cart); cart.setMember(member); this.cartDao.merge(cart); } } }
public Cart getCurrent() { RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes(); if (requestAttributes != null) { HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest(); Principal principal = (Principal) httpServletRequest.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME); Member member = principal != null ? (Member) this.memberDao.find(principal.getId()) : null; if (member != null) { Cart cart = member.getCart(); if (cart != null) { if (!cart.hasExpired()) { if (!DateUtils.isSameDay(cart.getUpdateDate(), new Date())) { cart.setUpdateDate(new Date()); this.cartDao.merge(cart); } return cart; } this.cartDao.remove(cart); } } else { String cartIdString = CookieUtils.getCookie(httpServletRequest, "cartId"); String cartKeyString = CookieUtils.getCookie(httpServletRequest, "cartKey"); if ((StringUtils.isNotEmpty(cartIdString)) && (StringUtils.isNumeric(cartIdString)) && (StringUtils.isNotEmpty(cartKeyString))) { Cart cart = (Cart) this.cartDao.find(Long.valueOf(cartIdString)); if ((cart != null) && (cart.getMember() == null) && (StringUtils.equals(cart.getKey(), cartKeyString))) { if (!cart.hasExpired()) { if (!DateUtils.isSameDay(cart.getUpdateDate(), new Date())) { cart.setUpdateDate(new Date()); this.cartDao.merge(cart); } return cart; } this.cartDao.remove(cart); } } } } return null; }