Technical Articles
Introducing the XCO Library Cloud Platform edition
Introduction
Starting with CE 2008 the XCO Library is now publicly available on the SAP Cloud Platform ABAP Environment (aka Steampunk) for the first time. It is a completely newly engineered development library for ABAP with the goal to offer a highly intuitive and effective development experience for tasks that go beyond the mere scope of the ABAP programming language.
With this blog post I want to give you a first impression of what working with the XCO Library looks like. A detailed explanation of the overall organization, design principles and available functionality is provided with the official documentation, which can be found at https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/c154dffe892b4d9ea4566722f0bcd5f1.html.
A first impression of the library
The initial CE 2008 shipment of the XCO Library already comes with a wealth of functionality for different areas. To give you an idea of how the XCO Library looks and feels like in action I will provide a simple, integrated example that showcases the following aspects:
- Creating and releasing transports
- Generating ABAP Repository objects
- Searching for and accessing the content of ABAP Repository objects
At first we will have a look at how a new Workbench transport request can be created. Before being able to create a new transport request the transport target which is usually derived from the transport layer of a development package needs to be known. With the XCO Library, we can perform these operations concisely with just the following statements:
DATA(lo_package) = xco_cp_abap_repository=>package->for( 'Z_MY_PACKAGE' ).
DATA(lv_transport_target) = lo_package->read(
)-property-transport_layer->get_transport_target(
)->value.
DATA(lo_transport_request) = xco_cp_cts=>transports->workbench( lv_transport_target
)->create_request( 'My generated transport request' ).
Equipped with a development package and a modifiable transport request we can use the XCO Generation APIs to easily create a new database table with a simple CDS view entity on top of it:
DATA(lo_put_operation) = xco_cp_generation=>environment->dev_system( lo_transport_request->value
)->create_put_operation( ).
" Add the database table to the PUT operation.
DATA(lo_database_table) = lo_put_operation->for-tabl-for-database_table->add_object( 'ZMY_DBT'
)->set_package( lo_package->name
)->create_form_specification( ).
lo_database_table->set_short_description( 'My generated database table' ).
lo_database_table->set_delivery_class( xco_cp_database_table=>delivery_class->l ).
lo_database_table->set_data_maintenance( xco_cp_database_table=>data_maintenance->allowed ).
lo_database_table->add_field( 'KEY_FIELD'
)->set_key_indicator(
)->set_type( xco_cp_abap_dictionary=>built_in_type->char( 30 )
)->set_not_null( ).
" Further fields (including information about foreign keys, search helps, etc.) can be
" added following the same pattern.
" Add the data definition for the CDS view entity to the PUT operation
DATA(lo_data_definition) = lo_put_operation->for-ddls->add_object( 'ZMY_VIEW_ENTITY'
)->set_package( lo_package->name
)->create_form_specification( ).
lo_data_definition->set_short_description( 'My generated view entity' ).
DATA(lo_view_entity) = lo_data_definition->add_view_entity( ).
lo_view_entity->data_source->set_view_entity( 'ZMY_DBT' ).
DATA(lo_key_field) = lo_view_entity->add_field( xco_cp_ddl=>field( 'KEY_FIELD' ) ).
lo_key_field->set_key( )->set_alias( 'keyField' ).
lo_key_field->add_annotation( 'EndUserText.label' )->value->build( )->add_string( 'Key field' ).
lo_put_operation->execute( ).
Executing this coding will result in the following database table and CDS view entity being created (or updated in case they already exist) and activated together in a single mass activation:
Generated database table
Generated view entity
Database tables and data definitions are just two examples of object types that are already supported by the XCO Generation APIs. With the CE 2008 shipment it is possible to generate all the objects that go into a full-fledged RAP business object, starting from domains, data elements and database tables over CDS view entities and behavior definitions all the way up to service definitions and service bindings. An overview of all currently supported object types is available here.
Where the XCO Generation APIs allow you to modify the ABAP Repository by creating, updating or deleting objects, the XCO ABAP Repository APIs can be used to search for objects and read their contents:
DATA(lo_name_constraint) = xco_cp_abap_sql=>constraint->contains_pattern( 'ZMY_%' ).
DATA(lo_name_filter) = xco_cp_abap_repository=>object_name->get_filter( lo_name_constraint ).
" This query will retrieve all database tables whose name starts with ZMY_ that are contained
" in the package denoted by LO_PACKAGE.
DATA(lt_database_tables) = xco_cp_abap_repository=>objects->tabl->database_tables->where( VALUE #(
( lo_name_filter )
) )->in( lo_package )->get( ).
LOOP AT lt_database_tables INTO DATA(lo_database_table).
" LS_CONTENT is a structure comprised of the header attributes of the
" database table such as the delivery class or the technical settings.
DATA(ls_content) = lo_database_table->content( )->get( ).
" LT_FIELDS is a table of objects representing the fields of the database
" table. As with the header information, the field objects can be used to
" access the content of the field (e.g. its type or foreign key).
DATA(lt_fields) = lo_database_table->fields->all->get( ).
ENDLOOP.
Finally, releasing a transport request is as simple as
DATA(lt_transport_tasks) = lo_transport_request->get_tasks( ).
LOOP AT lt_transport_tasks INTO DATA(lo_transport_task).
IF lo_transport_task->get_status( ) EQ xco_cp_transport=>status->modifiable.
lo_transport_task->release( ).
ENDIF.
ENDLOOP.
lo_transport_request->release( ).
Next steps
The simple example presented above is meant to give you a sense of the expressiveness and power that is now available with the XCO Library.
The functionality offered with CE 2008 already ranges far beyond working with objects of the ABAP Repository. Other highlights include APIs for integrating business application logs into your own application logic as well as an already rich set of standard library functionality. This includes in particular the XCO JSON module which offers new ideas of how to work with JSON data in ABAP.
Feel free to try out the XCO Library and post your questions and thoughts in the comments. The XCO Library is still under very active development, so stay tuned for lots more features to come!
Last but not least I can highly recommend the blog from Andre Fischer about the RAP Generator that he has built based on the XCO Library. It allows you to easily generate complete RAP business objects which are defined in a JSON file and showcases what is now possible with the XCO Library on the SAP Cloud Platform ABAP Environment.
Hi Sebastian, thanks a lot for the great blog. This is really a very helpful improvement!
Great stuff Sebastian. This is just amazing.
The XCO libraries are very useful, indeed.
Without them I would not have been able to develop my RAP Generator.
Even better to get a first hand explanation from the product owner.
Thanks, Sebastian.
Will XCO Library be available in the ABAP Platform 2020 as well, so you could use it on prem?
Hi Stehan,
yes, the Cloud Platform edition of the XCO Library will also be available in the ABAP Platform 2020.
I am currently preparing a dedicated blog that will provide an overview of best practices and considerations when using the XCO Library in an On Premise environment.
Best regards,
Sebastian
Hello Sebastian,
Thank you for the Blog.
PS : The library is nicely designed, especially ability for method chaining.
Hi Santhosh,
first of all thanks for the feedback!
While the XCO Library does not provide built-in support for the ODM format its various modules should be combinable to realize such a use case:
in the ABAP Dictionary, over CDS view entities and behavior definitions (and their implementations) to service definitions and bindings (see https://help.sap.com/viewer/65de2977205c403bbc107264b8eccf4b/Cloud/en-US/65a7efb4e3114e2aaaf95dc8612c0945.html)
Best regards,
Sebastian
Hello Sebastian,
we are currently using ABAP Platform 2021 on premise.
Is there a possibility to get (and generate/modify) the local implementation types section of classes via XCO?
Is there possibility to get the content (and generate/modify) of a CDS behaviour definition via XCO?
If not is one of both or even both planned for future releases?
Hi Tobias,
this is possible, indeed.
Checkout the code of my RAP Generator, especially in the branch for on premise 2021.
GitHub - SAP-samples/cloud-abap-rap at On-Premise-2021
Here I generator behavior definitions using the method:
and behavior implementation classes for each definition in method:
when I call
Please note that there are two types of interfaces that have to be used when generating code for ABAP language version standard and ABAP language version 5 .
Therefore I had to implement two different classes zdmo_cl_rap_generator and zdmo_cl_rap_generator_on_prem.
The on_prem version will generate code for ABAP language version standard. If you want to generate language version 5 you have to use zdmo_cl_rap_generator and a package that has this language version.
My new Fiori Elements UI will choose the correct class based on the language version of the package you chose.
cloud-abap-rap/zdmo_cl_rap_generator_on_prem.clas.abap at On-Premise-2021 · SAP-samples/cloud-abap-rap · GitHub
cloud-abap-rap/zdmo_cl_rap_generator.clas.abap at On-Premise-2021 · SAP-samples/cloud-abap-rap · GitHub
Hope this helps.
Kind regards,
Andre
Hello Andre,
yes, that helps a lot. Thank you very much!
Generation works, but I still don't get how the read/get works (f.e. for a BDEF name or a class name).
Read via xco_cp_abap_repository=>object->clas->for( i_classname ) or
xco_cp_abap=>class( i_classname ) or
xco_cp_abap_repository=>object->bdef->for( i_bdef_name ) .
Hello Tobias,
reading the content of a behavior definition is currently not supported. It is however possible to read the content of a class in a structured way. You can refer to https://help.sap.com/products/BTP/65de2977205c403bbc107264b8eccf4b/bc23e26f236042c48171d850e82843b9.html for a detailed code sample.
Best regards,
Sebastian
Hi Sebastian,
Thanks so much for the blog. is it possible to read and copy database tables from remote destination. I realized there is an option to read classes and interfaces from remote destination but not for dictionary objects.
Something like this:
DATA(lt_database_tables) = xco_cp_abap_repository=>objects->tabl->database_tables->all->in( 'ZPACKAGE')->get( io_origin = lo_remote_origin ).
Appreciate your response.
Regards
Kasi
Hi Kasi,
thanks for the feedback! As a matter of fact, the extension of the XCO remote reading functionality to objects of the ABAP Dictionary (in a similar fashion as it is offered for classes and interfaces) is currently under evaluation internally.
Unfortunately, I cannot yet make any statements as to when (or if) this feature might become available, but it is a topic we are looking into.
Best regards,
Sebastian