Overview
TheOrder class represents the central aggregate in the ordering module. It manages order lines, parties, pricing, and orchestrates interactions with fulfillment, inventory, and payment services through the order lifecycle.
Class Definition
Constructor
Unique identifier for the order
Current status of the order (DRAFT, CONFIRMED, PROCESSING, FULFILLED, etc.)
Collection of order lines, each representing a product with quantity and specifications
Parties involved in the order (buyer, receiver, delivery contact, etc.)
External services for pricing, inventory, payment, and fulfillment
Key Methods
id()
Returns the order identifier.The unique order identifier
status()
Returns the current order status.Current status: DRAFT, CONFIRMED, PROCESSING, FULFILLED, CLOSED, or CANCELLED
lines()
Returns an immutable copy of order lines.Immutable list of order lines
addLine()
Adds a new line to the order. Only allowed in DRAFT status.The order line to add
IllegalStateExceptionif order status doesn’t allow adding linesIllegalArgumentExceptionif line is null
removeLine()
Removes an order line by ID. Only allowed when status permits modifications.ID of the line to remove
IllegalStateExceptionif status doesn’t allow modifications or line not foundIllegalStateExceptionif removing would leave order with no lines
changeLineQuantity()
Changes the quantity of an existing order line.ID of the line to modify
New quantity value
IllegalStateExceptionif order status doesn’t allow modificationsIllegalStateExceptionif line not found
priceLines()
Calculates and applies pricing to all order lines using the configured pricing service.PricingContext with effective parties and delegates to OrderServices.pricing() to calculate the price.
confirm()
Confirms the order, triggering inventory allocation, payment capture, and fulfillment initiation.- Validates order is in DRAFT status
- Allocates inventory for each line
- Authorizes and captures payment for total order amount
- Changes status to CONFIRMED
- Starts fulfillment process
IllegalStateExceptionif not in DRAFT statusIllegalStateExceptionif inventory allocation failsIllegalStateExceptionif payment fails
cancel()
Cancels the order and cancels fulfillment if already started.IllegalStateExceptionif order status doesn’t allow cancellation (CLOSED or already CANCELLED)
updateFulfillmentStatus()
Updates order status based on fulfillment progress.New fulfillment status (IN_PROGRESS, PARTIALLY_COMPLETED, COMPLETED)
IN_PROGRESSorPARTIALLY_COMPLETED→ Order status becomes PROCESSINGCOMPLETED→ Order status becomes FULFILLED
getEffectivePartiesFor()
Returns effective parties for a specific order line, merging line-level parties with order-level parties.The order line to get parties for
Merged parties with line-level overrides taking precedence
totalPrice()
Calculates total order price if all lines are priced.Total price summed across all lines, or empty if any line is not priced
isFullyPriced()
Checks if all order lines have pricing applied.True if all lines are priced
Builder Pattern
Orders are constructed using a fluent builder:Builder Methods
Creates a new order builderParameters:
id(OrderId): Order identifierparties(OrderParties): Order-level partiesservices(OrderServices): External services
Adds an order line using a configuration function or direct parametersOverloads:
addLine(Function<LineBuilder, LineBuilder>)- Fluent configurationaddLine(ProductIdentifier, Quantity)- Simple lineaddLine(ProductIdentifier, Quantity, OrderLineSpecification)- With specification
Constructs the order in DRAFT statusThrows: IllegalStateException if order has no lines
Usage Examples
Creating and Confirming an Order
Adding Line with Specifications
Line-Level Party Overrides
Modifying Order Lines
Related Types
- OrderId: Value object for order identification
- OrderLine: Individual line item in the order (see
/api/ordering/ordersource code at line 10) - OrderStatus: Enum defining order lifecycle states (see
/api/ordering/ordersource code at line 21) - OrderParties: Collection of parties involved in order and their roles
- OrderServices: Service facade for pricing, inventory, payment, and fulfillment
- OrderLineSpecification: Key-value attributes for product configuration
Lifecycle State Diagram
- DRAFT: Initial state, lines can be added/modified
- CONFIRMED: Order confirmed, inventory allocated, payment captured
- PROCESSING: Fulfillment in progress
- FULFILLED: All lines fulfilled
- CLOSED: Order archived (final state)
- CANCELLED: Order cancelled (terminal state)
