ForkContext

Overview

ForkContext is the operation-scoped state for a single fork. It bundles three things:

Field Purpose

forkFrom (ForkSource)

The source the operation is forking from — mode + source identifier + timestamp.

spawnedBy (Command)

The command that initiated the operation, used for traceability.

generator (SourceIdentifierGenerator)

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 SourceIdentifier that should be used for the forked copy of the given entity. Backed by the context’s SourceIdentifierGenerator — 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.