forked from shaw1236/sapSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samplebopf.abap
504 lines (429 loc) · 20.1 KB
/
samplebopf.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
** BOPF Media Customer Action CRUD: Create/Change/Display
**
** Purpose: Sample to use BOPF Object (MAM_CUSTOMER)
**
** Author : Simon Li Jul 2017
**
CLASS lcl_bopf_common DEFINITION CREATE PUBLIC.
PROTECTED SECTION.
DATA mo_txn_mngr TYPE REF TO /bobf/if_tra_transaction_mgr.
DATA mo_svc_mngr TYPE REF TO /bobf/if_tra_service_manager.
DATA mo_bo_conf TYPE REF TO /bobf/if_frw_configuration.
METHODS:
constructor RAISING /bobf/cx_frw.
ENDCLASS.
CLASS lcl_bopf_common IMPLEMENTATION.
METHOD constructor.
"Obtain a reference to the BOPF transaction manager:
me->mo_txn_mngr =
/bobf/cl_tra_trans_mgr_factory=>get_transaction_manager( ).
"Obtain a reference to the BOPF service manager:
me->mo_svc_mngr =
/bobf/cl_tra_serv_mgr_factory=>get_service_manager(
/bobf/if_demo_customer_c=>sc_bo_key ).
"Access the metadata for the /BOBF/MAM_CUSTOMER BO:
me->mo_bo_conf =
/bobf/cl_frw_factory=>get_configuration(
/bobf/if_demo_customer_c=>sc_bo_key ).
ENDMETHOD. " METHOD constructor
ENDCLASS.
CLASS lcl_demo DEFINITION inheriting from lcl_bopf_common FINAL CREATE PRIVATE.
PUBLIC SECTION.
CLASS-METHODS:
create_customer IMPORTING iv_customer_id TYPE /bobf/demo_customer_id,
display_customer IMPORTING iv_customer_id TYPE /bobf/demo_customer_id,
change_customer IMPORTING iv_customer_id TYPE /bobf/demo_customer_id.
PRIVATE SECTION.
METHODS:
constructor RAISING /bobf/cx_frw,
display_messages IMPORTING io_message
TYPE REF TO /bobf/if_frw_message,
get_customer_for_id IMPORTING iv_customer_id
TYPE /bobf/demo_customer_id
RETURNING VALUE(rv_customer_key)
TYPE /bobf/conf_key
RAISING /bobf/cx_frw,
get_node_table IMPORTING iv_key TYPE /bobf/conf_key
iv_node_key TYPE /bobf/obm_node_key
iv_edit_mode TYPE /bobf/conf_edit_mode
DEFAULT /bobf/if_conf_c=>sc_edit_read_only
RETURNING VALUE(rr_data) TYPE REF TO data
RAISING /bobf/cx_frw,
get_node_row IMPORTING iv_key TYPE /bobf/conf_key
iv_node_key TYPE /bobf/obm_node_key
iv_edit_mode TYPE /bobf/conf_edit_mode
DEFAULT /bobf/if_conf_c=>sc_edit_read_only
iv_index TYPE i DEFAULT 1
RETURNING VALUE(rr_data) TYPE REF TO data
RAISING /bobf/cx_frw,
get_node_table_by_assoc IMPORTING iv_key TYPE /bobf/conf_key
iv_node_key TYPE /bobf/obm_node_key
iv_assoc_key TYPE /bobf/obm_assoc_key
iv_edit_mode TYPE /bobf/conf_edit_mode
DEFAULT /bobf/if_conf_c=>sc_edit_read_only
RETURNING VALUE(rr_data) TYPE REF TO data
RAISING /bobf/cx_frw,
get_node_row_by_assoc IMPORTING iv_key TYPE /bobf/conf_key
iv_node_key TYPE /bobf/obm_node_key
iv_assoc_key TYPE /bobf/obm_assoc_key
iv_edit_mode TYPE /bobf/conf_edit_mode
DEFAULT /bobf/if_conf_c=>sc_edit_read_only
iv_index TYPE i DEFAULT 1
RETURNING VALUE(rr_data) TYPE REF TO data
RAISING /bobf/cx_frw.
ENDCLASS.
" The key is provided to us via the BO’s constants interface
" (/BOBF/IF_DEMO_CUSTOMER_C)
CLASS lcl_demo IMPLEMENTATION.
METHOD constructor.
super->constructor( ). " Get all the manager references
ENDMETHOD. " METHOD constructor
METHOD display_messages.
"Sanity check:
CHECK io_message IS BOUND.
"Output each of the messages in the collection:
DATA lt_messages TYPE /bobf/t_frw_message_k.
io_message->get_messages( IMPORTING et_message = lt_messages ).
LOOP AT lt_messages ASSIGNING FIELD-SYMBOLS(<ls_message>).
DATA(lv_msg_text) = <ls_message>-message->get_text( ).
WRITE: / lv_msg_text.
ENDLOOP.
ENDMETHOD. " METHOD display_messages
METHOD get_customer_for_id.
"Instantiate the test driver class:
"DATA lo_driver TYPE REF TO lcl_demo.
DATA(lo_driver) = new lcl_demo( ).
"Though we could conceivably lookup the customer using an SQL query,
"the preferred method of selection is a BOPF query:
DATA(lt_parameters) = VALUE /bobf/t_frw_query_selparam(
( attribute_name =
/bobf/if_demo_customer_c=>sc_query_attribute-root-select_by_attributes-customer_id
sign = 'I' option = 'EQ' low = iv_customer_id )
)
DATA lt_customer_keys TYPE /bobf/t_frw_key.
CALL METHOD lo_driver->mo_svc_mngr->query
EXPORTING
iv_query_key =
/bobf/if_demo_customer_c=>sc_query-root-select_by_attributes
it_selection_parameters = lt_parameters
IMPORTING
et_key = lt_customer_keys.
"Return the matching customer's KEY value:
READ TABLE lt_customer_keys INDEX 1 ASSIGNING field-symbols(<customer_key>).
IF sy-subrc EQ 0.
rv_customer_key = <customer_key>-key.
ENDIF.
ENDMETHOD. " METHOD get_customer_for_id
METHOD get_node_table.
"Lookup the node's configuration:
DATA ls_node_conf TYPE /bobf/s_confro_node.
CALL METHOD mo_bo_conf->get_node
EXPORTING
iv_node_key = iv_node_key
IMPORTING
es_node = ls_node_conf.
"Use the node configuration metadata to create the result table:
CREATE DATA rr_data TYPE (ls_node_conf-data_table_type).
FIELD-SYMBOLS <lt_data> TYPE INDEX TABLE.
ASSIGN rr_data->* TO <lt_data>.
"Retrieve the target node:
DATA(lt_key) = VALUE /bobf/t_frw_key( ( key = iv_key ) ).
DATA lo_change TYPE REF TO /bobf/if_tra_change.
DATA lo_message TYPE REF TO /bobf/if_frw_message.
CALL METHOD me->mo_svc_mngr->retrieve
EXPORTING
iv_node_key = iv_node_key
it_key = lt_key
IMPORTING
eo_message = lo_message
eo_change = lo_change
et_data = <lt_data>.
"Check the results:
IF lo_message IS BOUND AND lo_message->check( ) EQ abap_true.
me->display_messages( lo_message ).
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
ENDMETHOD. " METHOD get_node_table
METHOD get_node_row.
"Lookup the node data:
DATA lr_t_data TYPE REF TO data.
lr_t_data =
get_node_table( iv_key = iv_key
iv_node_key = iv_node_key
iv_edit_mode = iv_edit_mode ).
IF lr_t_data IS NOT BOUND.
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
"Try to pull the record at the specified index:
FIELD-SYMBOLS <lt_data> TYPE INDEX TABLE.
ASSIGN lr_t_data->* TO <lt_data>.
FIELD-SYMBOLS <row> TYPE ANY.
READ TABLE <lt_data> INDEX iv_index ASSIGNING <row>.
IF sy-subrc EQ 0.
rr_data = REF #( <row> ).
ELSE.
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
ENDMETHOD. " METHOD get_node_row
METHOD get_node_table_by_assoc.
"Lookup the association metadata to find out more
"information about the target sub-node:
DATA ls_association TYPE /bobf/s_confro_assoc.
CALL METHOD me->mo_bo_conf->get_assoc
EXPORTING
iv_assoc_key = iv_assoc_key
iv_node_key = iv_node_key
IMPORTING
es_assoc = ls_association.
IF ls_association-target_node IS NOT BOUND.
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
"Use the node configuration metadata to create the result table:
DATA ls_node_conf TYPE /bobf/s_confro_node.
ls_node_conf = ls_association-target_node->*.
CREATE DATA rr_data TYPE (ls_node_conf-data_table_type).
FIELD-SYMBOLS <lt_data> TYPE INDEX TABLE.
ASSIGN rr_data->* TO <lt_data>.
"Retrieve the target node:
DATA(lt_key) = VALUE /bobf/t_frw_key( ( key = iv_key ) ).
DATA lo_change TYPE REF TO /bobf/if_tra_change.
DATA lo_message TYPE REF TO /bobf/if_frw_message.
CALL METHOD mo_svc_mngr->retrieve_by_association
EXPORTING
iv_node_key = iv_node_key
it_key = lt_key
iv_association = iv_assoc_key
iv_fill_data = abap_true
IMPORTING
eo_message = lo_message
eo_change = lo_change
et_data = <lt_data>.
"Check the results:
IF lo_message IS BOUND AND lo_message->check( ) EQ abap_true.
display_messages( lo_message ).
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
ENDMETHOD. " METHOD get_node_table_by_assoc
METHOD get_node_row_by_assoc.
"Lookup the node data:
DATA lr_t_data TYPE REF TO data.
lr_t_data =
get_node_table_by_assoc( iv_key = iv_key
iv_node_key = iv_node_key
iv_assoc_key = iv_assoc_key
iv_edit_mode = iv_edit_mode ).
IF lr_t_data IS NOT BOUND.
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
"Try to pull the record at the specified index:
FIELD-SYMBOLS <lt_data> TYPE INDEX TABLE.
ASSIGN lr_t_data->* TO <lt_data>.
FIELD-SYMBOLS <row> TYPE ANY.
READ TABLE <lt_data> INDEX iv_index ASSIGNING <row>.
IF sy-subrc EQ 0.
rr_data = REF #( <row> ).
ELSE.
RAISE EXCEPTION TYPE /bobf/cx_dac.
ENDIF.
ENDMETHOD. " METHOD get_node_row_by_assoc
METHOD create_customer.
"Method-Local Data Declarations:
"Use the BOPF API to create a new customer record:
TRY.
"Instantiate the driver class:
DATA(lo_driver) = new lcl_demo( ).
"Build the 'ROOT' node:
DATA(lr_s_root) = new /bobf/s_demo_customer_hdr_k( ).
lr_s_root->key = /bobf/cl_frw_factory=>get_new_key( ).
lr_s_root->customer_id = iv_customer_id.
lr_s_root->sales_org = 'SALE'.
lr_s_root->cust_curr = 'CAD'.
lr_s_root->address_contry = 'CA'.
lr_s_root->address = '1234 Bloor Street'.
DATA lt_mod TYPE /bobf/t_frw_modification.
FIELD-SYMBOLS <mod> LIKE LINE OF lt_mod.
APPEND INITIAL LINE TO lt_mod ASSIGNING <mod>.
<mod>-node = /bobf/if_demo_customer_c=>sc_node-root.
<mod>-change_mode = /bobf/if_frw_c=>sc_modify_create.
<mod>-key = lr_s_root->key.
<Mod>-data = lr_s_root.
"Build the 'ROOT_TEXT' node:
DATA lr_s_txt TYPE REF TO /bobf/s_demo_short_text_k.
CREATE DATA lr_s_txt.
lr_s_txt->key = /bobf/cl_frw_factory=>get_new_key( ).
lr_s_txt->text = 'Sample Customer Record'.
lr_s_txt->language = sy-langu.
APPEND INITIAL LINE TO lt_mod ASSIGNING <mod>.
<mod>-node = /bobf/if_demo_customer_c=>sc_node-root_text.
<mod>-change_mode = /bobf/if_frw_c=>sc_modify_create.
<mod>-source_node = /bobf/if_demo_customer_c=>sc_node-root.
<mod>-association = /bobf/if_demo_customer_c=>sc_association-root-root_text.
<mod>-source_key = lr_s_root->key.
<mod>-key = lr_s_txt->key.
<mod>-data = lr_s_txt.
"Build the 'ROOT_LONG_TEXT' node:
"If you look at the node type for this node, you'll notice that
"it's a "Delegated Node". In other words, it is defined in terms
"of the /BOBF/DEMO_TEXT_COLLECTION business object. The following
"code accounts for this indirection.
DATA lr_s_txt_hdr TYPE REF TO /bobf/s_demo_longtext_hdr_k.
CREATE DATA lr_s_txt_hdr.
lr_s_txt_hdr->key = /bobf/cl_frw_factory=>get_new_key( ).
APPEND INITIAL LINE TO lt_mod ASSIGNING <mod>.
<mod>-node = /bobf/if_demo_customer_c=>sc_node-root_long_text.
<mod>-change_mode = /bobf/if_frw_c=>sc_modify_create.
<mod>-source_node = /bobf/if_demo_customer_c=>sc_node-root.
<mod>-association =
/bobf/if_demo_customer_c=>sc_association-root-root_long_text.
<mod>-source_key = lr_s_root->key.
<mod>-key = lr_s_txt_hdr->key.
<mod>-data = lr_s_txt_hdr.
"Create the 'CONTENT' node:
DATA lr_s_txt_cont TYPE REF TO /bobf/s_demo_longtext_item_k.
CREATE DATA lr_s_txt_cont.
lr_s_txt_cont->key = /bobf/cl_frw_factory=>get_new_key( ).
lr_s_txt_cont->language = sy-langu.
lr_s_txt_cont->text_type = 'MEMO'.
lr_s_txt_cont->text_content = 'Demo customer created via BOPF API.'.
APPEND INITIAL LINE TO lt_mod ASSIGNING <mod>.
<mod>-node =
lo_driver->mo_bo_conf->query_node(
iv_proxy_node_name = 'ROOT_LONG_TXT.CONTENT' ).
<mod>-change_mode = /bobf/if_frw_c=>sc_modify_create.
<mod>-source_node = /bobf/if_demo_customer_c=>sc_node-root_long_text.
<mod>-source_key = lr_s_txt_hdr->key.
<mod>-key = lr_s_txt_cont->key.
<mod>-data = lr_s_txt_cont.
<mod>-association =
lo_driver->mo_bo_conf->query_assoc(
iv_node_key = /bobf/if_demo_customer_c=>sc_node-root_long_text
iv_assoc_name = 'CONTENT' ).
"
" ==> Action
"
"Create the customer record:
" Once the modification table is filled out, we can call the MODIFY()
" method to insert the record(s). Assuming all is successful, we can
" then commit the transaction by calling the SAVE() method on the
" /BOBF/IF_TRA_TRANSACTION_MANAGER instance. Should any errors occur,
" we can display the error messages using methods of the /BOBF/IF_FRW_MESSAGE
" object reference which is returned from both methods. This is evidenced by
" the simple utility method DISPLAY_MESSAGES() shown below. That’s pretty much
" all there is to it.
DATA lo_change TYPE REF TO /bobf/if_tra_change.
DATA lo_message TYPE REF TO /bobf/if_frw_message.
CALL METHOD lo_driver->mo_svc_mngr->modify
EXPORTING
it_modification = lt_mod
IMPORTING
eo_change = lo_change
eo_message = lo_message.
"Check for errors:
IF lo_message IS BOUND AND lo_message->check( ) EQ abap_true.
lo_driver->display_messages( lo_message ).
RETURN.
ENDIF.
"Apply the transactional changes:
DATA lv_rejected TYPE boole_d.
CALL METHOD lo_driver->mo_txn_mngr->save
IMPORTING
eo_message = lo_message
ev_rejected = lv_rejected.
IF lv_rejected EQ abap_true.
lo_driver->display_messages( lo_message ).
RETURN.
ENDIF.
"If we get to here, then the operation was successful:
WRITE: / 'Customer', iv_customer_id, 'created successfully.'.
CATCH /bobf/cx_frw INTO DATA(lx_bopf_ex).
DATA(lv_err_msg) = lx_bopf_ex->get_text( ).
WRITE: / lv_err_msg.
ENDTRY.
ENDMETHOD. " METHOD create_customer
METHOD display_customer.
"Try to display the selected customer:
TRY.
"Instantiate the test driver class:
DATA(lo_driver) = new lcl_demo( ).
"Lookup the customer's key attribute using a query:
DATA(lv_customer_key) = lo_driver->get_customer_for_id( iv_customer_id ).
"Display the header-level details for the customer:
DATA lr_s_root TYPE REF TO /bobf/s_demo_customer_hdr_k.
lr_s_root ?=
lo_driver->get_node_row(
iv_key = lv_customer_key
iv_node_key = /bobf/if_demo_customer_c=>sc_node-root
iv_index = 1 ).
WRITE: / 'Display Customer', lr_s_root->customer_id.
ULINE.
WRITE: / 'Sales Organization:', lr_s_root->sales_org.
WRITE: / 'Address:', lr_s_root->address.
SKIP.
"Traverse to the ROOT_TEXT node to display the customer short text:
DATA lr_s_text TYPE REF TO /bobf/s_demo_short_text_k.
lr_s_text ?=
lo_driver->get_node_row_by_assoc(
iv_key = lv_customer_key
iv_node_key = /bobf/if_demo_customer_c=>sc_node-root
iv_assoc_key = /bobf/if_demo_customer_c=>sc_association-root-root_text
iv_index = 1 ).
WRITE: / 'Short Text:', lr_s_text->text.
CATCH /bobf/cx_frw INTO data(lx_bopf_ex).
data(lv_err_msg) = lx_bopf_ex->get_text( ).
WRITE: / lv_err_msg.
ENDTRY.
ENDMETHOD. " METHOD display_customer
METHOD change_customer.
"Try to change the address on the selected customer:
TRY.
"Instantiate the test driver class:
data(lo_driver) = new lcl_demo( ).
"Access the customer ROOT node:
DATA lv_customer_key TYPE /bobf/conf_key.
lv_customer_key = lo_driver->get_customer_for_id( iv_customer_id ).
DATA lr_s_root TYPE REF TO /bobf/s_demo_customer_hdr_k.
lr_s_root ?=
lo_driver->get_node_row( iv_key = lv_customer_key
iv_node_key = /bobf/if_demo_customer_c=>sc_node-root
iv_edit_mode = /bobf/if_conf_c=>sc_edit_exclusive
iv_index = 1 ).
"Change the address string on the customer:
lr_s_root->address = '1234 Davenport Ave.'.
DATA lt_mod TYPE /bobf/t_frw_modification.
APPEND INITIAL LINE TO lt_mod ASSIGNING field-symbols(<mod>).
<mod>-node = /bobf/if_demo_customer_c=>sc_node-root.
<mod>-change_mode = /bobf/if_frw_c=>sc_modify_update.
<mod>-key = lr_s_root->key.
<mod>-data = lr_s_root.
"Update the customer record:
DATA lo_change TYPE REF TO /bobf/if_tra_change.
DATA lo_message TYPE REF TO /bobf/if_frw_message.
CALL METHOD lo_driver->mo_svc_mngr->modify
EXPORTING
it_modification = lt_mod
IMPORTING
eo_change = lo_change
eo_message = lo_message.
"Check for errors:
IF lo_message IS BOUND AND lo_message->check( ) EQ abap_true.
lo_driver->display_messages( lo_message ).
RETURN.
ENDIF.
"Apply the transactional changes:
DATA lv_rejected TYPE boole_d.
CALL METHOD lo_driver->mo_txn_mngr->save
IMPORTING
eo_message = lo_message
ev_rejected = lv_rejected.
IF lv_rejected EQ abap_true.
lo_driver->display_messages( lo_message ).
RETURN.
ENDIF.
"If we get to here, then the operation was successful:
WRITE: / 'Customer', iv_customer_id, 'updated successfully.'.
CATCH /bobf/cx_frw INTO DATA(lx_bopf_ex).
DATA(lv_err_msg) = lx_bopf_ex->get_text( ).
WRITE: / lv_err_msg.
ENDTRY.
ENDMETHOD. " METHOD change_customer
ENDCLASS.