Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions guides/uis/fiori.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,55 @@ You can override generated side-effect annotations per entity, for example:
Can be disabled with <Config>cds.fiori.draft_messages: false</Config>.
:::

### Showing a Custom User Description { #draft-user-description}

By default, the SAP Fiori draft UI displays the unique user name in fields such as *Locked by*. To show a human-readable name instead, enable the `draftUserDescription` CDS compiler option and populate the generated `InProcessByUserDescription` field via a custom handler.

**1. Enable the compiler option** in your `.cdsrc.json`:

```json
{
"cdsc": {
"draftUserDescription": true
}
}
```

The compiler now adds `InProcessByUserDescription`, `CreatedByUserDescription`, and `LastChangedByUserDescription` to the `DraftAdministrativeData` entity.

**2. Register a handler** on draft events across all draft services to populate the field:

This is how the handler could look for CAP Java:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is how the handler could look for CAP Java:
This is how the handler could look for CAP Java that fills in the user decription for the draft-enabled entity `Orders`:


```java
@Component
@ServiceName(value = "*", type = DraftService.class)
public class UserDescriptionHandler implements EventHandler {

@Autowired private UserInfo userInfo;

@Before
protected void addDraftFields(DraftPatchEventContext context, Order order) {
order.setInProcessByUserDescription(userInfo.getAdditionalAttribute("given_name") + " "
+ userInfo.getAdditionalAttribute("family_name")description());
}
}
```
Comment on lines +505 to +518

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This handler is specific for CAP Java. I guess it should be possible to have a corresponding handler for CAP Node.js

-> https://github.tools.sap/cap/cdsnode/issues/708#issuecomment-677683

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.tools.sap/cap/cdsnode/issues/708#issuecomment-677683

This handler is specific for Order. A corresponding handler in AP Java would be simpler, of course:

    @Before
    protected void addDraftFields(DraftCreateEventContext context, List<Order> orders) {
        orders.forEach(o -> o.setInProcessByUserDescription(description()));
    }

    private String description() {
        return userInfo.getAdditionalAttribute("given_name")
            + " "
            + userInfo.getAdditionalAttribute("family_name");
    }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check if this looks good now :)


This is the corresponding handler for CAP node.js:

```javascript
srv.on('PATCH', Order, req => {
req.data.InProcessByUserDescription = req.user.attr.givenName + ' ' + req.user.attr.familyName
})
```


::: tip
The attribute names `given_name` and `family_name` correspond to JWT token claims provided by your IdP. Adjust them to match the claims configured in your environment.
:::


### Draft for Localized Data

Annotate the underlying base entity in the base model with `@fiori.draft.enabled` to also support drafts for [localized data](./localized-data):
Expand Down
43 changes: 1 addition & 42 deletions java/fiori-drafts.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,48 +333,7 @@ public void createDraft(CreateDraftContext context) {

## Showing a Custom User Description { #draft-user-description}

By default, the SAP Fiori draft UI displays the unique user name in fields such as *Locked by*. To show a human-readable name instead, enable the `draftUserDescription` CDS compiler option and populate the generated `InProcessByUserDescription` field via a custom handler.

**1. Enable the compiler option** in your `.cdsrc.json`:

```json
{
"cdsc": {
"draftUserDescription": true
}
}
```

The compiler now adds `InProcessByUserDescription`, `CreatedByUserDescription`, and `LastChangedByUserDescription` to the `DraftAdministrativeData` entity.

**2. Register a `@Before` handler** on `DRAFT_CREATE` across all draft services to populate the field:

```java
@Component
@ServiceName(value = "*", type = DraftService.class)
public class UserDescriptionHandler implements EventHandler {

@Autowired private UserInfo userInfo;

@Before
protected void addDraftFields(DraftCreateEventContext context, List<CdsData> entries) {
CdsDataProcessor.Filter filter =
(path, element, type) -> "InProcessByUserDescription".equals(element.getName());
CdsDataProcessor.Generator generator = (path, element, isNull) -> description();
CdsDataProcessor.create().addGenerator(filter, generator).process(entries, context.getTarget());
}

private String description() {
return userInfo.getAdditionalAttribute("given_name")
+ " "
+ userInfo.getAdditionalAttribute("family_name");
}
}
```

::: tip
The attribute names `given_name` and `family_name` correspond to JWT token claims provided by your IdP. Adjust them to match the claims configured in your environment.
:::
[Learn how to show a custom user description in drafts.](../guides/uis/fiori#draft-user-description){.learn-more}

## Consuming Draft Services { #draftservices}

Expand Down
Loading