⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rolehelper.java

📁 国外的一套开源CRM
💻 JAVA
字号:
/*
 * 
 * Copyright (c) 2004 SourceTap - www.sourcetap.com
 *
 *  The contents of this file are subject to the SourceTap Public License 
 * ("License"); You may not use this file except in compliance with the 
 * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 */

package com.sourcetap.sfa.role;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;

import com.sourcetap.sfa.replication.GenericReplicator;


/**
 * DOCUMENT ME!
 *
 */
public class RoleHelper {
	public static final String module = RoleHelper.class.getName();
    /**
     * DOCUMENT ME!
     *
     * @param entity 
     * @param entityId 
     * @param contactId 
     * @param roleId 
     * @param userName 
     * @param delegator 
     */
    public static void storeRolesForEntity(String entity, String entityId,
        String contactId, String roleId, String userName,
        GenericDelegator delegator) {
        try {
            String rolePath = getRolePath(contactId, roleId, delegator);
            HashMap accessMap = new HashMap();
            List entityExists = null;
            accessMap.put("entity", entity);
            accessMap.put("entityId", entityId);
            accessMap.put("partyId", rolePath);
            accessMap.put("partyEntityType", "Role");
            entityExists = delegator.findByAnd("EntityAccess", accessMap, null);

            if ((entityExists == null) || (entityExists.size() <= 0)) {
                GenericValue entityAccess = new GenericValue(delegator.getModelEntity(
                            "EntityAccess"));
                entityAccess.setDelegator(delegator);
                entityAccess.set("entityAccessId",
                    GenericReplicator.getNextSeqId("EntityAccess", delegator));
                entityAccess.set("entity", entity);
                entityAccess.set("entityId", entityId);
                entityAccess.set("partyId", rolePath);
                entityAccess.set("partyEntityType", "Role");
                entityAccess.set("modifiedBy", userName);
                entityAccess.set("modifiedDate",
                    new Timestamp(new java.util.Date().getTime()));
                entityAccess.set("createdBy", userName);
                entityAccess.set("createdDate",
                    new Timestamp(new java.util.Date().getTime()));
                delegator.create(entityAccess);
            }
        } catch (GenericEntityException gnee) {
            gnee.printStackTrace();
            Debug.logError(gnee,module);
        }
    }

    /**
     * DOCUMENT ME!
     *
     * @param partyId 
     * @param roleId 
     * @param delegator 
     *
     * @return 
     */
    public static List getParentRolesForParty(String partyId, String roleId,
        GenericDelegator delegator) {
        ArrayList returnList = new ArrayList();

        try {
            //find all roles
            ArrayList orderBy = new ArrayList();
            orderBy.add("roleParentId");

            List roles = delegator.findAllCache("Role", orderBy);
            HashMap roleMap = new HashMap();

            if ((roles != null) && (roles.size() > 0)) {
                GenericValue[] roleValues = (GenericValue[]) roles.toArray(new GenericValue[0]);
                GenericValue roleValue = null;

                for (int i = 0; i < roleValues.length; i++) {
                    roleValue = roleValues[i];
                    roleMap.put(roleValue.getString("roleId"),
                        roleValue.getString("roleParentId"));
                }
            }

            List entityExists = null;

            if ((roleId != null) || (roleId.trim().length() > 0)) {
                //store every role above current users role in Entity_Access
                String roleParentId = "";
                boolean moreParents = true;
                int i = 0;

                while (moreParents) {
                    if (i > 0) {
                        roleParentId = (String) roleMap.get(roleId);
                    } else {
                        roleParentId = roleId;
                    }

                    if (roleParentId == null) {
                        moreParents = false;
                    } else {
                        returnList.add(roleParentId);
                    }

                    roleId = roleParentId;
                    i++;
                }
            } else {
                return new ArrayList();
            }
        } catch (GenericEntityException gnee) {
            gnee.printStackTrace();
            Debug.logError(gnee, module);
        }

        return returnList;
    }

    /**
     * DOCUMENT ME!
     *
     * @param partyId 
     * @param roleId 
     * @param delegator 
     *
     * @return 
     */
    public static String getRolePath(String partyId, String roleId,
        GenericDelegator delegator) {
        ArrayList roleList = (ArrayList) getParentRolesForParty(partyId,
                roleId, delegator);
        StringBuffer returnString = new StringBuffer();

        for (int i = 0; i < roleList.size(); i++) {
            returnString.append((String) roleList.get(i));

            if (i < (roleList.size() - 1)) {
                returnString.append(", ");
            }
        }

        return returnString.toString();
    }

    /**
     * DOCUMENT ME!
     *
     * @param partyId 
     * @param roleId 
     * @param delegator 
     *
     * @return 
     */
    public static List getSubordinateRolesForParty(String partyId,
        String roleId, GenericDelegator delegator) {

        ArrayList orderBy = new ArrayList();
        orderBy.add("roleId");

        List roles = null;
        ArrayList returnList = new ArrayList();

        try {
            roles = delegator.findAllCache("Role", orderBy);

            HashMap roleMap = new HashMap();

            if ((roles != null) && (roles.size() > 0)) {
                GenericValue[] roleValues = (GenericValue[]) roles.toArray(new GenericValue[0]);
                GenericValue roleValue = null;

                for (int i = 0; i < roleValues.length; i++) {
                    roleValue = roleValues[i];
                    roleMap.put(roleValue.getString("roleId"),
                        roleValue.getString("roleParentId"));
                }
            }

            ArrayList childNodes = new ArrayList();
            returnList.add(roleId);

            Iterator iter = null;
            String testRoleId = roleId;
            boolean noMoreChildren = true;

            while (noMoreChildren) {
                iter = roleMap.keySet().iterator();

                while (iter.hasNext()) {
                    String key = (String) iter.next();

                    if ((key != null) && (roleMap.get(key) != null) &&
                            roleMap.get(key).equals(testRoleId)) {
                        if (!childNodes.contains(key)) {
                            childNodes.add(key);
                            returnList.add(key);
                        }
                    }
                }

                if (childNodes.size() == 0) {
                    noMoreChildren = false;
                } else {
                    int remove = childNodes.size() - 1;
                    testRoleId = (String) childNodes.get(remove);
                    childNodes.remove(remove);
                }
            }
        } catch (GenericEntityException gee) {
            gee.printStackTrace();
            Debug.logError(gee, module);
        } catch (Exception gee) {
            gee.printStackTrace();
            Debug.logError(gee, module);
        }

        return returnList;
    }

    /**
     * DOCUMENT ME!
     *
     * @param entity 
     * @param entityId 
     * @param contactId 
     * @param roleId 
     * @param userName 
     * @param delegator 
     */
    public static void removeRolesForEntity(String entity, String entityId,
        String contactId, String roleId, String userName,
        GenericDelegator delegator) {
        try {
            String rolePath = getRolePath(contactId, roleId, delegator);
            HashMap accessMap = new HashMap();
            accessMap.put("entity", entity);
            accessMap.put("entityId", entityId);
            accessMap.put("partyId", rolePath);
            accessMap.put("partyEntityType", "Role");
            delegator.removeByAnd("EntityAccess", accessMap);
        } catch (GenericEntityException gnee) {
            gnee.printStackTrace();
            Debug.logError(gnee, module);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -