CDS Views — Part 7
CDS Annotations
Hey peeps! Welcome back. Annotation isn’t new it exists in other languages like java etc. in another context. A simple definition is — An annotation is an extra information associated with a particular point in a document or other piece of information. CDS is sometimes referred to as a rich data model, what makes a CDS View rich? or rich in what? Long answer short Annotations. Annotations make a simple/plain CDS view rich by giving a contextual meaning to it.
There are various classifications in annotations based on the purpose of their use as below.
- Analytics Annotations
- AnalyticsDetails Annotations
- Consumption Annotations
- DefaultAggregation Annotations
- EnterpriseSearch Annotations
- Hierarchy Annotations
- ObjectModel Annotations
- OData Annotations
- Search Annotations
- Semantics Annotations
- UI Annotations
- VDM Annotations
You can explore CDS Annotations from the above links also check table “ABDOC_CDS_ANNOS” for a list of all annotations.
Annotations provide metadata for a field/view which is understandable by different runtime frameworks eg. SADL framework/Analytic Framework. We can further group annotations into -
- View Annotations — For the entire CDS View, placed in front of the DEFINE VIEW statement.
- Element Annotations — For elements in the Projection list, placed in front of or after the element.
- Parameters Annotations — For Parametrized CDS view, placed in front of or after the parameter.
- Extension Annotations — For CDS View extension, placed in front of the EXTEND VIEW statement.
- Function Annotations — For the CDS Table function, placed in front of the DEFINE TABLE FUNCTION statement.
Let's see an example of a CDS view with annotations,
You would be familiar with the above CDS View from our previous blogs, we have been using annotations all along but haven’t paid attention to them. Let's check it now.
- @AbapCatalog.sqlViewName: ‘XXXX’ — Auto creates SQL view corresponding to CDS view with given name ‘XXXX’.
- @AbapCatalog.preserveKey: true — Preserves the CDS key fields, note the Keyword ‘Key’ next to EBELN. Preserve key true ignores defaults keys from the database table/view and preserves keys defined explicitly in the View. Possible Values — true/false
- @AccessControl.authorizationCheck: #CHECK — Tells whether an authorization check should be performed or not, how can we perform an authorization check in CDS? Check upcoming blogs. Possible Values — #CHECK, #NOT_ALLOWED, #NOT_REQUIRED, #PRIVLEDGE_ONLY.
- @EndUserText.label: ‘CARDINALITY’ — It's a free text which provides info to the end user. This has its own advantage in FIORI, UI5 apps etc. can be used as field catalogues. 40 Characters is the max length.
- @AbapCatalog.compiler.compareFilter: true — Defines the evaluation of filter conditions in path expressions of the CDS view. Possible Values — true/false
- @ClientDependent: true — Defines Client handling when Open SQL is used to access the CDS View. Is optional and the default is “true”. Possible Values — true/false.
- @AbapCatalog.buffering<…> — Define the buffering of the CDS view in SAP buffering. e.g. @AbapCatalog.buffering.numberoffields, @AbapCatalog.buffering.status, @AbapCatalog.buffering.type.
Semantic Annotations are used to define currency fields and quantity fields of CDS views and CDS table functions. This can refer to as the Currency/Quantity fields tab in SE11.
- @Semantics.amount.CurrencyCode:’CurrencyFieldName/AliasName’
- @Semantics.CurrencyCode: true
- @Semantics.quantity.unitOfMeasure:’UoMFieldName/AliasName’
- @Semantics.unitOfMeasure: true
Example for Semantics Annotations
Note — If you want to use before field/element then refer to line 20 & 21, all annotations above i.e. line 18,19,20 is applicable for field ‘item.kwmeng’. We also use/specify annotations after the field/element refer to line 16. The addition ‘<’ next to ‘@’, allows the use of annotations after elements as well.
We can extract all annotations of the CDS view for analysis or for building a requirement using a class “CL_DD_DDL_ANALYZE” method “GET_ANNOTATIONS”. Check the below code.
Remember I mentioned @EndUserText.label can be used as Field Catalog in FIORI/UI5 apps. If so how will or there will be translations maintained? Yes, we can use the same old SE63 for maintaining translations as shown below.
Go to SE63 > Click on Short Texts > Expand A5 User Interface Text > Double click DDLS CDS Views. Provide CDS View name and provide translations.
We are good to close this blog. Practice and explore annotations, each annotation with its own purpose. See you soon!!!