001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.replication;
018:
019: import org.ofbiz.base.util.Debug;
020: import org.ofbiz.entity.GenericDelegator;
021:
022: import com.sourcetap.sfa.util.UserInfo;
023:
024: /**
025: * This abstract class is used for full data replication. <P>
026: *
027: * Full replication is required to populate a newly registered replication node, or
028: * to re-populate an inactive replication node.<P>
029: *
030: * This class should be extended, and the populateRelatedEntityMapVector() method should be
031: * called in the descendant class tospecify what entities are to be
032: * replicated.
033: *
034: * To perform full replication, instantiate the descendant class, and call the replicateFull()
035: * method.
036: *
037: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
038: */
039: public abstract class FullReplicator extends EntityReplicator {
040: /**
041: * Debug switch
042: */
043: public static final String module = FullReplicator.class.getName();
044:
045: /**
046: * Constructor with no args.
047: */
048: public FullReplicator() {
049: super ();
050: }
051:
052: /**
053: * Constructor with args.
054: *
055: * @param mainInstance Main entity instance for which related entities will be replicated
056: * @param localDelegator Delegator to attach to local data base
057: * @param masterDelegator Delegator to attach to master data base
058: * @param entityName Name of the entity to be replicated
059: * @param userInfo UserInfo object containing user information
060: */
061: public FullReplicator(GenericDelegator localDelegator,
062: GenericDelegator masterDelegator, String entityName,
063: UserInfo userInfo) {
064: super (localDelegator, masterDelegator, entityName, userInfo);
065: }
066:
067: /**
068: * This method removes all instances of all entities named
069: * in the relatedEntityMapVector.
070: *
071: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
072: *
073: * @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
074: */
075: protected int removeAll() {
076:
077: return removeAllRelated(null);
078: }
079:
080: /**
081: * This method replicates all instances of all entities in the related entity
082: * map vector. It should be used to start full replication after the removeAll method is called.
083: *
084: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
085: *
086: * @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
087: */
088: protected int replicateAll() {
089:
090: return replicateAllRelated(null);
091: }
092:
093: /**
094: * This method removes and replicates all entities in the related entity map vector.
095: * It should be called from the appplication to do a full replication.
096: *
097: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
098: *
099: * @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
100: */
101: public int replicateFull() {
102:
103: if (getUserInfo() == null) {
104: Debug.logError("[replicateFull] User info is required.",
105: module);
106:
107: return STATUS_ERROR;
108: }
109:
110: ;
111:
112: int status = removeAll();
113:
114: if (status == STATUS_CONTINUE) {
115: // Data removal was successful.
116: Debug.logVerbose("[replicateFull] Data removal complete.",
117: module);
118:
119: // Replicate.
120: status = replicateAll();
121:
122: if (status == STATUS_CONTINUE) {
123: // Replication was successful.
124: Debug
125: .logVerbose(
126: "[replicateFull] Replication complete.",
127: module);
128:
129: // Set the node's status to Active.
130: status = markNodeReplicated(getReplNodeId(),
131: getUserInfo().getPartyId());
132:
133: if (status == STATUS_CONTINUE) {
134: // Node status update was successful.
135: Debug.logVerbose(
136: "[replicateFull] Node status updated.",
137: module);
138: } else {
139: // Node status update was unsuccessful. Log a warning.
140: Debug
141: .logError(
142: "[replicateAll] Node status update failed.",
143: module);
144: }
145: } else {
146: // Replication failed.
147: Debug.logError("[replicateAll] Replication failed.",
148: module);
149: }
150: } else {
151: // Removal failed.
152: Debug.logError("[replicateAll] Data removal failed.",
153: module);
154: }
155:
156: return status;
157: }
158: }
|