com.yuga.ygplatform.modules.sys.web.AreaController.java Source code

Java tutorial

Introduction

Here is the source code for com.yuga.ygplatform.modules.sys.web.AreaController.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.yuga.ygplatform.modules.sys.web;

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

import javax.servlet.http.HttpServletResponse;

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.yuga.ygplatform.common.config.Global;
import com.yuga.ygplatform.common.web.BaseController;
import com.yuga.ygplatform.modules.sys.entity.Area;
import com.yuga.ygplatform.modules.sys.service.AreaService;
import com.yuga.ygplatform.modules.sys.utils.UserUtils;

/**
 * Controller
 * 
 * @author ThinkGem
 * @version 2013-5-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/area")
public class AreaController extends BaseController {

    @Autowired
    private AreaService areaService;

    @ModelAttribute("area")
    public Area get(@RequestParam(required = false) Long id) {
        if (id != null) {
            return areaService.get(id);
        } else {
            return new Area();
        }
    }

    @RequestMapping(value = { "list", "" })
    public String list(Area area, Model model) {
        // User user = UserUtils.getUser();
        // if(user.isAdmin()){
        area.setId(1L);
        // }else{
        // area.setId(user.getArea().getId());
        // }
        model.addAttribute("area", area);
        List<Area> list = Lists.newArrayList();
        List<Area> sourcelist = areaService.findAll();
        Area.sortList(list, sourcelist, area.getId());
        model.addAttribute("list", list);
        return "modules/sys/areaList";
    }

    @RequestMapping(value = "form")
    public String form(Area area, Model model) {
        if ((area.getParent() == null) || (area.getParent().getId() == null)) {
            area.setParent(UserUtils.getUser().getOffice().getArea());
        }
        area.setParent(areaService.get(area.getParent().getId()));
        model.addAttribute("area", area);
        return "modules/sys/areaForm";
    }

    @RequestMapping(value = "save")
    public String save(Area area, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, area)) {
            return form(area, model);
        }
        areaService.save(area);
        addMessage(redirectAttributes, "?'" + area.getName() + "'?");
        return "redirect:" + Global.getAdminPath() + "/sys/area/";
    }

    @RequestMapping(value = "delete")
    public String delete(Long id, RedirectAttributes redirectAttributes) {
        if (Area.isAdmin(id)) {
            addMessage(redirectAttributes, ", ???");
        } else {
            areaService.delete(id);
            addMessage(redirectAttributes, "?");
        }
        return "redirect:" + Global.getAdminPath() + "/sys/area/";
    }

    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) Long extId,
            HttpServletResponse response) {
        response.setContentType("application/json; charset=UTF-8");
        List<Map<String, Object>> mapList = Lists.newArrayList();
        // User user = UserUtils.getUser();
        List<Area> list = areaService.findAll();
        for (int i = 0; i < list.size(); i++) {
            Area e = list.get(i);
            if ((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",
                // !user.isAdmin()&&e.getId().equals(user.getArea().getId())?0:e.getParent()!=null?e.getParent().getId():0);
                map.put("pId", e.getParent() != null ? e.getParent().getId() : 0);
                map.put("name", e.getName());
                mapList.add(map);
            }
        }
        return mapList;
    }
}