com.aistor.modules.cms.web.CategoryController.java Source code

Java tutorial

Introduction

Here is the source code for com.aistor.modules.cms.web.CategoryController.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/Dopas/dopas">Dopas</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.aistor.modules.cms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.aistor.common.config.Global;
import com.aistor.common.mapper.JsonMapper;
import com.aistor.common.web.BaseController;
import com.aistor.modules.cms.entity.Category;
import com.aistor.modules.cms.service.CategoryService;

/**
 * ?Controller
 * @author Zaric
 * @version 2013-3-23
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH + "/cms/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @ModelAttribute("category")
    public Category get(@RequestParam(required = false) Long id) {
        if (id != null) {
            return categoryService.get(id);
        } else {
            return new Category();
        }
    }

    @RequiresPermissions("cms:category:view")
    @RequestMapping(value = { "list", "" })
    public String list(Category category, Model model) {
        List<Category> list = Lists.newArrayList();
        List<Category> sourcelist = categoryService.findByUser(true);
        Category.sortList(list, sourcelist, 1L);
        model.addAttribute("list", list);
        return "modules/cms/categoryList";
    }

    @RequiresPermissions("cms:category:view")
    @RequestMapping(value = "form")
    public String form(Category category, Model model) {
        if (category.getParent() == null || category.getParent().getId() == null) {
            category.setParent(new Category(1L));
        }
        category.setParent(categoryService.get(category.getParent().getId()));
        model.addAttribute("category", category);
        return "modules/cms/categoryForm";
    }

    @RequiresPermissions("cms:category:edit")
    @RequestMapping(value = "save")
    public String save(Category category, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, category)) {
            return form(category, model);
        }
        categoryService.save(category);
        addMessage(redirectAttributes, "??'" + category.getName() + "'?");
        return "redirect:" + Global.ADMIN_PATH + "/cms/category/";
    }

    @RequiresPermissions("cms:category:edit")
    @RequestMapping(value = "delete")
    public String delete(Long id, RedirectAttributes redirectAttributes) {
        if (Category.isRoot(id)) {
            addMessage(redirectAttributes, "?, ????");
        } else {
            categoryService.delete(id);
            addMessage(redirectAttributes, "??");
        }
        return "redirect:" + Global.ADMIN_PATH + "/cms/category/";
    }

    @RequiresUser
    @ResponseBody
    @RequestMapping(value = "treeData")
    public String treeData(String module, @RequestParam(required = false) Long extId,
            HttpServletResponse response) {
        response.setContentType("application/json; charset=UTF-8");
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Category> list = Lists.newArrayList();
        if (StringUtils.isNotBlank(module)) {
            list = categoryService.findByUserAndModule(module);
        } else {
            list = categoryService.findByUser(true);
        }
        for (int i = 0; i < list.size(); i++) {
            Category e = list.get(i);
            if (extId == null || (extId != null && !extId.equals(e.getId())
                    && e.getParentIds().indexOf("," + extId + ",") == -1)) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParent() != null ? e.getParent().getId() : 0);
                map.put("name", e.getName());
                map.put("module", e.getModule());
                mapList.add(map);
            }
        }
        return JsonMapper.getInstance().toJson(mapList);
    }
}