com.glaf.base.modules.sys.springmvc.SysDictoryController.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.base.modules.sys.springmvc.SysDictoryController.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.glaf.base.modules.sys.springmvc;

import java.io.IOException;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.glaf.base.modules.BaseDataManager;
import com.glaf.base.modules.Constants;
import com.glaf.base.modules.sys.model.Dictory;
import com.glaf.base.modules.sys.model.DictoryDefinition;
import com.glaf.base.modules.sys.model.SysTree;
import com.glaf.base.modules.sys.query.DictoryQuery;
import com.glaf.base.modules.sys.service.DictoryDefinitionService;
import com.glaf.base.modules.sys.service.DictoryService;
import com.glaf.base.modules.sys.service.SysTreeService;
import com.glaf.base.utils.ParamUtil;
import com.glaf.core.config.ViewProperties;
import com.glaf.core.execution.MultiDBHibernateSchemaUpdate;
import com.glaf.core.res.MessageUtils;
import com.glaf.core.res.ViewMessage;
import com.glaf.core.res.ViewMessages;
import com.glaf.core.util.JsonUtils;
import com.glaf.core.util.PageResult;
import com.glaf.core.util.Paging;
import com.glaf.core.util.ParamUtils;
import com.glaf.core.util.RequestUtils;
import com.glaf.core.util.ResponseUtils;
import com.glaf.core.util.Tools;

@Controller("/sys/dictory")
@RequestMapping("/sys/dictory.do")
public class SysDictoryController {
    private static final Log logger = LogFactory.getLog(SysDictoryController.class);

    protected DictoryDefinitionService dictoryDefinitionService;

    private DictoryService dictoryService;

    private SysTreeService sysTreeService;

    /**
     * ??
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=batchDelete")
    public ModelAndView batchDelete(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        boolean ret = true;
        long[] id = ParamUtil.getLongParameterValues(request, "id");
        ret = dictoryService.deleteAll(id);
        ViewMessages messages = new ViewMessages();
        if (ret) {// ??
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.delete_success"));
        } else { // 
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.delete_failure"));
        }
        MessageUtils.addMessages(request, messages);
        return new ModelAndView("show_msg2", modelMap);
    }

    @RequestMapping(params = "method=json")
    @ResponseBody
    public byte[] json(HttpServletRequest request) throws IOException {
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        DictoryQuery query = new DictoryQuery();
        query.setNodeId(-1L);
        Tools.populate(query, params);

        String gridType = ParamUtils.getString(params, "gridType");
        if (gridType == null) {
            gridType = "easyui";
        }
        int start = 0;
        int limit = 10;
        String orderName = null;
        String order = null;

        int pageNo = ParamUtils.getInt(params, "page");
        limit = ParamUtils.getInt(params, "rows");
        start = (pageNo - 1) * limit;
        orderName = ParamUtils.getString(params, "sortName");
        order = ParamUtils.getString(params, "sortOrder");

        if (start < 0) {
            start = 0;
        }

        if (limit <= 0) {
            limit = Paging.DEFAULT_PAGE_SIZE;
        }

        JSONObject result = new JSONObject();
        int total = dictoryService.getDictoryCountByQueryCriteria(query);
        if (total > 0) {
            result.put("total", total);
            result.put("totalCount", total);
            result.put("totalRecords", total);
            result.put("start", start);
            result.put("startIndex", start);
            result.put("limit", limit);
            result.put("pageSize", limit);

            if (StringUtils.isNotEmpty(orderName)) {
                query.setSortOrder(orderName);
                if (StringUtils.equals(order, "desc")) {
                    query.setSortOrder(" desc ");
                }
            }

            List<Dictory> list = dictoryService.getDictorysByQueryCriteria(start, limit, query);

            if (list != null && !list.isEmpty()) {
                JSONArray rowsJSON = new JSONArray();

                result.put("rows", rowsJSON);

                for (Dictory dictory : list) {
                    JSONObject rowJSON = dictory.toJsonObject();
                    rowJSON.put("id", dictory.getId());
                    rowJSON.put("startIndex", ++start);
                    rowsJSON.add(rowJSON);
                }

            }
        } else {
            result.put("total", total);
            result.put("totalCount", total);
            JSONArray rowsJSON = new JSONArray();
            result.put("rows", rowsJSON);
        }
        return result.toString().getBytes("UTF-8");
    }

    @RequestMapping
    public ModelAndView list(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        String x_query = request.getParameter("x_query");
        if (StringUtils.equals(x_query, "true")) {
            Map<String, Object> paramMap = RequestUtils.getParameterMap(request);
            String x_complex_query = JsonUtils.encode(paramMap);
            x_complex_query = RequestUtils.encodeString(x_complex_query);
            request.setAttribute("x_complex_query", x_complex_query);
        } else {
            request.setAttribute("x_complex_query", "");
        }

        String x_view = ViewProperties.getString("dictory.list");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        String view = request.getParameter("view");
        if (StringUtils.isNotEmpty(view)) {
            return new ModelAndView(view, modelMap);
        }

        return new ModelAndView("/modules/sys/dictory/list", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=loadDictory")
    public ModelAndView loadDictory(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);

        String x_view = ViewProperties.getString("dictory.loadDictory");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/sys/dictory/dictory_load", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=prepareAdd")
    public ModelAndView prepareAdd(HttpServletRequest request, ModelMap modelMap) {
        // ?
        RequestUtils.setRequestParameterToAttribute(request);
        request.removeAttribute("parent");
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        Long nodeId = ParamUtils.getLong(params, "parent");
        if (nodeId > 0) {
            List<DictoryDefinition> list = dictoryDefinitionService.getDictoryDefinitions(nodeId, "sys_dictory");
            if (list != null && !list.isEmpty()) {
                Collections.sort(list);
                request.setAttribute("list", list);
            }
        }

        String x_view = ViewProperties.getString("dictory.prepareAdd");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/sys/dictory/dictory_add", modelMap);
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=prepareModify")
    public ModelAndView prepareModify(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        request.removeAttribute("parent");
        long id = ParamUtil.getIntParameter(request, "id", 0);
        Dictory bean = dictoryService.find(id);
        request.setAttribute("bean", bean);

        long nodeId = ParamUtil.getLongParameter(request, "parent", 0);
        if (bean != null && bean.getNodeId() > 0) {
            nodeId = bean.getNodeId();
        }
        if (nodeId > 0) {
            List<DictoryDefinition> list = dictoryDefinitionService.getDictoryDefinitions(nodeId, "sys_dictory");
            if (list != null && !list.isEmpty()) {
                if (bean != null) {
                    Map<String, Object> dataMap = Tools.getDataMap(bean);
                    for (DictoryDefinition d : list) {
                        Object value = dataMap.get(d.getName());
                        d.setValue(value);
                    }
                }
                Collections.sort(list);
                request.setAttribute("list", list);
            }
        }

        if (bean == null) {
            SysTree parent = sysTreeService.getSysTreeByCode(Constants.TREE_DICTORY);
            List<SysTree> list = new java.util.ArrayList<SysTree>();
            parent.setDeep(0);
            list.add(parent);
            sysTreeService.loadSysTrees(list, parent.getId(), 1);
            request.setAttribute("parent", list);
        }

        String x_view = ViewProperties.getString("dictory.prepareModify");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/sys/dictory/dictory_modify", modelMap);
    }

    /**
     * ???
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=saveAdd")
    public ModelAndView saveAdd(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        logger.debug("params:" + params);
        Dictory bean = new Dictory();
        try {
            Tools.populate(bean, params);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        bean.setCreateBy(RequestUtils.getActorId(request));

        ViewMessages messages = new ViewMessages();
        if (dictoryService.create(bean)) {// ??
            BaseDataManager.getInstance().loadDictInfo();
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.add_success"));
        } else {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.add_failure"));
        }
        MessageUtils.addMessages(request, messages);
        request.setAttribute("url", "dictory.do?method=showList");

        return new ModelAndView("show_msg", modelMap);
    }

    /**
     * ??
     * 
     * @param request
     * @param modelMap
     * @return
     */

