Package web

Source Code of web.IndexController

package web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.WebRequest;

import pojo.Bill;
import pojo.Billdetail;
import pojo.Color;
import pojo.Condition;
import pojo.Deliverstatus;
import pojo.MyInteger;
import pojo.Price;
import pojo.Product;
import pojo.Provider;
import pojo.Rating;
import pojo.Type;
import pojo.User;
import util.CalculatorProducts;
import dao.BillDAO;
import dao.ColorDAO;
import dao.ConditionDAO;
import dao.DeliverstatusDAO;
import dao.PriceDAO;
import dao.ProductDAO;
import dao.ProviderDAO;
import dao.RatingDAO;
import dao.TypeDAO;
import dao.UserDAO;
import dao_impl.BillDAOImpl;
import dao_impl.ColorDAOImpl;
import dao_impl.ConditionDAOImpl;
import dao_impl.DeliverstatusDAOImpl;
import dao_impl.PriceDAOImpl;
import dao_impl.ProductDAOImpl;
import dao_impl.ProviderDAOImpl;
import dao_impl.RatingDAOImpl;
import dao_impl.TypeDAOImpl;
import dao_impl.UserDAOImpl;

@Controller
public class IndexController {

  ProductDAO spDAO = new ProductDAOImpl();
  TypeDAO dmDAO = new TypeDAOImpl();
  ProviderDAO pvDAO = new ProviderDAOImpl();
  ColorDAO colorDAO = new ColorDAOImpl();
  RatingDAO ratingDAO = new RatingDAOImpl();
  PriceDAO priceDAO = new PriceDAOImpl();
  ConditionDAO conditionDAO = new ConditionDAOImpl();
  BillDAO billDAO = new BillDAOImpl();
  UserDAO userDAO = new UserDAOImpl();
  DeliverstatusDAO deliverstatusDAO = new DeliverstatusDAOImpl();
  private static final Logger logger = Logger
      .getLogger(IndexController.class);

  // prepare data
  private void prepareData(Model model, WebRequest request) {
    logger.debug("prepareData start");
    int nSanPham = CalculatorProducts.CountProduct(request);
    List<Type> dms = dmDAO.getListType();
    List<Product> sanPhamMoi = spDAO.getNewListProduct(5);
    List<Provider> providers = pvDAO.getTopProvider(9);
    List<Color> colors = colorDAO.getListColor();
    List<Price> prices = priceDAO.getListPrice();
    List<Rating> ratings = ratingDAO.getListRating();
    List<Condition> conditions = conditionDAO.getListCondition();

    String filter = "-*-*-*-*";
    if (request.getParameter("filter") != null) {
      filter = request.getParameter("filter");
      String[] types = filter.split("\\-");

      List<String[]> filters = new ArrayList<String[]>();
      for (int i = 0; i < types.length; i++) {
        filters.add(types[i].replace("*", "").split("\\|"));
        if (!filters.get(i)[0].equals("")) {
          switch (i) {
          case 0:
            for (int j = 0; j < filters.get(i).length; j++) {
              providers.get(
                  Integer.parseInt(filters.get(i)[j]) - 1)
                  .setChecked(true);
            }
            break;

          case 1:
            for (int j = 0; j < filters.get(i).length; j++) {
              prices.get(Integer.parseInt(filters.get(i)[j]) - 1)
                  .setChecked(true);
            }
            break;

          case 2:
            for (int j = 0; j < filters.get(i).length; j++) {
              conditions.get(
                  Integer.parseInt(filters.get(i)[j]) - 1)
                  .setChecked(true);
            }
            break;

          case 3:
            for (int j = 0; j < filters.get(i).length; j++) {
              ratings.get(Integer.parseInt(filters.get(i)[j]) - 1)
                  .setChecked(true);
            }
            break;

          case 4:
            for (int j = 0; j < filters.get(i).length; j++) {
              colors.get(Integer.parseInt(filters.get(i)[j]) - 1)
                  .setChecked(true);
            }
            break;

          }

        }

      }
    }

    model.addAttribute("nSanPham", nSanPham);
    model.addAttribute("dms", dms);
    model.addAttribute("brands", providers);
    model.addAttribute("prices", prices);
    model.addAttribute("conditions", conditions);
    model.addAttribute("ratings", ratings);
    model.addAttribute("colors", colors);
    model.addAttribute("sanPhamMoi", sanPhamMoi);
    logger.debug("prepareData end");

  }

  // controller index
  @RequestMapping(value = "/index")
  public String index(Model model, WebRequest request) {
    logger.debug("index start");
    if (request.getParameter("signout") != null) {
      request.removeAttribute("TaiKhoan", WebRequest.SCOPE_SESSION);
      request.removeAttribute("HoTen", WebRequest.SCOPE_SESSION);
    }

    prepareData(model, request);
    List<Product> listBestSeller = spDAO.getBestSellerListProduct(6);

    model.addAttribute("listBestSeller", listBestSeller);
    model.addAttribute("page", "index");
    logger.debug("index end");
    return "index";

  }

  // controller products
  @RequestMapping(value = "/products")
  public String products(Model model, WebRequest request) {
    logger.debug("products start");
    prepareData(model, request);

    String id;
    List<Product> sanphams = null;

    if (request.getParameter("id") != null) {
      id = request.getParameter("id");
    } else {
      id = "";
    }

    if (request.getParameter("idSearch") != null) {
      id = request.getParameter("idSearch");
    }

    String page = "";
    if (request.getParameter("p") != null) {
      page = request.getParameter("p");
    } else {
      page = "products";
    }

    if (request.getParameter("Search") != null) {
      page = "search";
    }

    int block = 6;
    if (request.getParameter("block") != null) {
      block = Integer.parseInt(request.getParameter("block"));
    }

    int trang = 1;
    if (request.getParameter("trang") != null) {
      trang = Integer.parseInt(request.getParameter("trang"));
    }

    String filter = "-*-*-*-*";
    if (request.getParameter("filter") != null) {
      filter = request.getParameter("filter");
    }
    String[] types = filter.split("\\-");

    List<String[]> filters = new ArrayList<String[]>();
    for (int i = 0; i < types.length; i++) {
      filters.add(types[i].replace("*", "").split("\\|"));
    }

    MyInteger nPage = new MyInteger(0);
    boolean bSearch = false;
    if (page.equals("search")) {
      bSearch = true;
    }

    List<Product> listProduct = spDAO.findListProduct(id, bSearch, filters,
        block, trang, nPage);

    int soTrang = nPage.getValue();
    model.addAttribute("id", id);
    if (bSearch == true) {
      model.addAttribute("idSearch", id);
    }
    model.addAttribute("trang", trang);
    model.addAttribute("soTrang", soTrang);
    model.addAttribute("sanphams", listProduct);
    model.addAttribute("filter", filter);
    model.addAttribute("p", page);
    model.addAttribute("page", "products");

    logger.debug("products end");
    return "index";
  }

  // controller productdetail
  @RequestMapping(value = "/productdetail")
  public String productdetail(Model model, WebRequest request) {
    logger.debug("productdetail start");
    prepareData(model, request);

    String idSanPham = "";
    if (request.getParameter("idSanPham") != null) {
      idSanPham = request.getParameter("idSanPham");
    }
    Product sp = spDAO.getInfoProduct(idSanPham);
    List<Product> sanPhamLienQuans = spDAO.getListProduct(sp.getType()
        .getIdtype().toString(), 5, 1);

    model.addAttribute("sp", sp);
    model.addAttribute("sanPhamLienQuans", sanPhamLienQuans);

    model.addAttribute("page", "productdetail");
    logger.debug("productdetail end");
    return "index";
  }

  // ajax controoler update value search
  @RequestMapping(value = "/searchupdate.do", method = RequestMethod.POST)
  public @ResponseBody
  String searchUpdate(@RequestParam("filter") String filter,
      @RequestParam("id") String id, @RequestParam("b") String b,
      @RequestParam("p") String p, HttpServletRequest request) {
    logger.debug("searchUpdate start");
    String[] types = filter.split("\\-");

    List<String[]> conditions = new ArrayList<String[]>();
    for (int i = 0; i < types.length; i++) {
      conditions.add(types[i].replace("*", "").split("\\|"));
    }

    int trang = 1;
    int block = Integer.parseInt(b);

    boolean bSearch = false;
    if (p.equals("products")) {
      bSearch = false;
    } else {
      bSearch = true;
    }
    String page = p;

    MyInteger nPage = new MyInteger(0);
    List<Product> listProduct = spDAO.findListProduct(id, bSearch,
        conditions, block, trang, nPage);

    int soTrang = nPage.getValue();

    String html = "";

    html += "<form name='frmSoSanPhamTrenTrang' section='products.do' method='get'>";
    html += "    <h3 style='float: left'>";
    html += "      Page";
    for (int i = 1; i <= soTrang; i++) {
      html += "        <a href='products.do?id=" + id + "&p=" + page
          + "&trang=" + trang + "&block=" + block + "&filter="
          + filter + "'>";
      if (i == trang) {
        html += "              <b>" + i + "</b>";
      } else {

        html += i;
      }

      html += "        </a>";
      if (i < soTrang) {

        html += "                |";
      }
    }
    html += "    </h3>";
    html += "    <div style='float: right'>";
    html += "      <label for='block'>View</label> <select id='block' name='block' onchange='submit();' >";
    for (int i = 1; i <= 5; i++) {
      html += "          <option";
      if (i * 2 == block) {
        html += "                        selected='selected' ";
      }
      html += "            value='" + i * 2 + "'>" + i * 2
          + " per page</option>";
    }
    html += "        <option";
    if (block == -1) {
      html += "                    selected='selected' ";
    }
    html += "          value='-1'>All</option>";
    html += "      </select>";
    html += "    </div>";

    html += "    <input type='hidden' id='id' name='id' value='" + id
        + "' />";
    html += "    <input type='hidden' id='p' name='p' value='" + page
        + "' />";
    html += "    <input type='hidden' id='trang' name='trang' value='"
        + trang + "' />";
    html += "    <input type='hidden' id='filter' name='filter' value='"
        + filter + "' />";
    html += "  </form>";
    html += "  <div class='cleaner'></div>";
    html += "  <hr>";
    html += "  <div class='cleaner'></div>";
    html += "";

    for (int i = 0; i < listProduct.size(); i++) {
      if ((i + 1) % 3 == 0) {
        html += "   <div class='product_box'>";
      } else {
        html += "   <div class='product_box no_margin_right'>";
      }

      html += "<div style='height: 80px'>";
      html += "<p>" + listProduct.get(i).getName() + "</p>";
      html += "</div>";

      html += "<a href='productdetail.do?idSanPham="
          + listProduct.get(i).getIdproduct() + "'><img src='"
          + listProduct.get(i).getPicture()
          + "' alt='2' width='150' height='150'></a>";
      html += "<p class='product_price'>";
      html += listProduct.get(i).getPrice();
      html += "$";
      html += "</p>";
      html += "<a href='shoppingcart.do?idSanPham="
          + listProduct.get(i).getIdproduct()
          + "' class='addtocart'></a> <a href='productdetail.do?idSanPham="
          + listProduct.get(i).getIdproduct()
          + "' class='detail'></a>";
      html += "</div>";
    }

    html += "    </h3>";
    logger.debug("searchUpdate end");
    return html;
  }

  // controller about
  @RequestMapping(value = "/about")
  public String about(Model model, WebRequest request) {
    logger.debug("about start");
    prepareData(model, request);
    model.addAttribute("page", "about");
    logger.debug("about end");
    return "index";

  }

  // controoler login

  @RequestMapping(value = "/login")
  public String login(Model model, WebRequest request) {
    logger.debug("login start");
    prepareData(model, request);
    String page = "login";
    if (request.getParameter("Login") != null) {
      String taikhoan = request.getParameter("taikhoan");
      String matkhau = request.getParameter("matkhau");
      UserDAO khDAO = new UserDAOImpl();
      User kh = khDAO.getUserInfo(taikhoan);
      if (kh != null && matkhau.equals(kh.getPassword())) {

        request.setAttribute("TaiKhoan", taikhoan,
            WebRequest.SCOPE_SESSION);
        request.setAttribute("HoTen", kh.getName(),
            WebRequest.SCOPE_SESSION);
        request.setAttribute("role", kh.getRole(),
            WebRequest.SCOPE_SESSION);
        page = "index";
      } else {
        model.addAttribute("info",
            "Your account and password not valid");
      }
    }

    model.addAttribute("page", page);
    logger.debug("login end");
    return "index";
  }

  // cotroller register
  @RequestMapping(value = "/register")
  public String register(Model model, WebRequest request) {
    logger.debug("register start");
    prepareData(model, request);
    String page = "register";
    if (request.getParameter("DangKi") != null) {
      String taikhoan = request.getParameter("taikhoan");
      String hoten = request.getParameter("hoten");
      String diachi = request.getParameter("diachi");
      String matkhau = request.getParameter("matkhau");
      String dienthoai = request.getParameter("dienthoai");
      String email = request.getParameter("email");
      String zipCode = request.getParameter("zipcode");
      UserDAO khDAO = new UserDAOImpl();
      User khTest = khDAO.getUserInfo(taikhoan);
      if (khTest != null) {
        model.addAttribute("info", "account is available!");
      } else {

        User kh = new User(taikhoan, hoten, diachi, email, dienthoai,
            matkhau, zipCode, null);
        boolean result = khDAO.addUser(kh);
        if (result == true) {
          request.setAttribute("TaiKhoan", taikhoan,
              WebRequest.SCOPE_SESSION);
          request.setAttribute("HoTen", kh.getName(),
              WebRequest.SCOPE_SESSION);
          page = "index";
        }
      }
    }
    model.addAttribute("page", page);
    logger.debug("register end");
    return "index";
  }

  // controller checkout
  @RequestMapping(value = "/checkout")
  public String checkout(Model model, WebRequest request) {
    logger.debug("checkout start");
    prepareData(model, request);
    if (request.getAttribute("TaiKhoan", WebRequest.SCOPE_SESSION) != null) {
      List<Integer> soluongs = new ArrayList<Integer>();
      List<Product> sanphams = new ArrayList<Product>();

      float tongTien = 0;
      if (request.getAttribute("Cart", WebRequest.SCOPE_SESSION) != null
          && ((List<Product>) request.getAttribute("Cart",
              WebRequest.SCOPE_SESSION)).size() != 0) {
        sanphams = (List<Product>) request.getAttribute("Cart",
            WebRequest.SCOPE_SESSION);
        soluongs = (List<Integer>) request.getAttribute("CartSoLuong",
            WebRequest.SCOPE_SESSION);
        for (int i = 0; i < soluongs.size(); i++) {
          tongTien += sanphams.get(i).getPrice() * soluongs.get(i);
        }

        String hoten = request.getParameter("hoten");
        String diachi = request.getParameter("diachi");
        String dienthoai = request.getParameter("dienthoai");

        if (request.getParameter("Checkout") != null) {
          String idKhachHang = (String) request.getAttribute(
              "TaiKhoan", WebRequest.SCOPE_SESSION);

          UserDAO khDAO = new UserDAOImpl();
          User kh = khDAO.getUserInfo(idKhachHang);

          if (hoten.equals("") || diachi.equals("")
              || dienthoai.equals("")) {
            model.addAttribute("notice",
                "Please enter all information");
          } else if (sanphams.size() == 0) {
            model.addAttribute("notice",
                "Please buys something to checkout");
          } else {
            Date ngaymua = new Date();
            Deliverstatus status = deliverstatusDAO
                .getDefaultDeliverstatus();
            BillDAO billDAO = new BillDAOImpl();

            Bill hd = new Bill(status, kh.getIduser(), tongTien,
                hoten, diachi, dienthoai, ngaymua, null);
            billDAO.addBill(hd);

            Set<Billdetail> billdetails = new HashSet<Billdetail>();
            for (int i = 0; i < sanphams.size(); i++) {
              Billdetail dt = new Billdetail(hd, sanphams.get(i),
                  sanphams.get(i).getPrice(), soluongs.get(i));

              billDAO.addBill(dt);

            }

            request.removeAttribute("Cart",
                WebRequest.SCOPE_SESSION);
            request.removeAttribute("CartSoLuong",
                WebRequest.SCOPE_SESSION);
            request.removeAttribute("tongTien",
                WebRequest.SCOPE_SESSION);
            model.addAttribute("nSanPham", 0);
            tongTien = 0;
            model.addAttribute("notice",
                "Success! Thanks for your using");
            // model.addAttribute("success", true);
          }
        } else {
          request.setAttribute("tongTien", tongTien,
              WebRequest.SCOPE_SESSION);
        }
      } else {
        model.addAttribute("notice", "Please buy something to checkout");
        request.setAttribute("tongTien", 0, WebRequest.SCOPE_SESSION);
      }

      model.addAttribute("page", "checkout");
    } else {
      model.addAttribute("page", "login");
    }
    logger.debug("checkout end");
    return "index";
  }

  // controller shoppingcart
  @RequestMapping(value = "/shoppingcart")
  public String shoppingcart(Model model, WebRequest request) {
    logger.debug("shoppingcart start");
    List<Integer> soLuongs = new ArrayList<Integer>();
    List<Product> sanPhams = new ArrayList<Product>();

    if (request.getAttribute("Cart", WebRequest.SCOPE_SESSION) != null) {
      sanPhams = (List<Product>) request.getAttribute("Cart",
          WebRequest.SCOPE_SESSION);
      soLuongs = (List<Integer>) request.getAttribute("CartSoLuong",
          WebRequest.SCOPE_SESSION);
    }

    String idSanPham = "";
    if (request.getParameter("idSanPham") != null) {
      idSanPham = request.getParameter("idSanPham");
      boolean flag = false;
      for (int i = 0; i < sanPhams.size(); i++) {
        if (sanPhams.get(i).getIdproduct().toString().equals(idSanPham)) {
          soLuongs.set(i, soLuongs.get(i) + 1);
          flag = true;
          break;
        }
      }
      if (flag == false) {
        Product sanpham = spDAO.getInfoProduct(idSanPham);
        sanPhams.add(sanpham);
        soLuongs.add(1);
      }
    }

    if (request.getParameter("removeCart") != null) {
      int vt = Integer.parseInt(request.getParameter("removeCart"));
      if (soLuongs.get(vt) > 1) {
        soLuongs.set(vt, soLuongs.get(vt) - 1);
      } else {
        soLuongs.remove(vt);
        sanPhams.remove(vt);
      }

      request.setAttribute("Cart", sanPhams, WebRequest.SCOPE_SESSION);
      request.setAttribute("CartSoLuong", soLuongs,
          WebRequest.SCOPE_SESSION);
    }

    if (request.getParameter("updateCart") != null) {
      for (int i = 0; i < sanPhams.size(); i++) {
        int soluong = Integer.parseInt(request.getParameter("soluong"
            + i));
        if (soluong > 0) {
          soLuongs.set(i, soluong);
        } else {
          soLuongs.remove(i);
          sanPhams.remove(i);
        }
      }
      request.setAttribute("Cart", sanPhams, WebRequest.SCOPE_SESSION);
      request.setAttribute("CartSoLuong", soLuongs,
          WebRequest.SCOPE_SESSION);
    }
    float tongTien = 0;
    for (int i = 0; i < sanPhams.size(); i++) {
      tongTien += sanPhams.get(i).getPrice() * soLuongs.get(i);
    }

    request.setAttribute("Cart", sanPhams, WebRequest.SCOPE_SESSION);
    request.setAttribute("CartSoLuong", soLuongs, WebRequest.SCOPE_SESSION);
    prepareData(model, request);

    model.addAttribute("idSanPham", idSanPham);
    model.addAttribute("tongTien", tongTien);

    model.addAttribute("page", "shoppingcart");
    logger.debug("shoppingcart end");
    return "index";
  }

  // controller history shopping
  @RequestMapping(value = "/shoppinghistory")
  public String shoppinghistory(Model model, WebRequest request) {
    logger.debug("shoppinghistory start");
    prepareData(model, request);
    BillDAO hdDAO = new BillDAOImpl();
    if (request.getAttribute("TaiKhoan", WebRequest.SCOPE_SESSION) != null) {
      String taikhoan = request.getAttribute("TaiKhoan",
          WebRequest.SCOPE_SESSION).toString();
      List<Bill> listHoaDon = hdDAO.getListBill(taikhoan);

      float tongTien = 0;
      for (int i = 0; i < listHoaDon.size(); i++) {
        tongTien += listHoaDon.get(i).getToTal();
      }
      model.addAttribute("listHoaDon", listHoaDon);
      model.addAttribute("tongTien", tongTien);
      model.addAttribute("page", "shoppinghistory");
    } else {
      model.addAttribute("page", "login");
    }
    logger.debug("shoppinghistory end");
    return "index";
  }

  // check Role for user
  public boolean checkRole(WebRequest request) {
    logger.debug("checkRole start");
    if (request.getAttribute("role", WebRequest.SCOPE_SESSION) != null) {
      String role = request
          .getAttribute("role", WebRequest.SCOPE_SESSION).toString();
      if (role.equals("Admin")) {
        logger.debug("checkRole end");
        return true;
      }
      logger.debug("checkRole end");
      return false;
    }
    logger.debug("checkRole end");
    return false;
  }

  // init page index_admin
  @RequestMapping(value = "/index_admin")
  public String index_admin(Model model, WebRequest request) {
    logger.debug("index_admin start");
    if (checkRole(request)) {
      List<Bill> listBill = billDAO.getListBill();
      model.addAttribute("listBill", listBill);

      List<Deliverstatus> listDiliverstatus = deliverstatusDAO
          .getListDeliverstatus();

      model.addAttribute("listDiliverstatus", listDiliverstatus);

      model.addAttribute("page", "index_admin");
    } else {
      prepareData(model, request);
      model.addAttribute("page", "index");
    }
    logger.debug("index_admin end");
    return "index";
  }

  // init page order_admin
  @RequestMapping(value = "/order_admin")
  public String order_admin(Model model, WebRequest request) {
    logger.debug("order_admin start");
    if (request.getParameter("id") != null) {
      Integer id = Integer.parseInt(request.getParameter("id"));
      Integer value = Integer.parseInt(request.getParameter("value"));
      billDAO.updateBill(id, value);
    }
    int trang = 1;
    int block = 10;
    if (request.getParameter("trang") != null) {
      trang = Integer.parseInt(request.getParameter("trang"));

    }
    String operator = "";
    if (request.getParameter("operator") != null) {
      operator = request.getParameter("operator");
    }

    if (operator.equals("<")) {
      trang--;
    } else if (operator.equals(">")) {
      trang++;
    }

    MyInteger nPage = new MyInteger(1);
    List<Bill> listBill = billDAO.getListBill(trang, block, nPage);

    model.addAttribute("listBill", listBill);
    List<Deliverstatus> listDeliverstatus = deliverstatusDAO
        .getListDeliverstatus();

    model.addAttribute("listDeliverstatus", listDeliverstatus);

    model.addAttribute("trang", trang);
    model.addAttribute("nPage", nPage.getValue());
    model.addAttribute("page", "order_admin");
    logger.debug("order_admin end");
    return "index";
  }

  // init page menu_admin
  @RequestMapping(value = "/menu_admin")
  public String menu_admin(Model model, WebRequest request) {
    logger.debug("menu_admin start");
    model.addAttribute("page", "product_admin");
    logger.debug("menu_admin end");
    return "index";

  }

  // init page product_admin
  @RequestMapping(value = "/product_admin")
  public String product_admin(Model model, WebRequest request) {
    logger.debug("product_admin start");
    if (request.getParameter("id") != null) {
      int id = Integer.parseInt(request.getParameter("id"));

      // Product product = spDAO.getInfoProduct(id);
      // model.addAttribute("product", product);
      spDAO.changeActive(id, "0");
    }

    int trang = 1;
    int block = 10;
    if (request.getParameter("trang") != null) {
      trang = Integer.parseInt(request.getParameter("trang"));
    }
    String operator = "";
    if (request.getParameter("operator") != null) {
      operator = request.getParameter("operator");
    }

    if (operator.equals("<")) {
      trang--;
    } else if (operator.equals(">")) {
      trang++;
    }

    MyInteger nPage = new MyInteger(1);
    List<Product> listProduct = spDAO.getListProduct();// (trang, block,
                              // nPage);

    model.addAttribute("listProduct", listProduct);

    model.addAttribute("trang", trang);
    model.addAttribute("nPage", nPage.getValue());
    model.addAttribute("page", "product_admin");
    logger.debug("product_admin end");
    return "index";

  }

  // init page promotion_admin
  @RequestMapping(value = "/promotion_admin")
  public String promotion_admin(Model model, WebRequest request) {
    logger.debug("promotion_admin start");
    model.addAttribute("page", "promotion_admin");
    logger.debug("promotion_admin end");
    return "index";

  }

  // init page user_admin
  @RequestMapping(value = "/user_admin")
  public String user_admin(Model model, WebRequest request) {
    logger.debug("user_admin start");
    if (request.getParameter("id") != null) {
      String id = request.getParameter("id");
      if (request.getParameter("role") != null) {
        String role = request.getParameter("role");
        userDAO.changeRole(id, role);
      } else {
        userDAO.changeState(id);
      }
    }

    int trang = 1;
    int block = 10;
    if (request.getParameter("trang") != null) {
      trang = Integer.parseInt(request.getParameter("trang"));

    }
    String operator = "";
    if (request.getParameter("operator") != null) {
      operator = request.getParameter("operator");
    }

    if (operator.equals("<")) {
      trang--;
    } else if (operator.equals(">")) {
      trang++;
    }

    MyInteger nPage = new MyInteger(1);
    List<User> listUser = userDAO.getListUser(trang, block, nPage);

    model.addAttribute("listUser", listUser);

    model.addAttribute("trang", trang);
    model.addAttribute("nPage", nPage.getValue());
    model.addAttribute("page", "user_admin");
    logger.debug("user_admin end");
    return "index";

  }
}
TOP

Related Classes of web.IndexController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.