com.glaf.base.security.BaseIdentityFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.glaf.base.security.BaseIdentityFactory.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.security;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;

import com.glaf.base.modules.sys.model.SysDepartment;
import com.glaf.base.modules.sys.model.SysRole;
import com.glaf.base.modules.sys.model.SysUser;
import com.glaf.base.modules.sys.service.SysApplicationService;
import com.glaf.base.modules.sys.service.SysDepartmentService;
import com.glaf.base.modules.sys.service.SysDeptRoleService;
import com.glaf.base.modules.sys.service.SysRoleService;
import com.glaf.base.modules.sys.service.SysTreeService;
import com.glaf.base.modules.sys.service.SysUserRoleService;
import com.glaf.base.modules.sys.service.SysUserService;
import com.glaf.base.utils.ContextUtil;
import com.glaf.core.context.ContextFactory;
import com.glaf.core.identity.Agent;
import com.glaf.core.service.EntityService;

public class BaseIdentityFactory {
    protected static final Log logger = LogFactory.getLog(BaseIdentityFactory.class);

    protected static volatile EntityService entityService;

    protected static volatile SysApplicationService sysApplicationService;

    protected static volatile SysDepartmentService sysDepartmentService;

    protected static volatile SysDeptRoleService sysDeptRoleService;

    protected static volatile SysRoleService sysRoleService;

    protected static volatile SysTreeService sysTreeService;

    protected static volatile SysUserRoleService sysUserRoleService;

    protected static volatile SysUserService sysUserService;

    /**
     * ?????
     * 
     * @param assignTo
     *            ???
     * @return
     */
    public static List<String> getAgentIds(String assignTo) {
        List<String> agentIds = new java.util.ArrayList<String>();
        List<Object> rows = getEntityService().getList("getAgents", assignTo);
        if (rows != null && !rows.isEmpty()) {
            for (Object object : rows) {
                if (object instanceof Agent) {
                    Agent agent = (Agent) object;
                    if (!agent.isValid()) {
                        continue;
                    }
                    switch (agent.getAgentType()) {
                    case 0:// ?
                        agentIds.add(agent.getAssignFrom());
                        break;
                    default:
                        break;
                    }
                }
            }
        }
        return agentIds;
    }

    /**
     * ????
     * 
     * @param deptId
     *            ?
     * @param roleId
     *            ?
     * @return
     */
    public static List<SysUser> getChildrenMembershipUsers(Long deptId, Long roleId) {
        return getSysUserRoleService().getChildrenMembershipUsers(deptId, roleId);
    }

    /**
     * ????
     * 
     * @param deptId
     *            ?
     * @param roleId
     *            ?
     * @return
     */
    public static List<SysUser> getChildrenMembershipUsers(Long deptId, String roleCode) {
        SysRole role = getSysRoleService().findByCode(roleCode);
        return getSysUserRoleService().getChildrenMembershipUsers(deptId, role.getId());
    }

    public static List<SysUser> getChildrenMembershipUsers(String deptCode, String roleCode) {
        SysDepartment dept = getSysDepartmentService().findByCode(deptCode);
        SysRole role = getSysRoleService().findByCode(roleCode);
        return getSysUserRoleService().getChildrenMembershipUsers(dept.getId(), role.getId());
    }

    /**
     * ???(sys_departmentcode)
     * 
     * @param code
     *            ?
     * @return
     */
    public static SysDepartment getDepartmentByCode(String code) {
        SysDepartment model = getSysDepartmentService().findByCode(code);
        return model;
    }

    /**
     * ???(sys_departmentid)
     * 
     * @param id
     *            ID
     * @return
     */
    public static SysDepartment getDepartmentById(Long id) {
        SysDepartment model = getSysDepartmentService().findById(id);
        return model;
    }

    /**
     * ???(sys_departmentdeptno)
     * 
     * @param deptno
     *            deptno
     * @return
     */
    public static SysDepartment getDepartmentByNo(String deptno) {
        SysDepartment model = getSysDepartmentService().findByNo(deptno);
        return model;
    }

    /**
     * ?Map
     * 
     * @return
     */
    public static Map<String, SysDepartment> getDepartmentMap() {
        Map<String, SysDepartment> deptMap = new java.util.HashMap<String, SysDepartment>();
        List<SysDepartment> depts = getSysDepartmentService().getSysDepartmentList();
        if (depts != null && !depts.isEmpty()) {
            for (SysDepartment dept : depts) {
                depts.add(dept);
            }
        }
        return deptMap;
    }

    /**
     * ?
     * 
     * @return
     */
    public static List<SysDepartment> getDepartments() {
        List<SysDepartment> depts = getSysDepartmentService().getSysDepartmentList();
        return depts;
    }

    public static EntityService getEntityService() {
        if (entityService == null) {
            entityService = ContextFactory.getBean("entityService");
        }
        return entityService;
    }

    /**
     * ?
     * 
     * @return
     */
    public static Map<String, SysUser> getLowerCaseUserMap() {
        Map<String, SysUser> userMap = new LinkedHashMap<String, SysUser>();
        List<SysUser> users = getSysUserService().getSysUserList();
        if (users != null && !users.isEmpty()) {
            for (SysUser user : users) {
                userMap.put(user.getAccount().toLowerCase(), user);
            }
        }
        return userMap;
    }

    /**
     * ???
     * 
     * @param deptIds
     *            ID
     * @param roleId
     *            ID
     * @return
     */
    public static List<SysUser> getMembershipUsers(List<Long> deptIds, int roleId) {
        return getSysUserRoleService().getMembershipUsers(deptIds, roleId);
    }

    /**
     * ???
     * 
     * @param deptIds
     *            ID
     * @param roleCode
     *            ?
     * @return
     */
    public static List<SysUser> getMembershipUsers(List<Long> deptIds, String roleCode) {
        SysRole role = getSysRoleService().findByCode(roleCode);
        return getSysUserRoleService().getMembershipUsers(deptIds, role.getId());
    }

    /**
     * ??
     * 
     * @param deptId
     *            ID
     * @return
     */
    public static List<SysUser> getMembershipUsers(Long deptId) {
        return getSysUserService().getSysUserList(deptId);
    }

    /**
     * ???
     * 
     * @param deptId
     *            ID
     * @param roleId
     *            ID
     * @return
     */
    public static List<SysUser> getMembershipUsers(Long deptId, Long roleId) {
        return getSysUserRoleService().getMembershipUsers(deptId, roleId);
    }

    /**
     * ???
     * 
     * @param deptId
     *            ID
     * @param roleCode
     *            ?
     * @return
     */
    public static List<SysUser> getMembershipUsers(Long deptId, String roleCode) {
        SysRole role = getSysRoleService().findByCode(roleCode);
        return getSysUserRoleService().getMembershipUsers(deptId, role.getId());
    }

    /**
     * ????
     * 
     * @param deptId
     *            ?
     * @param deptLevel
     *             0-?,1(B)-,2(K)-,3-,4-(?)
     * @return
     */
    public static List<SysDepartment> getParentAndChildrenDepartments(Long deptId, Integer deptLevel) {
        List<SysDepartment> list = new java.util.ArrayList<SysDepartment>();
        SysDepartment dept = getSysDepartmentService().findById(deptId);
        if (dept != null) {
            getSysDepartmentService().findNestingDepartment(list, dept.getId());
        }
        return list;
    }

    /**
     * ??
     * 
     * @param code
     *            ?
     * @return
     */
    public static SysRole getRoleByCode(String code) {
        return getSysRoleService().findByCode(code);
    }

    /**
     * ??
     * 
     * @param id
     *            ID
     * @return
     */
    public static SysRole getRoleById(Long id) {
        return getSysRoleService().findById(id);
    }

    /**
     * ? Map
     * 
     * @return
     */
    public static Map<String, SysRole> getRoleMap() {
        Map<String, SysRole> roleMap = new java.util.HashMap<String, SysRole>();
        List<SysRole> roles = getSysRoleService().getSysRoleList();
        if (roles != null && !roles.isEmpty()) {
            for (SysRole role : roles) {
                roleMap.put(role.getCode(), role);
            }
        }
        return roleMap;
    }

    /**
     * ?
     * 
     * @return
     */
    public static List<SysRole> getRoles() {
        List<SysRole> roles = getSysRoleService().getSysRoleList();
        return roles;
    }

    public static SysApplicationService getSysApplicationService() {
        if (sysApplicationService == null) {
            sysApplicationService = ContextFactory.getBean("sysApplicationService");
        }
        return sysApplicationService;
    }

    public static SysDepartmentService getSysDepartmentService() {
        if (sysDepartmentService == null) {
            sysDepartmentService = ContextFactory.getBean("sysDepartmentService");
        }
        return sysDepartmentService;
    }

    public static SysDeptRoleService getSysDeptRoleService() {
        if (sysDeptRoleService == null) {
            sysDeptRoleService = ContextFactory.getBean("sysDeptRoleService");
        }
        return sysDeptRoleService;
    }

    public static SysRoleService getSysRoleService() {
        if (sysRoleService == null) {
            sysRoleService = ContextFactory.getBean("sysRoleService");
        }
        return sysRoleService;
    }

    public static SysTreeService getSysTreeService() {
        if (sysTreeService == null) {
            sysTreeService = ContextFactory.getBean("sysTreeService");
        }

        return sysTreeService;
    }

    /**
     * ????
     * 
     * @param actorId
     *            ?
     * @return
     */
    public static SysUser getSysUser(String actorId) {
        return getSysUserService().findByAccountWithAll(actorId);
    }

    /**
     * ????
     * 
     * @param id
     *            ID
     * @return
     */
    public static SysUser getSysUserById(Long id) {
        return getSysUserService().findById(id);
    }

    public static SysUserRoleService getSysUserRoleService() {
        if (sysUserRoleService == null) {
            sysUserRoleService = ContextFactory.getBean("sysUserRoleService");
        }
        return sysUserRoleService;
    }

    public static SysUserService getSysUserService() {
        if (sysUserService == null) {
            sysUserService = ContextFactory.getBean("sysUserService");
        }
        return sysUserService;
    }

    /**
     * ????
     * 
     * @param actorId
     *            ?
     * @return
     */
    public static SysUser getSysUserWithAll(String actorId) {
        SysUser user = getSysUserService().findByAccountWithAll(actorId);
        if (user != null) {
            ContextUtil.put(actorId, user);
        }
        return user;
    }

    /**
     * ?Map
     * 
     * @return
     */
    public static Map<String, SysUser> getUserMap() {
        Map<String, SysUser> userMap = new LinkedHashMap<String, SysUser>();
        List<SysUser> users = getSysUserService().getSysUserList();
        if (users != null && !users.isEmpty()) {
            for (SysUser user : users) {
                userMap.put(user.getAccount(), user);
            }
        }
        return userMap;
    }

    /**
     * ???
     * 
     * @param parentId
     *            ?
     * @param actorId
     *            ?
     * @return
     */
    public static JSONArray getUserMenu(Long parentId, String actorId) {
        return getSysApplicationService().getUserMenu(parentId, actorId);
    }

    /**
     * ???
     * 
     * @param actorIds
     * @return
     */
    public static List<String> getUserRoleCodes(List<String> actorIds) {
        List<String> codes = new java.util.ArrayList<String>();
        List<SysRole> list = getUserRoles(actorIds);
        if (list != null && !list.isEmpty()) {
            for (SysRole role : list) {
                if (!codes.contains(role.getCode())) {
                    codes.add(role.getCode());
                }
            }
        }
        return codes;
    }

    public static List<SysRole> getUserRoles(List<String> actorIds) {
        return getSysUserService().getUserRoles(actorIds);
    }

    /**
     * ?????
     * 
     * @param actorId
     *            ?
     * @return
     */
    public static List<String> getUserRoles(String actorId) {
        List<String> actorIds = new java.util.ArrayList<String>();
        actorIds.add(actorId);
        return getUserRoleCodes(actorIds);
    }

    /**
     * ?
     * 
     * @return
     */
    public static List<SysUser> getUsers() {
        return getSysUserService().getSysUserList();
    }

    public static void setSysApplicationService(SysApplicationService sysApplicationService) {
        BaseIdentityFactory.sysApplicationService = sysApplicationService;
    }

    public static void setSysDepartmentService(SysDepartmentService sysDepartmentService) {
        BaseIdentityFactory.sysDepartmentService = sysDepartmentService;
    }

    public static void setSysDeptRoleService(SysDeptRoleService sysDeptRoleService) {
        BaseIdentityFactory.sysDeptRoleService = sysDeptRoleService;
    }

    public static void setSysRoleService(SysRoleService sysRoleService) {
        BaseIdentityFactory.sysRoleService = sysRoleService;
    }

    public static void setSysTreeService(SysTreeService sysTreeService) {
        BaseIdentityFactory.sysTreeService = sysTreeService;
    }

    public static void setSysUserRoleService(SysUserRoleService sysUserRoleService) {
        BaseIdentityFactory.sysUserRoleService = sysUserRoleService;
    }

    public static void setSysUserService(SysUserService sysUserService) {
        BaseIdentityFactory.sysUserService = sysUserService;
    }

}