com.wiiyaya.provider.main.service.impl.RoleServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.wiiyaya.provider.main.service.impl.RoleServiceImpl.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.provider.main.service.impl;

import com.mysema.query.jpa.JPQLQuery;
import com.wiiyaya.framework.common.exception.BusinessException;
import com.wiiyaya.framework.common.model.easyui.EasyUiComboBox;
import com.wiiyaya.framework.common.model.easyui.EasyUiDataGrid;
import com.wiiyaya.framework.common.utils.StringUtils;
import com.wiiyaya.framework.provider.tools.JF;
import com.wiiyaya.provider.main.constant.MainConstant;
import com.wiiyaya.provider.main.entity.Privilege;
import com.wiiyaya.provider.main.entity.Role;
import com.wiiyaya.provider.main.model.RoleDto;
import com.wiiyaya.provider.main.repository.PrivilegeDao;
import com.wiiyaya.provider.main.repository.RoleDao;
import com.wiiyaya.provider.main.repository.querydsl.RoleQry;
import com.wiiyaya.provider.main.service.RoleService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PrivilegeDao privilegeDao;

    @Override
    @Transactional(readOnly = true)
    public EasyUiDataGrid<RoleDto> getAllRoles(final RoleDto roleDto, Pageable pageRequest) {
        List<RoleDto> rstList = new ArrayList<>();
        Page<Role> roleDbs = roleDao.findAllJF(new JF() {
            @Override
            public void prepareQry(JPQLQuery qry, boolean notCountQry) {
                RoleQry.searchRole(qry, notCountQry, roleDto);
            }
        }, pageRequest);
        for (Role roleDb : roleDbs) {
            RoleDto dto = new RoleDto();
            dto.setId(roleDb.getId());
            dto.setName(roleDb.getName());
            List<String> pNames = new ArrayList<>();
            for (Privilege p : roleDb.getPrivileges()) {
                pNames.add(p.getDesc());
            }
            dto.setPrivilegeNames(StringUtils.join(pNames, StringUtils.SPACE));
            rstList.add(dto);
        }
        return new EasyUiDataGrid<>(rstList, roleDbs.getTotalElements());
    }

    @Override
    public List<EasyUiComboBox> getAllRoles() {
        List<EasyUiComboBox> rstList = new ArrayList<>();
        List<Role> roleDbs = roleDao.findAll();
        for (Role roleDb : roleDbs) {
            EasyUiComboBox combo = new EasyUiComboBox();
            combo.setValue(roleDb.getId().toString());
            combo.setText(roleDb.getName());
            rstList.add(combo);
        }
        return rstList;
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void saveRole(RoleDto roleDto) throws BusinessException {
        Role roleDb = new Role();
        roleDb.setName(roleDto.getName());

        Set<Privilege> privileges = new HashSet<>();
        for (Long privilegeId : roleDto.getPrivilegeIds()) {
            privileges.add(privilegeDao.findOne(privilegeId));
        }
        roleDb.setPrivileges(privileges);
        roleDao.save(roleDb);
    }

    @Override
    @Transactional(readOnly = true)
    public RoleDto getRoleById(Long roleId) throws BusinessException {
        Role roleDb = roleDao.findOne(roleId);
        if (roleDb == null) {
            throw new BusinessException(MainConstant.ERROR_ROLE_NOT_FOUND);
        }
        RoleDto roleDto = new RoleDto();
        roleDto.setId(roleDb.getId());
        roleDto.setVersion(roleDb.getVersion());

        roleDto.setName(roleDb.getName());

        List<Long> privilegeIds = new ArrayList<>();
        for (Privilege privilege : roleDb.getPrivileges()) {
            privilegeIds.add(privilege.getId());
        }
        roleDto.setPrivilegeIds(privilegeIds);
        return roleDto;
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void updateRole(RoleDto roleDto) throws BusinessException {
        Role roleDb = roleDao.findOne(roleDto.getId());
        if (roleDb == null) {
            throw new BusinessException(MainConstant.ERROR_ROLE_NOT_FOUND);
        }
        roleDb.setName(roleDto.getName());
        roleDb.setVersion(roleDto.getVersion());

        if (CollectionUtils.isNotEmpty(roleDto.getPrivilegeIds())) {
            for (Iterator<Privilege> itDb = roleDb.getPrivileges().iterator(); itDb.hasNext();) {
                final Privilege privilegeDb = itDb.next();
                boolean existInPage = IterableUtils.matchesAny(roleDto.getPrivilegeIds(), new Predicate<Long>() {
                    @Override
                    public boolean evaluate(Long object) {
                        return privilegeDb.getId().equals(object);
                    }
                });
                if (!existInPage) {
                    itDb.remove();
                }
            }
            roleDb.getPrivileges().addAll(privilegeDao.findAll(roleDto.getPrivilegeIds()));
        } else {
            roleDb.getPrivileges().clear();
        }
        roleDao.save(roleDb);
    }

    @Override
    @Transactional(rollbackFor = BusinessException.class)
    public void deleteRole(Long roleId) throws BusinessException {
        roleDao.delete(roleId);
    }

}