Ejemplo n.º 1
0
 @Override
 public Cart getCurrent() {
   RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
   if (requestAttributes != null) {
     HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
     Principal principal =
         (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
     Member member = principal != null ? memberDao.find(principal.getId()) : null;
     if (member != null) {
       Cart cart = member.getCart();
       if (cart != null) {
         if (!cart.hasExpired()) {
           if (!DateUtils.isSameDay(cart.getModifyDate(), new Date())) {
             cart.setModifyDate(new Date());
             cartDao.merge(cart);
           }
           return cart;
         } else {
           cartDao.remove(cart);
         }
       }
     } else {
       String id = WebUtils.getCookie(request, Cart.ID_COOKIE_NAME);
       String key = WebUtils.getCookie(request, Cart.KEY_COOKIE_NAME);
       if (StringUtils.isNotEmpty(id)
           && StringUtils.isNumeric(id)
           && StringUtils.isNotEmpty(key)) {
         Cart cart = cartDao.find(Long.valueOf(id));
         if (cart != null && cart.getMember() == null && StringUtils.equals(cart.getKey(), key)) {
           if (!cart.hasExpired()) {
             if (!DateUtils.isSameDay(cart.getModifyDate(), new Date())) {
               cart.setModifyDate(new Date());
               cartDao.merge(cart);
             }
             return cart;
           } else {
             cartDao.remove(cart);
           }
         }
       }
     }
   }
   return null;
 }