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

Java tutorial

Introduction

Here is the source code for com.wiiyaya.consumer.web.main.controller.RoleController.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.RoleDto;
import com.wiiyaya.provider.main.service.PrivilegeService;
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 RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PrivilegeService privilegeService;

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_LIST_INIT, method = { RequestMethod.GET })
    public String searchInit(@ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto) {
        return MainURIResource.PAGE_SYS_ROLE_LIST;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_LIST, method = { RequestMethod.GET })
    @ResponseBody
    public EasyUiDataGrid<RoleDto> search(@ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto,
            Pageable pageRequest) {
        return roleService.getAllRoles(roleDto, pageRequest);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_ADD_INIT, method = { RequestMethod.GET })
    public String addInit(Model model, @ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto) {
        List<EasyUiComboBox> privilegeList = privilegeService.getAllPrivilege();
        model.addAttribute(MainURIResource.MODEL_ROLE_PRIVILEGE_LIST, privilegeList);
        return MainURIResource.PAGE_SYS_ROLE_MAINTAIN;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_ADD, method = { RequestMethod.POST })
    @ResponseBody
    public void add(@Validated @ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto,
            BindingResult result) throws ValidateException, BusinessException {
        if (result.hasErrors()) {
            throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                    result.getFieldError().getDefaultMessage());
        }
        roleService.saveRole(roleDto);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_AMD_INIT, method = { RequestMethod.GET })
    public String amendInit(Model model, @PathVariable(MainURIResource.ROLE_ID) Long roleId)
            throws BusinessException {
        RoleDto roleDto = roleService.getRoleById(roleId);
        List<EasyUiComboBox> privilegeList = privilegeService.getAllPrivilege();

        model.addAttribute(MainURIResource.MODEL_ROLE_DTO, roleDto);
        model.addAttribute(MainURIResource.MODEL_ROLE_PRIVILEGE_LIST, privilegeList);
        return MainURIResource.PAGE_SYS_ROLE_MAINTAIN;
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_AMD, method = { RequestMethod.PUT })
    @ResponseBody
    public void amend(@Validated @ModelAttribute(MainURIResource.MODEL_ROLE_DTO) RoleDto roleDto,
            BindingResult result) throws ValidateException, BusinessException {
        if (result.hasErrors()) {
            throw new ValidateException(result.getFieldError().getObjectName(), result.getFieldError().getField(),
                    result.getFieldError().getDefaultMessage());
        }
        roleService.updateRole(roleDto);
    }

    @RequestMapping(value = MainURIResource.PATH_SYS_ROLE_DEL, method = { RequestMethod.DELETE })
    @ResponseBody
    public void delete(@PathVariable(MainURIResource.ROLE_ID) Long roleId) throws BusinessException {
        roleService.deleteRole(roleId);
    }
}