atg.commerce.order.edit
Class CollectionEditHandler

java.lang.Object
  extended by atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
      extended by atg.nucleus.GenericService
          extended by atg.commerce.order.edit.CloneEditHandler
              extended by atg.commerce.order.edit.CollectionEditHandler
All Implemented Interfaces:
atg.naming.NameContextBindingListener, atg.naming.NameContextElement, atg.naming.NameResolver, atg.nucleus.AdminableService, atg.nucleus.logging.ApplicationLogging, atg.nucleus.logging.ApplicationLoggingSender, atg.nucleus.logging.TraceApplicationLogging, atg.nucleus.logging.VariableArgumentApplicationLogging, atg.nucleus.naming.ComponentNameResolver, atg.nucleus.Service, atg.nucleus.ServiceListener, java.util.EventListener
Direct Known Subclasses:
AgentCommentEditHandler, ListEditHandler, MarkerEditHandler

public class CollectionEditHandler
extends CloneEditHandler

This handler implements a pattern to support automatic reconciling of objects in a collection.

The CloneEditHandler interface is implemented as follows:

initializeCloneEditState
This step is executed after the order has been clone at the repository level to initialized the CloneEditState object. This handler maps all the objects between the two collections by id, or some other configured property keyPropertyName, and saves the maps in the CloneEditState. There are two maps created by this step. One maps the clone to original objects and the other maps original to clone objects. The maps are stored in the CloneEditState/code>. These pre-edit snapshots of items in the collection provide the necessary information to identify changes to the collection once the edit is complete.
applyChanges
This step executes once the edit process is complete and the changes in the clone order must be reconciled with the original order. This handler, using the maps created by initializeCloneEditState, reconciles the objects between the clone and original collections. Each add, update and delete is identified and automatically applied. An object representing each add AddItem, update UpdateItem and delete DeleteItem is created and stored in the CloneEditState.

An UpdateItem object is created for each object that exists in the clone collection and has a matching counterpart in the original collection. Objects that fall into this category are reconciled by copying properties from the clone to original objects. Properties are configurable using propertiesToCopyOnUpdate.

An AddItem object is created for each object that exists in the clone collection but doesn't have a matching counterpart in the original collection. Objects that fall into this category are added to the original collection at the same index they are found in the clone collection.

A DeleteItem object is created for each object that was mapped in the original collection, but its counterpart no longer exists in the clone collection. Objects that fall into this category are removed from the original collection.

Configurable properties:

collectionPropertyName
the property name on the order that contains the collection of items
keyPropertyName
the property used to map each object in the collection from the clone to the original order. If this is null, the object itself is used.
sortPropertyName
the property used to sort the collection before mapping objects from the clone to the original order. If the collection is ordered then it isn't necessary to provide this configuration.
propertiesToCopyOnUpdate
These property values are copied from the clone object to the original object when updated.


Field Summary
static java.lang.String CLASS_VERSION
           
protected  java.lang.String mCollectionPropertyName
           
protected  java.lang.String mSortPropertyName
           
protected  boolean mThrowExceptionsForCollectionChanges
           
protected static java.lang.String STATE_CLONETOORIGINAL_MAP
          the key used for storing the clone to original object map in the clone order state
protected static java.lang.String STATE_ORIGINALTOCLONE_MAP
          the key used for storing the original to clone object map in the clone order state
 
Fields inherited from class atg.commerce.order.edit.CloneEditHandler
mFulfillmentMessageSender, mKeyPropertyName, mOrderFulfillmentTools, mPropertiesToCopyOnUpdate, STATE_ADD_ITEMS, STATE_DELETE_ITEMS, STATE_UPDATE_ITEMS
 
Fields inherited from class atg.nucleus.GenericService
SERVICE_INFO_KEY
 
Fields inherited from interface atg.nucleus.logging.TraceApplicationLogging
DEFAULT_LOG_TRACE_STATUS
 
Fields inherited from interface atg.nucleus.logging.ApplicationLogging
DEFAULT_LOG_DEBUG_STATUS, DEFAULT_LOG_ERROR_STATUS, DEFAULT_LOG_INFO_STATUS, DEFAULT_LOG_WARNING_STATUS
 
Constructor Summary
CollectionEditHandler()
           
 
Method Summary
protected  void addItem(CloneEditState pCloneEditState, AddItem pAddItem)
          Reconciles the AddItem by adding the object it to the original container's collection property.
 void applyChanges(CloneEditState pCloneEditState)
          This method is called during the reconciliation process to reconcile the data between the original and clone object.
protected  void deleteItem(CloneEditState pCloneEditState, DeleteItem pDeleteItem)
          Reconciles the DeleteItem by deleting the original object from the original container.
 void detectChanges(CloneEditState pCloneEditState)
          Detects updates, adds and deletes in the collection.
protected  void detectChangesToCollection(CloneEditState pCloneEditState, java.lang.Object pOriginalCollectionContainer, java.lang.Object pCloneCollectionContainer)
          Detects changes to the collections using the maps created when the CloneEditState was initialized.
protected  void detectDeletes(CloneEditState pCloneEditState, java.lang.Object pOriginalContainer, java.lang.Object pCloneContainer)
          A DeleteItem object is created for each object in the original container that was mapped to a counterpart that no longer exists in the clone container.
protected  java.lang.Object findCounterpartObject(CloneEditState pCloneEditState, java.lang.Object pObject, java.util.Collection pCounterpartCollection, java.util.Map pCounterpartMap)
          Finds the counter part object for the given object.
protected  java.util.Map getCloneToOriginalMap(CloneEditState pCloneEditState)
          Returns the clone to original map.
protected  java.util.Collection getCollection(java.lang.Object pCollectionContainer)
          gets the collection in property defined by CollectionPropertyName
 java.lang.String getCollectionPropertyName()
          This property on the order that contains the collection of objects
protected  java.lang.Object getMatchingCloneKey(CloneEditState pCloneEditState, java.lang.Object pOriginalKey)
          Returns the matching clone key for the given original key.
protected  java.lang.Object getMatchingOriginalKey(CloneEditState pCloneEditState, java.lang.Object pCloneKey)
          Returns the matching original key for the given clone key.
protected  java.util.Map getOriginalToCloneMap(CloneEditState pCloneEditState)
          Returns the original to clone map.
 java.lang.String getSortPropertyName()
          The value of this property is used to sort the collection before mapping the original to clone object in the collection
 void initializeCloneEditState(CloneEditState pCloneEditState)
          This maps the objects in the original collection to the object in the clone collection and stores the maps in the clone edit state in the CloneEditState
 boolean isThrowExceptionsForCollectionChanges()
          determines if exceptions are thrown when the reconciliation process detects that the original collection has changed after it was cloned.
protected  void mapOriginalToClone(CloneEditState pCloneEditState, atg.commerce.order.Order pOriginalOrder, atg.commerce.order.Order pCloneOrder)
          Maps the objects in the original collection to objects in the clone collection.
protected  java.util.Map mapOriginalToCloneCollection(CloneEditState pCloneEditState, java.util.Collection pOriginalCollection, java.util.Collection pCloneCollection)
          Maps objects in the original collection to the clone collection and saves the map in the clone edit state
protected  void saveOriginalToCloneObjectMappings(CloneEditState pCloneEditState, java.util.Map pOriginalToCloneObjectMap)
          Saves the map of originalToClone objects in the clone edit state
 void setCollectionPropertyName(java.lang.String pCollectionPropertyName)
           
 void setSortPropertyName(java.lang.String pSortPropertyName)
           
 void setThrowExceptionsForCollectionChanges(boolean pThrowExceptionsForCollectionChanges)
           
protected  java.util.Collection sortCollection(java.util.Collection pCollection)
          This method is called for both the original and clone collections before attempting to map the objects in the collection.
 void validateCloneOrder(atg.commerce.order.Order pOriginalOrder, atg.commerce.order.Order pCloneOrder)
          Validates the collections after the cloning process
protected  void validateCollection(atg.commerce.order.Order pOriginalOrder, atg.commerce.order.Order pCloneOrder)
          Validates the collection
protected  void validateCollectionSize(java.lang.Object pOriginalCollectionContainer, java.lang.Object pCloneCollectionContainer)
          Ensures that the collections in the property defined by collectionPropertyName are the same size.
 
Methods inherited from class atg.commerce.order.edit.CloneEditHandler
addAddItem, addDeleteItem, addStateData, addUpdateItem, cloneOrder, copyPricingModels, createAddItem, createDeleteItem, createUpdateItem, generateModifications, getAddItems, getCloneEditManager, getDeleteItems, getFulfillmentMessageSender, getKeyForObject, getKeyPropertyName, getObjectType, getOrderFulfillmentTools, getPropertiesToCopy, getPropertiesToCopyOnUpdate, getStateData, getStateMap, getUpdateItems, initializeReconciliationProcess, isAddedItem, parseConfigString, processAddItems, processDeleteItems, processUpdateItems, reconcileObjectProperties, sendFulfillmentNotifications, sendMessage, setCloneEditManager, setFulfillmentMessageSender, setKeyPropertyName, setOrderFulfillmentTools, setPropertiesToCopyOnUpdate, updateItem
 
Methods inherited from class atg.nucleus.GenericService
addLogListener, createAdminServlet, doStartService, doStopService, getAbsoluteName, getAdminServlet, getLoggingForVlogging, getLogListenerCount, getLogListeners, getName, getNameContext, getNucleus, getRoot, getServiceConfiguration, getServiceInfo, isLoggingDebug, isLoggingError, isLoggingInfo, isLoggingTrace, isLoggingWarning, isRunning, logDebug, logDebug, logDebug, logError, logError, logError, logInfo, logInfo, logInfo, logTrace, logTrace, logTrace, logWarning, logWarning, logWarning, nameContextElementBound, nameContextElementUnbound, removeLogListener, resolveName, resolveName, resolveName, resolveName, sendLogEvent, setLoggingDebug, setLoggingError, setLoggingInfo, setLoggingTrace, setLoggingWarning, setNucleus, setServiceInfo, startService, stopService
 
Methods inherited from class atg.nucleus.logging.VariableArgumentApplicationLoggingImpl
vlogDebug, vlogDebug, vlogDebug, vlogDebug, vlogError, vlogError, vlogError, vlogError, vlogInfo, vlogInfo, vlogInfo, vlogInfo, vlogTrace, vlogTrace, vlogTrace, vlogTrace, vlogWarning, vlogWarning, vlogWarning, vlogWarning
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_VERSION

public static final java.lang.String CLASS_VERSION
See Also:
Constant Field Values

STATE_CLONETOORIGINAL_MAP

protected static final java.lang.String STATE_CLONETOORIGINAL_MAP
the key used for storing the clone to original object map in the clone order state

See Also:
Constant Field Values

STATE_ORIGINALTOCLONE_MAP

protected static final java.lang.String STATE_ORIGINALTOCLONE_MAP
the key used for storing the original to clone object map in the clone order state

See Also:
Constant Field Values

mThrowExceptionsForCollectionChanges

protected boolean mThrowExceptionsForCollectionChanges

mSortPropertyName

protected java.lang.String mSortPropertyName

mCollectionPropertyName

protected java.lang.String mCollectionPropertyName
Constructor Detail

CollectionEditHandler

public CollectionEditHandler()
Method Detail

isThrowExceptionsForCollectionChanges

public boolean isThrowExceptionsForCollectionChanges()
determines if exceptions are thrown when the reconciliation process detects that the original collection has changed after it was cloned.

Returns:
true by default

setThrowExceptionsForCollectionChanges

public void setThrowExceptionsForCollectionChanges(boolean pThrowExceptionsForCollectionChanges)

getSortPropertyName

public java.lang.String getSortPropertyName()
The value of this property is used to sort the collection before mapping the original to clone object in the collection

Returns:
sort property name

setSortPropertyName

public void setSortPropertyName(java.lang.String pSortPropertyName)

getCollectionPropertyName

public java.lang.String getCollectionPropertyName()
This property on the order that contains the collection of objects

Returns:

setCollectionPropertyName

public void setCollectionPropertyName(java.lang.String pCollectionPropertyName)

mapOriginalToClone

protected void mapOriginalToClone(CloneEditState pCloneEditState,
                                  atg.commerce.order.Order pOriginalOrder,
                                  atg.commerce.order.Order pCloneOrder)
                           throws atg.commerce.CommerceException
Maps the objects in the original collection to objects in the clone collection. The collections are obtained from the Order objects using the property defined by collectionPropertyName

Parameters:
pCloneEditState -
Throws:
atg.commerce.CommerceException
See Also:
mapOriginalToCloneCollection(CloneEditState, Collection, Collection)

findCounterpartObject

protected java.lang.Object findCounterpartObject(CloneEditState pCloneEditState,
                                                 java.lang.Object pObject,
                                                 java.util.Collection pCounterpartCollection,
                                                 java.util.Map pCounterpartMap)
Finds the counter part object for the given object.

Parameters:
pCloneEditState -
pObject - the object for which the counterpart is to be found
pCounterpartCollection - the collection of counter parts to search for the counterpart
pCounterpartMap - the counter part map for pObject
Returns:
the counterpart object

sortCollection

protected java.util.Collection sortCollection(java.util.Collection pCollection)
This method is called for both the original and clone collections before attempting to map the objects in the collection. If the collection order is a not guaranteed, as with a List, this method should produce a sorted colletion so the objects can be successfully mapped.

The default behavior is to return the given collection sort is ascending order using the property defined by sortPropertyName. If sortPropertyName is not valued, pCollection is returned unchanged.

Parameters:
pCollection -
Returns:
a sorted collection.

mapOriginalToCloneCollection

protected java.util.Map mapOriginalToCloneCollection(CloneEditState pCloneEditState,
                                                     java.util.Collection pOriginalCollection,
                                                     java.util.Collection pCloneCollection)
Maps objects in the original collection to the clone collection and saves the map in the clone edit state

Parameters:
pCloneEditState -
pOriginalCollection -
pCloneCollection -
Returns:
Map of original to clone objects

initializeCloneEditState

public void initializeCloneEditState(CloneEditState pCloneEditState)
                              throws atg.commerce.CommerceException
This maps the objects in the original collection to the object in the clone collection and stores the maps in the clone edit state in the CloneEditState

Specified by:
initializeCloneEditState in class CloneEditHandler
Throws:
atg.commerce.CommerceException

getMatchingCloneKey

protected java.lang.Object getMatchingCloneKey(CloneEditState pCloneEditState,
                                               java.lang.Object pOriginalKey)
Returns the matching clone key for the given original key.

Parameters:
pCloneEditState -
pOriginalKey -
Returns:
clone key. null if not found.

getMatchingOriginalKey

protected java.lang.Object getMatchingOriginalKey(CloneEditState pCloneEditState,
                                                  java.lang.Object pCloneKey)
Returns the matching original key for the given clone key.

Parameters:
pCloneEditState -
pCloneKey -
Returns:
original key. null if not found.

applyChanges

public void applyChanges(CloneEditState pCloneEditState)
                  throws atg.commerce.CommerceException
This method is called during the reconciliation process to reconcile the data between the original and clone object.

First, all DeleteItem, UpdateItem and AddItem objects are created by calling detectChanges.

After the reconciliation items are created, they are processed by calling processDeleteItems, processAddItems and processUpdateItems.

Specified by:
applyChanges in class CloneEditHandler
Parameters:
pCloneEditState -
Throws:
atg.commerce.CommerceException
See Also:
detectChanges(CloneEditState), CloneEditHandler.processDeleteItems(CloneEditState), CloneEditHandler.processAddItems(CloneEditState), CloneEditHandler.processUpdateItems(CloneEditState)

detectChanges

public void detectChanges(CloneEditState pCloneEditState)
                   throws atg.commerce.CommerceException
Detects updates, adds and deletes in the collection.

Throws:
atg.commerce.CommerceException
See Also:
detectChangesToCollection(CloneEditState, Object, Object)

getCollection

protected java.util.Collection getCollection(java.lang.Object pCollectionContainer)
                                      throws atg.beans.PropertyNotFoundException
gets the collection in property defined by CollectionPropertyName

Parameters:
CollectionContainer -
Returns:
Collection
Throws:
atg.beans.PropertyNotFoundException

detectDeletes

protected void detectDeletes(CloneEditState pCloneEditState,
                             java.lang.Object pOriginalContainer,
                             java.lang.Object pCloneContainer)
                      throws atg.commerce.CommerceException
A DeleteItem object is created for each object in the original container that was mapped to a counterpart that no longer exists in the clone container.

Parameters:
pCloneEditState -
pOriginalContainer -
pCloneContainer -
Throws:
atg.commerce.CommerceException

saveOriginalToCloneObjectMappings

protected void saveOriginalToCloneObjectMappings(CloneEditState pCloneEditState,
                                                 java.util.Map pOriginalToCloneObjectMap)
Saves the map of originalToClone objects in the clone edit state

Parameters:
pCloneEditState -
pOriginalToCloneObjectMap -

detectChangesToCollection

protected void detectChangesToCollection(CloneEditState pCloneEditState,
                                         java.lang.Object pOriginalCollectionContainer,
                                         java.lang.Object pCloneCollectionContainer)
                                  throws atg.commerce.CommerceException
Detects changes to the collections using the maps created when the CloneEditState was initialized.

The collection are obtained from the original and clone containers using the configured collections property name defined by collectionPropertyName.

This is where the UpdateItem, AddItem and DeleteItem objects are created for objects processed.

An UpdateItem object is created for each object that exists in the clone collection container and has a matching counterpart in the original collection container. The cloneToOrigMap for this handler is used to determine the objects that fall in this category.

Parameters:
pCloneEditState - the CloneEditState that was created at the beginning of the clone process.
pOriginalContainer - the object that has the original collection in the property defined by collectionPropertyName
pCloneContainer - the object that has the clone collection in the property defined by collectionPropertyName
Throws:
atg.commerce.CommerceException

deleteItem

protected void deleteItem(CloneEditState pCloneEditState,
                          DeleteItem pDeleteItem)
                   throws atg.commerce.CommerceException
Reconciles the DeleteItem by deleting the original object from the original container.

Overrides:
deleteItem in class CloneEditHandler
Parameters:
pCloneEditState -
pDeleteItem - the DeleteItem object
Throws:
atg.commerce.CommerceException

addItem

protected void addItem(CloneEditState pCloneEditState,
                       AddItem pAddItem)
                throws atg.commerce.CommerceException
Reconciles the AddItem by adding the object it to the original container's collection property.

Overrides:
addItem in class CloneEditHandler
Parameters:
pCloneEditState -
pAddItem -
Throws:
atg.commerce.CommerceException

getOriginalToCloneMap

protected java.util.Map getOriginalToCloneMap(CloneEditState pCloneEditState)
Returns the original to clone map. This method will lazily create the map if one doesn't already exist.

Parameters:
pCloneEditState -
Returns:
the original to clone object map

getCloneToOriginalMap

protected java.util.Map getCloneToOriginalMap(CloneEditState pCloneEditState)
Returns the clone to original map. This method will lazily create the map if one doesn't already exist.

Parameters:
pCloneEditState -
Returns:
the clone to original object map

validateCloneOrder

public void validateCloneOrder(atg.commerce.order.Order pOriginalOrder,
                               atg.commerce.order.Order pCloneOrder)
                        throws atg.commerce.CommerceException
Validates the collections after the cloning process

Specified by:
validateCloneOrder in class CloneEditHandler
Parameters:
pOriginalOrder -
pCloneOrder -
Throws:
atg.commerce.CommerceException
See Also:
validateCollectionSize(Object, Object)

validateCollection

protected void validateCollection(atg.commerce.order.Order pOriginalOrder,
                                  atg.commerce.order.Order pCloneOrder)
                           throws atg.commerce.CommerceException
Validates the collection

Parameters:
pOriginalOrder -
pCloneOrder -
Throws:
atg.commerce.CommerceException
See Also:
validateCollectionSize(Object, Object)

validateCollectionSize

protected void validateCollectionSize(java.lang.Object pOriginalCollectionContainer,
                                      java.lang.Object pCloneCollectionContainer)
                               throws atg.commerce.CommerceException
Ensures that the collections in the property defined by collectionPropertyName are the same size.

Parameters:
pOriginalCollectionContainer -
pCloneCollectionContainer -
Throws:
atg.commerce.CommerceException