com.lcw.one.modules.sys.web.UserController.java Source code

Java tutorial

Introduction

Here is the source code for com.lcw.one.modules.sys.web.UserController.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.lcw.one.modules.sys.web;

import com.google.common.collect.Lists;
import com.lcw.one.common.beanvalidator.BeanValidators;
import com.lcw.one.common.config.Global;
import com.lcw.one.common.persistence.Page;
import com.lcw.one.common.utils.DateUtils;
import com.lcw.one.common.utils.StringUtils;
import com.lcw.one.common.utils.excel.ExportExcel;
import com.lcw.one.common.utils.excel.ImportExcel;
import com.lcw.one.common.web.BaseController;
import com.lcw.one.modules.sys.entity.User;
import com.lcw.one.modules.sys.service.SystemService;
import com.lcw.one.modules.sys.utils.UserUtils;
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.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;

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

    @Autowired
    private SystemService systemService;

    @ModelAttribute
    public User get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return systemService.getUser(id);
        } else {
            return new User();
        }
    }

    @RequiresPermissions("sys:user:view")
    @RequestMapping({ "list", "" })
    public String list() {
        return "modules/sys/userList";
    }

    @RequestMapping({ "selectUser" })
    public String selectUser() {
        return "modules/esign/common/select_user";
    }

    @RequiresPermissions("sys:user:view")
    @RequestMapping("form")
    public String form(String id, Model model) {
        model.addAttribute("id", id);
        return "modules/sys/userForm";
    }

    //    TODO REST
    @RequiresPermissions("sys:user:view")
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportFile(User user, HttpServletRequest request, HttpServletResponse response,
            RedirectAttributes redirectAttributes) {
        try {
            String fileName = "?" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<User> page = systemService.findUser(new Page<User>(request, response), user);
            new ExportExcel("?", User.class).setDataList(page.getList()).write(response, fileName)
                    .dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "??" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/sys/user/?repage";
    }

    //    TODO REST
    @RequiresPermissions("sys:user:edit")
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "?????");
            return "redirect:" + Global.getAdminPath() + "/sys/user/?repage";
        }

        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<User> list = ei.getDataList(User.class);
            for (User user : list) {
                try {
                    if ("true".equals(checkLoginName("", user.getLoginName()))) {
                        user.setPassword(SystemService.entryptPassword("123456"));
                        BeanValidators.validateWithException(validator, user);
                        systemService.saveUser(user);
                        successNum++;
                    } else {
                        failureMsg.append("<br/>?? " + user.getLoginName() + " ; ");
                        failureNum++;
                    }
                } catch (ConstraintViolationException ex) {
                    failureMsg.append("<br/>?? " + user.getLoginName() + " ");
                    List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
                    for (String message : messageList) {
                        failureMsg.append(message + "; ");
                        failureNum++;
                    }
                } catch (Exception ex) {
                    failureMsg
                            .append("<br/>?? " + user.getLoginName() + " " + ex.getMessage());
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, " " + failureNum + " ??");
            }
            addMessage(redirectAttributes, "? " + successNum + " ?" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "??" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/sys/user/?repage";
    }

    //    TODO REST
    @RequiresPermissions("sys:user:view")
    @RequestMapping("import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "??.xlsx";
            List<User> list = Lists.newArrayList();
            list.add(UserUtils.getUser());
            new ExportExcel("?", User.class, 2).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "???" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/sys/user/?repage";
    }

    //    TODO REST
    @ResponseBody
    @RequiresPermissions("sys:user:edit")
    @RequestMapping("checkLoginName")
    public String checkLoginName(String oldLoginName, String loginName) {
        if (loginName != null && loginName.equals(oldLoginName)) {
            return "true";
        } else if (loginName != null && systemService.getUserByLoginName(loginName) == null) {
            return "true";
        }
        return "false";
    }

    @RequiresUser
    @RequestMapping("info")
    public String info() {
        return "modules/sys/userInfo";
    }

    @RequiresUser
    @RequestMapping("modifyPwd")
    public String modifyPwd() {
        return "modules/sys/userModifyPwd";
    }

}