In this exercise, you add the child entity to the business object projection and the definition of the OData UI service. Then you add the relevant metadata to display the flight travel items in the service preview.
Note
In this exercise, replace ## with your group number.
Solution
| Repository Object Type | Repository Object ID |
|---|
| CDS Data Definition (Projection, Child) | /LRN/437E_C_TRAVELITEM |
| CDS Behavior Definition (Projection) | /LRN/437E_C_TRAVEL |
| Service Definition | /LRN/437E_TRAVEL |
| CDS Metadata Extension | /LRN/437E_C_TRAVELITEM |
Task 1: Copy Template for the Projection (Child Entity)
Create copies of the following repository objects and place them in your ABAP package. For the repository object names, take the name of the template and replace /LRN/D437T with Z##. Make sure that the copies refer to your own repository objects and not to the template objects.
Template
| Repository Object Type | Repository Object ID |
|---|
| CDS Data Definition (Projection, Child) | /LRN/437T_C_TRAVELITEM |
| CDS Metadata Extension | /LRN/437T_C_TRAVELITEM |
Steps
Create a copy of data definition /LRN/437T_C_TRAVELITEM (suggested name: Z##_C_TravelItem).
In the Project Explorer view, locate the /LRN/437T_C_TRAVELITEM data definition in the /LRN/S4D437_TEMPLATE package and right-click it to open the context menu.
From the context menu, select Duplicate....
Enter the name of your package and the name for the copy. Choose Next.
Assign the new object to a transport request and choose Finish.
Replace the selection target after the projection on keyword with the name of your own travel items view entity Z##_R_TravelItem and remove the root keyword.
Adjust the code as follows:
1234
define view entity Z##_C_TravelItem
provider contract transactional_query
as projection on Z##_R_TravelItem
{
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Create a copy of metadata extension /LRN/437T_C_TRAVELITEM (suggested name: Z##_C_TRAVELITEM).
In the Project Explorer view, locate the /LRN/437T_C_TRAVELITEM metadata extension in the /LRN/S4D437_TEMPLATE package and right-click it to open the context menu.
From the context menu, select Duplicate....
Enter the name of your package and the name for the copy. Then choose Next.
Assign the new object to a transport request and choose Finish.
Replace the CDS view after the annotate view keyword with your own projection view for travel items Z##_C_TravelItem.
Adjust the code as follows:
12
annotate view Z##_C_TravelItem with
{
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Task 2: Add Composition to the Data Model Projection
In the data definitions on projection level Z##_C_Travel and Z##_C_TravelItem, expose the associations from the underlying data model views. Make sure that the associations do not point at the views on data model level but that they are redirected to point at the respective views on projection level. Make sure that the associations keep their special character as composition and to parent association.
Steps
Edit the data definition of your projection view for travel items Z##_C_TravelItem. At the end of the projection list, add the name of the to parent association, which you defined in the underlying view entity.
Adjust the code as follows:
123
LocChangedAt,
_Travel
}
Redirect the association to the projection view for flight travels.
Adjust the code as follows:
123
LocChangedAt,
_Travel : redirected to Z##_C_Travel
}
Classify the redirected association as a to parent association.
Adjust the code as follows:
123
LocChangedAt,
_Travel : redirected to parent Z##_C_Travel
}
Perform a syntax check.
Press Ctrl + F2 and analyze the Problems view.
Remove or comment the provider contract addition, then activate the data definition.
Adjust the code as follows:
123
define view entity Z##_C_Travelitem
as projection on Z##_R_TravelItem
{
Press Ctrl + F3 to activate the development object.
Edit the data definition of your projection view for travels Z##_C_Travel. At the end of the projection list, add the name of the composition, which you defined in the underlying view entity.
Adjust the code as follows:
123
LocChangedAt,
_TravelItem
}
Redirect the association to the projection view for flight travel items and classify it as composition.
Adjust the code as follows:
123
LocChangedAt,
_TravelItem : redirected to composition child Z##_C_TravelItem
}
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Task 3: Add Composition to the Behavior Projection
Edit the behavior definition on projection level Z##_C_TRAVEL. Add a behavior projection for the child entity. Expose its etag definition and add the associations that establish the composition.
Steps
Edit the behavior definition on projection level Z##_C_TRAVEL. At the end of the source code, use a code template to add a new define behavior statement for the child entity Z##_C_TravelItem.
After the last closing bracket, add a new code row, enter def, and press Ctrl + Space to invoke code completion.
From the suggestion list, choose defineBehaviorFor - Define Behavior For.
Replace entity with Z##_C_TravelItem.
Remove or comment the alias addition.
Add the etag definition to the behavior projection.
Adjust the code as follows:
12345
define behavior for Z##_C_TravelItem
use etag
{
}
For the child entity, add the standard operations update and delete to the projection.
Adjust the code as follows:
123456
define behavior for Z##_C_TravelItem
use etag
{
use update;
use delete;
}
Add the to parent association _Travel to the behavior projection of the child entity and the composition_TravelItem to the behavior projection of the root entity.
Note
Do not forget to draft-enable both associations and create-enable the composition.
Add the following code to the behavior definition for the root entity:
1
use association _TravelItem { create; with draft; }
Adjust the behavior definition for the child entity as follows:
12345678
define behavior for Z##_C_TravelItem
use etag
{
use update;
use delete;
use association _Travel { with draft; }
}
Activate the behavior projection.
Press Ctrl + F3 to activate the development object.
Task 4: Add Composition to the OData UI Service
Add the child entity of your business object to your service definition Z##_TRAVEL and extend the UI metadata of the service to display a list of child entities in a second facet on the object page for flight travels.
Steps
Open your service definition Z##_TRAVEL and add the projection view for flight travel items. Then activate the service definition.
Adjust the code as follows:
12345
@EndUserText.label: 'Flight Travel Service Definition'
define service Z##_TRAVEL {
expose Z##_C_Travel;
expose Z##_C_TravelItem;
}
Press Ctrl + F3 to activate the development object.
Open your service binding Z##_UI_TRAVEL_O2 and analyze the information under Entity Set and Association.
Note
There is no Refresh button available in service bindings but you can press F5 to reload the service binding.
Open the metadata extension for flight travels Z##_C_TRAVEL and locate the @UI.facet annotation. Inside the square brackets, add a comma (,) after the closing curly bracket and insert a copy of the existing facet definition.
Adjust the code as follows:
123456789101112
@UI.facet: [ { id: 'Travel',
purpose: #STANDARD,
type: #IDENTIFICATION_REFERENCE,
label: 'Travel',
position: 10 },
{ id: 'Travel',
purpose: #STANDARD,
type: #IDENTIFICATION_REFERENCE,
label: 'Travel',
position: 10 }
]
Adjust the second facet definition according to the following table:
| Property | Value |
|---|
| id | 'TravelItem' |
| purpose | #STANDARD |
| type | #LINEITEM_REFERENCE |
| label | 'Travel Items' |
| position | 20 |
Adjust the code as follows:
123456789101112
@UI.facet: [ { id: 'Travel',
purpose: #STANDARD,
type: #IDENTIFICATION_REFERENCE,
label: 'Travel',
position: 10 },
{ id: 'TravelItem',
purpose: #STANDARD,
type: #LINEITEM_REFERENCE,
label: 'Travel Items',
position: 20 }
]
Inside the second facet definition, add the property targetElement with the name of the composition as value.
Adjust the code as follows:
12345678910111213
@UI.facet: [ { id: 'Travel',
purpose: #STANDARD,
type: #IDENTIFICATION_REFERENCE,
label: 'Travel',
position: 10 },
{ id: 'TravelItem',
purpose: #STANDARD,
type: #LINEITEM_REFERENCE,
targetElement: '_TravelItem',
label: 'Travel Items',
position: 20 }
]
Activate the metadata extension and retest the preview for the OData UI service. Make sure that the object page for flight travels displays an (empty) list of flight travel items and that you can create new flight travel items.
Press Ctrl + F3 to activate the development object.
Open the service binding, place the cursor on the root entity and choose Preview....
In the preview, display the details for one of the travels.
Result
The object page consists of two facets. The first facet shows the flight travel details and the second facet a list of the related flight travel items. This list is empty.
Choose Edit to change to edit mode.
Result
On the second facet, you now see a Create button above the list of flight travel items.
Choose Create and Apply to create a new travel item with initial values in all input fields.
Result
The list of travel items now contains one entry with initial values.
Choose Save to save your changes.