com.glaf.base.modules.sys.service.mybatis.SysUserRoleServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.base.modules.sys.service.mybatis.SysUserRoleServiceImpl.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.glaf.base.modules.sys.service.mybatis;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.glaf.base.modules.Constants;
import com.glaf.base.modules.sys.mapper.SysDeptRoleMapper;
import com.glaf.base.modules.sys.mapper.SysRoleMapper;
import com.glaf.base.modules.sys.mapper.SysUserMapper;
import com.glaf.base.modules.sys.mapper.SysUserRoleMapper;
import com.glaf.base.modules.sys.model.SysDepartment;
import com.glaf.base.modules.sys.model.SysDeptRole;
import com.glaf.base.modules.sys.model.SysUser;
import com.glaf.base.modules.sys.model.SysUserRole;
import com.glaf.base.modules.sys.query.SysUserQuery;
import com.glaf.base.modules.sys.query.SysUserRoleQuery;
import com.glaf.base.modules.sys.service.SysDepartmentService;
import com.glaf.base.modules.sys.service.SysUserRoleService;
import com.glaf.base.modules.sys.service.SysUserService;
import com.glaf.core.base.TableModel;
import com.glaf.core.domain.Membership;
import com.glaf.core.id.IdGenerator;
import com.glaf.core.security.Authentication;
import com.glaf.core.service.ITableDataService;
import com.glaf.core.service.MembershipService;
import com.glaf.core.util.DateUtils;
import com.glaf.core.util.PageResult;

@Service("sysUserRoleService")
@Transactional(readOnly = true)
public class SysUserRoleServiceImpl implements SysUserRoleService {
    protected final static Log logger = LogFactory.getLog(SysUserRoleServiceImpl.class);

    protected IdGenerator idGenerator;

    protected MembershipService membershipService;

    protected SqlSessionTemplate sqlSessionTemplate;

    protected SysDepartmentService sysDepartmentService;

    protected SysDeptRoleMapper sysDeptRoleMapper;

    protected SysUserMapper sysUserMapper;

    protected SysRoleMapper sysRoleMapper;

    protected SysUserRoleMapper sysUserRoleMapper;

    protected SysUserService sysUserService;

    protected ITableDataService tableDataService;

    public SysUserRoleServiceImpl() {

    }

    @Transactional
    public boolean addRole(long fromUserId, long toUserId, String startDate, String endDate, int mark,
            String processNames, String processDescriptions) {
        boolean ret = false;
        if (null != processNames && processNames.length() > 0)
            processNames = processNames.substring(0, processNames.lastIndexOf(","));
        if (null != processDescriptions && processDescriptions.length() > 0)
            processDescriptions = processDescriptions.substring(0, processDescriptions.lastIndexOf(","));
        SysUser fromUser = sysUserService.findById(fromUserId);
        SysUser toUser = sysUserService.findById(toUserId);
        if (fromUser == null || toUser == null || fromUserId == toUserId) {
            return ret;
        }
        // ??fromUser?
        SysUserRoleQuery query = new SysUserRoleQuery();
        query.userId(fromUserId);
        query.authorized(0);
        List<SysUserRole> userRoles = this.list(query);
        Iterator<SysUserRole> iter = userRoles.iterator();
        while (iter.hasNext()) {
            SysUserRole userRole = (SysUserRole) iter.next();
            logger.debug("##" + userRole.getUserId() + ":" + userRole.getDeptRoleId());
            // ?
            SysUserRole bean = new SysUserRole();
            bean.setId(this.idGenerator.nextId());
            bean.setAuthorizeFrom(fromUser.getId());
            bean.setUser(toUser);
            bean.setUserId(toUser.getId());
            bean.setDeptRoleId(userRole.getDeptRoleId());
            bean.setAuthorized(1);
            bean.setAvailDateStart(DateUtils.toDate(startDate));
            bean.setAvailDateEnd(DateUtils.toDate(endDate));
            bean.setProcessDescription(processDescriptions);
            if (mark == 1) {
                bean.setProcessDescription("?");
            }

            bean.setCreateDate(new Date());
            bean.setCreateBy(Authentication.getAuthenticatedActorId());

            sysUserRoleMapper.insertSysUserRole(bean);

        }
        // ?
        insertAgent(fromUser, toUser, startDate, endDate, mark, processNames);
        return true;
    }

    public int count(SysUserRoleQuery query) {
        query.ensureInitialized();
        return sysUserRoleMapper.getSysUserRoleCount(query);
    }

    @Transactional
    public boolean create(SysUserRole bean) {
        if (bean.getId() == 0) {
            bean.setId(idGenerator.nextId());
        }
        bean.setCreateDate(new Date());
        sysUserRoleMapper.insertSysUserRole(bean);
        return true;
    }

    @Transactional
    public boolean delete(long id) {
        this.deleteById(id);
        return true;
    }

    @Transactional
    public boolean delete(SysUserRole bean) {
        this.deleteById(bean.getId());
        return true;
    }

    @Transactional
    public void deleteById(Long id) {
        if (id != null) {
            sysUserRoleMapper.deleteSysUserRoleById(id);
        }
    }

    @Transactional
    public void deleteByIds(List<Long> rowIds) {
        if (rowIds != null && !rowIds.isEmpty()) {
            SysUserRoleQuery query = new SysUserRoleQuery();
            query.rowIds(rowIds);
            sysUserRoleMapper.deleteSysUserRoles(query);
        }
    }

    public SysUserRole findById(long id) {
        return this.getSysUserRole(id);
    }

    public PageResult getAllAuthorizedUser(Map<String, String> filter) {
        int pageNo = 1;
        if ((String) filter.get("page_no") != null) {
            pageNo = Integer.parseInt((String) filter.get("page_no"));
        }
        int pageSize = 2 * Constants.PAGE_SIZE;
        if ((String) filter.get("page_size") != null) {
            pageSize = Integer.parseInt((String) filter.get("page_size"));
        }

        SysUserQuery query = new SysUserQuery();

        String deptId = (String) filter.get("deptId");
        if (deptId != null) {
            query.deptId(Long.parseLong(deptId));
        }

        String name = (String) filter.get("name");
        if (name != null) {
            query.nameLike(name);
        }

        String startDate = (String) filter.get("startDate");
        if (startDate != null) {
            query.setAvailDateStartGreaterThanOrEqual(DateUtils.toDate(startDate));
        }

        String endDate = (String) filter.get("endDate");
        if (endDate != null) {
            query.setAvailDateEndLessThanOrEqual(DateUtils.toDate(endDate));
        }

        // 
        PageResult pager = new PageResult();

        int count = sysUserMapper.getCountAuthorizedUsers(query);
        if (count == 0) {// 
            pager.setPageSize(pageSize);
            return pager;
        }
        query.setOrderBy(" E.SORT desc");

        int start = pageSize * (pageNo - 1);

        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<SysUser> rows = sqlSessionTemplate.selectList("getAuthorizedUsers", query, rowBounds);
        this.initUserDepartments(rows);
        this.initUserRoles(rows);
        pager.setResults(rows);
        pager.setPageSize(pageSize);
        pager.setCurrentPageNo(pageNo);
        pager.setTotalRecordCount(count);

        return pager;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List getAuthorizedUser(SysUser user) {
        List list = new java.util.ArrayList();
        Map<Long, SysUser> userMap = new java.util.HashMap<Long, SysUser>();
        List<SysUser> users = sysUserMapper.getAuthorizedUsersByUserId(user.getId());
        if (users != null && !users.isEmpty()) {
            for (SysUser u : users) {
                userMap.put(u.getId(), u);
            }
        }

        this.initUserDepartments(users);

        SysUserRoleQuery query = new SysUserRoleQuery();
        query.authorizeFrom(user.getId());

        List<SysUserRole> roles = this.list(query);
        if (roles != null && !roles.isEmpty()) {
            for (SysUserRole role : roles) {
                Object[] array = new Object[4];
                array[0] = userMap.get(role.getUserId());
                array[1] = role.getAvailDateStart();
                array[2] = role.getAvailDateEnd();
                array[3] = role.getProcessDescription();
                list.add(array);
            }
        }

        return list;
    }

    public List<SysUser> getChildrenMembershipUsers(long deptId, long roleId) {
        List<SysUser> users = new java.util.ArrayList<SysUser>();
        SysDepartment dept = sysDepartmentService.findById(deptId);
        if (dept != null) {
            List<SysDepartment> list = new java.util.ArrayList<SysDepartment>();
            // sysDepartmentService.findNestingDepartment(list, deptId);
            sysDepartmentService.findAllChildrenDepartments(list, deptId);
            if (!list.isEmpty()) {
                for (SysDepartment dp : list) {
                    List<SysUser> userlist = getMembershipUsers((int) dp.getId(), roleId);
                    if (!userlist.isEmpty()) {
                        users.addAll(userlist);
                    }
                }
            } else {
                return this.getMembershipUsers(deptId, roleId);
            }
        }
        return users;
    }

    public List<SysUser> getMembershipUsers(List<Long> deptIds, long roleId) {
        List<SysUser> users = new java.util.ArrayList<SysUser>();
        for (Long deptId : deptIds) {
            List<SysUser> list = this.getMembershipUsers(deptId, roleId);
            if (!list.isEmpty()) {
                users.addAll(list);
            }
        }
        this.initUserDepartments(users);
        return users;
    }

    public List<SysUser> getMembershipUsers(long deptId, long roleId) {
        List<SysUser> users = new java.util.ArrayList<SysUser>();
        SysDepartment dept = sysDepartmentService.findById(deptId);
        if (dept != null && dept.getRoles() != null) {
            Set<SysDeptRole> roles = dept.getRoles();
            for (SysDeptRole role : roles) {
                if (role.getRole() != null && role.getRole().getId() == roleId) {
                    List<SysUser> usersadd = sysUserService.getSysUsersByDeptRoleId(role.getId());
                    if (usersadd != null && !usersadd.isEmpty()) {
                        users.addAll(usersadd);
                    }
                }
            }
        }
        this.initUserDepartments(users);
        return users;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public List getProcessByUser(SysUser user) {
        List list = new java.util.ArrayList();
        List<Map<String, Object>> rows = sysUserRoleMapper.getProcessByUser(user.getId());
        if (rows != null && !rows.isEmpty()) {
            for (Map<String, Object> dataMap : rows) {
                Object[] array = new Object[3];
                array[0] = dataMap.get("moduleName");
                array[1] = dataMap.get("processName");
                array[2] = dataMap.get("objectValue");
                list.add(array);
            }
        }

        return list;
    }

    public SysUserRole getSysUserRole(Long id) {
        if (id == null) {
            return null;
        }
        SysUserRole sysUserRole = sysUserRoleMapper.getSysUserRoleById(id);
        return sysUserRole;
    }

    public int getSysUserRoleCountByQueryCriteria(SysUserRoleQuery query) {
        return sysUserRoleMapper.getSysUserRoleCount(query);
    }

    public List<SysUserRole> getSysUserRolesByQueryCriteria(int start, int pageSize, SysUserRoleQuery query) {
        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<SysUserRole> rows = sqlSessionTemplate.selectList("getSysUserRoles", query, rowBounds);
        return rows;
    }

    public List<SysUser> getUnAuthorizedUser(SysUser user) {
        if (user == null) {
            return new java.util.ArrayList<SysUser>();
        }
        logger.info("name:" + user.getName());
        user = sysUserService.findById(user.getId());

        // ?,?
        SysDepartment dept = user.getDepartment();
        logger.info("dept:" + dept.getName());
        List<SysUser> list = sysUserService.getSysUserList((int) dept.getId());

        // ??
        List<SysDepartment> deptList = sysDepartmentService.getSysDepartmentList((int) dept.getId());
        Iterator<SysDepartment> iter = deptList.iterator();
        while (iter.hasNext()) {
            SysDepartment dept2 = (SysDepartment) iter.next();
            logger.info("dept:" + dept2.getName());
            list.addAll(sysUserService.getSysUserList((int) dept2.getId()));
        }
        // 
        list.remove(user);
        // ?
        list.removeAll(getAuthorizedUser(user));

        return list;
    }

    protected void initUserDepartments(List<SysUser> users) {
        if (users != null && !users.isEmpty()) {
            List<SysDepartment> depts = sysDepartmentService.getSysDepartmentList();
            Map<Long, SysDepartment> deptMap = new java.util.HashMap<Long, SysDepartment>();
            if (depts != null && !depts.isEmpty()) {
                for (SysDepartment dept : depts) {
                    deptMap.put(dept.getId(), dept);
                }
            }
            for (SysUser user : users) {
                user.setDepartment(deptMap.get(user.getDeptId()));
            }
        }
    }

    protected void initUserRoles(List<SysUser> users) {
        if (users != null && !users.isEmpty()) {
            for (SysUser user : users) {
                List<SysUserRole> userRoles = sysUserRoleMapper.getSysUserRolesByUserId(user.getId());
                user.getUserRoles().addAll(userRoles);
            }
        }
    }

    @Transactional
    public void insertAgent(SysUser fromUser, SysUser toUser, String startDate, String endDate, int mark,
            String processNames) {
        if (endDate.length() == 10) {
            endDate += " 23:59:59";
        }
        if (mark == 1) {// ?
            TableModel table = new TableModel();
            table.setTableName("SYS_AGENT");
            table.addStringColumn("ID_", String.valueOf(idGenerator.getNextId()));
            table.addStringColumn("ASSIGNTO_", toUser.getAccount());
            table.addStringColumn("ASSIGNFROM_", fromUser.getAccount());
            table.addIntegerColumn("AGENTTYPE_", 0);
            table.addDateColumn("STARTDATE_", DateUtils.toDate(startDate));
            table.addDateColumn("ENDDATE_", DateUtils.toDate(endDate));
            table.addStringColumn("SERVICEKEY_", "JBPM");
            table.addIntegerColumn("LOCKED_", 0);
            tableDataService.insertTableData(table);
        } else {
            String[] ss = processNames.split(",");
            for (int i = 0; i < ss.length; i++) {
                String processName = ss[i];
                TableModel table = new TableModel();
                table.setTableName("SYS_AGENT");
                table.addStringColumn("ID_", String.valueOf(idGenerator.getNextId()));
                table.addStringColumn("ASSIGNTO_", toUser.getAccount());
                table.addStringColumn("ASSIGNFROM_", fromUser.getAccount());
                table.addIntegerColumn("AGENTTYPE_", 0);
                table.addDateColumn("STARTDATE_", DateUtils.toDate(startDate));
                table.addDateColumn("ENDDATE_", DateUtils.toDate(endDate));
                table.addStringColumn("SERVICEKEY_", "JBPM");
                table.addStringColumn("PROCESSNAME_", processName);
                table.addIntegerColumn("LOCKED_", 0);
                tableDataService.insertTableData(table);
            }
        }
    }

    public boolean isAuthorized(long fromUserId, long toUserId) {
        SysUserRoleQuery query = new SysUserRoleQuery();
        query.userId(toUserId);
        query.authorizeFrom(fromUserId);

        int count = this.count(query);
        logger.info("count:" + count + ",fromUserId:" + fromUserId + ",toUserId:" + toUserId);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    public List<SysUserRole> list(SysUserRoleQuery query) {
        query.ensureInitialized();
        List<SysUserRole> list = sysUserRoleMapper.getSysUserRoles(query);
        return list;
    }

    @Transactional
    public void removeAgent(SysUser fromUser, SysUser toUser) {
        TableModel table = new TableModel();
        table.setTableName("SYS_AGENT");
        table.addStringColumn("ASSIGNTO_", toUser.getAccount());
        table.addStringColumn("ASSIGNFROM_", fromUser.getAccount());
        tableDataService.deleteTableData(table);
    }

    @Transactional
    public boolean removeRole(long fromUserId, long toUserId) {
        boolean ret = false;
        SysUser fromUser = sysUserService.findById(fromUserId);
        SysUser toUser = sysUserService.findById(toUserId);
        if (fromUser == null || toUser == null)
            return ret;

        // ??toUser?
        SysUserRoleQuery query = new SysUserRoleQuery();
        query.userId(toUser.getId());

        List<SysUserRole> userRoles = this.list(query);
        Iterator<SysUserRole> iter = userRoles.iterator();
        while (iter.hasNext()) {
            SysUserRole userRole = (SysUserRole) iter.next();
            // ??
            if (userRole.getAuthorizeFrom() != 0) {
                delete(userRole);// ??
            }
        }
        // ??
        removeAgent(fromUser, toUser);
        return true;
    }

    @Transactional
    public boolean removeRoles() {
        SysUserRoleQuery query = new SysUserRoleQuery();
        query.availDateEndLessThanOrEqual(new Date());
        query.authorized(1);

        // ??toUser?
        List<SysUserRole> userRoles = this.list(query);
        Iterator<SysUserRole> iter = userRoles.iterator();
        while (iter.hasNext()) {
            SysUserRole userRole = (SysUserRole) iter.next();
            long fromUserId = userRole.getAuthorizeFrom();
            SysUser fromUser = sysUserService.findById(fromUserId);
            SysUser toUser = userRole.getUser();
            // ??
            logger.info("toUser:" + toUser.getName() + ",fromUser:" + fromUser.getName() + ",remove role:"
                    + userRole.getDeptRole().getRole().getName() + ",availDateEnd=" + userRole.getAvailDateEnd());
            delete(userRole);// ??
            removeAgent(fromUser, toUser);// ?
        }
        return true;
    }

    @Transactional
    public void save(SysUserRole sysUserRole) {
        if (sysUserRole.getId() == 0) {
            sysUserRole.setId(idGenerator.nextId());
            sysUserRole.setCreateDate(new Date());
            sysUserRoleMapper.insertSysUserRole(sysUserRole);
        } else {
            sysUserRoleMapper.updateSysUserRole(sysUserRole);
        }

        SysDeptRole sysDeptRole = sysDeptRoleMapper.getSysDeptRoleById(sysUserRole.getDeptRoleId());
        if (sysDeptRole != null) {
            SysUser user = sysUserMapper.getSysUserById(sysUserRole.getUserId());
            Membership membership = new Membership();
            membership.setActorId(user.getAccount());
            membership.setModifyBy(sysUserRole.getCreateBy());
            membership.setModifyDate(new java.util.Date());
            membership.setNodeId(sysDeptRole.getDeptId());
            membership.setRoleId(sysDeptRole.getSysRoleId());
            membership.setObjectId("SYS_USER_ROLE");
            membership.setObjectValue(String.valueOf(sysUserRole.getId()));
            membership.setType("SysUserRole");
            membershipService.save(membership);
        }
    }

    @javax.annotation.Resource
    public void setIdGenerator(IdGenerator idGenerator) {
        this.idGenerator = idGenerator;
    }

    @javax.annotation.Resource
    public void setMembershipService(MembershipService membershipService) {
        this.membershipService = membershipService;
    }

    @javax.annotation.Resource
    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }

    @javax.annotation.Resource
    public void setSysDepartmentService(SysDepartmentService sysDepartmentService) {
        this.sysDepartmentService = sysDepartmentService;
    }

    @javax.annotation.Resource
    public void setSysRoleMapper(SysRoleMapper sysRoleMapper) {
        this.sysRoleMapper = sysRoleMapper;
    }

    @javax.annotation.Resource
    public void setSysDeptRoleMapper(SysDeptRoleMapper sysDeptRoleMapper) {
        this.sysDeptRoleMapper = sysDeptRoleMapper;
    }

    @javax.annotation.Resource
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @javax.annotation.Resource
    public void setSysUserRoleMapper(SysUserRoleMapper sysUserRoleMapper) {
        this.sysUserRoleMapper = sysUserRoleMapper;
    }

    @javax.annotation.Resource
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @javax.annotation.Resource
    public void setTableDataService(ITableDataService tableDataService) {
        this.tableDataService = tableDataService;
    }

    @Transactional
    public boolean update(SysUserRole bean) {
        this.save(bean);
        return true;
    }

}