forked from abapGit/abapGit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzcl_abapgit_persist_migrate.clas.abap
432 lines (327 loc) · 12.8 KB
/
zcl_abapgit_persist_migrate.clas.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
CLASS zcl_abapgit_persist_migrate DEFINITION PUBLIC CREATE PUBLIC.
PUBLIC SECTION.
CLASS-METHODS: run RAISING zcx_abapgit_exception.
PRIVATE SECTION.
TYPES: BEGIN OF ty_settings_to_migrate,
name TYPE string,
value TYPE string,
END OF ty_settings_to_migrate,
tty_settings_to_migrate TYPE STANDARD TABLE OF ty_settings_to_migrate
WITH NON-UNIQUE DEFAULT KEY.
CONSTANTS c_text TYPE string VALUE 'Generated by abapGit' ##NO_TEXT.
CLASS-METHODS table_create
RAISING
zcx_abapgit_exception .
CLASS-METHODS table_exists
RETURNING
VALUE(rv_exists) TYPE abap_bool .
CLASS-METHODS lock_create
RAISING
zcx_abapgit_exception .
CLASS-METHODS lock_exists
RETURNING
VALUE(rv_exists) TYPE abap_bool .
CLASS-METHODS migrate_settings
RAISING
zcx_abapgit_exception.
CLASS-METHODS migrate_setting
IMPORTING
iv_name TYPE string
CHANGING
ct_settings_to_migrate TYPE tty_settings_to_migrate
ci_document TYPE REF TO if_ixml_document.
CLASS-METHODS distribute_settings_to_users
IMPORTING
it_settings_to_migrate TYPE tty_settings_to_migrate
RAISING
zcx_abapgit_exception.
CLASS-METHODS update_global_settings
IMPORTING
ii_document TYPE REF TO if_ixml_document
RAISING
zcx_abapgit_exception.
CLASS-METHODS read_global_settings_xml
RETURNING
VALUE(rv_global_settings_xml) TYPE string
RAISING
zcx_abapgit_not_found.
CLASS-METHODS get_global_settings_document
RETURNING
VALUE(ri_global_settings_dom) TYPE REF TO if_ixml_document
RAISING
zcx_abapgit_not_found.
ENDCLASS.
CLASS ZCL_ABAPGIT_PERSIST_MIGRATE IMPLEMENTATION.
METHOD distribute_settings_to_users.
DATA: lt_abapgit_users TYPE STANDARD TABLE OF char12
WITH NON-UNIQUE DEFAULT KEY,
ls_user_settings TYPE zif_abapgit_definitions=>ty_s_user_settings,
li_user_persistence TYPE REF TO zif_abapgit_persist_user.
FIELD-SYMBOLS: <lv_user> LIKE LINE OF lt_abapgit_users,
<ls_setting_to_migrate> TYPE zcl_abapgit_persist_migrate=>ty_settings_to_migrate,
<lg_user_specific_setting_val> TYPE data.
" distribute settings to all abapGit users
SELECT value FROM (zcl_abapgit_persistence_db=>c_tabname)
INTO TABLE lt_abapgit_users
WHERE type = zcl_abapgit_persistence_db=>c_type_user.
LOOP AT lt_abapgit_users ASSIGNING <lv_user>.
li_user_persistence = zcl_abapgit_persistence_user=>get_instance( <lv_user> ).
ls_user_settings = li_user_persistence->get_settings( ).
LOOP AT it_settings_to_migrate ASSIGNING <ls_setting_to_migrate>.
ASSIGN COMPONENT <ls_setting_to_migrate>-name
OF STRUCTURE ls_user_settings
TO <lg_user_specific_setting_val>.
ASSERT sy-subrc = 0.
<lg_user_specific_setting_val> = <ls_setting_to_migrate>-value.
ENDLOOP.
li_user_persistence->set_settings( ls_user_settings ).
ENDLOOP.
ENDMETHOD.
METHOD get_global_settings_document.
DATA: lv_global_settings_xml TYPE string.
lv_global_settings_xml = read_global_settings_xml( ).
ri_global_settings_dom = cl_ixml_80_20=>parse_to_document( stream_string = lv_global_settings_xml ).
ENDMETHOD.
METHOD lock_create.
DATA: lv_obj_name TYPE tadir-obj_name,
ls_dd25v TYPE dd25v,
lt_dd26e TYPE STANDARD TABLE OF dd26e WITH DEFAULT KEY,
lt_dd27p TYPE STANDARD TABLE OF dd27p WITH DEFAULT KEY.
FIELD-SYMBOLS: <ls_dd26e> LIKE LINE OF lt_dd26e,
<ls_dd27p> LIKE LINE OF lt_dd27p.
ls_dd25v-viewname = zcl_abapgit_persistence_db=>c_lock.
ls_dd25v-aggtype = 'E'.
ls_dd25v-roottab = zcl_abapgit_persistence_db=>c_tabname.
ls_dd25v-ddlanguage = zif_abapgit_definitions=>c_english.
ls_dd25v-ddtext = c_text.
APPEND INITIAL LINE TO lt_dd26e ASSIGNING <ls_dd26e>.
<ls_dd26e>-viewname = zcl_abapgit_persistence_db=>c_lock.
<ls_dd26e>-tabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd26e>-tabpos = '0001'.
<ls_dd26e>-fortabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd26e>-enqmode = 'E'.
APPEND INITIAL LINE TO lt_dd27p ASSIGNING <ls_dd27p>.
<ls_dd27p>-viewname = zcl_abapgit_persistence_db=>c_lock.
<ls_dd27p>-objpos = '0001'.
<ls_dd27p>-viewfield = 'TYPE'.
<ls_dd27p>-tabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd27p>-fieldname = 'TYPE'.
<ls_dd27p>-keyflag = abap_true.
APPEND INITIAL LINE TO lt_dd27p ASSIGNING <ls_dd27p>.
<ls_dd27p>-viewname = zcl_abapgit_persistence_db=>c_lock.
<ls_dd27p>-objpos = '0002'.
<ls_dd27p>-viewfield = 'VALUE'.
<ls_dd27p>-tabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd27p>-fieldname = 'VALUE'.
<ls_dd27p>-keyflag = abap_true.
CALL FUNCTION 'DDIF_ENQU_PUT'
EXPORTING
name = zcl_abapgit_persistence_db=>c_lock
dd25v_wa = ls_dd25v
TABLES
dd26e_tab = lt_dd26e
dd27p_tab = lt_dd27p
EXCEPTIONS
enqu_not_found = 1
name_inconsistent = 2
enqu_inconsistent = 3
put_failure = 4
put_refused = 5
OTHERS = 6.
IF sy-subrc <> 0.
zcx_abapgit_exception=>raise( 'migrate, error from DDIF_ENQU_PUT' ).
ENDIF.
lv_obj_name = zcl_abapgit_persistence_db=>c_lock.
CALL FUNCTION 'TR_TADIR_INTERFACE'
EXPORTING
wi_tadir_pgmid = 'R3TR'
wi_tadir_object = 'ENQU'
wi_tadir_obj_name = lv_obj_name
wi_set_genflag = abap_true
wi_test_modus = abap_false
wi_tadir_devclass = '$TMP'
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
zcx_abapgit_exception=>raise( 'migrate, error from TR_TADIR_INTERFACE' ).
ENDIF.
CALL FUNCTION 'DDIF_ENQU_ACTIVATE'
EXPORTING
name = zcl_abapgit_persistence_db=>c_lock
EXCEPTIONS
not_found = 1
put_failure = 2
OTHERS = 3.
IF sy-subrc <> 0.
zcx_abapgit_exception=>raise( 'migrate, error from DDIF_ENQU_ACTIVATE' ).
ENDIF.
ENDMETHOD.
METHOD lock_exists.
DATA: lv_viewname TYPE dd25l-viewname.
SELECT SINGLE viewname FROM dd25l INTO lv_viewname
WHERE viewname = zcl_abapgit_persistence_db=>c_lock.
rv_exists = boolc( sy-subrc = 0 ).
ENDMETHOD.
METHOD migrate_setting.
DATA: li_element TYPE REF TO if_ixml_element,
ls_setting_to_migrate LIKE LINE OF ct_settings_to_migrate.
li_element = ci_document->find_from_name( iv_name ).
IF li_element IS BOUND.
" The element is present in the global config.
" Therefore we have to migrate it
ls_setting_to_migrate-name = iv_name.
ls_setting_to_migrate-value = li_element->get_value( ).
INSERT ls_setting_to_migrate INTO TABLE ct_settings_to_migrate.
li_element->remove_node( ).
ENDIF.
ENDMETHOD.
METHOD migrate_settings.
DATA: li_global_settings_document TYPE REF TO if_ixml_document,
lt_settings_to_migrate TYPE tty_settings_to_migrate,
lx_error TYPE REF TO zcx_abapgit_not_found.
" migrate global settings to user specific settings
TRY.
li_global_settings_document = get_global_settings_document( ).
CATCH zcx_abapgit_not_found INTO lx_error.
" No global settings available, nothing todo.
RETURN.
ENDTRY.
migrate_setting(
EXPORTING
iv_name = |MAX_LINES|
CHANGING
ct_settings_to_migrate = lt_settings_to_migrate
ci_document = li_global_settings_document ).
migrate_setting(
EXPORTING
iv_name = |ADT_JUMP_ENABLED|
CHANGING
ct_settings_to_migrate = lt_settings_to_migrate
ci_document = li_global_settings_document ).
IF lines( lt_settings_to_migrate ) > 0.
distribute_settings_to_users( lt_settings_to_migrate ).
update_global_settings( li_global_settings_document ).
ENDIF.
ENDMETHOD.
METHOD read_global_settings_xml.
rv_global_settings_xml = zcl_abapgit_persistence_db=>get_instance( )->read(
iv_type = zcl_abapgit_persistence_db=>c_type_settings
iv_value = '' ).
ENDMETHOD.
METHOD run.
IF table_exists( ) = abap_false.
table_create( ).
ENDIF.
IF lock_exists( ) = abap_false.
lock_create( ).
ENDIF.
migrate_settings( ).
ENDMETHOD.
METHOD table_create.
DATA: lv_rc LIKE sy-subrc,
lv_obj_name TYPE tadir-obj_name,
ls_dd02v TYPE dd02v,
ls_dd09l TYPE dd09l,
lt_dd03p TYPE STANDARD TABLE OF dd03p WITH DEFAULT KEY.
FIELD-SYMBOLS: <ls_dd03p> LIKE LINE OF lt_dd03p.
ls_dd02v-tabname = zcl_abapgit_persistence_db=>c_tabname.
ls_dd02v-ddlanguage = zif_abapgit_definitions=>c_english.
ls_dd02v-tabclass = 'TRANSP'.
ls_dd02v-ddtext = c_text.
ls_dd02v-contflag = 'L'.
ls_dd02v-exclass = '1'.
ls_dd09l-tabname = zcl_abapgit_persistence_db=>c_tabname.
ls_dd09l-as4local = 'A'.
ls_dd09l-tabkat = '1'.
ls_dd09l-tabart = 'APPL1'.
ls_dd09l-bufallow = 'N'.
APPEND INITIAL LINE TO lt_dd03p ASSIGNING <ls_dd03p>.
<ls_dd03p>-tabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd03p>-fieldname = 'TYPE'.
<ls_dd03p>-position = '0001'.
<ls_dd03p>-keyflag = 'X'.
<ls_dd03p>-datatype = 'CHAR'.
<ls_dd03p>-leng = '000012'.
APPEND INITIAL LINE TO lt_dd03p ASSIGNING <ls_dd03p>.
<ls_dd03p>-tabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd03p>-fieldname = 'VALUE'.
<ls_dd03p>-position = '0002'.
<ls_dd03p>-keyflag = 'X'.
<ls_dd03p>-datatype = 'CHAR'.
<ls_dd03p>-leng = '000012'.
APPEND INITIAL LINE TO lt_dd03p ASSIGNING <ls_dd03p>.
<ls_dd03p>-tabname = zcl_abapgit_persistence_db=>c_tabname.
<ls_dd03p>-fieldname = 'DATA_STR'.
<ls_dd03p>-position = '0003'.
<ls_dd03p>-datatype = 'STRG'.
CALL FUNCTION 'DDIF_TABL_PUT'
EXPORTING
name = zcl_abapgit_persistence_db=>c_tabname
dd02v_wa = ls_dd02v
dd09l_wa = ls_dd09l
TABLES
dd03p_tab = lt_dd03p
EXCEPTIONS
tabl_not_found = 1
name_inconsistent = 2
tabl_inconsistent = 3
put_failure = 4
put_refused = 5
OTHERS = 6.
IF sy-subrc <> 0.
zcx_abapgit_exception=>raise( 'migrate, error from DDIF_TABL_PUT' ).
ENDIF.
lv_obj_name = zcl_abapgit_persistence_db=>c_tabname.
CALL FUNCTION 'TR_TADIR_INTERFACE'
EXPORTING
wi_tadir_pgmid = 'R3TR'
wi_tadir_object = 'TABL'
wi_tadir_obj_name = lv_obj_name
wi_set_genflag = abap_true
wi_test_modus = abap_false
wi_tadir_devclass = '$TMP'
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
zcx_abapgit_exception=>raise( 'migrate, error from TR_TADIR_INTERFACE' ).
ENDIF.
CALL FUNCTION 'DDIF_TABL_ACTIVATE'
EXPORTING
name = zcl_abapgit_persistence_db=>c_tabname
auth_chk = abap_false
IMPORTING
rc = lv_rc
EXCEPTIONS
not_found = 1
put_failure = 2
OTHERS = 3.
IF sy-subrc <> 0 OR lv_rc <> 0.
zcx_abapgit_exception=>raise( 'migrate, error from DDIF_TABL_ACTIVATE' ).
ENDIF.
ENDMETHOD.
METHOD table_exists.
DATA: lv_tabname TYPE dd02l-tabname.
SELECT SINGLE tabname FROM dd02l INTO lv_tabname
WHERE tabname = zcl_abapgit_persistence_db=>c_tabname.
rv_exists = boolc( sy-subrc = 0 ).
ENDMETHOD.
METHOD update_global_settings.
DATA: li_ixml TYPE REF TO if_ixml,
lv_settings_xml TYPE string,
li_ostream TYPE REF TO if_ixml_ostream,
li_renderer TYPE REF TO if_ixml_renderer,
li_streamfactory TYPE REF TO if_ixml_stream_factory.
" finally update global settings
" migrated elements are already removed from document
li_ixml = cl_ixml=>create( ).
li_streamfactory = li_ixml->create_stream_factory( ).
li_ostream = li_streamfactory->create_ostream_cstring( lv_settings_xml ).
li_renderer = li_ixml->create_renderer( ostream = li_ostream
document = ii_document ).
li_renderer->render( ).
zcl_abapgit_persistence_db=>get_instance( )->update(
iv_type = zcl_abapgit_persistence_db=>c_type_settings
iv_value = ''
iv_data = lv_settings_xml ).
ENDMETHOD.
ENDCLASS.