📄 entitysyncservices.java
字号:
// store result of service call on history with results so far, should run in own tx
Map updateHistoryMap = UtilMisc.toMap("entitySyncId", entitySyncId, "startDate", startDate, "lastSuccessfulSynchTime", currentRunEndTime);
updateHistoryMap.put("toCreateInserted", new Long(toCreateInserted));
updateHistoryMap.put("toCreateUpdated", new Long(toCreateUpdated));
updateHistoryMap.put("toCreateNotUpdated", new Long(toCreateNotUpdated));
updateHistoryMap.put("toStoreInserted", new Long(toStoreInserted));
updateHistoryMap.put("toStoreUpdated", new Long(toStoreUpdated));
updateHistoryMap.put("toStoreNotUpdated", new Long(toStoreNotUpdated));
updateHistoryMap.put("toRemoveDeleted", new Long(toRemoveDeleted));
updateHistoryMap.put("toRemoveAlreadyDeleted", new Long(toRemoveAlreadyDeleted));
updateHistoryMap.put("runningTimeMillis", new Long(runningTimeMillis));
updateHistoryMap.put("totalStoreCalls", new Long(totalStoreCalls));
updateHistoryMap.put("totalSplits", new Long(totalSplits));
updateHistoryMap.put("totalRowsToCreate", new Long(totalRowsToCreate));
updateHistoryMap.put("totalRowsToStore", new Long(totalRowsToStore));
updateHistoryMap.put("totalRowsToRemove", new Long(totalRowsToRemove));
updateHistoryMap.put("perSplitMinMillis", new Long(perSplitMinMillis));
updateHistoryMap.put("perSplitMaxMillis", new Long(perSplitMaxMillis));
updateHistoryMap.put("perSplitMinItems", new Long(perSplitMinItems));
updateHistoryMap.put("perSplitMaxItems", new Long(perSplitMaxItems));
updateHistoryMap.put("userLogin", userLogin);
Map updateEsHistRunResult = dispatcher.runSync("updateEntitySyncHistory", updateHistoryMap);
// now we have updated EntitySync and EntitySyncHistory, check both ops for errors...
if (ServiceUtil.isError(updateEsRunResult)) {
String errorMsg = "Error running EntitySync [" + entitySyncId + "], update of EntitySync record with lastSuccessfulSynchTime failed.";
List errorList = new LinkedList();
saveSyncErrorInfo(entitySyncId, startDate, "ESR_DATA_ERROR", errorList, dispatcher, userLogin);
return ServiceUtil.returnError(errorMsg, errorList, null, updateEsRunResult);
}
if (ServiceUtil.isError(updateEsHistRunResult)) {
String errorMsg = "Error running EntitySync [" + entitySyncId + "], update of EntitySyncHistory (startDate:[" + startDate + "]) record with lastSuccessfulSynchTime and result stats failed.";
List errorList = new LinkedList();
saveSyncErrorInfo(entitySyncId, startDate, "ESR_DATA_ERROR", errorList, dispatcher, userLogin);
return ServiceUtil.returnError(errorMsg, errorList, null, updateEsHistRunResult);
}
// update start time, loop
currentRunStartTime = currentRunEndTime;
}
// the lastSuccessfulSynchTime on EntitySync will already be set, so just set status as completed
Map completeEntitySyncRes = dispatcher.runSync("updateEntitySyncRunning", UtilMisc.toMap("entitySyncId", entitySyncId, "runStatusId", "ESR_COMPLETE", "userLogin", userLogin));
if (ServiceUtil.isError(completeEntitySyncRes)) {
// what to do here? try again?
return ServiceUtil.returnError("Could not mark Entity Sync as complete, but all synchronization was successful", null, null, completeEntitySyncRes);
}
// if nothing moved over, remove the history record, otherwise store status
long totalRows = totalRowsToCreate + totalRowsToStore + totalRowsToRemove;
if (totalRows == 0) {
Map deleteEntitySyncHistRes = dispatcher.runSync("deleteEntitySyncHistory", UtilMisc.toMap("entitySyncId", entitySyncId, "startDate", startDate, "userLogin", userLogin));
if (ServiceUtil.isError(deleteEntitySyncHistRes)) {
return ServiceUtil.returnError("Could not remove Entity Sync History (done becuase nothing was synced in this call), but all synchronization was successful", null, null, deleteEntitySyncHistRes);
}
} else {
// the lastSuccessfulSynchTime on EntitySync will already be set, so just set status as completed
Map completeEntitySyncHistRes = dispatcher.runSync("updateEntitySyncHistory", UtilMisc.toMap("entitySyncId", entitySyncId, "startDate", startDate, "runStatusId", "ESR_COMPLETE", "userLogin", userLogin));
if (ServiceUtil.isError(completeEntitySyncHistRes)) {
// what to do here? try again?
return ServiceUtil.returnError("Could not mark Entity Sync History as complete, but all synchronization was successful", null, null, completeEntitySyncHistRes);
}
}
if (Debug.infoOn()) Debug.logInfo("Finished runEntitySync: totalRows=" + totalRows + ", totalRowsToCreate=" + totalRowsToCreate + ", totalRowsToStore=" + totalRowsToStore + ", totalRowsToRemove=" + totalRowsToRemove, module);
} catch (GenericEntityException e) {
String errorMessage = "Error running EntitySync [" + entitySyncId + "], data access error: " + e.toString();
Debug.logError(e, errorMessage, module);
List errorList = new LinkedList();
saveSyncErrorInfo(entitySyncId, startDate, "ESR_DATA_ERROR", errorList, dispatcher, userLogin);
return ServiceUtil.returnError(errorMessage, errorList, null, null);
} catch (GenericServiceException e) {
String errorMessage = "Error running EntitySync [" + entitySyncId + "], service call error: " + e.toString();
Debug.logError(e, errorMessage, module);
List errorList = new LinkedList();
saveSyncErrorInfo(entitySyncId, startDate, "ESR_SERVICE_ERROR", errorList, dispatcher, userLogin);
return ServiceUtil.returnError(errorMessage, errorList, null, null);
}
return ServiceUtil.returnSuccess();
}
protected static void saveSyncErrorInfo(String entitySyncId, Timestamp startDate, String runStatusId, List errorMessages, LocalDispatcher dispatcher, GenericValue userLogin) {
// set error statuses on the EntitySync and EntitySyncHistory entities
try {
Map errorEntitySyncRes = dispatcher.runSync("updateEntitySyncRunning", UtilMisc.toMap("entitySyncId", entitySyncId, "runStatusId", runStatusId, "userLogin", userLogin));
if (ServiceUtil.isError(errorEntitySyncRes)) {
errorMessages.add("Could not save error run status [" + runStatusId + "] on EntitySync with ID [" + entitySyncId + "]: " + errorEntitySyncRes.get(ModelService.ERROR_MESSAGE));
}
} catch (GenericServiceException e) {
errorMessages.add("Could not save error run status [" + runStatusId + "] on EntitySync with ID [" + entitySyncId + "]: " + e.toString());
}
if (startDate != null) {
try {
Map errorEntitySyncHistoryRes = dispatcher.runSync("updateEntitySyncHistory", UtilMisc.toMap("entitySyncId", entitySyncId, "startDate", startDate, "runStatusId", runStatusId, "userLogin", userLogin));
if (ServiceUtil.isError(errorEntitySyncHistoryRes)) {
errorMessages.add("Could not save error run status [" + runStatusId + "] on EntitySyncHistory with ID [" + entitySyncId + "]: " + errorEntitySyncHistoryRes.get(ModelService.ERROR_MESSAGE));
}
} catch (GenericServiceException e) {
errorMessages.add("Could not save error run status [" + runStatusId + "] on EntitySyncHistory with ID [" + entitySyncId + ":" + startDate + "]: " + e.toString());
}
}
}
/** prepare a list of all entities we want to synchronize: remove all view-entities and all entities that don't match the patterns attached to this EntitySync */
protected static List makeEntityModelToUseList(GenericDelegator delegator, GenericValue entitySync) throws GenericEntityException {
List entityModelToUseList = new LinkedList();
List entitySyncIncludes = entitySync.getRelated("EntitySyncInclude");
Iterator entityNameIter = delegator.getModelReader().getEntityNamesIterator();
while (entityNameIter.hasNext()) {
String entityName = (String) entityNameIter.next();
ModelEntity modelEntity = delegator.getModelEntity(entityName);
// if view-entity, throw it out
if (modelEntity instanceof ModelViewEntity) {
continue;
}
// if it doesn't have either or both of the two update stamp fields, throw it out
if (!modelEntity.isField(ModelEntity.STAMP_FIELD) || !modelEntity.isField(ModelEntity.STAMP_TX_FIELD)) {
continue;
}
// if no includes records, always include; otherwise check each one to make sure at least one matches
if (entitySyncIncludes.size() == 0) {
entityModelToUseList.add(modelEntity);
} else {
// we have different types of include applications: ESIA_INCLUDE, ESIA_EXCLUDE, ESIA_ALWAYS
// if we find an always we can break right there because this will always be include regardless of excludes, etc
// if we find an include or exclude we have to finish going through the rest of them just in case there is something that overrides it (ie an exclude for an include or an always for an exclude)
boolean matchesInclude = false;
boolean matchesExclude = false;
boolean matchesAlways = false;
Iterator entitySyncIncludeIter = entitySyncIncludes.iterator();
while (entitySyncIncludeIter.hasNext()) {
GenericValue entitySyncInclude = (GenericValue) entitySyncIncludeIter.next();
String entityOrPackage = entitySyncInclude.getString("entityOrPackage");
boolean matches = false;
if (entityName.equals(entityOrPackage)) {
matches = true;
} else if (modelEntity.getPackageName().startsWith(entityOrPackage)) {
matches = true;
}
if (matches) {
if ("ESIA_INCLUDE".equals(entitySyncInclude.getString("applEnumId"))) {
matchesInclude = true;
} else if ("ESIA_EXCLUDE".equals(entitySyncInclude.getString("applEnumId"))) {
matchesExclude = true;
} else if ("ESIA_ALWAYS".equals(entitySyncInclude.getString("applEnumId"))) {
matchesAlways = true;
break;
}
}
}
if (matchesAlways || (matchesInclude && !matchesExclude)) {
// make sure this log message is not checked in uncommented:
//Debug.log("In runEntitySync adding [" + modelEntity.getEntityName() + "] to list of Entities to sync", module);
entityModelToUseList.add(modelEntity);
}
}
}
if (Debug.infoOn()) Debug.logInfo("In runEntitySync with ID [" + entitySync.get("entitySyncId") + "] syncing " + entityModelToUseList.size() + " entities", module);
return entityModelToUseList;
}
/**
* Store Entity Sync Data
*@param dctx The DispatchContext that this service is operating in
*@param context Map containing the input parameters
*@return Map with the result of the service, the output parameters
*/
public static Map storeEntitySyncData(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
String overrideDelegatorName = (String) context.get("delegatorName");
if (UtilValidate.isNotEmpty(overrideDelegatorName)) {
delegator = GenericDelegator.getGenericDelegator(overrideDelegatorName);
if (delegator == null) {
return ServiceUtil.returnError("Could not find delegator with specified name " + overrideDelegatorName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -