diff --git a/04_ABAP_Object_Orientation.md b/04_ABAP_Object_Orientation.md index a99d2a5..9d9d8fc 100644 --- a/04_ABAP_Object_Orientation.md +++ b/04_ABAP_Object_Orientation.md @@ -561,7 +561,7 @@ In the simplest form, methods can have no parameter at all. Apart from that, met - If passing by reference is used, a local data object is not created for the actual parameter. Instead, the procedure is given a [reference](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreference_glosry.htm "Glossary Entry") to the actual parameter during the call and works with the actual parameter itself. - Note that parameters that are input and passed by reference cannot be modified in the procedure. However, the use of a reference is beneficial regarding the performance compared to creating a local data object. -The following example shows a class with a simple method demonstrating the syntax for formal parameter specifications. Complete types are used. +The following example (which anticipates aspects described in the following sections, such as calling methods) shows a class with a simple method demonstrating the syntax for formal parameter specifications. Complete types are used. ```abap CLASS zcl_some_class DEFINITION @@ -725,7 +725,7 @@ ENDCLASS. #### Generic Typing of Formal Parameters -Find more information on generic types in the [Data Types and Data Objects](16_Data_Types_and_Objects.md#generic-types) cheat sheet. +Find more information on generic types in the [Data Types and Data Objects](16_Data_Types_and_Objects.md#generic-types) cheat sheet. The following code snippet anticipates aspects described in the following sections, such as calling methods. ```abap CLASS zcl_some_class DEFINITION @@ -964,7 +964,7 @@ ENDCLASS. - The preferred parameter is implicitly optional, but you should explicitly specify it as `OPTIONAL` or `DEFAULT`, or a warning will be displayed. - When you call a method and specify a single actual parameter without specifying the name of the formal parameter in an assignment, the actual parameter is automatically assigned to the preferred parameter. -The following example shows a simple method with input parameters of type `i` (an addition is performed using the actual parameter values), where one parameter is preferred. The `IS SUPPLIED` addition in `COND` statements checks whether parameters are supplied. The final output shows the preferred parameter assigned automatically when the formal parameter is not specified explicitly. +The following example (which anticipates aspects described in the following sections, such as calling methods) shows a simple method with input parameters of type `i` (an addition is performed using the actual parameter values), where one parameter is preferred. The `IS SUPPLIED` addition in `COND` statements checks whether parameters are supplied. The final output shows the preferred parameter assigned automatically when the formal parameter is not specified explicitly. To try the example out, create a demo class named `zcl_some_class` and paste the code into it. After activation, choose *F9* in ADT to execute the class. The example is set up to display output in the console. The example should display the following: ``` @@ -1066,6 +1066,82 @@ ENDCLASS. instantiated and an instance is created. - Can have `IMPORTING` parameters and raise exceptions. +The following example (which anticipates aspects described in the following sections, such as creating instances of classes) demonstrates static and instance constructors. To try the example out, create a demo class named `zcl_some_class` and paste the code into it. After activation, choose *F9* in ADT to execute the class. The example is set up to display output in the console. + +Notes: +- The example class defines the instance and static constructors. +- The instance constructor includes an optional importing parameter, which static constructors do not allow. If the parameter were not optional, the class would not run with F9, as it could not pass an actual parameter. In the example, the actual parameter indicates the name of the instance created for output purposes. +- The example class creates multiple instances. +- Both instance and static attributes of each instance are accessed and added to an internal table, which is then output. +- The constructor implementations include: + - Instance constructor: + - Stores the current timestamp in an instance attribute. + - Maintains a call counter for the number of times the instance constructor is called, stored in a static attribute to reflect the count per internal session. + - Stores the manually provided instance name in an instance attribute. + - Static constructor: + - Stores the current timestamp in a static attribute. + - Maintains a call counter for static constructor calls, stored in a static attribute. +- The constructors demonstrate: + - The static constructor is called only once, even when multiple class instances are created, leading to a constant `static_timestamp` value and `stat_constr_call_count` value, which remains 1. + - The `instance_timestamp` attribute shows different timestamps for each created instance. + - The static attribute `instance_constr_call_count` increases with each instance. Note that running the class with F9 in ADT also calls the instance and static constructors. Thus, the final `instance_constr_call_count` totals the number of `DO` loop passes plus 1, starting with 2 for `inst1` instead of 1. + + +```abap +CLASS zcl_some_class DEFINITION + PUBLIC + FINAL + CREATE PUBLIC . + + PUBLIC SECTION. + INTERFACES if_oo_adt_classrun. + METHODS constructor IMPORTING text TYPE string OPTIONAL. + CLASS-METHODS class_constructor. + PROTECTED SECTION. + PRIVATE SECTION. + DATA instance_timestamp TYPE utclong. + CLASS-DATA static_timestamp TYPE utclong. + DATA instance_name TYPE string. + CLASS-DATA stat_constr_call_count TYPE i. + CLASS-DATA instance_constr_call_count TYPE i. +ENDCLASS. + +CLASS zcl_some_class IMPLEMENTATION. + METHOD if_oo_adt_classrun~main. + + DATA itab TYPE string_table. + + DO 5 TIMES. + DATA(inst) = NEW zcl_some_class( |inst{ sy-index }| ). + APPEND |-------------- Instance "{ inst->instance_name }" --------------| TO itab. + APPEND |instance_timestamp: { inst->instance_timestamp }| TO itab. + APPEND |static_timestamp: { inst->static_timestamp }| TO itab. + APPEND |instance_constr_call_count: { inst->instance_constr_call_count }| TO itab. + APPEND |stat_constr_call_count: { inst->stat_constr_call_count }| TO itab. + APPEND INITIAL LINE TO itab. + ENDDO. + + out->write( itab ). + ENDMETHOD. + + METHOD class_constructor. + static_timestamp = utclong_current( ). + stat_constr_call_count += 1. + ENDMETHOD. + + METHOD constructor. + instance_timestamp = utclong_current( ). + instance_constr_call_count += 1. + + IF text IS SUPPLIED AND text IS NOT INITIAL. + instance_name = text. + ENDIF. + ENDMETHOD. + +ENDCLASS. +``` + +
#### Example for Method Definitions diff --git a/07_String_Processing.md b/07_String_Processing.md index bd37747..536b718 100644 --- a/07_String_Processing.md +++ b/07_String_Processing.md @@ -2415,7 +2415,7 @@ SPLIT xstr AT blank_xstr INTO TABLE DATA(xstr_tab) IN BYTE MODE. #### SET BIT and GET BIT Statements - Unlike the ABAP statements in the previous section, the following statements are are only for byte string processing: - - [`SET BIT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABAPSET_BIT.html) and . + - [`SET BIT`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABAPSET_BIT.html) - Syntax pattern: `SET BIT pos OF byte_string [TO value].` - In a byte-like data object, the bit is set to 1 by default at a specified position, or to 0 or 1 as specified after `TO`. - Alternatively, you can use the built-in function [`bit-set`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/ABENBIT_FUNCTIONS.html). @@ -2424,7 +2424,7 @@ SPLIT xstr AT blank_xstr INTO TABLE DATA(xstr_tab) IN BYTE MODE. - Reads a bit at a specified position in a byte string into a target data object. -Expand the following collapsible section for example code, which experiments with byte string processing (converting a hexadecimal value to the character-like representation of the binary values by reading bits, getting hexadecimal values from from the character-like representation of the binary values, setting bits). To try it out, create a demo class named `zcl_some_class` and paste the code into it. After activation, choose *F9* in ADT to execute the class. The example is set up to display output in the console. +Expand the following collapsible section for example code, which experiments with byte string processing (converting a hexadecimal value to the character-like representation of the binary values by reading bits, getting hexadecimal values from the character-like representation of the binary values, setting bits). To try it out, create a demo class named `zcl_some_class` and paste the code into it. After activation, choose *F9* in ADT to execute the class. The example is set up to display output in the console.| Variant/Addition | Notes | +
| + +Short form of `COMMIT ENTITIES` + + | +
+
The following snippet shows a create operation. This operation has only
an impact on the database with the `COMMIT ENTITIES` statement.
Triggering the save sequence means that the execution of the statement
triggers the calling of the saver methods available in the saver class
of a behavior implementation. In managed scenarios (except for some
special variants), the saving is done automatically without implementing
-a dedicated saver method.
-``` abap
+a dedicated saver method. Optionally, you can specify the `RESPONSES` addition followed by a data object of type `ABP_BEHV_RESPONSE_TAB` to retrieve information returned by the response parameters of RAP saver methods.
+
+ + +```abap MODIFY ENTITIES OF root_ent ENTITY root_ent CREATE FIELDS ( key_field field1 field2 ) WITH @@ -1629,11 +1648,185 @@ MODIFY ENTITIES OF root_ent COMMIT ENTITIES. +IF sy-subrc <> 0. + ... +ENDIF. + +... "Some modifications + +DATA f_resp TYPE abp_behv_response_tab. +DATA r_resp TYPE abp_behv_response_tab. +"Note: Inline declarations for the data objects are also possible. +COMMIT ENTITIES RESPONSES FAILED f_resp REPORTED r_resp. + IF sy-subrc <> 0. ... ENDIF. ``` + |
+
| + +Long form of `COMMIT ENTITIES` + + | + +
+
+The long syntax form includes the retrieval of information for one or more RAP BO entities. Root entities must be specified.
+
+ + +``` abap +COMMIT ENTITIES + RESPONSE OF zdemo_abap_rap_ro_m + FAILED DATA(f1) + REPORTED DATA(r1) + RESPONSE OF zdemo_abap_rap_draft_m + FAILED DATA(f2) + REPORTED DATA(r2). + +IF sy-subrc <> 0. + ... +ENDIF. +``` + + |
+
| + +Dynamic form of `COMMIT ENTITIES` + + | + +
+
+The dynamic syntax form allows you to retrieve RAP responses from RAP saver methods by dynamically specifying one or more RAP BO root entities.
+
+
+ + +``` abap +DATA: root_name1 TYPE abp_root_entity_name VALUE 'ZDEMO_ABAP_RAP_RO_M', + root_name2 TYPE abp_root_entity_name VALUE 'ZDEMO_ABAP_RAP_DRAFT_M', + failed_resp TYPE abp_behv_response_tab. + +DATA(dyn_tab) = VALUE abp_entity_name_tab( ( root_name1 ) + ( root_name2 ) ). + +COMMIT ENTITIES RESPONSES OF dyn_tab + FAILED failed_resp + REPORTED DATA(reported_resp). + +IF sy-subrc <> 0. + ... +ENDIF. +``` + + |
+
| + +Specifying a commit scope + + | + +
+
+- Especially used in late numbering scenarios to get the final keys from the preliminary keys (using the [`CONVERT KEY`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapconvert_key.htm) addition)
+- The commit scope is opened by `COMMIT ENTITIES BEGIN ...` (`...` stands for further syntax options such as the specification of `RESPONSES ...` or `RESPONSE OF ...` ), and closed by `COMMIT ENTITIES END.`.
+
+
+ + +``` abap +COMMIT ENTITIES BEGIN. + +... + +"CONVERT KEY OF some_bo FROM ...-%pid TO DATA(some_key). + +"For example, converting the preliminary keys (e.g. %pid components) +"based on entries in the MAPPED table of a RAP BO entity. +"Using a CONVERT KEY statement, you can retrieve the final keys of +"individual instances, and store them in a local variable. + +COMMIT ENTITIES END. +``` + + |
+
| + +Simulating a commit + + | + +
+
+- The optional `IN SIMULATION MODE` addition can be specified with `COMMIT ENTITIES` statements allowing a RAP transaction to be checked.
+- With this addition, only the RAP early save phase is processed without saving data, i.e. a final `COMMIT WORK` is not performed.
+- The addition can be used to check the consistency of the transaction by evaluating, for example, the `sy-subrc` value as a result of executing the commit in simulation mode.
+
+
+ + +``` abap +DELETE FROM zdemo_abap_rapt1. + +DO 2 TIMES. + MODIFY ENTITIES OF zdemo_abap_rap_ro_m + ENTITY root + CREATE FIELDS ( key_field ) + AUTO FILL CID WITH VALUE #( ( key_field = 100 ) ) + MAPPED DATA(m1) + FAILED DATA(f1) + REPORTED DATA(r1). + + IF sy-index = 1. + COMMIT ENTITIES IN SIMULATION MODE. + ELSE. + COMMIT ENTITIES. + ENDIF. + + IF sy-subrc <> 0. + ... + ENDIF. + +"Data is not saved + SELECT FROM zdemo_abap_rapt1 + FIELDS key_field + ORDER BY key_field + INTO TABLE @DATA(tab). + + IF sy-index = 1. + ASSERT tab IS INITIAL. + "Clearing the transactional buffer + ROLLBACK ENTITIES. + ELSE. + ASSERT lines( tab ) = 1. + ASSERT tab[ 1 ]-key_field = 100. + ENDIF. +ENDDO. +``` + + |
+
CL_ABAP_PARALLEL CL_ABAP_PARALLEL class. For more information, refer to the class documentation.
+For performing the parallel processing for instances of ABAP Objects. For more information, refer to the class documentation.