Class BatchContext<T extends ExchangeObject>
java.lang.Object
com.broadleafcommerce.dataexchange.service.BatchContext<T>
- Type Parameters:
 T-
Container for holding important data that is relevant to the overall processing of a batch of
 
ExchangeObjects.
 This is thread safe and is expected to share state between multiple threads for a given batch.- Author:
 - Kelly Tisdell (ktisdell)
 
- 
Constructor Summary
ConstructorsConstructorDescriptionBatchContext(com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Constructor, which takes in an optionalContextInfo. - 
Method Summary
Modifier and TypeMethodDescription<V> voidaddAttribute(String key, V value) voidaddSavedEntity(ExchangeObject exchangeObject) Adds a saved entity to thesavedEntitiesMap.voidaddSavedTopLevelEntity(T exchangeObject) Adds a saved entity to thesavedEntitiesMapandsavedTopLevelEntities.<V extends ExchangeObject>
List<V>findAllOriginalChildrenOfTypeForCorrelationId(String correlationId, Class<V> type) findOriginalChildrenForParent(ExchangeObject exchangeObject) findOriginalChildrenForParent(String correlationId) <V extends ExchangeObject>
VfindOriginalEntityExchangeObject(String correlationId) Utility method to iterate over thegetFlattenedHierarchy()Mapand find theExchangeObjectthat has the provided correlationId.<V extends ExchangeObject>
VfindOriginalEntityExchangeObject(String correlationId, Class<V> type) Utility method to iterate over thegetFlattenedHierarchy()Mapand find theExchangeObjectthat has the provided correlationId and whose type matches the providedClasstype.<V extends ExchangeObject>
List<V>findTopLevelEntitiesOfType(Class<V> type) Arbitrary additional state that can be stored for batch processing or to be shared between threads.<V> VgetAttribute(String key) <V> VgetAttribute(String key, V defaultValue) Returns the value in theadditionalAttributesMap, or the default value if no entry is available for the provided key.longTime in millis that thisBatchContextwas created and therefore when the processing of the batch started.The children of entities, keyed by their parent correlationId.com.broadleafcommerce.data.tracking.core.context.ContextInfogetContextInfo(com.broadleafcommerce.data.tracking.core.type.OperationType operationType) Convenience method to allow us to return a cached ContextInfo for this batch.com.broadleafcommerce.data.tracking.core.context.ContextInfoMap<com.broadleafcommerce.data.tracking.core.type.OperationType,com.broadleafcommerce.data.tracking.core.context.ContextInfo> Map ofContextInfoinstances based on the initial value passed to the constructor.com.broadleafcommerce.data.tracking.core.context.ContextInfolongProvides the time in millis since this object was created, which is usually when a batch stars.getExceptions(ExchangeObject exchangeObject) Represents aMapof the entire object graph provided by a caller, flattened so that theExchangeObject.getCorrelationId()is the key.com.broadleafcommerce.data.tracking.core.context.ContextInfoTheContextInfoprovided when the request was made to this service.getParentSavedEntity(ExchangeObject exchangeObject) Returns the saved parent ExchangeObject of the provided ExchangeObject.com.broadleafcommerce.data.tracking.core.context.ContextInfoError details to return to the caller.The saved entities, keyed by their correlationId.<R extends ExchangeObject>
RgetSavedEntity(ExchangeObject exchangeObject) Returns a saved ExchangeObject based on the provided correlationId.<R extends ExchangeObject>
RgetSavedEntity(String correlationId) Returns a saved ExchangeObject based on the provided correlationId.getSavedEntityUnsafe(String correlationId) Returns a saved ExchangeObject based on the provided correlationId.Top level entities that are saved, to be returned.Returns an immutableMap, keyed byExchangeObject.getCorrelationId()and containing a list of exceptions that may have been encountered.com.broadleafcommerce.data.tracking.core.context.ContextInfobooleanhasException(ExchangeObject exchangeObject) Indicates if the providedExchangeObjecthas a registered exception with this batch.booleanhasException(String key) Indicates if anything associated with this key has an exception associated with it for this batch.voidregisterException(ExchangeObject exchangeObject, Throwable ex) Adds anExceptionreference to theExchangeObject.voidregisterException(String key, Throwable ex) Method to allow us to track more than one exception per entity that we are tracking.protected voidvalidateCorrelationId(ExchangeObject exchangeObject)  
- 
Constructor Details
- 
BatchContext
public BatchContext(@Nullable com.broadleafcommerce.data.tracking.core.context.ContextInfo contextInfo) Constructor, which takes in an optionalContextInfo.- Parameters:
 contextInfo-
 
 - 
 - 
Method Details
- 
getContextInfo
@Nullable public com.broadleafcommerce.data.tracking.core.context.ContextInfo getContextInfo(@NonNull com.broadleafcommerce.data.tracking.core.type.OperationType operationType) Convenience method to allow us to return a cached ContextInfo for this batch. This is helpful when we need to do more than one operation (e.g. read, create. update, delete) and need multiple contexts. ReturnsnullifgetInitialContextInfo()is null.- Parameters:
 operationType-OperationTypefor which to provide aContextInfo- Returns:
 - a 
ContextInfowith the requestedOperationType - See Also:
 
 - 
getReadContext
@Nullable public com.broadleafcommerce.data.tracking.core.context.ContextInfo getReadContext() - 
getCreateContext
@Nullable public com.broadleafcommerce.data.tracking.core.context.ContextInfo getCreateContext() - 
getUpdateContext
@Nullable public com.broadleafcommerce.data.tracking.core.context.ContextInfo getUpdateContext() - 
getDeleteContext
@Nullable public com.broadleafcommerce.data.tracking.core.context.ContextInfo getDeleteContext() - 
getElapsedTimeMillis
public long getElapsedTimeMillis()Provides the time in millis since this object was created, which is usually when a batch stars.- Returns:
 - returns the time, in millis, since this 
BatchContextwas created 
 - 
registerException
Method to allow us to track more than one exception per entity that we are tracking. The key is theExchangeObject.getCorrelationId().- Parameters:
 key- the correlationId of the entityex- an exception that was encountered saving or processing the entity
 - 
registerException
Adds anExceptionreference to theExchangeObject. These can be aggregated at the end of a process.- Parameters:
 exchangeObject- anyExchangeObjectin the object graph that may have an exception that needs to be tracked and reportedex- an exception that was caught processing the givenExchangeObject
 - 
getThrowables
Returns an immutableMap, keyed byExchangeObject.getCorrelationId()and containing a list of exceptions that may have been encountered. This should only be used during the aggregation phase.- Returns:
 - immutable 
Map, keyed byExchangeObject.getCorrelationId()containing one or more Exceptions - See Also:
 
 - 
hasException
Indicates if anything associated with this key has an exception associated with it for this batch.- Parameters:
 key- any arbitrary string, but typicallyExchangeObject.getCorrelationId()- Returns:
 - true if the provided key has an associated exception
 
 - 
hasException
Indicates if the providedExchangeObjecthas a registered exception with this batch.- Parameters:
 exchangeObject- anyExchangeObjectin the object graph that may have an exception that needs to be tracked and reported- Returns:
 - true if the provided object has an associated exception
 
 - 
getExceptions
 - 
addAttribute
 - 
getAttribute
 - 
getAttribute
Returns the value in theadditionalAttributesMap, or the default value if no entry is available for the provided key. This is essentially the same is asMap.getOrDefault(Object, Object).- Type Parameters:
 V-- Parameters:
 key-defaultValue-- Returns:
 
 - 
findOriginalEntityExchangeObject
@Nullable public <V extends ExchangeObject> V findOriginalEntityExchangeObject(String correlationId, Class<V> type) Utility method to iterate over thegetFlattenedHierarchy()Mapand find theExchangeObjectthat has the provided correlationId and whose type matches the providedClasstype.- Parameters:
 correlationId-type-- Returns:
 
 - 
findOriginalEntityExchangeObject
@Nullable public <V extends ExchangeObject> V findOriginalEntityExchangeObject(String correlationId) Utility method to iterate over thegetFlattenedHierarchy()Mapand find theExchangeObjectthat has the provided correlationId.- Parameters:
 correlationId-- Returns:
 
 - 
findAllOriginalChildrenOfTypeForCorrelationId
public <V extends ExchangeObject> List<V> findAllOriginalChildrenOfTypeForCorrelationId(String correlationId, Class<V> type)  - 
findOriginalChildrenForParent
 - 
findOriginalChildrenForParent
 - 
findTopLevelEntitiesOfType
 - 
addSavedTopLevelEntity
Adds a saved entity to thesavedEntitiesMapandsavedTopLevelEntities. This should be called after the primaryExchangeObject(e.g. a product) has been saved. It is not necessary to calladdSavedEntity(ExchangeObject)if this method is used.- Parameters:
 exchangeObject-
 - 
addSavedEntity
Adds a saved entity to thesavedEntitiesMap. If this is the primaryExchangeObjectthat is being saved, this method should not be used. Instead, useaddSavedTopLevelEntity(ExchangeObject).- Parameters:
 exchangeObject- theExchangeObjectto add to the saved entities map
 - 
getSavedEntity
Returns a saved ExchangeObject based on the provided correlationId. This method will throw aCorrelationIdExceptionif no entity is found with the provided correlationId, as it's generally expected that this method will only be called for entities that have been saved. If validation isn't desired, usegetSavedEntityUnsafe(String)instead.- Parameters:
 correlationId- the correlationId of the saved entity- Returns:
 - the saved entity
 
 - 
getSavedEntity
@Nullable public <R extends ExchangeObject> R getSavedEntity(@Nullable ExchangeObject exchangeObject) Returns a saved ExchangeObject based on the provided correlationId. This method will throw aCorrelationIdExceptionif no entity is found with the provided correlationId, as it's generally expected that this method will only be called for entities that have been saved. If validation isn't desired, usegetSavedEntityUnsafe(String)instead.- Parameters:
 exchangeObject- the ExchangeObject to get the saved entity- Returns:
 - the saved entity
 
 - 
getParentSavedEntity
Returns the saved parent ExchangeObject of the provided ExchangeObject.- Parameters:
 exchangeObject- the ExchangeObject to get the parent of- Returns:
 - the saved parent
 
 - 
getSavedEntityUnsafe
Returns a saved ExchangeObject based on the provided correlationId. This method does not validate the entity exists, which is generally desired. Most use cases should usegetSavedEntity(String)instead.- Parameters:
 correlationId- the correlationId of the saved entity- Returns:
 - the saved entity
 
 - 
validateCorrelationId
 - 
getBatchStartTimeMillis
public long getBatchStartTimeMillis()Time in millis that thisBatchContextwas created and therefore when the processing of the batch started. This can be used to determine elapsed time. - 
getSavedTopLevelEntities
Top level entities that are saved, to be returned. May also contain the original requested entity (not saved) if that entity has an error. - 
getResponseErrors
Error details to return to the caller. These are usually derived fromhasException(ExchangeObject)after processing of all entities and immediately prior to returning the response to the client. - 
getSavedEntitiesMap
The saved entities, keyed by their correlationId. - 
getFlattenedHierarchy
Represents aMapof the entire object graph provided by a caller, flattened so that theExchangeObject.getCorrelationId()is the key. - 
getChildrenMap
The children of entities, keyed by their parent correlationId. - 
getAdditionalAttributes
Arbitrary additional state that can be stored for batch processing or to be shared between threads. - 
getInitialContextInfo
public com.broadleafcommerce.data.tracking.core.context.ContextInfo getInitialContextInfo()TheContextInfoprovided when the request was made to this service. - 
getCtxInfoMap
public Map<com.broadleafcommerce.data.tracking.core.type.OperationType,com.broadleafcommerce.data.tracking.core.context.ContextInfo> getCtxInfoMap()Map ofContextInfoinstances based on the initial value passed to the constructor. This is keyed byOperationType. 
 -