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

📄 genericreplicator.java

📁 国外的一套开源CRM
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 
 * 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 com.sourcetap.sfa.util.*;

import org.ofbiz.entity.*;
import org.ofbiz.entity.model.*;
import org.ofbiz.base.util.Debug;

import java.sql.Timestamp;

import java.util.*;


/**
 * This class is used for data replication.
 *
 * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
 */
public class GenericReplicator {
    /**
     * Debug switch
     */
	public static final String module = GenericReplicator.class.getName();


    /**
     * Status - Continue (success)
     */
    public static final int STATUS_CONTINUE = 1;

    /**
     * Status - Canceled
     */
    public static final int STATUS_CANCELED = 0;

    /**
     * Status - Error
     */
    public static final int STATUS_ERROR = -1;

    /**
     * Action - Error
     */
    public static final int ACTION_ERROR = -1;

    /**
     * Action - Skip
     */
    public static final int ACTION_SKIP = 0;

    /**
     * Action - Insert
     */
    public static final int ACTION_INSERT = 1;

    /**
     * Action - Update
     */
    public static final int ACTION_UPDATE = 2;

    /**
     * Action - Delete
     */
    public static final int ACTION_DELETE = 3;

    /**
     * Local delegator.  This is used to connect to the local data base.
     */
    protected GenericDelegator localDelegator;

    /**
     * Master delegator.  This is used to connect to the master data base.
     */
    protected GenericDelegator masterDelegator;

    /**
     * Constructor with no args.
     */
    public GenericReplicator() {
    }

    /**
     * Constructor with delegators.
     */
    public GenericReplicator(GenericDelegator localDelegator_,
        GenericDelegator masterDelegator_) {
        setLocalDelegator(localDelegator_);
        setMasterDelegator(masterDelegator_);
    }

    /**
     * Sets the local delegator attribute.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
     *
     */
    public void setLocalDelegator(GenericDelegator delegator) {
        localDelegator = delegator;

        return;
    }

    /**
     * Gets the local delegator attribute.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
     *
     * @return Reference to the local delegator
     */
    public GenericDelegator getLocalDelegator() {
        return localDelegator;
    }

    /**
     * Sets the master delegator attribute.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
     *
     */
    public void setMasterDelegator(GenericDelegator delegator) {
        masterDelegator = delegator;

        return;
    }

    /**
     * Gets the master delegator attribute.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
     *
     * @return Reference to the master delegator
     */
    public GenericDelegator getMasterDelegator() {
        return masterDelegator;
    }

    /**
     * Gets the node ID under which the current machine is registered for replication
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param localDelegator Generic delegator pointing to the local data base.
     *
     * @return Replication node ID of the local computer
     */
    public static String getReplNodeId(GenericDelegator localDelegator) {
        Debug.logVerbose("[getReplNodeId] Start", module);

        HashMap codeSearchMap = new HashMap();
        codeSearchMap.put("codeTypeId", "SYSTEM_PARAMETER");
        codeSearchMap.put("codeId", "REPL_NODE_ID");

        try {
            GenericValue codeGV = localDelegator.findByPrimaryKey("Code",
                    codeSearchMap);

            if (codeGV == null) {
                return "";
            }

            String replNodeId = codeGV.getString("codeValue");

            if (replNodeId == null) {
                return "";
            }

            return replNodeId;
        } catch (GenericEntityException e) {
                Debug.logError(
                    "[getReplNodeId] Error looking for REPL_NODE_ID in code table: " +
                    e.getLocalizedMessage(), module);

            return "";
        }
    }

    /**
     * Gets the node ID under which the current machine is registered for replication
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return Replication node ID of the local computer
     */
    public String getReplNodeId() {
        if (getLocalDelegator() == null) {
            Debug.logError("[getReplNodeId] Local delegator is required.", module);

            return null;
        }

        ;

        return getReplNodeId(getLocalDelegator());
    }

    /**
     * Gets the ReplNode record for the current machine from the master data base.
     *
     * Requires that the masterDelegator argument passed to the constructor is valid.
     * Alternatively, requires the setMasterDelegator method to have been called.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return GenericValue holding the replication node record for the local computer
     *   retrieved from the master data base.
     */
    public GenericValue getReplNodeGenericValue(String replNodeId) {

        if (getMasterDelegator() == null) {
            Debug.logError(
                "GenericReplicator.getReplNodeGenericValue] Master delegator is required.  Cannot get node value.", module);

            return null;
        }

        ;

        HashMap replNodeSearchMap = new HashMap();
        replNodeSearchMap.put("replNodeId", replNodeId);

        try {
            GenericValue replNodeGV = getMasterDelegator().findByPrimaryKey("ReplNode",
                    replNodeSearchMap);

            return replNodeGV;
        } catch (GenericEntityException e) {
        	 Debug.logError("[getReplNodeGenericValue] Error looking for " +
                    "ReplNode in master data base for replNodeId " +
                    replNodeId + ": " + e.getLocalizedMessage(), module);

            return null;
        }
    }

    /**
     * Determines whether the local computer is registered for replication
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @return True if local node is registered for replication, false otherwise
     */
    public static boolean getIsRegistered(GenericDelegator delegator) {

        if (getReplNodeId(delegator).equals("")) {
            return false;
        }

        return true;
    }

    /**
     * Registers the local computer for replication.  Assigns the next available node ID
     * to this computer, saves it in the system parameters in the local data base, and
     * creates a new record in the ReplNode table in the master data base.
     *
     * Requires that the localDelegator and masterDelegator arguments passed to the
     * constructor are valid.  Alternatively, requires the setLocalDelegator and
     * setMasterDelegator methods to have been called.
     *
     * @author  <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
     *
     * @param replNodeName Name to be assigned to the new replication node
     * @param ownerId The Contact ID of the user who will own the new replication node
     * @param isActive Flag indicating whether the new replication node will participate in replication
     *
     * @return The new replication node ID assigned to this computer
     */
    public String register(String replNodeName, String ownerId,
        boolean isActive, String userPartyId) {

        if (getLocalDelegator() == null) {
            Debug.logError(
                "[register] Local delegator is required.  Registration canceled.", module);

            return "";
        }

        ;

        if (getMasterDelegator() == null) {
            Debug.logError(
                "[register] Master delegator is required.  Registration canceled.", module);

            return "";
        }

        ;

        if ((replNodeName == null) || replNodeName.equals("")) {
            Debug.logError("[register] Node Name is required.  Registration canceled.", module);

            return "";
        }

        ;

        if ((ownerId == null) || ownerId.equals("")) {
            Debug.logError("[register] ownerId is required.  Registration canceled.", module);

            return "";
        }

⌨️ 快捷键说明

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