per.mnn.controller.CartController.java Source code

Java tutorial

Introduction

Here is the source code for per.mnn.controller.CartController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package per.mnn.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import per.mnn.cart.ShoppingCart;
import per.mnn.cart.ShoppingCartItem;
import per.mnn.hibernate.Category;
import per.mnn.hibernate.HibernateUtil;
import per.mnn.hibernate.Product;

/**
 *
 * @author myu
 */
@Controller
public class CartController {

    @RequestMapping(value = "/addToCart", method = RequestMethod.POST)
    public ModelAndView addToCart(HttpServletRequest request, HttpServletResponse respond) {

        ShoppingCart cart = checkCart(request);
        String productId = request.getParameter("productId");
        Session sess = HibernateUtil.getSessionFactory().openSession();

        try {
            if (productId != null && !productId.isEmpty()) {
                Query query = sess.createQuery("FROM Category");
                query.list();
                query = sess.createQuery("FROM Product as p WHERE p.id = :id");
                query.setInteger("id", Integer.parseInt(productId));
                if (!query.list().isEmpty())
                    cart.add((Product) query.list().get(0), 1);
            }
        } finally {
            sess.close();
        }

        return new ModelAndView("redirect:/category");
    }

    @RequestMapping(value = "/viewCart")
    public ModelAndView viewCart(HttpServletRequest request, HttpServletResponse respond) {

        ShoppingCart cart = checkCart(request);
        String clear = request.getParameter("clear");

        if (clear != null && clear.equals("true"))
            cart.clear();

        return new ModelAndView("cart");
    }

    @RequestMapping(value = "/updateCart", method = RequestMethod.POST)
    public ModelAndView updateCart(HttpServletRequest request, HttpServletResponse respond) {

        String productId = request.getParameter("productId");
        String quantity = request.getParameter("quantity");
        ShoppingCart cart = checkCart(request);

        if (productId != null && !productId.isEmpty() && quantity != null && !quantity.isEmpty()) {
            for (ShoppingCartItem item : cart.getItems()) {
                if (item.getProduct().getId() == Integer.parseInt(productId))
                    item.setQuantity(Integer.parseInt(quantity));
            }
        }

        return new ModelAndView("cart");
    }

    private ShoppingCart checkCart(HttpServletRequest request) {

        HttpSession httpSess = request.getSession();
        ShoppingCart cart = (ShoppingCart) httpSess.getAttribute("cart");
        if (cart == null) {
            cart = new ShoppingCart();
            httpSess.setAttribute("cart", cart);
        }

        return cart;
    }
}