com.wiiyaya.consumer.web.main.controller.UserController.java Source code

Java tutorial

Introduction

Here is the source code for com.wiiyaya.consumer.web.main.controller.UserController.java

Source

/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed 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.wiiyaya.consumer.web.main.controller;

import com.wiiyaya.consumer.web.main.constant.MainURIResource;
import com.wiiyaya.framework.common.exception.BusinessException;
import com.wiiyaya.framework.common.exception.ValidateException;
import com.wiiyaya.framework.common.model.easyui.EasyUiComboBox;
import com.wiiyaya.framework.common.model.easyui.EasyUiDataGrid;
import com.wiiyaya.provider.main.model.BackendUserDto;
import com.wiiyaya.provider.main.service.BackendUserService;
import com.wiiyaya.provider.main.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
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.ResponseBody;

import java.util.List;

/**
 *
 * <p>?</p>
 *
 * <p>??</p>
 *
 * <p></p>
 *
 * @author wiiyaya
 *
 */
@Controller
public class UserController {

    @Autowired
    private BackendUserService backendUserService;

    @Autowired
    private RoleService roleService;

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_LIST_INIT, method = { RequestMethod.GET })
    public String searchInit(
            @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto) {
        return MainURIResource.PAGE_SYS_USER_LIST;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_LIST, method = { RequestMethod.GET })
    @ResponseBody
    public EasyUiDataGrid<BackendUserDto> search(
            @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto,
            Pageable pageRequest) {
        return backendUserService.getAllUsers(backendUserDto, pageRequest);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_ADD_INIT, method = { RequestMethod.GET })
    public String addInit(Model model,
            @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto) {
        List<EasyUiComboBox> roleList = roleService.getAllRoles();
        model.addAttribute(MainURIResource.MODEL_USER_ROLE_LIST, roleList);
        return MainURIResource.PAGE_SYS_USER_MAINTAIN;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_ADD, method = { RequestMethod.POST })
    @ResponseBody
    public void add(
            @Validated @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto,
            BindingResult result) throws BusinessException, ValidateException {
        if (result.hasErrors()) {
            throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                    result.getFieldError().getDefaultMessage());
        }
        backendUserService.saveUser(backendUserDto);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_AMD_INIT, method = { RequestMethod.GET })
    public String amendInit(Model model, @PathVariable(MainURIResource.USER_ID) Long userId)
            throws BusinessException {
        BackendUserDto backendUserDto = backendUserService.getUserById(userId);
        List<EasyUiComboBox> roleList = roleService.getAllRoles();

        model.addAttribute(MainURIResource.MODEL_BACKEND_USER_DTO, backendUserDto);
        model.addAttribute(MainURIResource.MODEL_USER_ROLE_LIST, roleList);
        return MainURIResource.PAGE_SYS_USER_MAINTAIN;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_AMD, method = { RequestMethod.PUT })
    @ResponseBody
    public void amend(
            @Validated @ModelAttribute(MainURIResource.MODEL_BACKEND_USER_DTO) BackendUserDto backendUserDto,
            BindingResult result) throws ValidateException, BusinessException {
        if (result.hasErrors()) {
            throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                    result.getFieldError().getDefaultMessage());
        }
        backendUserService.updateUser(backendUserDto);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_ENABLE, method = { RequestMethod.PUT })
    @ResponseBody
    public void enable(@PathVariable(MainURIResource.USER_ID) Long userId) throws BusinessException {
        backendUserService.enableUser(userId);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_USER_DISABLE, method = { RequestMethod.PUT })
    @ResponseBody
    public void disable(@PathVariable(MainURIResource.USER_ID) Long userId) throws BusinessException {
        backendUserService.disableUser(userId);
    }

}