org.zhangmz.pickles.controller.admin.account.AccountController.java Source code

Java tutorial

Introduction

Here is the source code for org.zhangmz.pickles.controller.admin.account.AccountController.java

Source

/*******************************************************************************
 * Copyright (c) 2015, 2016  104446930@qq.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package org.zhangmz.pickles.controller.admin.account;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
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.servlet.ModelAndView;
import org.zhangmz.pickles.modules.constants.Messages;
import org.zhangmz.pickles.modules.convert.JsonMapper;
import org.zhangmz.pickles.orm.model.Account;
import org.zhangmz.pickles.service.AccountService;
import com.github.pagehelper.PageInfo;

/**
 * Title:AccountController.java
 * Description:?
 * Company: DigitalChina 2016
 * @author:
 * @date:2016127 ?11:07:34
 * ?
 */
@Controller
@RequestMapping("/admin/accounts")
public class AccountController {
    private static Logger logger = LoggerFactory.getLogger(AccountController.class);

    private static JsonMapper binder = JsonMapper.nonDefaultMapper();

    @Autowired
    private AccountService accountService;

    @RequestMapping
    public ModelAndView search(@RequestParam("TOKEN") String token, Account account) {
        ModelAndView result = new ModelAndView("admin/account/index");
        List<Account> accountList = accountService.search(account);
        logger.debug(binder.toJson(accountList));
        result.addObject("pageInfo", new PageInfo<Account>(accountList));
        result.addObject("queryParam", account);
        result.addObject("page", account.getPage());
        result.addObject("rows", account.getRows());
        result.addObject("TOKEN", token);

        logger.debug(binder.toJson(result));
        return result;
    }

    @RequestMapping(value = "/add")
    public ModelAndView add(@RequestParam("TOKEN") String token) {
        ModelAndView result = new ModelAndView("admin/account/view");
        result.addObject("account", new Account());
        // add by zhangmz 2016-01-09 ?
        result.addObject("action", "add");
        result.addObject("TOKEN", token);
        return result;
    }

    @RequestMapping(value = "/view/{id}")
    public ModelAndView view(@RequestParam("TOKEN") String token, @PathVariable Long id) {
        ModelAndView result = new ModelAndView("admin/account/view");
        Account account = accountService.getById(id);
        result.addObject("account", account);
        // add by zhangmz 2016-01-09 ?
        // result.addObject("action", "view");
        result.addObject("action", "edit");
        result.addObject("TOKEN", token);
        return result;
    }

    /**
     * 
     * @Title: delete 
     * @Description: 
     * @param token
     * @param id
     * @param ra
     * @return
     * @throws 
     * :
     * :2016127 ?5:03:56
     * AOPModelAndView
     *       
     */
    @RequestMapping(value = "/delete/{id}")
    public ModelAndView delete(@RequestParam("TOKEN") String token, @PathVariable Long id) {
        ModelAndView result = new ModelAndView("admin/account/index");
        String message;
        // ????
        if (accountService.isAdmin(token) && 1 != id.intValue()) {
            accountService.deleteById(id);
            message = Messages.DELETE_SUCCESS;
        } else {
            message = Messages.MUST_BE_ADMIN + Messages.ADMINISTRATOR_IS_STATIC;
        }

        // AOPModelAndView??
        Account account = new Account();
        List<Account> accountList = accountService.search(account);
        result.addObject("pageInfo", new PageInfo<Account>(accountList));
        result.addObject("queryParam", account);
        result.addObject("page", account.getPage());
        result.addObject("rows", account.getRows());
        result.addObject("message", message);
        result.addObject("TOKEN", token);
        return result;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView save(@RequestParam("TOKEN") String token, Account account) {
        ModelAndView result = new ModelAndView("admin/account/view");
        String message = account.getId() == null ? Messages.INSERT_SUCCESS : Messages.UPDATE_SUCCESS;

        // ??/??????/????
        // MySQLIntegrityConstraintViolationException: Column 'email' cannot be null
        // ??bug
        try {
            // modify by zhangmz ????
            if (!accountService.isAdministrator(token) && accountService.isAdministrator(account)) {
                message = Messages.MUST_BE_ADMINISTRATOR;
            } else {
                accountService.save(account);
            }
        } catch (Exception e) {
            // msg = e.getMessage();
            message = Messages.SYSTEM_BUSY;
        }

        result.addObject("account", account);
        result.addObject("message", message);
        // add by zhangmz 2016-01-09 ?
        // result.addObject("action", "view");
        result.addObject("action", "edit");
        result.addObject("TOKEN", token);
        return result;
    }
}