📄 entitysyncservices.java
字号:
/* * $Id: EntitySyncServices.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */package org.ofbiz.entityext.synchronization;import java.sql.Timestamp;import java.util.ArrayList;import java.util.Calendar;import java.util.Collection;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Date;import java.text.SimpleDateFormat;import java.io.IOException;import java.net.URL;import javax.xml.parsers.ParserConfigurationException;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilMisc;import org.ofbiz.base.util.UtilValidate;import org.ofbiz.base.util.UtilXml;import org.ofbiz.base.util.UtilURL;import org.ofbiz.entity.GenericDelegator;import org.ofbiz.entity.GenericEntity;import org.ofbiz.entity.GenericEntityException;import org.ofbiz.entity.GenericValue;import org.ofbiz.entity.serialize.XmlSerializer;import org.ofbiz.entity.serialize.SerializeException;import org.ofbiz.entity.condition.EntityExpr;import org.ofbiz.entity.condition.EntityOperator;import org.ofbiz.entity.model.ModelEntity;import org.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException;import org.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GenericServiceException;import org.ofbiz.service.LocalDispatcher;import org.ofbiz.service.ServiceUtil;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.SAXException;/** * Entity Engine Sync Services * * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 5462 $ * @since 3.0 */public class EntitySyncServices { public static final String module = EntitySyncServices.class.getName(); /** * Run an Entity Sync (checks to see if other already running, etc) *@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 runEntitySync(DispatchContext dctx, Map context) { EntitySyncContext esc = null; try { esc = new EntitySyncContext(dctx, context); if ("Y".equals(esc.entitySync.get("forPullOnly"))) { return ServiceUtil.returnError("Cannot do Entity Sync Push because entitySyncId [] is set for Pull Only."); } esc.runPushStartRunning(); // 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()) { // TODO make sure the following message is commented out before commit: // Debug.logInfo("Doing runEntitySync split, currentRunStartTime=" + esc.currentRunStartTime + ", currentRunEndTime=" + esc.currentRunEndTime, module); esc.totalSplits++; // tx times are indexed // keep track of how long these sync runs take and store that info on the history table // saves info about removed, all entities that don't have no-auto-stamp set, this will be done in the GenericDAO like the stamp sets // ===== INSERTS ===== ArrayList valuesToCreate = esc.assembleValuesToCreate(); // ===== UPDATES ===== ArrayList valuesToStore = esc.assembleValuesToStore(); // ===== DELETES ===== List keysToRemove = esc.assembleKeysToRemove(); esc.runPushSendData(valuesToCreate, valuesToStore, keysToRemove); esc.saveResultsReportedFromDataStore(); esc.advanceRunTimes(); } esc.saveFinalSyncResults(); } catch (SyncAbortException e) { return e.returnError(module); } catch (SyncErrorException e) { e.saveSyncErrorInfo(esc); return e.returnError(module); } return ServiceUtil.returnSuccess(); } /** * 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); } } //LocalDispatcher dispatcher = dctx.getDispatcher(); String entitySyncId = (String) context.get("entitySyncId"); // incoming lists will already be sorted by lastUpdatedStamp (or lastCreatedStamp) List valuesToCreate = (List) context.get("valuesToCreate"); List valuesToStore = (List) context.get("valuesToStore"); List keysToRemove = (List) context.get("keysToRemove"); if (Debug.infoOn()) Debug.logInfo("Running storeEntitySyncData (" + entitySyncId + ") - [" + valuesToCreate.size() + "] to create; [" + valuesToStore.size() + "] to store; [" + keysToRemove.size() + "] to remove.", module); try { long toCreateInserted = 0; long toCreateUpdated = 0; long toCreateNotUpdated = 0; long toStoreInserted = 0; long toStoreUpdated = 0; long toStoreNotUpdated = 0; long toRemoveDeleted = 0; long toRemoveAlreadyDeleted = 0; // create all values in the valuesToCreate List; if the value already exists update it, or if exists and was updated more recently than this one dont update it Iterator valueToCreateIter = valuesToCreate.iterator(); while (valueToCreateIter.hasNext()) { GenericValue valueToCreate = (GenericValue) valueToCreateIter.next(); // to Create check if exists (find by pk), if not insert; if exists check lastUpdatedStamp: if null or before the candidate value insert, otherwise don't insert // NOTE: use the delegator from this DispatchContext rather than the one named in the GenericValue // maintain the original timestamps when doing storage of synced data, by default with will update the timestamps to now valueToCreate.setIsFromEntitySync(true); // check to make sure all foreign keys are created; if not create dummy values as place holders valueToCreate.checkFks(true); GenericValue existingValue = delegator.findByPrimaryKey(valueToCreate.getPrimaryKey()); if (existingValue == null) { delegator.create(valueToCreate); toCreateInserted++; } else { // if the existing value has a stamp field that is AFTER the stamp on the valueToCreate, don't update it if (existingValue.get(ModelEntity.STAMP_FIELD) != null && existingValue.getTimestamp(ModelEntity.STAMP_FIELD).after(valueToCreate.getTimestamp(ModelEntity.STAMP_FIELD))) { toCreateNotUpdated++; } else { delegator.store(valueToCreate); toCreateUpdated++; } } } // iterate through to store list and store each Iterator valueToStoreIter = valuesToStore.iterator(); while (valueToStoreIter.hasNext()) { GenericValue valueToStore = (GenericValue) valueToStoreIter.next(); // to store check if exists (find by pk), if not insert; if exists check lastUpdatedStamp: if null or before the candidate value insert, otherwise don't insert // maintain the original timestamps when doing storage of synced data, by default with will update the timestamps to now valueToStore.setIsFromEntitySync(true); // check to make sure all foreign keys are created; if not create dummy values as place holders valueToStore.checkFks(true); GenericValue existingValue = delegator.findByPrimaryKey(valueToStore.getPrimaryKey()); if (existingValue == null) { delegator.create(valueToStore); toStoreInserted++; } else { // if the existing value has a stamp field that is AFTER the stamp on the valueToStore, don't update it if (existingValue.get(ModelEntity.STAMP_FIELD) != null && existingValue.getTimestamp(ModelEntity.STAMP_FIELD).after(valueToStore.getTimestamp(ModelEntity.STAMP_FIELD))) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -