ForkContext
Overview
ForkContext is the operation-scoped state for a single fork.
It bundles three things:
| Field | Purpose |
|---|---|
|
The source the operation is forking from — mode + source identifier + timestamp. |
|
The command that initiated the operation, used for traceability. |
|
Cache that maps source identifiers to freshly generated ones, so the same source UUID is rewritten consistently across the whole operation. |
Construct one via the factory:
ForkContext ctx = ForkContext.of(cmd.getForkFrom(), cmd);
The factory installs a fresh SourceIdentifierGenerator.
You typically create one ForkContext per top-level fork command and pass it into every nested generator/service that produces commands for child entities.
Using the context
The two methods you’ll reach for most often:
forkMode()-
Shortcut for
forkFrom().getForkMode(). resolveIdentifier(ForkAware<?>)/resolveIdentifier(SourceIdentifier)-
Returns the new
SourceIdentifierthat should be used for the forked copy of the given entity. Backed by the context’sSourceIdentifierGenerator— calling it twice with the same input returns the same output, so cross-references stay consistent.
SourceIdentifier newId = ctx.resolveIdentifier(originalAsset);
// Use newId.uuid1() / uuid2() / uuid3() to populate the forked entity's primary key
// and any references that pointed at the original.
toSpawnedByRef() returns the Command identifier as a String (e.g. cmd:7d1f-…), suitable for stamping onto audit fields:
forkedEntity.setCreatedByRef(ctx.toSpawnedByRef());
The generator is intentionally per-operation: starting a new ForkContext resets it, so two independent forks never collide on a UUID.