In this exercise, you develop an extension of the business object for flight travels and its projection. The goal of this extension is to have an additional booking class field for the flight travel items. To achieve this, you first extend the extension include structure Z##_S_EXT_TRITEM, which indirectly extends the database table for flight travel items and the related draft table.
After that, you extend the business object and its projection. You begin by extending the extension include view Z##_E_TravelItem because, in our case, the extension include view is the only allowed data source when extending the view entity on data model level Z##_R_TravelItem. Then you extend the view entity on data model level Z##_R_TravelItem and - based on that extension - the projection view entity Z##_C_TravelItem. Finally, you create a new metadata extension to add the booking class field to the UI metadata of the OData service.
Note
In this exercise, you extend your own data model objects. This is done for simplicity. To motivate the use of extensions, during this exercise, pretend that the database tables Z##_TRITEM and Z##_TRITEM_D, the extension include structure Z##_S_EXT_TRITEM, the CDS view entities Z##_R_TravelItem and Z##_C_TravelItem, and the extension include view Z##_E_TravelItem lie in a different namespace and that you are not authorized to make direct changes to these objects.
Note
In this exercise, replace ## with your group number.
Solution
| Repository Object Type | Repository Object ID |
|---|
| Structure Type (Append Structure) | /LRN/ZZX_S_TRITEM_CLASS |
| CDS Data Definition (Extension Include Extension) | /LRN/ZZX_E_TRAVELITEM_CLASS |
| CDS Data Definition (Model Extension) | /LRN/ZZX_R_TRAVELITEM_CLASS |
| CDS Data Definition (Projection Extension) | /LRN/ZZX_C_TRAVELITEM_CLASS |
| CDS Metadata Extension (Projection) | /LRN/ZZX_C_TRAVELITEM_CLASS |
Task 1: Extend the Database Tables
Create an append structure (suggested name: Z##X_S_TRITEM_CLASS) for your extension include structure Z##_S_EXT_TRITEM. Add a field for the booking class (suggested name: ZZCLASSZ## ) and type the new field with data element /LRN/CLASS_ID.
Steps
Locate your extension include structure in the Project Explorer.
In the Project Explorer, expand the Favorite Packages node.
Expand your own package ZS4D437_##.
Under your package, expand Dictionary→Structures.
For the extension include structure, create an append structure (suggested name: Z##X_S_TRITEM_CLASS).
In the Project Explorer, right-click Z##_S_EXT_TRITEM and select New Append Structure.
On the New Append Structure dialog that opens, enter Z##X_S_TRITEM_CLASS in the Name field and Extend Travel Item with Booking Class in the Description field. Then choose Next.
Assign the append structure to a transport request and choose Finish.
In the component list of the append structure, define a new component (suggested name: zzclassz##) with data element /LRN/CLASS_ID as component type.
Adjust the code as follows:
123
extend type z##_s_ext_tritem with z##x_s_tritem_class {
zzclassz## : /lrn/class_id;
}
Activate the append structure.
Press Ctrl + F3 to activate the development object.
Open the definition of the database table for active flight travel items. Open the Tooltip Description to confirm that the table contains the new field.
In the Project Explorer, under your own package, expand Dictionary→Database Tables.
Double-click Z##_TRITEM to open the definition of the database table.
In the code row that begins with define table, place the cursor on z##_tritem and press F2.
Result
You see the new field at the end of the field list. You also see the name of your append structure in the Extended with section.
Repeat this for the corresponding draft table to confirm that this table also contains the new field.
In the Project Explorer, double-click Z##_TRITEM_D to open the definition of the draft table.
In the code row that begins with define table, place the cursor on z##_tritem_d and press F2.
Result
You see the new field at the end of the field list. You also see the name of your append structure in the Extended with section.
Task 2: Extend the Data Model Definition
Create a CDS view extension to add the booking class field to the element list of the extension include view (suggested name: Z##X_E_TravelItem_Class). Then create a similar CDS view extension for the view entity on data model level (suggested name: Z##X_R_TravelItem_Class).
Steps
In the Project Explorer, locate the CDS data definition that contains the definition of your extension include view.
In the Project Explorer, expand the Favorite Packages node.
Expand your own package ZS4D437_##.
Under your package, expand Core Data Services→Data Definitions.
For the extension include view, create a CDS view extension (suggested name: Z##X_E_TravelItem_Class).
In the Project Explorer, right-click Z##_E_TRAVELITEM and select New Data Definition.
In the Name field, enter Z##X_E_TravelItem_Class and in the Description field, enter Extend Travel Item with Booking Class. Then choose Next.
Confirm the transport request and choose Next.
Note
Do not choose Finish yet or you will not be able to choose the correct template.
From the list of templates, choose View Extend (creation)→extendViewEntity and choose Finish.
Edit the element list of the view extension. Remove the placeholder and use code completion to add the booking class field.
In the source code, replace base_data_source_name.element_name with zz and press Ctrl + Space to invoke code completion.
From the suggestion list, choose zzclassz## - z##_tritem as Item (column) to add the append field to the element list of the CDS view extension.
To increase readability, define an alias name for the new view element (suggested name: ZZClassZ##) and activate the data definition.
Caution
Make sure that the alias name and the field name only differ in uppercase or lowercase!
Adjust the code as follows:
1234
extend view entity Z##_E_TravelItem with
{
Item.zzclassz## as ZZClassZ##
}
Press Ctrl + F3 to activate the development object.
For the flight travel item view on data model level Z##_R_TravelItem, create a CDS view extension (suggested name: Z##X_R_TravelItem_Class).
In the Project Explorer, right-click Z##_R_TRAVELITEM and select New Data Definition.
Under Name, enter Z##X_R_TravelItem_Class and under Description, enter Extend Travel Item with Booking Class. Then choose Next.
Confirm the transport request and choose Next.
Note
Do not choose Finish yet or you will not be able to choose the correct template.
From the list of templates, choose View Extend (creation)→extendViewEntity and choose Finish.
Edit the element list of the view extension. Remove the placeholder and use code completion to add the booking class field.
In the source code, replace base_data_source_name.element_name with zz and press Ctrl + Space to invoke code completion.
From the suggestion list, choose ZZClassZ## - Z##_E_TravelItem as _Extension (column) to add the extension field to the element list of the CDS view extension.
Make sure that the code looks like this:
1234
extend view entity Z##_R_TravelItem with
{
_Extension.ZZClassZ##
}
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Task 3: Extend the Data Model Projection
Create a CDS view extension to add the booking class field to the CDS view entity on projection level (suggested name: Z##X_C_TravelItem_Class). Then create a new metadata extension for the projection view (suggested name: Z##X_C_TravelItem_Class). Set the metadata layer to #CUSTOMER and add UI metadata for the booking class field.
Steps
For the travel item view on projection level Z##_C_TravelItem, create a CDS view extension (suggested name: Z##X_C_TravelItem_Class).
In the Project Explorer, right-click Z##_C_TRAVELITEM and select New Data Definition.
Under Name, enter Z##X_C_TravelItem_Class and under Description, enter Extend Travel Item with Booking Class. Then choose Next.
Confirm the transport request and choose Next.
Note
Do not choose Finish yet or you will not be able to choose the correct template.
From the list of templates, choose View Extend (creation)→extendViewEntity and choose Finish.
Edit the element list of the view extension. Remove the placeholder and use code completion to add the booking class field.
In the source code, replace base_data_source_name.element_name with zz and press Ctrl + Space to invoke code completion.
From the suggestion list, choose ZZClassZ## - Z##_R_TravelItem as Item (column) to add the extension field to the element list of the CDS view extension.
Make sure that the code looks like this:
1234
extend view entity Z##_C_TravelItem with
{
Item.ZZClassZ##
}
Assign a value help to the new field, using /LRN/437_I_ClassStdVH as the data source.
Adjust the code as follows:
1234567
extend view entity Z##_C_TravelItem with
{
@Consumption.valueHelpDefinition: [
{ entity: { name: '/LRN/437_I_ClassStdVH',
element: 'ClassID' } } ]
Item.ZZClassZ##
}
Activate the data definition.
Press Ctrl + F3 to activate the development object.
For the flight travel item view on projection level Z##_C_TravelItem, create a CDS metadata extension (suggested name: Z##X_C_TRAVELITEM_CLASS).
In the Project Explorer, right-click Z##_C_TRAVELITEM and select New Metadata Extension.
Under Name, enter Z##X_C_TRAVELITEM_CLASS and under Description, enter Extend Travel Item with Booking Class. Then choose Next.
Confirm the transport request and choose Next.
Note
Do not choose Finish yet or you will not be able to choose the correct template.
From the list of templates, choose Annotate View (creation)→annotateView and choose Finish.
In the newly created metadata extension, set the metadata layer to #CUSTOMER.
In the @Metadata.layer annotation, replace the placeholder layer with #CUSTOMER.
Add UI metadata for the booking class field. Make sure, the new field appears on both, the list page and the object page, immediately after the BookingId field.
Hint
View element BookingId is annotated with @UI.lineItem.position: 60 and @UI.identification.position: 60.
Adjust the code as follows:
12345678
@Metadata.layer: #CUSTOMER
annotate view Z##_C_TravelItem with
{
@UI: {
lineItem: [ { position: 65, importance: #HIGH } ],
identification: [ { position: 65, importance: #HIGH } ] }
ZZClassZ##;
}
Activate the metadata extension.
Press Ctrl + F3 to activate the development object.
Refresh the preview of the OData UI service and confirm that the booking class extension field is displayed.