Interfaces and Methods
Order Cancel Service is defined by interfaces that enable access to order cancel functionality and support communication to and from the Warehouse. Several methods allow access to data model and configuration, and perform order cancel execution logic. State-based processing is based on predefined set of states. Default service configuration can be modified in order to adapt it to the particular requirements.
Introduction
Order Cancel service is defined using two interfaces:
- OrderCancelService
- OrderCancelCallbackService
OrderCancelService interface is the main service interface of Order Cancel service. It is designed to be used by "business user" which technically is expected to be another service or a web shop implementation.
The role of this interface is to enable access to:
- Order Cancel related functionality (e.g. performing order cancels)
- Order Cancel data model and configuration
The main class to be used with OrderCancelService interface is OrderCancelRequest class. This class allows to construct cancel requests and may represent either complete cancel request or partial cancel requests.
OrderCancelCallbackService interface is a callback interface to be used by a Warehouse adapter. Warehouse adapter is an interface between hybris platform and physical Warehouse. The role of OrderCancelCallbackService interface is to receive asynchronous messages from a Warehouse in response to a previously-issued cancel requests. Key class used with OrderCancelCallbackService interface is OrderCancelResponse class. This class represents a response sent by the Warehouse. The response contains order cancel results information, for example whether the cancel was successful or not and why.
Methods in OrderCancelService Interface
Methods for Data Model and Configuration Access
- OrderCancelConfigModel getConfiguration(): Returns current configuration.
- OrderCancelRecordModel getCancelRecordForOrder(OrderModel order): Returns OrderCancelRecordModel for given Order. This class is a main class of the Order Cancel Data Model.
- OrderCancelRecordEntryModel getPendingCancelRecordEntry(OrderModel order) throws OrderCancelException: Returns OrderCancelRecordEntryModel instance for currently pending cancel operation. This OrderCancelRecordEntryModel class represents data model for a single order cancel operation.
Methods Allowing Performance of the Order Cancel Execution Logic
- CancelDecision isCancelPossible(OrderModel order, PrincipalModel requestor, boolean partialCancel, boolean partialEntryCancel): Verifies if order cancel is possible for given conditions. This method should always be called first, before requestOrderCancel to check for order cancel availability!
- OrderCancelRecordEntryModel requestOrderCancel(OrderCancelRequest orderCancelRequest, PrincipalModel requestor): Issues order cancel request. You can call this method only if isCancelPossible allows the cancel, otherwise OrderCancelDeniedException will be thrown.
- Map<AbstractOrderEntryModel, Long> getAllCancelableEntries(OrderModel order, PrincipalModel requestor): This method provides fine-grained information about which order entries can be canceled.
Order Cancel Service Implementation Details
State-based Processing
Order Cancel Service logic is based on predefined set of states, defined by OrderCancelState enum type. These states are derived from the state diagrams that provide conceptual model for processing order cancels.
There are several states defined by de.hybris.platform.basecommerce.enums.OrderCancelState. The list contains available states, the names in parentheses denote corresponding state from a state diagram:
- PENDINGORHOLDINGAREA (QueueAwait)
- SENTTOWAREHOUSE (SentToWarehouse)
- SHIPPING (Shipping)
- PARTIALLYSHIPPED (PartiallyShipped)
- CANCELIMPOSSIBLE (CancelImpossible)
Additionally a new state has been defined for enum OrderStatus: CANCELLING, which is used to model "Cancelling" state from the diagrams.
Order Cancel Service uses a strategy: OrderCancelStateMappingStrategy, that is responsible for determining current OrderCancelState for a given order.
The default mapping strategy is described in the OrderCancel - Default Mapping Strategy document. This strategy is defined as a Spring bean, and it can be easily changed.
Default Service Implementation Configuration
Default OrderCancelService implementation: DefaultOrderCancelService uses a set of strategies to implement the required functionality. Most important objects are:
- OrderCancelStateMappingStrategy: Provides information about current OrderCancelState for the Order.
- OrderCancelDenialStrategy: Enables to perform checks if cancel is possible for given conditions.
- OrderCancelRequestExecutor: An object used to actually execute the logic involved with canceling order.
- OrderCancelResponseExecutor: An object used to process messages received from a Warehouse in response to a cancel request.
- OrderCancelCancelableEntriesStrategy: A strategy used to get a list of the given order entries that can be canceled.
All these objects are defined as Spring beans. As a result altering the Spring configuration can easily influence DefaultOrderCancelService and change the way it works.
Executor Mappings
DefaultOrderCancelService does not have "fixed" execution logic. All actions are performed by so-called executors. There are two types of executors:
- OrderCancelRequestExecutor
- OrderCancelResponseExecutor.
Request executors are used to process cancel requests received via OrderCancelService interface. Response executors are used to process cancel responses received via OrderCancelCallbackService.
DefaultOrderCancelService has two maps of executors: requestExecutorsMap and responseExecutorsMap. Each map provides a mapping between current OrderCancelState and an executor that handles order cancel logic in this state. Both maps are configured by Spring, so this gives a very flexible way to configure overall logic of DefaultOrderCancelService.
Verifying Possibility of the Order Cancel: OrderCancelDenialStrategy
DefaultOrderCancelService class uses a List of OrderCancelDenialStrategy instances to verify if cancel is possible for a given Order. There is a couple of predefined OrderCancelDenialStrategy classes. Also, new classes may be added if needed.
The Spring configuration defines which OrderCancelDenialStrategy classes should be used by DefaultOrderCancelService. If OrderCancelDenialStrategy finds that order cancel cannot be performed, then it returns non-null OrderCancelDenialReason. Otherwise it returns null.
Because DefaultOrderCancelService processes a list of OrderCancelDenialStrategy, cancel operation is allowed if all strategies return null result. If any strategy returns non-null OrderCancelDenialReason, then cancel operation is not possible, and the list of received OrderCancelDenialReason is returned to the user.
In hybris implementation every concrete OrderCancelDenialStrategy can have its OrderCancelDenialReason configured by Spring. Users can do the mapping between OrderCancelDenialStrategy and OrderCancelDenialReason as they require. By default we provide DefaultOrderCancelDenialReason class that is a reasonable implementation of the OrderCancelDenialReason interface. However, it is possible to easily plug in another implementation, if needed, for example when OrderCancelDenialReason instance should convey additional information or be able to execute some logic.
Other Collaborating Objects
OrderCancelNotificationServiceAdapter
This interface is used to broadcast notifications about cancel operations. It is used by classes: ImmediateCancelRequestExecutor, WarehouseProcessingCancelRequestExecutor, WarehouseResponseExecutor. Order Cancel Service does not provide default implementation of this interface, because no reasonable default can be defined. Users should supply their own implementation and plug it in using Spring configuration for the aforementioned classes. If not provided, notifcations are not sent.
OrderCancelPaymentServiceAdapter
This interface is used to recalculate order after cancel operation has been finished. It is used by classes: ImmediateCancelRequestExecutor, WarehouseResponseExecutor. Order Cancel Service does not provide default implementation of this interface. Users should supply their own implementation and plug it in using Spring configuration for the aforementioned classes. If not provided, orders are not automatically recalculated by Order Cancel Service after cancel operation is finished (order recalculation can be then performed externally to Order Cancel Service)
OrderCancelWarehouseAdapter
This interface is used to forward cancel requests to a warehouse. It is used by class: WarehouseProcessingCancelRequestExecutor. A mock implementation (DefaultWarehouseAdapterMock) is provided by default. Users should supply their own implementation and plug it in using Spring configuration for the aforementioned class.
Order Cancel Service Configuration
The following configuration entries are defined for Order Cancel Service using OrderCancelConfigModel class:
- orderCancelAllowed: Determines if the order canceling is possible.
- cancelAfterWarehouseAllowed: Determines if the order canceling is still possible after sending it to warehouse.
- completeCancelAfterShippingStartedAllowed: Determines if complete cancel request will be accepted after shipping has started. Such a request means: Cancel the part of the order that has not been shipped yet (in other words, cancel everything that can still be canceled). This parameter is evaluated only if cancelAfterWarehouseAllowed is set to true.
- partialCancelAllowed: Determines if the partial order canceling (discarding whole order entries) is possible.
- partialOrderEntryCancelAllowed: Determines if the partial order entry canceling (discarding parts of order entries) is possible. This parameter is evaluated only if partialCancelAllowed is set to true.
- queuedOrderWaitingTime: Determines how long an order should wait in queue before it is started to be fulfilled.
Almost all described entries are of type boolean. Only the queuedOrderWaitingTime is of type int. queuedOrderWaitingTime is defined as a part of Order Cancel configuration, but the queue itself must be implemented externally to Order Cancel Service. The reason for is that this Service does not provide any implementation for orders queue.
Data Model
The diagrams presents the data model for Order Cancel.