controllers.RenderController.java Source code

Java tutorial

Introduction

Here is the source code for controllers.RenderController.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 controllers;

import dao.DepartsDAO;
import dao.DepartsLeaderboardDAO;
import dao.StaffsLeaderboardDAO;
import dao.RecordsDAO;
import dao.StaffsDAO;
import dao.SummaryDAO;
import entities.Departs;
import entities.DepartsLeaderboard;
import entities.Records;
import entities.Staffs;
import entities.StaffsLeaderboard;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Phan Ba Hai
 */

@Controller
@RequestMapping(method = RequestMethod.GET)
public class RenderController {

    @RequestMapping("index")
    public String index(ModelMap model, HttpSession session) throws Exception {
        SummaryDAO summaryDAO = new SummaryDAO();
        StaffsLeaderboardDAO slDAO = new StaffsLeaderboardDAO();
        model.addAttribute("s", summaryDAO.getSummary());
        model.addAttribute("sls", slDAO.showTopThree());
        return checkVaildSession("index", session);
    }

    @RequestMapping("/management/departs")
    public String QLPB(ModelMap model, HttpSession session) {
        DepartsDAO departsDAO = new DepartsDAO();
        List<Departs> departsList = departsDAO.showAllDeparts();
        model.addAttribute("departs", departsList);
        return checkVaildSession("/management/departs", session);
    }

    @RequestMapping("/management/staffs")
    public String QLNV(ModelMap model, HttpSession session) {
        StaffsDAO staffsDAO = new StaffsDAO();
        DepartsDAO departsDAO = new DepartsDAO();
        List<Staffs> staffsList = staffsDAO.showAllStaffs();
        List<Departs> departsList = departsDAO.showAllDeparts();
        model.addAttribute("staffs", staffsList);
        model.addAttribute("departs", departsList);
        return checkVaildSession("/management/staffs", session);
    }

    @RequestMapping("record")
    public String record(ModelMap model, HttpSession session) {
        RecordsDAO recordsDAO = new RecordsDAO();
        StaffsDAO staffsDAO = new StaffsDAO();
        DepartsDAO departsDAO = new DepartsDAO();
        List<Records> recordsList = recordsDAO.showAllRecords();
        List<Staffs> staffsList = staffsDAO.showAllStaffs();
        List<Departs> departsList = departsDAO.showAllDeparts();
        model.addAttribute("records", recordsList);
        model.addAttribute("staffs", staffsList);
        model.addAttribute("departs", departsList);
        return checkVaildSession("record", session);
    }

    @RequestMapping("leaderboard/staffs")
    public String staffsLeaderboard(ModelMap model, HttpSession session) throws Exception {
        StaffsLeaderboardDAO slDAO = new StaffsLeaderboardDAO();
        List<StaffsLeaderboard> slList = slDAO.showStaffsLeaderboard();
        model.addAttribute("sls", slList);
        return checkVaildSession("leaderboard/staffs", session);
    }

    @RequestMapping("leaderboard/departs")
    public String departsLeaderboard(ModelMap model, HttpSession session) throws Exception {
        DepartsLeaderboardDAO dlDAO = new DepartsLeaderboardDAO();
        List<DepartsLeaderboard> dlList = dlDAO.showDepartsLeaderboard();
        model.addAttribute("dls", dlList);
        return checkVaildSession("leaderboard/departs", session);
    }

    @RequestMapping("forms")
    public String forms() {
        return "forms";
    }

    @RequestMapping("grid")
    public String grid() {
        return "grid";
    }

    @RequestMapping("icons")
    public String icons() {
        return "icons";
    }

    @RequestMapping("login")
    public String login(ModelMap model) {
        model.addAttribute("class", "");
        model.addAttribute("message", "");
        return "login";
    }

    @RequestMapping("morris")
    public String morris() {
        return "morris";
    }

    @RequestMapping("notifications")
    public String notifications() {
        return "notifications";
    }

    @RequestMapping("panels-wells")
    public String panelsWells() {
        return "panels-wells";
    }

    @RequestMapping("tables")
    public String tables() {
        return "tables";
    }

    @RequestMapping("typography")
    public String typography() {
        return "typography";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/login.htm";
    }

    public String checkVaildSession(String page, HttpSession session) {
        try {
            return (session.getAttribute("username") != null) ? page : "redirect:/login.htm";
        } catch (Exception e) {
            return "redirect:/login.htm";
        }
    }
}