com.kingen.web.StoreRoomController.java Source code

Java tutorial

Introduction

Here is the source code for com.kingen.web.StoreRoomController.java

Source

/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.kingen.web;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kingen.aop.ControllerLogAnnotation;
import com.kingen.bean.StoreRoom;
import com.kingen.service.storeRoom.StoreRoomService;
import com.kingen.util.Json;
import com.kingen.util.JsonResultBuilder;
import com.kingen.util.Page;
import com.kingen.util.TreeConverter;
import com.kingen.util.mapper.BeanMapper;
import com.kingen.vo.TreeNode;

/**
 * 
 * @author wj
 * @date 2016-12-16
 */
@Controller
@RequestMapping(value = "/storeRoom")
public class StoreRoomController extends CommonController {

    private static Logger logger = LoggerFactory.getLogger(StoreRoomController.class);

    @Autowired
    private StoreRoomService service;

    @RequestMapping(value = "/")
    @RequiresPermissions("storeRoom:view")
    @ControllerLogAnnotation(moduleName = "??", option = "?")
    public String execute(Model m, HttpServletResponse response) throws Exception {

        return "storeRoom/tree";
    }

    /**
     * ?grid
     */
    //?jsonstring ?jsonObject
    @RequestMapping(value = "/treeData")
    public @ResponseBody Object data(Page<StoreRoom> page, HttpServletResponse response) {

        List<StoreRoom> all = service.list();
        List<TreeNode> allConverted = BeanMapper.mapList(all, TreeNode.class);

        //      JsonResultBuilder.success(true).data(TreeConverter.toComplexJsonString(allConverted)).json();
        //      Json json = new Json();
        //      List<Map<String, Object>> children = Lists.newArrayList(TreeConverter.tree(allPermissions)); 
        //      json.setChildren(children);//??
        //      json.setSuccess(true);
        //      writeJson(response,json);

        //      return ExtUtils.toComplexJson(offices);

        Map<String, Object> json = Maps.newHashMapWithExpectedSize(1);
        //?chidlren,return toComplexJsonString(offices);?.jsonString???????
        json.put("children", TreeConverter.toComplexTree(allConverted));
        return json;

    }

    @RequestMapping(value = "toEdit")
    public String toEdit(String id, String pid, String action, HttpServletResponse response,
            HttpServletRequest request, Model model) {
        model.addAttribute("action", action);
        model.addAttribute("id", id);//null? ??
        model.addAttribute("pid", pid);//null? ??
        return "storeRoom/edit";
    }

    @RequestMapping(value = "/one")
    public void one(String id, HttpServletResponse response) {
        StoreRoom u = service.unique(id);
        writeJson(response, u);
    }

    /**
     * 
     */
    @ControllerLogAnnotation(moduleName = "??-?", option = "")
    @RequestMapping(value = "deleteCascade")
    public void deleteCascade(String id, HttpServletResponse response) {
        JSONObject json = new JSONObject();
        try {

            service.delCascade(id);
            json = JsonResultBuilder.success(true).msg("?").json();
        } catch (Exception e) {
            json = JsonResultBuilder.success(false).msg("").json();
            logger.error(e.getMessage());
            e.printStackTrace();
        }

        writeJson(response, json);
    }

    /**
     * 
     * @throws Exception 
     */
    @RequestMapping(value = "save")
    @ControllerLogAnnotation(moduleName = "??-?", option = "")
    public void save(StoreRoom data, HttpServletResponse response) {
        JSONObject json = new JSONObject();
        try {
            service.add(data);
            json = JsonResultBuilder.success(true).msg("??").json();
        } catch (Exception e) {//service ?
            // TODO Auto-generated catch block
            e.printStackTrace();
            json = JsonResultBuilder.success(false).msg(e.getMessage()).json();
            logger.error(e.getMessage());

        }
        writeJson(response, json);

    }

    /**
     * 
     */
    @RequestMapping(value = "update")
    @ControllerLogAnnotation(moduleName = "??-?", option = "")
    public void update(StoreRoom data, HttpServletResponse response) {
        JSONObject json = new JSONObject();
        try {
            service.updateForm(data);

            json = JsonResultBuilder.success(true).msg("??").json();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            json = JsonResultBuilder.success(false).msg(e.getMessage()).json();
            logger.error(e.getMessage());
        }

        writeJson(response, json);
    }

}