    @RequestMapping(params = "method=saveLoadDictory", method = RequestMethod.POST)
    public ModelAndView saveLoadDictory(HttpServletRequest request, ModelMap modelMap) {
        MultiDBHibernateSchemaUpdate schema = new MultiDBHibernateSchemaUpdate();
        schema.execute();
        BaseDataManager.getInstance().refreshBaseData();
        ViewMessages messages = new ViewMessages();
        messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.reload_success"));
        MessageUtils.addMessages(request, messages);

        String x_view = ViewProperties.getString("dictory.saveLoadDictory");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        // ?
        return new ModelAndView("/modules/sys/dictory/dictory_load", modelMap);
    }

    /**
     * ???
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=saveModify")
    public ModelAndView saveModify(HttpServletRequest request, ModelMap modelMap) {
        Map<String, Object> params = RequestUtils.getParameterMap(request);
        long id = ParamUtil.getIntParameter(request, "id", 0);
        Dictory bean = dictoryService.find(id);
        logger.debug("params:" + params);
        try {
            Tools.populate(bean, params);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        bean.setUpdateBy(RequestUtils.getActorId(request));

        ViewMessages messages = new ViewMessages();
        if (dictoryService.update(bean)) {// ??
            BaseDataManager.getInstance().loadDictInfo();
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.modify_success"));
        } else {// ?
            messages.add(ViewMessages.GLOBAL_MESSAGE, new ViewMessage("dictory.modify_failure"));
        }
        MessageUtils.addMessages(request, messages);

        return new ModelAndView("show_msg", modelMap);
    }

    @javax.annotation.Resource
    public void setDictoryDefinitionService(DictoryDefinitionService dictoryDefinitionService) {
        this.dictoryDefinitionService = dictoryDefinitionService;

    }

    @javax.annotation.Resource
    public void setDictoryService(DictoryService dictoryService) {
        this.dictoryService = dictoryService;

    }

    @javax.annotation.Resource
    public void setSysTreeService(SysTreeService sysTreeService) {
        this.sysTreeService = sysTreeService;

    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showDictData")
    public ModelAndView showDictData(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        ModelAndView view = null;
        String code = ParamUtil.getParameter(request, "code");
        Iterator<?> iter = null;
        long parent = ParamUtil.getLongParameter(request, "parent", -1);
        if (!(parent == -1)) {

        } else {
            iter = BaseDataManager.getInstance().getList(code);
            view = new ModelAndView("/modules/sys/dictory/dictory_select", modelMap);
        }
        request.setAttribute("list", iter);

        // ?
        return view;
    }

    /**
     * ?
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showFrame")
    public ModelAndView showFrame(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        SysTree bean = sysTreeService.getSysTreeByCode(Constants.TREE_DICTORY);
        request.setAttribute("parent", bean.getId() + "");

        String x_view = ViewProperties.getString("dictory.showFrame");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }

        return new ModelAndView("/modules/sys/dictory/dictory_frame", modelMap);
    }

    /**
     * 
     * 
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(params = "method=showList")
    public ModelAndView showList(HttpServletRequest request, ModelMap modelMap) {
        RequestUtils.setRequestParameterToAttribute(request);
        int parent = ParamUtil.getIntParameter(request, "parent", 0);
        int pageNo = ParamUtil.getIntParameter(request, "page_no", 1);
        int pageSize = ParamUtil.getIntParameter(request, "page_size", 10);
        PageResult pager = dictoryService.getDictoryList(parent, pageNo, pageSize);
        request.setAttribute("pager", pager);

        String x_view = ViewProperties.getString("dictory.showList");
        if (StringUtils.isNotEmpty(x_view)) {
            return new ModelAndView(x_view, modelMap);
        }
        // ?
        return new ModelAndView("/modules/sys/dictory/dictory_list", modelMap);
    }

    @ResponseBody
    @RequestMapping(params = "method=sort")
    public byte[] sort(@RequestParam(value = "parent") int parent, @RequestParam(value = "id") int id,
            @RequestParam(value = "operate") int operate) {
        logger.info("parent:" + parent + "; id:" + id + "; operate:" + operate);
        try {
            Dictory bean = dictoryService.find(id);
            dictoryService.sort(parent, bean, operate);
            return ResponseUtils.responseJsonResult(true);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return ResponseUtils.responseJsonResult(false);
    }
}