From 4081ecf02778bfbc34ca56b537a3761ace2d31dd Mon Sep 17 00:00:00 2001 From: danrega <16720986+danrega@users.noreply.github.com> Date: Tue, 17 Dec 2024 11:44:12 +0100 Subject: [PATCH] Update --- 22_Released_ABAP_Classes.md | 513 +++++++++++++++++++++++++++--------- 1 file changed, 393 insertions(+), 120 deletions(-) diff --git a/22_Released_ABAP_Classes.md b/22_Released_ABAP_Classes.md index 6c6ddc8..a006bb0 100644 --- a/22_Released_ABAP_Classes.md +++ b/22_Released_ABAP_Classes.md @@ -10,6 +10,7 @@ - [Transactional Consistency](#transactional-consistency) - [Numbers and Calculations](#numbers-and-calculations) - [String Processing](#string-processing) + - [Handling Codepages and Binary Data](#handling-codepages-and-binary-data) - [Regular Expressions](#regular-expressions) - [Time and Date](#time-and-date) - [Calendar-Related Information](#calendar-related-information) @@ -34,6 +35,7 @@ - [Reading and Writing XLSX Content](#reading-and-writing-xlsx-content) - [Zip Files](#zip-files) - [ABAP Unit](#abap-unit) + - [Units of Measurement](#units-of-measurement) This ABAP cheat sheet contains a selection of [released](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreleased_api_glosry.htm) ABAP classes that are available in [ABAP for Cloud Development](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenabap_for_cloud_dev_glosry.htm). It serves as a quick introduction, along with code snippets to explore the functionality in action. @@ -523,68 +525,7 @@ DATA(random_num2) = cl_abap_random_int=>create( seed = cl_abap_random=>seed( )
CL_ABAP_GZIP* CL_ABAP_GZIP,
-CL_ABAP_GZIP_BINARY_STREAM,
-CL_ABAP_GZIP_TEXT_STREAM,
-CL_ABAP_UNGZIP_BINARY_STREAM,
-CL_ABAP_UNGZIP_TEXT_STREAM
-CL_ABAP_CHAR_UTILITIES CL_ABAP_CONV_CODEPAGE XCO_CP | Class | Details/Code Snippet | +
CL_ABAP_GZIP* |
+
+(De)compressing character strings and byte strings using GZIP:
+CL_ABAP_GZIP,
+CL_ABAP_GZIP_BINARY_STREAM,
+CL_ABAP_GZIP_TEXT_STREAM,
+CL_ABAP_UNGZIP_BINARY_STREAM,
+CL_ABAP_UNGZIP_TEXT_STREAM
++ +``` abap +"------- (De)compressing binary data ------- +DATA(str) = `This is a data object of type string. It should be converted to xstring, compressed and decompressed.`. +DATA(xstr) = cl_abap_conv_codepage=>create_out( )->convert( str ). +DATA xstr_comp TYPE xstring. + +"Compressing binary data +TRY. + cl_abap_gzip=>compress_binary( EXPORTING raw_in = xstr + IMPORTING gzip_out = xstr_comp ). + CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_compression_error. +ENDTRY. + +"Comparing the length of the data objects +DATA(len_xstr) = xstrlen( xstr ). "101 +DATA(len_xstr_comp) = xstrlen( xstr_comp ). "81 + +"Decompressing binary data +DATA xstr_decomp TYPE xstring. +TRY. + cl_abap_gzip=>decompress_binary( EXPORTING gzip_in = xstr_comp + IMPORTING raw_out = xstr_decomp ). + CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_compression_error. +ENDTRY. + +DATA(len_xstr_decomp) = xstrlen( xstr_decomp ). "101 +DATA(conv_str) = cl_abap_conv_codepage=>create_in( )->convert( xstr_decomp ). + +"abap_true +DATA(is_equal) = xsdbool( len_xstr = len_xstr_decomp AND str = conv_str ). + +"------- (De)compressing character strings ------- +DATA zipped TYPE xstring. +TRY. + cl_abap_gzip=>compress_text( EXPORTING text_in = `Hello world` + IMPORTING gzip_out = zipped ). + CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_conversion_codepage cx_sy_compression_error. +ENDTRY. + +DATA txt TYPE string. +TRY. + cl_abap_gzip=>decompress_text( EXPORTING gzip_in = zipped + IMPORTING text_out = txt ). + CATCH cx_parameter_invalid_range cx_sy_buffer_overflow cx_sy_conversion_codepage cx_sy_compression_error. +ENDTRY. + +ASSERT txt = `Hello world`. +``` + + |
+
CL_ABAP_CONV_CODEPAGE |
+
+For handling code pages, converting strings to the binary representation of different code pages and vice versa.
+ + +``` abap +DATA(hi) = `Hello world`. + +"string -> xstring +"Note: UTF-8 is used by default. Here, it is specified explicitly. +TRY. + DATA(conv_xstring) = cl_abap_conv_codepage=>create_out( codepage = `UTF-8` )->convert( hi ). + CATCH cx_sy_conversion_codepage. +ENDTRY. +"48656C6C6F20776F726C64 + +"xstring -> string +DATA(conv_string) = cl_abap_conv_codepage=>create_in( )->convert( conv_xstring ). +"Hello world +``` + + |
+
XCO_CP |
+
+Converting strings to xstrings using a codepage using the XCO Library
+ + +``` abap +"536F6D6520737472696E67 +DATA(xstr) = xco_cp=>string( `Some string` )->as_xstring( xco_cp_character=>code_page->utf_8 )->value. + +"Some string +DATA(str) = xco_cp=>xstring( xstr )->as_string( xco_cp_character=>code_page->utf_8 )->value. +``` + + |
+
XCO_CP |
+
+Processing Base64 representations of raw binary data
+ + +``` abap +DATA(a_string) = `Hello world`. +"string -> xstring +"Result: 48656C6C6F20776F726C64 +DATA(conv_xstring) = xco_cp=>string( a_string + )->as_xstring( xco_cp_character=>code_page->utf_8 + )->value. +"Encoding of raw binary data into its Base64 representation +"Result: SGVsbG8gd29ybGQ= +DATA(raw2base64) = xco_cp=>xstring( conv_xstring + )->as_string( xco_cp_binary=>text_encoding->base64 + )->value. +"Decoding of a Base64 representation into raw binary data +"Result: 48656C6C6F20776F726C64 +DATA(base642raw) = xco_cp=>string( raw2base64 + )->as_xstring( xco_cp_binary=>text_encoding->base64 + )->value. +"xstring -> string +"Result: Hello world +DATA(conv_string_xco) = xco_cp=>xstring( base642raw + )->as_string( xco_cp_character=>code_page->utf_8 + )->value. +``` + + |
+
| Class | Details/Code Snippet | +
CL_UOM_DIM_MAINTENANCE |
+
+
+- Handling dimensions
+- Find more information [here](https://help.sap.com/docs/ABAP_ENVIRONMENT/250515df61b74848810389e964f8c367/8961c2c4cebf457f95fb080a736babdc.html?locale=en-US) and in the class documentation.
+- The example code snippet includes reading dimensions. See the link above for the methods to create, change and delete dimensions.
+
+ + +```abap +CLASS zcl_some_class DEFINITION + PUBLIC + FINAL + CREATE PUBLIC . + + PUBLIC SECTION. + INTERFACES if_oo_adt_classrun. + PROTECTED SECTION. + PRIVATE SECTION. +ENDCLASS. + +CLASS zcl_some_class IMPLEMENTATION. + METHOD if_oo_adt_classrun~main. + + DATA(dimension_inst) = cl_uom_dim_maintenance=>get_instance( ). + + TRY. + dimension_inst->read( EXPORTING dimid = 'TEMP' + IMPORTING dim_st = DATA(dimension_details) ). + + DATA(id) = dimension_details-dimid. + DATA(text) = dimension_details-txdim. + + out->write( data = id name = `id` ). + out->write( data = text name = `text` ). + CATCH cx_uom_error INTO DATA(read_error). + out->write( read_error->get_text( ) ). + ENDTRY. + + out->write( repeat( val = `-` occ = 50 ) ). + +********************************************************************** + + "Reading dimensions based on entries in the released view I_UnitOfMeasureDimension + "Various components and values from the returned structure are output for demonstration purposes. + + SELECT UnitOfMeasureDimension FROM I_UnitOfMeasureDimension INTO TABLE @DATA(dimensions) UP TO 10 ROWS. + + LOOP AT dimensions INTO DATA(wa). + + DATA(dim_inst) = cl_uom_dim_maintenance=>get_instance( ). + TYPES c6 TYPE c LENGTH 6. + TRY. + dim_inst->read( EXPORTING dimid = CONV c6( wa ) + IMPORTING dim_st = DATA(dim_details) ). + + LOOP AT CAST cl_abap_structdescr( cl_abap_typedescr=>describe_by_data( dim_details ) )->components INTO DATA(co). + "Applying string conversion to name and values for output purposes + "Note that many of the components have numeric types. + DATA(comp) = CONV string( co-name ). + DATA(value) = CONV string( dim_details-(comp) ). + + "Outputting only those entries that do not have an initial value + IF value IS NOT INITIAL AND value NP `0*`. + out->write( data = value name = comp ). + ENDIF. + ENDLOOP. + CATCH cx_uom_error INTO DATA(read_err). + out->write( read_err->get_text( ) ). + ENDTRY. + out->write( repeat( val = `-` occ = 20 ) ). + ENDLOOP. + + ENDMETHOD. + +ENDCLASS. +``` + + |
+
CL_UOM_MAINTENANCE |
+
+
+- Handling units of measurement
+- Find more information [here](https://help.sap.com/docs/ABAP_ENVIRONMENT/250515df61b74848810389e964f8c367/238be94930874ed9ba3a3dc6469e99b3.html?locale=en-US) and in the class documentation.
+- The example code snippet includes reading units of measurement. See the link above for the methods to create, change and delete dimensions.
+
+ + +```abap +DATA(unit_mea_inst) = cl_uom_maintenance=>get_instance( ). +TRY. + unit_mea_inst->read( EXPORTING unit = 'S' + IMPORTING unit_st = DATA(unit_mea) ). + + DATA(unit) = unit_mea-unit. + DATA(comm) = unit_mea-commercial. + DATA(tech) = unit_mea-technical. + DATA(iso) = unit_mea-isocode. + DATA(id) = unit_mea-dimid. + DATA(text) = unit_mea-long_text. + + CATCH cx_uom_error INTO DATA(unit_read_error). + DATA(error_msg) = unit_read_error->get_text( ). +ENDTRY. + +"Note: Released view for units of measurement: I_UNITOFMEASURE. +``` + + |
+
CL_UOM_CONVERSION |
+
+
+- Converting units of measurement
+- Find more information [here](https://help.sap.com/docs/ABAP_ENVIRONMENT/250515df61b74848810389e964f8c367/73109c66f397494abfa2bf3608740c12.html?locale=en-US) and in the class documentation.
+- The example code snippet explores unit of measurement conversion. See the link above for more methods available.
+
+ + +```abap +DATA output TYPE decfloat34. + +DATA(conv_unit_inst) = cl_uom_conversion=>create( ). + +conv_unit_inst->unit_conversion_simple( EXPORTING input = CONV decfloat34( '1' ) + round_sign = 'X' + unit_in = 'KG' + unit_out = 'G' + IMPORTING output = output + EXCEPTIONS conversion_not_found = 01 + division_by_zero = 02 + input_invalid = 03 + output_invalid = 04 + overflow = 05 + units_missing = 06 + unit_in_not_found = 07 + unit_out_not_found = 08 ). + +IF sy-subrc = 0. + "1000.000 + DATA(outp) = output. +ELSE. + DATA(subrc) = sy-subrc. +ENDIF. + +TYPES: BEGIN OF s, + input TYPE decfloat34, + round_sign TYPE c LENGTH 1, + unit_in TYPE cl_uom_conversion=>ty_msehi, + unit_out TYPE cl_uom_conversion=>ty_msehi, + END OF s, + tab_type TYPE TABLE OF s WITH EMPTY KEY. + +DATA(tab4conv) = VALUE tab_type( ( input = '1.9876543210' + round_sign = '+' "rounding up + unit_in = 'KG' + unit_out = 'G' ) + ( input = '1.9876543210' + round_sign = '-' "rounding down + unit_in = 'KG' + unit_out = 'G' ) + ( input = '1987.6543210' + round_sign = ' ' "no rounding + unit_in = 'G' + unit_out = 'KG' ) + ( input = '60' + round_sign = 'X' "commercial + unit_in = 'MIN' + unit_out = 'H' ) + ( input = '1' + round_sign = 'X' + unit_in = 'TAG' + unit_out = 'H' ) + ( input = '1' + round_sign = 'X' + unit_in = 'JHR' + unit_out = 'TAG' ) + ( input = '123456' + round_sign = 'X' + unit_in = 'ABC' + unit_out = 'H' ) ). + +LOOP AT tab4conv INTO DATA(conversion). + DATA(conv_inst) = cl_uom_conversion=>create( ). + + conv_inst->unit_conversion_simple( EXPORTING input = conversion-input + round_sign = conversion-round_sign + unit_in = conversion-unit_in + unit_out = conversion-unit_out + IMPORTING output = output + EXCEPTIONS conversion_not_found = 01 + division_by_zero = 02 + input_invalid = 03 + output_invalid = 04 + overflow = 05 + units_missing = 06 + unit_in_not_found = 07 + unit_out_not_found = 08 ). + + IF sy-subrc = 0. + outp = output. + ELSE. + subrc = sy-subrc. + ENDIF. +ENDLOOP. + +*Results: +*1987.655 +*1987.654 +*1.98765 +*1.0 +*24.0 +*365.0 +*7 (error, sy-subrc value) + +"Retrieving mass- and time-related units of measurement using +"a released API +SELECT * + FROM i_unitofmeasure + WHERE unitofmeasuredimension = `TIME` OR unitofmeasuredimension = `MASS` + INTO TABLE @DATA(umea). +``` + + |
+