📄 entitysynccontext.java
字号:
pkToRemove.set(ModelEntity.STAMP_TX_FIELD, entitySyncRemove.get(ModelEntity.STAMP_TX_FIELD)); pkToRemove.set(ModelEntity.STAMP_FIELD, entitySyncRemove.get(ModelEntity.STAMP_FIELD)); pkToRemove.set(ModelEntity.CREATE_STAMP_TX_FIELD, entitySyncRemove.get(ModelEntity.CREATE_STAMP_TX_FIELD)); pkToRemove.set(ModelEntity.CREATE_STAMP_FIELD, entitySyncRemove.get(ModelEntity.CREATE_STAMP_FIELD)); if (this.entityNameToUseSet.contains(pkToRemove.getEntityName())) { keysToRemove.add(pkToRemove); } } removeEli.close(); // if we didn't find anything for this entity, find the next value's Timestamp and keep track of it if (keysToRemove.size() == 0) { EntityCondition findNextCondition = new EntityExpr(ModelEntity.STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunEndTime); EntityListIterator eliNext = delegator.findListIteratorByCondition("EntitySyncRemove", findNextCondition, null, UtilMisc.toList(ModelEntity.STAMP_TX_FIELD)); // get the first element and it's tx time value... GenericValue firstVal = (GenericValue) eliNext.next(); eliNext.close(); if (firstVal != null) { Timestamp nextTxTime = firstVal.getTimestamp(ModelEntity.STAMP_TX_FIELD); if (this.nextUpdateTxTime == null || nextTxTime.before(this.nextUpdateTxTime)) { this.nextUpdateTxTime = nextTxTime; } } } } catch (GenericEntityException e) { try { TransactionUtil.rollback(beganTransaction, "Entity Engine error in assembleKeysToRemove", e); } catch (GenericTransactionException e2) { Debug.logWarning(e2, "Unable to call rollback()", module); } throw new SyncDataErrorException("Error getting keys to remove from the datasource", e); } catch (Throwable t) { try { TransactionUtil.rollback(beganTransaction, "General error in assembleKeysToRemove", t); } catch (GenericTransactionException e2) { Debug.logWarning(e2, "Unable to call rollback()", module); } throw new SyncDataErrorException("Caught runtime error while getting keys to remove", t); } try { TransactionUtil.commit(beganTransaction); } catch (GenericTransactionException e) { throw new SyncDataErrorException("Commit transaction failed", e); } // TEST SECTION: leave false for normal use boolean logValues = false; if (logValues && keysToRemove.size() > 0) { StringBuffer toRemoveInfo = new StringBuffer(); Iterator keysToRemoveIter = keysToRemove.iterator(); while (keysToRemoveIter.hasNext()) { GenericEntity keyToRemove = (GenericEntity) keysToRemoveIter.next(); toRemoveInfo.append("\n-->["); toRemoveInfo.append(keyToRemove.get(ModelEntity.STAMP_TX_FIELD)); toRemoveInfo.append(":"); toRemoveInfo.append(keyToRemove.get(ModelEntity.STAMP_FIELD)); toRemoveInfo.append("] "); toRemoveInfo.append(keyToRemove); } Debug.logInfo(toRemoveInfo.toString(), module); } return keysToRemove; } public void saveResultsReportedFromDataStore() throws SyncDataErrorException, SyncServiceErrorException { try { long runningTimeMillis = System.currentTimeMillis() - startDate.getTime(); // get the total for this split long splitTotalTime = System.currentTimeMillis() - this.splitStartTime; if (splitTotalTime < this.perSplitMinMillis) { this.perSplitMinMillis = splitTotalTime; } if (splitTotalTime > this.perSplitMaxMillis) { this.perSplitMaxMillis = splitTotalTime; } // start the timer for the next split setSplitStartTime(); // total the rows saved so far, and gather some info about them before saving this.totalRowsPerSplit = this.toCreateInserted + this.toCreateNotUpdated + this.toCreateUpdated + this.toStoreInserted + this.toStoreNotUpdated + this.toStoreUpdated + this.toRemoveAlreadyDeleted + this.toRemoveDeleted; if (this.totalRowsPerSplit < this.perSplitMinItems) { this.perSplitMinItems = this.totalRowsPerSplit; } if (this.totalRowsPerSplit > this.perSplitMaxItems) { this.perSplitMaxItems = this.totalRowsPerSplit; } this.totalRowsToCreate += this.toCreateInserted + this.toCreateNotUpdated + this.toCreateUpdated; this.totalRowsToStore += this.toStoreInserted + this.toStoreNotUpdated + this.toStoreUpdated; this.totalRowsToRemove += this.toRemoveAlreadyDeleted + this.toRemoveDeleted; // store latest result on EntitySync, ie update lastSuccessfulSynchTime, should run in own tx Map updateEsRunResult = dispatcher.runSync("updateEntitySyncRunning", UtilMisc.toMap("entitySyncId", entitySyncId, "lastSuccessfulSynchTime", this.currentRunEndTime, "userLogin", userLogin)); // 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", this.currentRunEndTime, "lastCandidateEndTime", this.getNextRunEndTime(), "lastSplitStartTime", new Long(this.splitStartTime)); 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("totalRowsExported", new Long(totalRowsExported)); 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."; throw new SyncDataErrorException(errorMsg, null, null, updateEsRunResult, null); } if (ServiceUtil.isError(updateEsHistRunResult)) { String errorMsg = "Error running EntitySync [" + entitySyncId + "], update of EntitySyncHistory (startDate:[" + startDate + "]) record with lastSuccessfulSynchTime and result stats failed."; throw new SyncDataErrorException(errorMsg, null, null, updateEsHistRunResult, null); } } catch (GenericServiceException e) { throw new SyncServiceErrorException("Error saving results reported from data store", e); } } public void saveFinalSyncResults() throws SyncDataErrorException, SyncServiceErrorException { String newStatusId = "ESR_COMPLETE"; if (this.isOfflineSync && totalRowsExported > 0) { newStatusId = "ESR_PENDING"; } // the lastSuccessfulSynchTime on EntitySync will already be set, so just set status as completed String esErrMsg = "Could not mark Entity Sync as complete, but all synchronization was successful"; try { Map completeEntitySyncRes = dispatcher.runSync("updateEntitySyncRunning", UtilMisc.toMap("entitySyncId", entitySyncId, "runStatusId", newStatusId, "userLogin", userLogin)); if (ServiceUtil.isError(completeEntitySyncRes)) { // what to do here? try again? throw new SyncDataErrorException(esErrMsg, null, null, completeEntitySyncRes, null); } } catch (GenericServiceException e) { throw new SyncServiceErrorException(esErrMsg, e); } // if nothing moved over, remove the history record, otherwise store status long totalRows = totalRowsToCreate + totalRowsToStore + totalRowsToRemove; if (totalRows == 0) { String eshRemoveErrMsg = "Could not remove Entity Sync History (done becuase nothing was synced in this call), but all synchronization was successful"; try { Map deleteEntitySyncHistRes = dispatcher.runSync("deleteEntitySyncHistory", UtilMisc.toMap("entitySyncId", entitySyncId, "startDate", startDate, "userLogin", userLogin)); if (ServiceUtil.isError(deleteEntitySyncHistRes)) { throw new SyncDataErrorException(eshRemoveErrMsg, null, null, deleteEntitySyncHistRes, null); } } catch (GenericServiceException e) { throw new SyncServiceErrorException(eshRemoveErrMsg, e); } } else { // the lastSuccessfulSynchTime on EntitySync will already be set, so just set status as completed String eshCompleteErrMsg = "Could not mark Entity Sync History as complete, but all synchronization was successful"; try { 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? throw new SyncDataErrorException(eshCompleteErrMsg, null, null, completeEntitySyncHistRes, null); } } catch (GenericServiceException e) { throw new SyncServiceErrorException(eshCompleteErrMsg, e); } } if (Debug.infoOn()) Debug.logInfo("Finished saveFinalSyncResults [" + entitySyncId + "]: totalRows=" + totalRows + ", totalRowsToCreate=" + totalRowsToCreate + ", totalRowsToStore=" + totalRowsToStore + ", totalRowsToRemove=" + totalRowsToRemove, module); } public Set makeEntityNameToUseSet() { Set entityNameToUseSet = new HashSet(); Iterator entityModelToUseUpdateIter = this.entityModelToUseList.iterator(); while (entityModelToUseUpdateIter.hasNext()) { ModelEntity modelEntity = (ModelEntity) entityModelToUseUpdateIter.next(); entityNameToUseSet.add(modelEntity.getEntityName()); } return entityNameToUseSet; } /** 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 List makeEntityModelToUseList() throws GenericEntityException { List entityModelToUseList = new LinkedList(); List entitySyncIncludes = entitySync.getRelated("EntitySyncInclude"); // get these ones as well, and just add them to the main list, it will have an extra field but that shouldn't hurt anything in the code below List entitySyncGroupIncludes = entitySync.getRelated("EntitySyncInclGrpDetailView"); entitySyncIncludes.addAll(entitySyncGroupIncludes); 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)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -