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

📄 sfafullreplicator.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.replication;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
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 org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;

import com.sourcetap.sfa.product.ProductHelper;
import com.sourcetap.sfa.security.SecurityLinkInfo;
import com.sourcetap.sfa.security.SecurityWrapper;
import com.sourcetap.sfa.util.UserInfo;


/**
 * This class is used for full data replication for the Sales Force Automation application. <P>
 *
 * Full replication is required to populate a newly registered replication node, or
 * to re-populate an inactive replication node.<P>
 *
 * This class calls the populateRelatedEntityMapVector() method to specify what
 * entities are to be replicated.
 *
 * To perform full replication, instantiate this class, and call the replicateFull()
 * method.
 *
 * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
 */
public class SFAFullReplicator extends FullReplicator {

	public static final String module = SFAFullReplicator.class.getName();

    /**
     * Constructor with no args.
     */
    public SFAFullReplicator() {
        super();
    }

    /**
     * Constructor with args.
     *
     * @param mainInstance Main entity instance for which related entities will be replicated
     * @param localDelegator Delegator to attach to local data base
     * @param masterDelegator Delegator to attach to master data base
     * @param entityName Name of the entity to be replicated
     * @param userInfo UserInfo object containing user information
     */
    public SFAFullReplicator(GenericDelegator localDelegator,
        GenericDelegator masterDelegator, String entityName, UserInfo userInfo) {
        super(localDelegator, masterDelegator, entityName, userInfo);
    }

    /**
     * Populate the related entity map vector. This method is called from the constructors
     * to allow the related entities to be specified before replication begins.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     */
    public void populateRelatedEntityMapVector() {
        // Add related entity maps for all entities to be replicated at the top level.
        // Set the relation title to "*" to indicate that all entities are to be found
        // and replicated, not just the ones related to a given entity instance.
        Debug.logVerbose("[populateRelatedEntityMapVector] Start", module);

        // Replicate all entities that do not depend on accounts.
        addRelatedEntityMap("", "AccountType", null, true, true, "");
        addRelatedEntityMap("", "ActivityType", null, true, true, "");
        addRelatedEntityMap("", "AddressType", null, true, true, "");
        addRelatedEntityMap("", "AlertType", null, true, true, "");
        addRelatedEntityMap("", "Alert", null, true, true, "");
        addRelatedEntityMap("", "CodeType", null, true, true, "");
        addRelatedEntityMap("", "Code", null, false, false, "");
        addRelatedEntityMap("", "ContactAttitudeType", null, true, true, "");
        addRelatedEntityMap("", "ContactType", null, true, true, "");
        addRelatedEntityMap("", "DealType", null, true, true, "");
        addRelatedEntityMap("", "DealStageType", null, true, true, "");
        addRelatedEntityMap("", "DealStage", null, true, true, "");
        addRelatedEntityMap("", "DealStageSteps", null, true, true, "");
        addRelatedEntityMap("", "DealStatus", null, true, true, "");
        addRelatedEntityMap("", "ExpenseType", null, true, true, "");
        addRelatedEntityMap("", "Expense", null, true, true, "");
        addRelatedEntityMap("", "ItDefault", null, true, true, "");
        addRelatedEntityMap("", "LeadSource", null, true, true, "");
        addRelatedEntityMap("", "OwnershipType", null, true, true, "");
        addRelatedEntityMap("", "ProductAssocType", null, true, true, "");
        addRelatedEntityMap("", "ProductFeature", null, true, true, "");
        addRelatedEntityMap("", "ProductFeatureApplType", null, true, true, "");
        addRelatedEntityMap("", "ProductFeatureType", null, true, true, "");
        addRelatedEntityMap("", "ProductType", null, true, true, "");
        addRelatedEntityMap("", "ProductTypeAttr", null, true, true, "");
        addRelatedEntityMap("", "SicCode", null, true, true, "");
        addRelatedEntityMap("", "SecurityGroupPermission", null, true, true, "");

        // Replicate the account and all the entities nested under it.
        addRelatedEntityMap("", "Account", null, false, true,
            "com.sourcetap.sfa.replication.AccountReplicator");

        // Replicate all entities that depend on accounts.
        addRelatedEntityMap("", "LargeData", null, true, true, "");
        addRelatedEntityMap("", "ItIssue", null, true, true, "");
        addRelatedEntityMap("", "ItIssueHistory", null, true, true, "");

        //		addRelatedEntityMap("", "ItIssue", null, true, true,
        //			"com.sourcetap.sfa.replication.ItIssueReplicator");
        addRelatedEntityMap("", "PartyAttribute", null, true, true, "");
        addRelatedEntityMap("", "Product", null, false, false,
            "com.sourcetap.sfa.replication.ProductReplicator");
        addRelatedEntityMap("", "ProductCategory", null, false, false,
            "com.sourcetap.sfa.replication.ProductCategoryReplicator");
        addRelatedEntityMap("", "UiApplication", null, true, true, "");
        addRelatedEntityMap("", "UiAttribute", null, true, true, "");
        addRelatedEntityMap("", "UiDisplayAttrib", null, true, true, "");
        addRelatedEntityMap("", "UiDisplayObject", null, true, true, "");
        addRelatedEntityMap("", "UiDisplayObjectAttrib", null, true, true, "");
        addRelatedEntityMap("", "UiDisplayType", null, true, true, "");
        addRelatedEntityMap("", "UiEntity", null, true, true, "");
        addRelatedEntityMap("", "UiLayoutType", null, true, true, "");
        addRelatedEntityMap("", "UiQuery", null, true, true, "");
        addRelatedEntityMap("", "UiQueryValue", null, true, true, "");
        addRelatedEntityMap("", "UiScreen", null, true, true, "");
        addRelatedEntityMap("", "UiScreenSection", null, true, true, "");
        addRelatedEntityMap("", "UiScreenSectionEntity", null, true, true, "");
        addRelatedEntityMap("", "UiScreenSectionInfo", null, true, true, "");
        addRelatedEntityMap("", "UserPreferenceDefinitions", null, true, true,
            "");
    }

    /**
     * This method finds all instances of an entity related to the main entity.
     *
     * This method overrides the ancestor to filter out instances for the SFA application.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param mainInstance Main entity instance for which related entities will be replicated
     * @param relationTitle Relation title to be used by the entity engine to find related
     *   entity instances
     * @param relatedEntityName Name of the related entity to be replicated
     * @param filterMap HashMap containing additional filter values to be used by the entity
     *   engine when finding related entity instances
     *
     * @return List of generic values related to the main entity instance
     */
    protected List findOneRelated(GenericDelegator delegator,
        GenericValue mainInstance, String relationTitle,
        String relatedEntityName, HashMap filterMap, boolean findAll) {
        Debug.logVerbose("[findOneRelated] Start", module);

        List relatedGVL = null;

        if (relatedEntityName.equals("Code")) {
            // Special processing for Code entity.  Exclude replication node ID parameter.
            return findCodes(delegator);
        } else if (relatedEntityName.equals("Account") && !findAll) {
            // Special processing for Account entity unless the findAll flag was set, which
            // means we are removing, in which case the default search will be done.
            // If findAll was not set, we are replicating, so we need to
            // Filter accounts according to the company's security settings.
            return findAccounts(delegator);
        } else if (relatedEntityName.equals("Product")) {
            // Special processing for Product entity.
            try {
                return ProductHelper.getProducts(delegator, getUserInfo());
            } catch (GenericEntityException e) {
                Debug.logError(
                    "[findOneRelated] An error occurred while finding " +
                    "Product instances: " + e.getLocalizedMessage(), module);

                return null;
            }
        } else if (relatedEntityName.equals("ProductCategory")) {
            // Special processing for ProductCategory entity.
            try {
                return ProductHelper.getProductCategories(delegator,
                    getUserInfo());
            } catch (GenericEntityException e) {
                Debug.logError(
                    "[findOneRelated] An error occurred while finding " +
                    "Product Category instances: " + e.getLocalizedMessage(), module);

                return null;
            }
        } else {
            // Default processing for all other entities.
            return super.findOneRelated(delegator, mainInstance, relationTitle,
                relatedEntityName, filterMap, findAll);
        }
    }

    /**
     * This method finds all instances of the Code entity for replication.  The code holding
     * the system parameter for the replication node ID is excluded.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return List of Code generic values
     */
    protected List findCodes(GenericDelegator delegator) {
        Debug.logVerbose("[findCodes] Start", module);
 
        List filteredGVL = new LinkedList();

        // Replicate all Codes except the system parameter that holds
        // the replication node ID.
        try {
            List codeGVL = delegator.findAll("Code");
            Iterator codeGVI = codeGVL.iterator();

            // Remove codes from the data base except the ones we want to skip.
            while (codeGVI.hasNext()) {
                GenericValue codeGV = (GenericValue) codeGVI.next();
                String codeTypeId = codeGV.getString("codeTypeId");
                String codeId = codeGV.getString("codeId");

                if (!codeTypeId.equals("SYSTEM_PARAMETER") &&
                        !codeId.equals("REPL_NODE_ID")) {
                    filteredGVL.add(codeGV);
                }
            }
        } catch (GenericEntityException e) {
            Debug.logError("[findCodes] An error occurred while finding " +
                "Code instances: " + e.getLocalizedMessage(), module);

            return null;
        }

        return filteredGVL;
    }

    /**
     * This method finds all Accounts for replication.  Filtering is applied according
     * to the company's security settings.  Accounts will be limited to the current
     * user's company regardless of security settings, even if the mode is ALL.
     * Team or territory filtering will be applied if specified in the company's
     * security settings.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return List of Account generic values
     */
    protected List findAccounts(GenericDelegator delegator) {
        Debug.logVerbose("[findAccounts] Start", module);

        if (getUserInfo() == null) {
            Debug.logError("[findAccounts] User info is required.", module);

            return null;
        }

        ;

        if (delegator == null) {
            Debug.logError("[findAccounts] Delegator is required.", module);

            return null;
        }

        ;

        Debug.logVerbose("[findAccounts] getUserInfo(): " + getUserInfo(), module);

        // Find the base account for this company so it will go first in the list.
        // We want to make sure this one gets replicated first in case there is a
        // problem so the user will still be able to log into the local system.
        HashMap companyFindMap = new HashMap();
        companyFindMap.put("accountId", getUserInfo().getAccountId());

        try {
            GenericValue companyGV = delegator.findByPrimaryKey("Account",
                    companyFindMap);

            Debug.logVerbose("[findAccounts] Company account: " +
                    companyGV.toString(), module);

            List accountGVL = new LinkedList();
            accountGVL.add(companyGV);

            // Find all the non-base accounts this user is allowed to see.
            EntityCondition condition = new EntityExpr("accountTypeId", EntityOperator.NOT_EQUAL, "base");

            try {
                accountGVL.addAll(SecurityWrapper.findByCondition("Account",
                        condition, null, getUserInfo(),
                        new SecurityLinkInfo("Account", "accountId", true),
                        delegator));

                Debug.logVerbose("[findAccounts] Account List: " +
                        accountGVL.toString(), module);

                return accountGVL;
            } catch (GenericEntityException e2) {
                Debug.logError("[findAccounts] An error occurred in " +
                    "SecurityWrapper.findByCondition while looking for non-base accounts: " +
                    e2.getLocalizedMessage(), module);

                return null;
            }
        } catch (GenericEntityException e1) {
            Debug.logError(
                "[findAccounts] An error occurred while looking for the " +
                "base company account: " + e1.getLocalizedMessage(), module);

            return null;
        }
    }
}

⌨️ 快捷键说明

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