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

📄 entityreplicator.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * 
 * 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.List;
import java.util.Vector;

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

import com.sourcetap.sfa.util.UserInfo;


/**
 * This class is used for entity instance replication. <P>
 *
 * Typically this class, or its descendant, is used to replicate a single instance
 * of an entity.  This is accomplished by instantiating this class and calling its
 * replicateInstance() method. <P>
 *
 * For each instance replicated, one or more related entity instances may be replicated
 * when the replicateAllRelated() method is called automatically by the replicateInstance()
 * method.  If the relatedEntityMapVector attribute has been populated, the
 * replicateAllRelated() method will call the replicateOneRelated() method for each item in
 * the vector to replicate one entities instances.  The related entities are located in the
 * findOneRelated() method, which is called by replicateOneRelated().  For each related
 * entity instance found, another instance of this class is used to replicate it. <P>
 *
 * Entity replication is used to do full replication.  Full replication is required to
 * populate a newly registered replication node, or to re-populate an inactive
 * replication node.  To perform full replication, the relatedEntityMapVector should be
 * populated with a list of all entities to be removed and replicated, and the information
 * required to find them.  Then the removeAll() and replicateAll() methods
 * should be called.
 *
 * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
 */
public class EntityReplicator extends GenericReplicator {
    private static final boolean TIMER = false;
	public static final String module = EntityReplicator.class.getName();

    /**
     * The name of the entity to be replicated.
     */
    protected String entityName;

    /**
     * Related Entity Map Vector <P>
     *
     * Each vector element contains a HashMap describing a related entity to be replicated
     * for each instance of the main entity that is replicated. <P>
     *
     * Each HashMap contains: <P>
     *
     *   String relationTitle <BR>
     *   String relatedEntityName <BR>
     *   HashMap filterMap <BR>
     *   String replicatorClassName <BR>
     *   Boolean replicateAll
     *   Boolean removeAll
     */
    protected Vector relatedEntityMapVector = new Vector();

    /**
     * User Information
     */
    protected UserInfo userInfo;

    /**
     * Constructor with no args.
     */
    public EntityReplicator() {
        populateRelatedEntityMapVector();
    }

    /**
     * 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 EntityReplicator(GenericDelegator localDelegator,
        GenericDelegator masterDelegator, String entityName, UserInfo userInfo) {
        super(localDelegator, masterDelegator);
        setEntityName(entityName);
        setUserInfo(userInfo);
        populateRelatedEntityMapVector();
    }

    /**
     * Gets the name of the entity to be replicated.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return Name of the entity to be replicated
     *
     */
    public String getEntityName() {
        return entityName;
    }

    /**
     * Sets the name of the entity to be replicated.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param entityName_ Name of the entity to be replicated
     *
     */
    public void setEntityName(String entityName_) {
        entityName = entityName_;

        return;
    }

    /**
     * Adds a related entity map to the related entity map vector.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param relatedEntityMap HashMap containing information about a releated entity
     */
    public void addRelatedEntityMap(HashMap relatedEntityMap) {

        relatedEntityMapVector.add(relatedEntityMap);

        return;
    }

    /**
     * Adds a related entity map to the related entity map vector.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @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
     * @param replicateAll Flag indicating to replicate all instances of this entity
     *   instead of just the ones related to the main entity.
     * @param removeAll Flag indicating to remove all instances of this entity instead of
     *   just the ones related to the main entity.
     * @param replicatorClassName Name of a descendant class of this class which will be used
     *   to replicate the related entity instances
     */
    public void addRelatedEntityMap(String relationTitle,
        String relatedEntityName, HashMap filterMap, boolean replicateAll,
        boolean removeAll, String replicatorClassName) {

        HashMap relatedEntityMap = new HashMap();
        relatedEntityMap.put("relationTitle", relationTitle);
        relatedEntityMap.put("relatedEntityName", relatedEntityName);
        relatedEntityMap.put("filterMap", filterMap);
        relatedEntityMap.put("replicateAll", new Boolean(replicateAll));
        relatedEntityMap.put("removeAll", new Boolean(removeAll));
        relatedEntityMap.put("replicatorClassName", replicatorClassName);
        addRelatedEntityMap(relatedEntityMap);

        return;
    }

    /**
     * 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() {
    }

    /**
     * Gets the related entity map vector.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return Related entity map vector.
     *
     */
    public Vector getRelatedEntityMapVector() {
        return relatedEntityMapVector;
    }

    /**
     * Gets the user information
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return UserInfo object
     *
     */
    public UserInfo getUserInfo() {
        return userInfo;
    }

    /**
     * Sets the user information
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param userInfo_ UserInfo object
     *
     */
    public void setUserInfo(UserInfo userInfo_) {
        userInfo = userInfo_;

        return;
    }

    /**
     * This method replicates one instance of the entity named in the entityName attribute. <P>
     *
     * Ths instance passed in the instance argument is copied from the master data base
     * to the local data base.  Then the replicateRelatedEntities() method is
     * called so child entity instance can be replicated. <P>
     *
     * This method requires that all arguments passed to the constructor were not null and
     * not empty.  Alternatively, this method requires the setEntityName, setLocalDelegator,
     * and setMasterDelegator methods to have been called since construction.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param mainInstance Entity instance being replicated
     *
     * @return The status of the replication.  Possible values: STATUS_CONTINUE, STATUS_ERROR,
     *   STATUS_CANCEL
     */
    public int replicateInstance(GenericValue mainInstance) {

        // Check pre-conditions.
        if (mainInstance == null) {
            Debug.logError(
                "[replicateInstance] Main entity instance is required.  " +
                "Replication canceled.", module);

            return STATUS_ERROR;
        }

        ;

        // Insert the instance into the local data base.
        int status = insertLocalInstance(mainInstance);

        if (status != STATUS_CONTINUE) {
            return status;
        }

        // Insert all related entity instances.
        status = replicateAllRelated(mainInstance);

        if (status != STATUS_CONTINUE) {
            return status;
        }

        return STATUS_CONTINUE;
    }

    /**

⌨️ 快捷键说明

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