📄 entityreplicator.java
字号:
return STATUS_ERROR;
}
String entityName = "*";
if (mainInstance != null) {
entityName = mainInstance.getEntityName();
}
if (TIMER) {
timer.timerString(1,
"[EntityReplicator.replicateOneRelated] Starting findOneRelated (" +
entityName + "/" + relationTitle + relatedEntityName + ")");
}
List relatedGVL = findOneRelated(getMasterDelegator(), mainInstance,
relationTitle, relatedEntityName, filterMap, replicateAll);
if (relatedGVL == null) {
return STATUS_ERROR;
}
if (TIMER) {
timer.timerString(1,
"[EntityReplicator.replicateOneRelated] Finished findOneRelated (" +
entityName + "/" + relationTitle + relatedEntityName + ")");
}
Iterator relatedGVI = relatedGVL.iterator();
while (relatedGVI.hasNext()) {
GenericValue relatedGV = (GenericValue) relatedGVI.next();
int status = relatedReplicator.replicateInstance(relatedGV);
if (status != STATUS_CONTINUE) {
return status;
}
}
if (TIMER) {
timer.timerString(1, "[replicateOneRelated] End");
}
return STATUS_CONTINUE;
}
/**
* This method removes 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 removeInstance 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 removeAll Flag indicating to remove all instances of this entity instead of
* just removing 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
*
* @return Status. Possible values: STATUS_CONTINUE, STATUS_ERROR, STATUS_CANCELED
*/
protected int removeOneRelated(GenericValue mainInstance,
String relationTitle, String relatedEntityName, HashMap filterMap,
boolean removeAll, String replicatorClassName) {
// Instantiate related replicator class.
EntityReplicator relatedReplicator = getEntityReplicator(replicatorClassName,
getLocalDelegator(), getMasterDelegator(), relatedEntityName,
getUserInfo());
if (relatedReplicator == null) {
return STATUS_ERROR;
}
List relatedGVL = findOneRelated(getLocalDelegator(), mainInstance,
relationTitle, relatedEntityName, filterMap, removeAll);
if (relatedGVL == null) {
return STATUS_ERROR;
}
Iterator relatedGVI = relatedGVL.iterator();
while (relatedGVI.hasNext()) {
GenericValue relatedGV = (GenericValue) relatedGVI.next();
int status = relatedReplicator.removeInstance(relatedGV);
if (status != STATUS_CONTINUE) {
return status;
}
}
return STATUS_CONTINUE;
}
/**
* This method finds all instances of an entity related to the main entity.
* This is used for replicating and removing, so it accepts the delegator as an
* argument to point it to the correct data base.
*
* @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 findAll Flag indicating to find all instances of the entity instead of using
* a relation
*
* @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) {
UtilTimer timer = new UtilTimer();
if (TIMER) {
timer.timerString(2, "[EntityReplicator.findOneRelated] Start");
}
String entityName = "*";
if (mainInstance != null) {
entityName = mainInstance.getEntityName();
}
if (delegator == null) {
Debug.logError("[findOneRelated] Delegator is required.", module);
return null;
}
if (userInfo == null) {
Debug.logError("[findOneRelated] User info object is required.", module);
return null;
}
GenericPK entityPK = null;
String entityKeyString = null;
List relatedGVL = null;
if (findAll) {
// Need to find all entity instances, not just the ones related to
// the main entity instance.
try {
if (TIMER) {
timer.timerString(2,
"[EntityReplicator.findOneRelated] Start findByAnd");
}
relatedGVL = delegator.findByAnd(relatedEntityName, filterMap);
if (TIMER) {
timer.timerString(2,
"[EntityReplicator.findOneRelated] Finished findByAnd");
}
return relatedGVL;
} catch (GenericEntityException e) {
Debug.logError("[findOneRelated] Error getting " +
relatedEntityName + " records by and: " +
e.getLocalizedMessage(), module);
return null;
}
} else {
// Need to find just the entity instances related to the main entity instance.
if (mainInstance == null) {
Debug.logError(
"[findOneRelated] Main instance is required if findAll is false.", module);
return null;
}
entityPK = mainInstance.getPrimaryKey();
entityKeyString = entityPK.toString();
Debug.logVerbose("[findOneRelated] Retrieving all " +
relatedEntityName + " records related to " +
entityKeyString, module);
try {
if (TIMER) {
timer.timerString(2,
"[EntityReplicator.findOneRelated] Start getRelated");
}
relatedGVL = delegator.getRelated(relationTitle +
relatedEntityName, filterMap, null, mainInstance);
if (TIMER) {
timer.timerString(2,
"[EntityReplicator.findOneRelated] Finished getRelated");
}
return relatedGVL;
} catch (GenericEntityException e) {
Debug.logError("[findOneRelated] Error getting the related " +
relatedEntityName + " records: " + e.getLocalizedMessage(), module);
return null;
}
}
}
/**
* Instantiates EntityReplicator or one of its descendant classes.
*
* @param className The class to be instantiated
*
* @return Object of class specified by className parameter
*/
public static EntityReplicator getEntityReplicator(String className,
GenericDelegator localDelegator, GenericDelegator masterDelegator,
String entityName, UserInfo userInfo) {
Debug.logVerbose("[getEntityReplicator] className: " + className, module);
Debug.logVerbose("[getEntityReplicator] entityName: " + entityName, module);
Class entityReplicatorClass = null;
EntityReplicator entityReplicator = null;
if ((className.length() > 0) && !className.equals("null")) {
try {
entityReplicatorClass = Class.forName(className);
} catch (ClassNotFoundException e) {
Debug.logError("[getEntityReplicator] Class \"" + className +
"\" specified in relatedEntityMapVector could not be found.", module);
Debug.logError(e, module);
return null;
}
try {
entityReplicator = (EntityReplicator) entityReplicatorClass.newInstance();
} catch (IllegalAccessException e) {
Debug.logError(
"[getEntityReplicator] EntityReplicator class \"" +
className + "\" could not be instantiated because " +
"the class or initializer is not accessible.", module);
Debug.logError(e, module);
return null;
} catch (InstantiationException e) {
Debug.logError(
"[getEntityReplicator] EntityReplicator class \"" +
className + "\" cannot be instantiated because it is an " +
"abstract class, an interface, an array class, a primitive type, or void.", module);
Debug.logError(e, module);
return null;
}
} else {
// Class name was not specified in the display object. Use the default class.
entityReplicator = new EntityReplicator();
}
// Set the delegators on the new entity replicator object.
entityReplicator.setLocalDelegator(localDelegator);
entityReplicator.setMasterDelegator(masterDelegator);
entityReplicator.setEntityName(entityName);
entityReplicator.setUserInfo(userInfo);
// Return the new instance of EntityReplicator.
return entityReplicator;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -