Skip to content

Commit 20c287c

Browse files
eugene-andreevagoerlerrenejeglinsky
authored
Describe recursive hierarchies (#1770)
Co-authored-by: Adrian Görler <[email protected]> Co-authored-by: René Jeglinsky <[email protected]>
1 parent 7719729 commit 20c287c

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

advanced/odata.md

+69
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,75 @@ GET SalesOrganizations?$apply=
11391139
/ancestors(..., ID, filter(contains(Name, 'New York')), keep start)
11401140
```
11411141

1142+
#### Modeling Recursive Hierarchies
1143+
1144+
Recursive hierarchies are parent-child hierarchies, where each entity references its parent and through that defines the hierarchical structure. A common example is a company organization structure or HR reporting, where each employee entity references another employee a as direct report or manager.
1145+
1146+
##### Domain Model
1147+
1148+
The simplest domain model looks as follows:
1149+
1150+
```cds
1151+
entity Employee : Hierarchy {
1152+
key ID : UUID;
1153+
parent : Association to Employee;
1154+
fullName : String;
1155+
}
1156+
1157+
aspect Hierarchy {
1158+
virtual LimitedDescendantCount : Integer64;
1159+
virtual DistanceFromRoot : Integer64;
1160+
virtual DrillState : String;
1161+
virtual LimitedRank : Integer64;
1162+
}
1163+
```
1164+
1165+
The entity `Employee` has the element `ID`, which identifies the hierarchy node. The `parent` association references the same entity, which establishes the parent-child relationship.
1166+
1167+
##### Virtual Elements of a Hierarchy
1168+
1169+
The `Hierarchy` aspect defines a set of virtual elements, automatically calculated by the backend at runtime, to describe the state of the hierarchy. This information is requested by the UI to correctly render the hierarchy in a *TreeTable* during user interaction.
1170+
1171+
##### Service Model
1172+
1173+
The following service defines the projection on the domain model.
1174+
1175+
```cds
1176+
@odata.apply.transformations
1177+
service HRService {
1178+
entity HREmployee as projection on Employee;
1179+
}
1180+
```
1181+
1182+
::: warning
1183+
The service must be annotated with `@odata.apply.transformations`. This instructs the Java Runtime to push down the whole transformation pipeline to the persistence layer.
1184+
:::
1185+
1186+
##### OData v4 Annotations for Fiori
1187+
1188+
To link the backend and Fiori UI, the projected service entity must be enriched with the following annotations.
1189+
1190+
```cds
1191+
annotate HRService.HREmployee with @Aggregation.RecursiveHierarchy #EmployeeHierarchy: {
1192+
$Type: 'Aggregation.RecursiveHierarchyType',
1193+
NodeProperty: ID,
1194+
ParentNavigationProperty: parent
1195+
};
1196+
```
1197+
1198+
Here the `EmployeeHierarchy` specifies a hierarchy qualifier, `NodeProperty` (identifying the hierarchy node) is linked to `ID` of the entity `HREmployee`, and the `ParentNavigationProperty` is linked to a corresponding `parent` association.
1199+
1200+
```cds
1201+
annotate HRService.HREmployee with @Hierarchy.RecursiveHierarchy #EmployeeHierarchy: {
1202+
$Type: 'Hierarchy.RecursiveHierarchyType',
1203+
LimitedDescendantCount: LimitedDescendantCount,
1204+
DistanceFromRoot: DistanceFromRoot,
1205+
DrillState: DrillState,
1206+
LimitedRank: LimitedRank
1207+
};
1208+
```
1209+
1210+
Here the same qualifier `EmployeeHierarchy` is referenced to list the names of the [virtual elements of the hierarchy](#virtual-elements-of-a-hierarchy).
11421211

11431212
### Aggregation Methods
11441213

0 commit comments

Comments
 (0)