You want to enrich your CDS view entity with metadata. In a first step, you define and maintain ABAP annotations for both, the entire view and individual view elements.
Task 1: Copy Template (Optional)
Copy the template data definition. If you have finished the previous exercise, you can skip this task and continue editing your data definition Z##_EMPLOYEE.
Steps
Copy the /LRN/R_EMPLOYEE data definition to a data definition in your own package (suggested name: Z##_EMPLOYEE, where ## stands for your group number).
In the Project Explorer view, right-click the /LRN/R_EMPLOYEE data definition to open the context menu.
From the context menu, choose Duplicate ....
Enter the name of your package in the Package field. In the Name field, enter the name for the CDS view entity (Z##_Employee, where ## stands for your group number).
Adjust the description and choose Next.
Confirm the transport request and choose Finish.
Activate the copy.
Press Ctrl + F3 to activate the data definition.
Task 2: Maintain View Annotations
In your data definition, maintain the view annotations. Add an annotation that can be used to restrict data preview for a CDS view entity and adjust the values of the ObjectModel.usageType annotations.
Steps
At the beginning of the data definition, use code-completion to add annotation @ABAPCatalog.dataMaintenance with its default value.
Place the cursor at the beginning of the first code row and press Enter to insert a new empty row.
In the empty row, enter @A and press Ctrl + Space to invoke code-completion.
From the suggestion list, choose @ABAPCatalog: { } (annotation.
Enter a period sign (.) and press Ctrl + Space again.
Choose dataMaintenance: #RESTRICTED (annotation).
Group the entity annotations that start with @ABAPCatalog.
Hint
You can use the syntax of the @ObjectModel.usageType annotation group as a blueprint.Adjust the code as follows:
123456789
// @AbapCatalog.dataMaintenance: #RESTRICTED
// @AbapCatalog.viewEnhancementCategory: [#NONE]
@AbapCatalog: {
dataMaintenance: #RESTRICTED,
viewEnhancementCategory: [#NONE]
}
@AccessControl.authorizationCheck: #NOT_REQUIRED
Display the documentation for the @ObjectModel.usageType annotations and maintain the values.
Note
You should make sure that the annotation values fit the technical table settings of your database table for employee data.Place the cursor on @ObjectModeland press F1 to open the ABAP language help.
On the ABAP Language Help view that opens below the editor, choose@ObjectModel, CDS View annotations, View Entity.
For the serviceQuality annotation, choose #D because the view does not support transactional processing.
For the sizeCategory annotation, choose #M because we expect less than 100,000 data sets.
For the dataClass annotation, choose #MASTER because employee data are maser data.
Task 3: Maintain Element Annotations
In your data definition, add the mandatory element annotation to establish the link between the annual salary and the currency code. Use the Active Annotations view to analyze the element labels that are derived from data elements and provide an alternative label for the CurrencyCode element.
Steps
In the element list, un-comment the AnnualSalary element and the CurrencyCode element.
Select both code rows and press Ctrl + Shift + < to remove the comment signs from the beginning of each row.
Add the mandatory element annotation that identifies the AnnualSalary element as an amount and links it to theCurrencyCode element.
Note
You might need to add this annotation manually with the support of code completion. At the time of writing, the editor for data definitions does not offer a quick fix for this error. (The editor for database table definitions does.)Adjust the code as follows:
123456
department_id as DepartmentId,
@Semantics.amount.currencyCode: 'CurrencyCode'
annual_salary as AnnualSalary,
currency_code as CurrencyCode,
Open the Active Annotations View and analyze the values of the EndUserText.Label annotation for the different view elements.
Right-click anywhere in the source code of the data definition and choose Open With → Active Annotations.
Result
The Active Annotations view appears as a new tab below the editor view. The source of the end user texts is listed in the Origin Data Type column.
Return to the source code of your data definition and add element annotation EndUSerText.label for view element CurrencyCode. Change the label from Currency to Currency Key.
Adjust the code as follows:
123456
@Semantics.amount.currencyCode: 'CurrencyCode'
annual_salary as AnnualSalary,
@EndUserText.label: 'Currency Key'
currency_code as CurrencyCode,
Activate the data definition.
Press Ctrl + F3 to activate the development object.
Return to the Active Annotations view, refresh the display, and confirm that now the new label is active.
Hint
To find a specific annotation more easily, you can first collapse the entire tree and then expand the element and main annotation you are interested in.From the toolbar on the Active Annotations view, choose Refresh (F5). Alternatively, place the cursor anywhere on the view and press F5.
From the toolbar on the Active Annotations view, choose Collapse All.
Expand Z##_R_EMPLOYEE → CurrencyCode → @EndUserText.