Search in sources :

Example 1 with SyncAbortException

use of org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException in project ofbiz-framework by apache.

the class EntitySyncServices method pullAndReportEntitySyncData.

/**
 * Pull and Report Entity Sync Data - Called Remotely to Push Results from last pull, the Pull next set of results.
 *@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<String, Object> pullAndReportEntitySyncData(DispatchContext dctx, Map<String, ? extends Object> context) {
    EntitySyncContext esc = null;
    Locale locale = (Locale) context.get("locale");
    try {
        esc = new EntitySyncContext(dctx, context);
        Debug.logInfo("Doing pullAndReportEntitySyncData for entitySyncId=" + esc.entitySyncId + ", currentRunStartTime=" + esc.currentRunStartTime + ", currentRunEndTime=" + esc.currentRunEndTime, module);
        if ("Y".equals(esc.entitySync.get("forPushOnly"))) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtCannotDoEntitySyncPush", locale));
        }
        // Part 1: if any results are passed, store the results for the given startDate, update EntitySync, etc
        // restore info from last pull, or if no results start new run
        esc.runPullStartOrRestoreSavedResults();
        // increment starting time to run until now
        while (esc.hasMoreTimeToSync()) {
            // make sure the following message is commented out before commit:
            // Debug.logInfo("(loop)Doing pullAndReportEntitySyncData 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
            // Part 2: get the next set of data for the given entitySyncId
            // Part 2a: return it back for storage but leave the EntitySyncHistory without results, and don't update the EntitySync last time
            // ===== INSERTS =====
            ArrayList<GenericValue> valuesToCreate = esc.assembleValuesToCreate();
            // ===== UPDATES =====
            ArrayList<GenericValue> valuesToStore = esc.assembleValuesToStore();
            // ===== DELETES =====
            List<GenericEntity> keysToRemove = esc.assembleKeysToRemove();
            esc.setTotalRowCounts(valuesToCreate, valuesToStore, keysToRemove);
            if (Debug.infoOn())
                Debug.logInfo("Service pullAndReportEntitySyncData returning - [" + valuesToCreate.size() + "] to create; [" + valuesToStore.size() + "] to store; [" + keysToRemove.size() + "] to remove; [" + esc.totalRowsPerSplit + "] total rows per split.", module);
            if (esc.totalRowsPerSplit > 0) {
                // stop if we found some data, otherwise look and try again
                Map<String, Object> result = ServiceUtil.returnSuccess();
                result.put("startDate", esc.startDate);
                result.put("valuesToCreate", valuesToCreate);
                result.put("valuesToStore", valuesToStore);
                result.put("keysToRemove", keysToRemove);
                return result;
            } else {
                // 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();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntity(org.apache.ofbiz.entity.GenericEntity) SyncAbortException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException) SyncErrorException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException)

Example 2 with SyncAbortException

use of org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException in project ofbiz-framework by apache.

the class EntitySyncServices method runEntitySync.

/**
 * 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<String, Object> runEntitySync(DispatchContext dctx, Map<String, ? extends Object> context) {
    Locale locale = (Locale) context.get("locale");
    EntitySyncContext esc = null;
    try {
        esc = new EntitySyncContext(dctx, context);
        if ("Y".equals(esc.entitySync.get("forPullOnly"))) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityExtCannotDoEntitySyncPush", locale));
        }
        esc.runPushStartRunning();
        // increment starting time to run until now
        // just run this the first time, will be updated between each loop automatically
        esc.setSplitStartTime();
        while (esc.hasMoreTimeToSync()) {
            // this will result in lots of log messages, so leaving commented out unless needed/wanted later
            // 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<GenericValue> valuesToCreate = esc.assembleValuesToCreate();
            // ===== UPDATES =====
            ArrayList<GenericValue> valuesToStore = esc.assembleValuesToStore();
            // ===== DELETES =====
            List<GenericEntity> 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();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) GenericEntity(org.apache.ofbiz.entity.GenericEntity) SyncAbortException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException) SyncErrorException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException)

Example 3 with SyncAbortException

use of org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException in project ofbiz-framework by apache.

the class EntitySyncServices method runOfflineEntitySync.

public static Map<String, Object> runOfflineEntitySync(DispatchContext dctx, Map<String, ? extends Object> 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
        // just run this the first time, will be updated between each loop automatically
        esc.setSplitStartTime();
        while (esc.hasMoreTimeToSync()) {
            esc.totalSplits++;
            ArrayList<GenericValue> valuesToCreate = esc.assembleValuesToCreate();
            ArrayList<GenericValue> valuesToStore = esc.assembleValuesToStore();
            List<GenericEntity> 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();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) Element(org.w3c.dom.Element) SyncAbortException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException) IOException(java.io.IOException) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) GenericEntity(org.apache.ofbiz.entity.GenericEntity) SyncErrorException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

GenericEntity (org.apache.ofbiz.entity.GenericEntity)3 GenericValue (org.apache.ofbiz.entity.GenericValue)3 SyncAbortException (org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException)3 SyncErrorException (org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException)3 Locale (java.util.Locale)2 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 SerializeException (org.apache.ofbiz.entity.serialize.SerializeException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1