Skip to content

Commit 7b130d1

Browse files
review: Transition flow (#2245)
Co-authored-by: sjvans <[email protected]>
1 parent b9ad174 commit 7b130d1

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

guides/providing-services.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ For CAP Java, support for flows is provided by the feature [cds-feature-flow](ht
12641264
The following example, taken from [@capire/xtravels](https://github.com/capire/xtravels), shows the simplest way to model a flow.
12651265
The annotations in the service model are sufficient to define and use the flow.
12661266

1267-
![](./assets/flows/xtravels-flow-simple.svg)
1267+
![A flow diagram showing three status states connected by arrows. The leftmost oval contains the word Open. An arrow labeled accept points from Open to an oval containing Accepted at the top right. Another arrow labeled reject points from Open to an oval containing Canceled at the bottom right. The diagram illustrates a simple state transition workflow where items can move from an open state to either accepted or canceled states.](./assets/flows/xtravels-flow-simple.svg)
12681268

12691269
The following is an extract of the relevant parts of the domain model:
12701270

@@ -1319,7 +1319,7 @@ For more complex scenarios, you can add custom handlers as explained later.
13191319

13201320
Flows consist of a _status element_ and a set of _flow actions_ that define transitions between states.
13211321

1322-
#### `@flow.status`
1322+
#### Declare Flow Using `@flow.status`
13231323

13241324
To model a flow, one of the entity fields needs to be annotated with `@flow.status`.
13251325
This field must be one of the following:
@@ -1336,34 +1336,37 @@ As no initial state can be provided on `CREATE`, there should be a default value
13361336

13371337
When you annotate `@flow.status: <element name>` at the entity level (as in the example above), the annotation is propagated to the respective element, which is also automatically annotated with `@readonly`.
13381338

1339-
**Notes:**
1340-
- This annotation is mandatory
1341-
- The annotated element must be either an enum or an association to a code list
1342-
- Only one status element per entity is supported
1343-
- Draft-enabled entities are supported, however flows are only applied to the active version
1344-
- `null` is **not** a valid state—model your empty state explicitly
1339+
**About the `@flow.status` annotation:**
1340+
- This annotation is **mandatory**.
1341+
- The annotated element must be either an enum or an association to a code list.
1342+
- Only one status element per entity is supported.
1343+
- Draft-enabled entities are supported, however flows are only applied to the active version.
1344+
- `null` is **not** a valid state—model your empty state explicitly.
13451345

13461346
::: warning Only simple projections are supported
13471347
The entity must be _writable_, and renaming the status element is currently not supported.
13481348
:::
13491349

13501350
After declaring `@flow.status`, use the following annotations on bound actions to model transitions:
13511351

1352-
#### `@from`
1352+
#### Model Transitions Using `@from` and `to`
13531353

1354-
- Defines valid entry states for the action
1355-
- Validates whether the entity is in a valid entry state before executing the action (the current state of the entity must be included in the states defined here)
1356-
- Can be a single value or an array of values (each element must be a value from the status enum)
1357-
- UI annotations to allow/disallow buttons and to refresh the page are automatically generated for UI5
1358-
- Annotation generation can be deactivated via <Config>cds.features.annotate_for_flows: false</Config>
1354+
Both annotations are optional, but at least one is required to mark an action as a flow action. Use either one or both depending on your needs. When you use both, no custom handlers are needed—generic handlers are registered automatically.
13591355

1360-
#### `@to`
1356+
**`@from`**
13611357

1362-
- Defines the desired target state of the entity after executing the action
1363-
- Changes the state of the entity to the value defined in this annotation after executing the action
1364-
- Must be a single value from the status enum
1358+
- Defines valid entry states for the action.
1359+
- Validates whether the entity is in a valid entry state before executing the action (the current state of the entity must be included in the states defined here).
1360+
- Can be a single value or an array of values (each element must be a value from the status enum).
1361+
- UI annotations to allow/disallow buttons and to refresh the page are automatically generated for UI5.
1362+
- Can be deactivated via <Config>cds.features.annotate_for_flows: false</Config>.
1363+
1364+
**`@to`**
1365+
1366+
- Defines the desired target state of the entity after executing the action.
1367+
- Changes the state of the entity to the value defined in this annotation after executing the action.
1368+
- Must be a single value from the status enum.
13651369

1366-
Both annotations are optional, but at least one is required to mark an action as a flow action. Use either one or both depending on your needs. When you use both, no custom handlers are needed—generic handlers are registered automatically.
13671370

13681371

13691372
### Generic Handlers
@@ -1372,7 +1375,7 @@ Generic handlers are registered automatically, so no custom implementations are
13721375

13731376
#### `before`
13741377

1375-
Based on the `@from` annotation, a handler validates that the entity is in a valid entry statethe current state must match one of the states specified in `@from`.
1378+
Based on the `@from` annotation, a handler validates that the entity is in a valid entry state - the current state must match one of the states specified in `@from`.
13761379
If validation fails, the request returns a `409 Conflict` HTTP status code with an appropriate error message.
13771380

13781381
#### `on`
@@ -1387,7 +1390,7 @@ For example, if the current state is `Open` and the target state is `Accepted`,
13871390
This ensures consistent state transitions without custom logic.
13881391

13891392
::: tip Generic handlers are not executed for draft entities
1390-
For example, calling `acceptTravel()` on a `Travels` entity that is currently being _edited_, i.e. is in _inactive_ state, has no effect.
1393+
For example, if you call `acceptTravel()` on a `Travels` entity that is currently being edited (in _inactive_ state), the call has no effect.
13911394
:::
13921395

13931396

@@ -1397,7 +1400,7 @@ You can use the target state `$flow.previous` to restore the previous state in a
13971400
The following example introduces a `Blocked` state with two possible previous states (`Open` and `InReview`) and an action `unblockTravel` that restores the previous state.
13981401
For instance, if `Blocked` was transitioned to from `Open`, calling `unblockTravel` transitions back to `Open`. The same applies for `InReview`.
13991402

1400-
![](./assets/flows/xtravels-flow-previous.svg)
1403+
![The graphic is explained in the accompanying text.](./assets/flows/xtravels-flow-previous.svg)
14011404

14021405
```cds [srv/travel-service.cds]
14031406
// srv/travel-service.cds
@@ -1448,7 +1451,7 @@ The `transitions_` composition automatically appended to the base entity is also
14481451

14491452
Flow annotations work well for basic flows. For more complex scenarios, implement custom event handlers.
14501453

1451-
**Common use cases for custom handlers:**
1454+
Common use cases for custom handlers:
14521455
- **Additional validation:** Implement a custom `before` handler when entry state validation depends on extra conditions
14531456
- **Non-void return types:** Implement a custom `on` handler when the action returns data
14541457
- **Conditional target states:** Implement a custom `on` or `after` handler (without `@to` annotation) when multiple target states depend on conditions

0 commit comments

Comments
 (0)