📄 entityreplicator.java
字号:
* This method removes one instance of the entity named in the entityName attribute. <P>
*
* First the removeRelatedEntities() method is called so child entity instances
* can be removed from the local data base. Then the instance passed in the
* instance argument is removed from the local data base.
*
* @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 removeInstance(GenericValue mainInstance) {
if (mainInstance == null) {
Debug.logError(
"[replicateInstance] Main entity instance is required.", module);
return STATUS_ERROR;
}
// Remove all related entity instances.
int status = removeAllRelated(mainInstance);
if (status != STATUS_CONTINUE) {
return status;
}
// Remove the instance from the local data base.
status = removeLocalInstance(mainInstance);
if (status != STATUS_CONTINUE) {
return status;
}
return STATUS_CONTINUE;
}
/**
* This method stores the instance in the local data base.
*
* @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
*
* @param masterInstance Entity instance from the master data base
*
* @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
*/
public int insertLocalInstance(GenericValue masterInstance) {
if (masterInstance == null) {
Debug.logError(
"[replicateInstance] Master entity instance is required.", module);
return STATUS_ERROR;
}
;
if (getLocalDelegator() == null) {
Debug.logError("[insertLocalInstance] Local delegator is required.", module);
return STATUS_ERROR;
}
;
// Copy the instance, and assign it to the local delegator instead of the master one.
GenericValue localInstance = new GenericValue(masterInstance);
localInstance.setDelegator(getLocalDelegator());
GenericPK localInstancePK = localInstance.getPrimaryKey();
try {
getLocalDelegator().create(localInstance);
Debug.logVerbose("Inserted key " + localInstancePK.toString(), module);
return STATUS_CONTINUE;
} catch (GenericEntityException gee) {
if (gee.getLocalizedMessage().indexOf("Duplicate entry") > 0) {
Debug.logError("[insertLocalInstance] Duplicate key " +
localInstancePK.toString(), module);
return STATUS_CONTINUE;
} else {
Debug.logError("[insertLocalInstance] Error inserting key " +
localInstancePK.toString() + ":", module);
Debug.logError("[insertLocalInstance] " +
gee.getLocalizedMessage(), module);
return STATUS_ERROR;
}
}
}
/**
* This method removes an instance of the main entity from the local data base.
*
* @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
*
* @param localInstance Entity instance being removed
*
* @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
*/
public int removeLocalInstance(GenericValue localInstance) {
if (localInstance == null) {
Debug.logError(
"[replicateInstance] Local entity instance is required.", module);
return STATUS_ERROR;
}
;
if (getLocalDelegator() == null) {
Debug.logError("[insertLocalInstance] Local delegator is required.", module);
return STATUS_ERROR;
}
;
GenericPK localInstancePK = localInstance.getPrimaryKey();
try {
getLocalDelegator().removeByPrimaryKey(localInstance.getPrimaryKey());
Debug.logVerbose("Removed key " + localInstancePK.toString(), module);
return STATUS_CONTINUE;
} catch (GenericEntityException gee) {
Debug.logError("[removeLocalInstance] Error removing key " +
localInstancePK.toString() + ":", module);
Debug.logError("[removeLocalInstance] " +
gee.getLocalizedMessage(), module);
return STATUS_ERROR;
}
}
/**
* This method replicates all instances for all related entities. <P>
*
* This method uses the relatedEntityMapVector to determine what related
* entities need to be replicated. For each related entity, this method calls
* replicateOneRelated(). Each call to replicateOneRelated() calls
* findOneRelated(), which finds all instances of the specified entity. Then
* replicateOneRelated() calls replicateInstance for each instance of the
* related entity to recursively replicate the tree of instances.
*
* @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
*
* @param mainInstance Main entity instance for which related entities will be replicated
*
* @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
*/
public int replicateAllRelated(GenericValue mainInstance) {
Debug.logVerbose("[replicateAllRelated] Start", module);
// if (mainInstance == null) {
// Debug.logError("[replicateInstance] Entity instance is required.");
// return STATUS_ERROR;
// };
Iterator relatedEntityMapVectorI = getRelatedEntityMapVector().iterator();
while (relatedEntityMapVectorI.hasNext()) {
HashMap relatedEntityMap = (HashMap) relatedEntityMapVectorI.next();
String relationTitle = (String) relatedEntityMap.get(
"relationTitle");
String relatedEntityName = (String) relatedEntityMap.get(
"relatedEntityName");
String replicatorClassName = (String) relatedEntityMap.get(
"replicatorClassName");
Boolean replicateAll = (Boolean) relatedEntityMap.get(
"replicateAll");
HashMap filterMap = (HashMap) relatedEntityMap.get("filterMap");
int status = replicateOneRelated(mainInstance, relationTitle,
relatedEntityName, filterMap, replicateAll.booleanValue(),
replicatorClassName);
if (status != STATUS_CONTINUE) {
return status;
}
}
return STATUS_CONTINUE;
}
/**
* This method removes all instances for all related entities. <P>
*
* This method uses the relatedEntityMapVector to determine what related
* entities need to be removed. For each related entity, this method calls
* removeOneRelated(). Each call to removeOneRelated() calls
* findOneRelated(), which finds all instances of the specified entity. Then
* removeOneRelated() calls removeLocalInstance for each instance of the
* related entity to recursively replicate the tree of instances.
*
* @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
*
* @param localInstance Local entity from which related entity instances are to be removed
*
* @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
*/
public int removeAllRelated(GenericValue localInstance) {
Debug.logVerbose("[removeAllRelated] Start", module);
// if (localInstance == null) {
// Debug.logError("[replicateInstance] Entity instance is required.");
// return STATUS_ERROR;
// };
// Remove the entities in reverse order in case RI is turned on.
for (int relatedEntityNbr = getRelatedEntityMapVector().size() - 1;
relatedEntityNbr >= 0; relatedEntityNbr--) {
// Iterator relatedEntityMapVectorI = getRelatedEntityMapVector().iterator();
// while (relatedEntityMapVectorI.hasNext()) {
HashMap relatedEntityMap = (HashMap) getRelatedEntityMapVector()
.get(relatedEntityNbr);
// HashMap relatedEntityMap = (HashMap)relatedEntityMapVectorI.next();
String relationTitle = (String) relatedEntityMap.get(
"relationTitle");
String relatedEntityName = (String) relatedEntityMap.get(
"relatedEntityName");
Boolean removeAll = (Boolean) relatedEntityMap.get("removeAll");
String replicatorClassName = (String) relatedEntityMap.get(
"replicatorClassName");
HashMap filterMap = (HashMap) relatedEntityMap.get("filterMap");
int status = removeOneRelated(localInstance, relationTitle,
relatedEntityName, filterMap, removeAll.booleanValue(),
replicatorClassName);
if (status != STATUS_CONTINUE) {
return status;
}
}
return STATUS_CONTINUE;
}
/**
* This method replicates all instances for one entity related to the main entity. <P>
*
* This method calls findOneRelated(), which finds all instances of the related entity.
* Then this method calls replicateInstance for each instance of the
* related entity to recursively replicate the tree of instances.
*
* @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
* @param replicatorClassName Name of a descendant class of this class which will be used
* to replicate the related entity instances
*
* @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
*/
protected int replicateOneRelated(GenericValue mainInstance,
String relationTitle, String relatedEntityName, HashMap filterMap,
boolean replicateAll, String replicatorClassName) {
UtilTimer timer = new UtilTimer();
if (TIMER) {
timer.timerString(1, "[replicateOneRelated] Start");
}
Debug.logVerbose("[replicateOneRelated] Start", module);
Debug.logVerbose("[replicateOneRelated] relationTitle: " + relationTitle, module);
Debug.logVerbose("[replicateOneRelated] relatedEntityName: " + relatedEntityName, module);
Debug.logVerbose("[replicateOneRelated] replicatorClassName: " + replicatorClassName, module);
// Instantiate related replicator class.
EntityReplicator relatedReplicator = getEntityReplicator(replicatorClassName,
getLocalDelegator(), getMasterDelegator(), relatedEntityName,
getUserInfo());
if (relatedReplicator == null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -