chiron.maxscore.controller.MenuController.java Source code

Java tutorial

Introduction

Here is the source code for chiron.maxscore.controller.MenuController.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 chiron.maxscore.controller;

import chiron.maxscore.entity.Menu;
import chiron.maxscore.jsonwrapper.MenuItem;
import chiron.maxscore.entity.Role;
import chiron.maxscore.service.MenuService;
import chiron.maxscore.util.Tools;
import com.google.common.base.Strings;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
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;

/**
 *
 * @author Diluka
 */
@Controller
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "save", method = RequestMethod.POST)
    @ResponseBody
    public Map save(@ModelAttribute Menu menu, @RequestParam int menuPid) {
        Map<String, Object> map = new HashMap<>();

        if (menuPid != 0) {
            menu.setMenuParent(menuService.getById(menuPid));
        } else {
            menu.setMenuParent(menuService.getRoot());
        }

        menuService.save(menu);
        map.put("success", true);
        map.put("msg", "??" + menu.getMenuTitle() + "????");
        return map;
    }

    @RequestMapping(value = { "search", "s" })
    @ResponseBody
    public Map search(@RequestParam int page, @RequestParam(value = "limit") int pageSize,
            @RequestParam(required = false) String menuTitle) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> conditions = new HashMap<>();

        if (!Strings.isNullOrEmpty(menuTitle)) {
            conditions.put("menu_title", menuTitle);
        }

        List<Menu> list = menuService.search(page, pageSize, conditions);
        int total = menuService.count(conditions);

        List<MenuItem> list2 = new ArrayList<>();

        for (Menu m : list) {
            list2.add(new MenuItem(m));
        }
        map.put("success", true);
        map.put("total", total);
        map.put("list", list2);

        return map;
    }

    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    public Map del(@RequestParam int[] id) {
        Map<String, Object> map = new HashMap<>();

        List<String> l1 = new ArrayList<>();
        List<String> l2 = new ArrayList<>();
        List<String> l3 = new ArrayList<>();

        for (int i = 0; i < id.length; i++) {
            Menu menu = menuService.getById(id[i]);
            if (menu != null) {
                if (menu.getMenuChildren().isEmpty()) {
                    l1.add(menu.getMenuName());
                    menuService.delete(menu);
                } else {
                    l3.add(menu.getMenuName());
                }

            } else {
                l2.add(id[i] + "");
            }
        }

        String s1 = Tools.toArrayString(l1);
        String s2 = Tools.toArrayString(l2);
        String s3 = Tools.toArrayString(l3);

        map.put("success", true);
        map.put("msg", "??" + s1 + "??"
                + (s2.isEmpty() ? "" : ("\n??" + s2 + "????"))
                + (s3.isEmpty() ? "" : ("\n??" + s3 + "????")));
        return map;
    }

    @RequestMapping(value = { "all" })
    @ResponseBody
    public Object all() {
        return menuService.getMenuItemRoot().getChildren();
    }

    @RequestMapping("menu")
    @ResponseBody
    public Object menu(HttpSession session) {
        Role role = (Role) session.getAttribute("role");

        return menuService.getMenuItemRoot(role).getChildren();
    }

    @RequestMapping("allMenu")
    @ResponseBody
    public Object allMenu() {
        return menuService.getAllMenuItem();
    }

    @RequestMapping("getParentMenuById")
    @ResponseBody
    public Menu getParentMenuById(@RequestParam int id) {
        return menuService.getById(id).getMenuParent();
    }

}