📄 entitysyncservices.java
字号:
// save the progress to EntitySync and EntitySyncHistory, and move on... esc.saveResultsReportedFromDataStore(); esc.advanceRunTimes(); } } // if no more results from database to return, save final settings if (!esc.hasMoreTimeToSync() ) { esc.saveFinalSyncResults(); } } catch (SyncAbortException e) { return e.returnError(module); } catch (SyncErrorException e) { e.saveSyncErrorInfo(esc); return e.returnError(module); } return ServiceUtil.returnSuccess(); } public static Map runOfflineEntitySync(DispatchContext dctx, Map context) { String fileName = (String) context.get("fileName"); EntitySyncContext esc = null; long totalRowsExported = 0; try { esc = new EntitySyncContext(dctx, context); Debug.logInfo("Doing runManualEntitySync for entitySyncId=" + esc.entitySyncId + ", currentRunStartTime=" + esc.currentRunStartTime + ", currentRunEndTime=" + esc.currentRunEndTime, module); Document mainDoc = UtilXml.makeEmptyXmlDocument("xml-entity-synchronization"); Element docElement = mainDoc.getDocumentElement(); docElement.setAttribute("xml:lang", "en-US"); esc.runOfflineStartRunning(); // increment starting time to run until now esc.setSplitStartTime(); // just run this the first time, will be updated between each loop automatically while (esc.hasMoreTimeToSync()) { esc.totalSplits++; ArrayList valuesToCreate = esc.assembleValuesToCreate(); ArrayList valuesToStore = esc.assembleValuesToStore(); List keysToRemove = esc.assembleKeysToRemove(); long currentRows = esc.setTotalRowCounts(valuesToCreate, valuesToStore, keysToRemove); totalRowsExported += currentRows; if (currentRows > 0) { // create the XML document Element syncElement = UtilXml.addChildElement(docElement, "entity-sync", mainDoc); syncElement.setAttribute("entitySyncId", esc.entitySyncId); syncElement.setAttribute("lastSuccessfulSynchTime", esc.currentRunEndTime.toString()); // serialize the list data for XML storage try { UtilXml.addChildElementValue(syncElement, "values-to-create", XmlSerializer.serialize(valuesToCreate), mainDoc); UtilXml.addChildElementValue(syncElement, "values-to-store", XmlSerializer.serialize(valuesToStore), mainDoc); UtilXml.addChildElementValue(syncElement, "keys-to-remove", XmlSerializer.serialize(keysToRemove), mainDoc); } catch (SerializeException e) { throw new EntitySyncContext.SyncOtherErrorException("List serialization problem", e); } catch (IOException e) { throw new EntitySyncContext.SyncOtherErrorException("XML writing problem", e); } } // update the result info esc.runSaveOfflineSyncInfo(currentRows); esc.advanceRunTimes(); } if (totalRowsExported > 0) { // check the file name; use a default if none is passed in if (UtilValidate.isEmpty(fileName)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); fileName = "offline_entitySync-" + esc.entitySyncId + "-" + sdf.format(new Date()) + ".xml"; } // write the XML file try { UtilXml.writeXmlDocument(fileName, mainDoc); } catch (java.io.FileNotFoundException e) { throw new EntitySyncContext.SyncOtherErrorException(e); } catch (java.io.IOException e) { throw new EntitySyncContext.SyncOtherErrorException(e); } } else { Debug.logInfo("No rows to write; no data exported.", module); } // save the final results esc.saveFinalSyncResults(); } catch (SyncAbortException e) { return e.returnError(module); } catch (SyncErrorException e) { e.saveSyncErrorInfo(esc); return e.returnError(module); } return ServiceUtil.returnSuccess(); } public static Map loadOfflineSyncData(DispatchContext dctx, Map context) { LocalDispatcher dispatcher = dctx.getDispatcher(); GenericDelegator delegator = dctx.getDelegator(); GenericValue userLogin = (GenericValue) context.get("userLogin"); String fileName = (String) context.get("xmlFileName"); URL xmlFile = UtilURL.fromResource(fileName); if (xmlFile != null) { Document xmlSyncDoc = null; try { xmlSyncDoc = UtilXml.readXmlDocument(xmlFile, false); } catch (SAXException e) { Debug.logError(e, module); } catch (ParserConfigurationException e) { Debug.logError(e, module); } catch (IOException e) { Debug.logError(e, module); } if (xmlSyncDoc == null) { return ServiceUtil.returnError("EntitySync XML document (" + fileName + ") is not valid!"); } List syncElements = UtilXml.childElementList(xmlSyncDoc.getDocumentElement()); if (syncElements != null) { Iterator i = syncElements.iterator(); while (i.hasNext()) { Element entitySync = (Element) i.next(); String entitySyncId = entitySync.getAttribute("entitySyncId"); String startTime = entitySync.getAttribute("lastSuccessfulSynchTime"); String createString = UtilXml.childElementValue(entitySync, "values-to-create"); String storeString = UtilXml.childElementValue(entitySync, "values-to-store"); String removeString = UtilXml.childElementValue(entitySync, "keys-to-remove"); // de-serialize the value lists try { List valuesToCreate = (List) XmlSerializer.deserialize(createString, delegator); List valuesToStore = (List) XmlSerializer.deserialize(storeString, delegator); List keysToRemove = (List) XmlSerializer.deserialize(removeString, delegator); Map storeContext = UtilMisc.toMap("entitySyncId", entitySyncId, "valuesToCreate", valuesToCreate, "valuesToStore", valuesToStore, "keysToRemove", keysToRemove, "userLogin", userLogin); // store the value(s) Map storeResult = dispatcher.runSync("storeEntitySyncData", storeContext); if (ServiceUtil.isError(storeResult)) { throw new Exception(ServiceUtil.getErrorMessage(storeResult)); } // TODO create a response document to send back to the initial sync machine } catch (Exception e) { return ServiceUtil.returnError("Unable to load EntitySync XML [" + entitySyncId + "] - Problem at '" + startTime + "' Error: " + e.getMessage()); } } } } else { return ServiceUtil.returnError("Offline EntitySync XML file not found (" + fileName + ")"); } return ServiceUtil.returnSuccess(); } public static Map updateOfflineEntitySync(DispatchContext dctx, Map context) { return ServiceUtil.returnError("Service not yet implemented."); } /** * Clean EntitySyncRemove Info *@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 cleanSyncRemoveInfo(DispatchContext dctx, Map context) { Debug.logInfo("Running cleanSyncRemoveInfo", module); GenericDelegator delegator = dctx.getDelegator(); try { // find the largest keepRemoveInfoHours value on an EntitySyncRemove and kill everything before that, if none found default to 10 days (240 hours) double keepRemoveInfoHours = 24; List entitySyncRemoveList = delegator.findAll("EntitySync"); Iterator entitySyncRemoveIter = entitySyncRemoveList.iterator(); while (entitySyncRemoveIter.hasNext()) { GenericValue entitySyncRemove = (GenericValue) entitySyncRemoveIter.next(); Double curKrih = entitySyncRemove.getDouble("keepRemoveInfoHours"); if (curKrih != null) { double curKrihVal = curKrih.doubleValue(); if (curKrihVal > keepRemoveInfoHours) { keepRemoveInfoHours = curKrihVal; } } } int keepSeconds = (int) Math.floor(keepRemoveInfoHours * 60); Calendar nowCal = Calendar.getInstance(); nowCal.setTimeInMillis(System.currentTimeMillis()); nowCal.add(Calendar.SECOND, -keepSeconds); Timestamp keepAfterStamp = new Timestamp(nowCal.getTimeInMillis()); int numRemoved = delegator.removeByCondition("EntitySyncRemove", new EntityExpr(ModelEntity.STAMP_TX_FIELD, EntityOperator.LESS_THAN, keepAfterStamp)); Debug.logInfo("In cleanSyncRemoveInfo removed [" + numRemoved + "] values with TX timestamp before [" + keepAfterStamp + "]", module); return ServiceUtil.returnSuccess(); } catch (GenericEntityException e) { String errorMsg = "Error cleaning out EntitySyncRemove info: " + e.toString(); Debug.logError(e, errorMsg, module); return ServiceUtil.returnError(errorMsg); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -