Snippets
Created by
Petr Plenkov
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 | class ZCL_OS_API definition
public
abstract
final
create public .
public section.
type-pools ABAP .
class-methods SELECT_BY_QUERY
importing
!IO_AGENT type ref to IF_OS_CA_PERSISTENCY
!IS_SELECTION type ANY
!IV_USE_HASH type ABAP_BOOL default ABAP_TRUE
exporting
!ET_DATA type TABLE
changing
!CO_TYPE type ref to CL_ABAP_STRUCTDESCR optional
returning
value(RT_RESULT) type OSREFTAB .
protected section.
private section.
types:
begin of range_ts,
sign type c length 1,
option type c length 2,
low type string,
high type string,
end of range_ts .
types:
begin of result_ts,
checksum type MD5_FIELDS-HASH,
t_result type osreftab,
end of result_ts .
types:
result_tt type hashed table of result_ts with unique key checksum .
types:
begin of HASH_TS ,
agent TYPE REF TO IF_OS_CA_PERSISTENCY,
t_hash type result_tt,
end of hash_ts .
class-data:
mt_hash type hashed table of HASH_TS with unique key agent .
class-methods GET_CHECKSUM
importing
!IV_SELECTION type ANY
returning
value(RV_HASH) type MD5_FIELDS-HASH .
class-methods GET_QUERY_RANGE_VALUE
importing
!IS_RANGE type RANGE_TS
!IO_EXPR type ref to IF_OS_QUERY_EXPR_FACTORY
!IV_NAME type STRING
returning
value(RO_EXPR) type ref to IF_OS_QUERY_FILTER_EXPR .
class-methods GET_QUERY_RANGE
importing
!IT_RANGE type TABLE
!IV_NAME type STRING
!IO_EXPR type ref to IF_OS_QUERY_EXPR_FACTORY
returning
value(RO_EXPR) type ref to IF_OS_QUERY_FILTER_EXPR .
class-methods TO_DATA
importing
!IO_OBJECT type ref to IF_SERIALIZABLE_OBJECT
changing
!CS_DATA type ANY .
class-methods IS_RANGE
importing
!IO_TYPE type ref to CL_ABAP_TABLEDESCR
returning
value(RV_RANGE) type ABAP_BOOL .
class-methods GET_QUERY
importing
!IS_SELECTION type ANY
changing
!CO_TYPE type ref to CL_ABAP_STRUCTDESCR optional
returning
value(RO_QUERY) type ref to IF_OS_QUERY .
ENDCLASS.
CLASS ZCL_OS_API IMPLEMENTATION.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Private Method ZCL_OS_API=>GET_CHECKSUM
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_SELECTION TYPE ANY
* | [<-()] RV_HASH TYPE MD5_FIELDS-HASH
* +--------------------------------------------------------------------------------------</SIGNATURE>
method get_checksum.
try.
" serialize the request
call transformation id source request = iv_selection
result xml data(lv_xml).
call function 'MD5_CALCULATE_HASH_FOR_RAW'
EXPORTING
DATA = lv_xml
IMPORTING
HASH = rv_hash
EXCEPTIONS
INTERNAL_ERROR = 1
OTHERS = 2
.
if sy-subrc <> 0.
" ToDo
endif.
catch cx_transformation_error.
"ToDo
endtry.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Private Method ZCL_OS_API=>GET_QUERY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_SELECTION TYPE ANY
* | [<-->] CO_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR(optional)
* | [<-()] RO_QUERY TYPE REF TO IF_OS_QUERY
* +--------------------------------------------------------------------------------------</SIGNATURE>
method get_query.
data(lo_query) = cl_os_query_manager=>get_query_manager( )->if_os_query_manager~create_query( ).
data(lo_expr) = lo_query->get_expr_factory( ).
if co_type is not bound.
try.
co_type = cast #( cl_abap_typedescr=>describe_by_data( is_selection ) ).
catch cx_sy_move_cast_error.
" ToDo: message
return.
endtry.
endif.
data: lt_and type table of ref to if_os_query_filter_expr.
" for each selection criteria
loop at co_type->get_included_view(
* p_level =
) into data(ls_view).
" parameter or range?
case ls_view-type->kind.
" parameter
when ls_view-type->kind_elem.
field-symbols: <lv_component> type any.
unassign <lv_component>.
assign component ls_view-name of structure is_selection to <lv_component>.
check <lv_component> is assigned.
try.
" goes to and condition
append
lo_expr->create_operator_expr(
i_attr1 = ls_view-name
i_operator = 'EQ'
i_val = conv #( <lv_component> )
) to lt_and.
catch cx_os_query_expr_fact_error. "
endtry.
when ls_view-type->kind_table.
" check: is range?
check is_range( cast #( ls_view-type ) ) eq abap_true.
" must be not initial
field-symbols: <lt_range> type table.
assign component ls_view-name of structure is_selection to <lt_range>.
check <lt_range> is assigned.
check <lt_range> is not initial.
" goes to and condition
append get_query_range(
iv_name = ls_view-name
it_range = <lt_range>
io_expr = lo_expr ) to lt_and.
endcase.
endloop.
" build and conditions
loop at lt_and into data(lo_and).
if sy-tabix eq 1.
data(lo_filter) = lt_and[ 1 ].
else.
lo_filter = lo_expr->create_and_expr(
exporting
i_expr1 = lo_filter
i_expr2 = lo_and
).
endif.
endloop.
lo_query->set_filter_expr( lo_filter ).
ro_query = lo_query.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Private Method ZCL_OS_API=>GET_QUERY_RANGE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IT_RANGE TYPE TABLE
* | [--->] IV_NAME TYPE STRING
* | [--->] IO_EXPR TYPE REF TO IF_OS_QUERY_EXPR_FACTORY
* | [<-()] RO_EXPR TYPE REF TO IF_OS_QUERY_FILTER_EXPR
* +--------------------------------------------------------------------------------------</SIGNATURE>
method get_query_range.
data: lt_and type table of ref to if_os_query_filter_expr,
lt_or type table of ref to if_os_query_filter_expr.
data: ls_range type range_ts.
" .. for each range value
loop at it_range assigning field-symbol(<ls_range>).
move-corresponding exact <ls_range> to ls_range.
" E = AND, I = OR
case ls_range-sign.
when 'E'.
append io_expr->create_not_expr(
get_query_range_value(
is_range = ls_range
io_expr = io_expr
iv_name = iv_name ) ) to lt_and..
when 'I'.
append get_query_range_value(
is_range = ls_range
io_expr = io_expr
iv_name = iv_name ) to lt_or.
endcase.
endloop.
" First of all combine all OR in to a single expression
loop at lt_or into data(lo_or).
if sy-tabix eq 1.
data(lo_filter_or) = lt_or[ 1 ].
else.
lo_filter_or = io_expr->create_or_expr(
exporting
i_expr1 = lo_filter_or
i_expr2 = lo_or
).
endif.
endloop.
" make all or statements as one of ANDs
append lo_filter_or to lt_and.
loop at lt_and into data(lo_and).
if sy-tabix eq 1.
ro_expr = lt_and[ 1 ].
else.
ro_expr = io_expr->create_and_expr(
exporting
i_expr1 = ro_expr
i_expr2 = lo_and
).
endif.
endloop.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Private Method ZCL_OS_API=>GET_QUERY_RANGE_VALUE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_RANGE TYPE RANGE_TS
* | [--->] IO_EXPR TYPE REF TO IF_OS_QUERY_EXPR_FACTORY
* | [--->] IV_NAME TYPE STRING
* | [<-()] RO_EXPR TYPE REF TO IF_OS_QUERY_FILTER_EXPR
* +--------------------------------------------------------------------------------------</SIGNATURE>
method get_query_range_value.
try.
case is_range-option.
" is operator
when
'EQ' or
'NE' or
'LE' or
'LT' or
'GE' or
'GT'
.
ro_expr = io_expr->create_operator_expr(
i_attr1 = iv_name
i_operator = conv #( is_range-option )
i_val = is_range-low
).
" is mask
when 'CP'.
data(lv_pattern) = is_range-low.
replace all occurrences of '*' in lv_pattern with '%'.
ro_expr = io_expr->create_like_expr(
i_attr = iv_name
i_pattern = lv_pattern
* i_not = OSCON_FALSE
).
" is mask with not
when 'NP'.
lv_pattern = is_range-low.
replace all occurrences of '*' in lv_pattern with '%'.
ro_expr = io_expr->create_like_expr(
i_attr = iv_name
i_pattern = lv_pattern
i_not = oscon_true
).
* when 'BT'.
when others.
" not supported
endcase.
catch cx_os_query_expr_fact_error. "
endtry.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Private Method ZCL_OS_API=>IS_RANGE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IO_TYPE TYPE REF TO CL_ABAP_TABLEDESCR
* | [<-()] RV_RANGE TYPE ABAP_BOOL
* +--------------------------------------------------------------------------------------</SIGNATURE>
method IS_RANGE.
CHECK io_type->table_kind eq io_type->tablekind_std AND
io_type->key_defkind eq io_type->KEYDEFKIND_DEFAULT AND
io_type->key eq value ABAP_KEYDESCR_TAB(
( name = 'SIGN')
( name = 'OPTION')
( name = 'LOW')
( name = 'HIGH') ).
rv_range = abap_true.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZCL_OS_API=>SELECT_BY_QUERY
* +-------------------------------------------------------------------------------------------------+
* | [--->] IO_AGENT TYPE REF TO IF_OS_CA_PERSISTENCY
* | [--->] IS_SELECTION TYPE ANY
* | [--->] IV_USE_HASH TYPE ABAP_BOOL (default =ABAP_TRUE)
* | [<---] ET_DATA TYPE TABLE
* | [<-->] CO_TYPE TYPE REF TO CL_ABAP_STRUCTDESCR(optional)
* | [<-()] RT_RESULT TYPE OSREFTAB
* +--------------------------------------------------------------------------------------</SIGNATURE>
method select_by_query.
" check agent
check io_agent is bound.
try.
" get hash by agent
data(lrs_hash) = ref #( mt_hash[ agent = io_agent ] ).
catch cx_sy_itab_line_not_found.
" create agent hash
insert value #( agent = io_agent ) into table mt_hash reference into lrs_hash.
endtry.
" read hash
data(lv_sum) = get_checksum( is_selection ).
check lv_sum is not initial.
try.
rt_result = lrs_hash->t_hash[ checksum = lv_sum ]-t_result.
catch cx_sy_itab_line_not_found.
try.
" get result by using generated method
rt_result = io_agent->get_persistent_by_query(
exporting
" create query by selection criteria
i_query = get_query(
exporting
is_selection = is_selection " Must be structure
changing
co_type = co_type " Runtime Type Services
) " Query
).
" update hash
insert value #(
checksum = lv_sum
t_result = rt_result ) into table lrs_hash->t_hash.
if et_data is requested.
try.
loop at rt_result into data(lo_result).
append initial line to et_data assigning field-symbol(<ls_data>).
to_data(
exporting
io_object = cast #( lo_result ) " Serializable Object
changing
cs_data = <ls_data>
).
endloop.
catch cx_sy_move_cast_error.
endtry.
endif.
catch cx_os_object_not_found. "
catch cx_os_query_error. "
endtry.
endtry.
endmethod.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Private Method ZCL_OS_API=>TO_DATA
* +-------------------------------------------------------------------------------------------------+
* | [--->] IO_OBJECT TYPE REF TO IF_SERIALIZABLE_OBJECT
* | [<-->] CS_DATA TYPE ANY
* +--------------------------------------------------------------------------------------</SIGNATURE>
method TO_DATA.
try.
" Object to asXml:serializable object
call transformation id
source struc = io_object
result xml data(lv_xml).
" asXml:serializable object to asXml:data
call transformation zcw_obj2struc
source xml lv_xml
result xml data(lv_xml_struc).
" asXml:data to data
call transformation id
source xml lv_xml_struc
result struct = cs_data.
catch cx_transformation_error into data(lo_cx).
if 1 eq 2.
" here you can go from the debugger
data(lo_output) = cl_demo_output=>new( ). " <-place the cursor here and press Shift+F12
lo_output->begin_section( 'Object to asXml:serializable object' ).
lo_output->write_xml( xml = lv_xml ).
lo_output->begin_section( 'asXml:serializable object to asXml:data' ).
lo_output->write_xml( xml = lv_xml_struc ).
lo_output->write_text( lo_cx->get_text( ) ).
lo_output->display( ).
endif.
endtry.
endmethod.
ENDCLASS.
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.