Odata Helper

Ooooh DATA, der Fiori Datenmotor
Alles zum Thema ODATA Gateway Services, CDS Views

Odata Helper

Postby Tron » Mon Sep 20, 2021 11:39 am

001.png
001.png (47.93 KiB) Viewed 135 times

002.png
002.png (50.5 KiB) Viewed 135 times

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. class ZCL_ODATA_V2_HELPER definition
  2.   public
  3.   final
  4.   create public .
  5.  
  6. public section.
  7.  
  8.   class-methods PARSE_SELECT_FIELDS
  9.     importing
  10.       !I_SELECT type /IWBEP/T_MGW_TECH_FIELD_NAMES
  11.       !I_STRUCT_DESCR type ref to CL_ABAP_STRUCTDESCR
  12.     returning
  13.       value(R_SELECT_FIELDS) type STRING .
  14.   class-methods PARSE_ORDERBY
  15.     importing
  16.       !I_ORDER type /IWBEP/T_MGW_TECH_ORDER
  17.     exporting
  18.       !E_ORDERBY type STRING .
  19.   class-methods GET_DATA_FROM_VIEW
  20.     importing
  21.       !I_KEYS type ANY
  22.       !I_SELECT_OPT type /IWBEP/T_MGW_SELECT_OPTION
  23.       !I_ORDER type /IWBEP/T_MGW_TECH_ORDER optional
  24.       !I_TOP type INT4 optional
  25.       !I_SELECT_FIELDS type /IWBEP/T_MGW_TECH_FIELD_NAMES optional
  26.       !I_PAGING type /IWBEP/S_MGW_PAGING optional
  27.     changing
  28.       !C_DATA type ANY TABLE .
  29.   class-methods TIMESTAMP_TO_DATETIME_SQL
  30.     importing
  31.       !I_SELECT_OPT type /IWBEP/S_COD_SELECT_OPTION
  32.       !I_DATE_NAME type STRING
  33.       !I_TIME_NAME type STRING
  34.       !I_TIMESTAMP_NAME type STRING optional
  35.     changing
  36.       !C_WHERE type RSDS_WHERE_TAB optional .
  37.   class-methods CONVERSION_INPUT
  38.     importing
  39.       !I_TABLENAME type TABNAME
  40.       !I_FIELDNAME type DFIES-FIELDNAME
  41.       !I_VALUE type ANY
  42.     exporting
  43.       !E_VALUE type ANY .
  44.   class-methods CONVERT_IMPORT_STRUC_TO_WHERE
  45.     importing
  46.       !I_STRUCTDESCR type ref to CL_ABAP_STRUCTDESCR
  47.       !I_KEYS type ANY
  48.     exporting
  49.       !E_SQL_WHERE type RSDS_WHERE .
  50.   class-methods EXTRACT_PARAMETER
  51.     importing
  52.       !I_KEYS type ANY
  53.       !I_SELECT_OPT type /IWBEP/T_MGW_SELECT_OPTION
  54.       !I_PARAMETER_NAME type FIELDNAME
  55.     exporting
  56.       !E_PARAMETER type ANY
  57.     raising
  58.       CX_SY_MOVE_CAST_ERROR .
  59.   class-methods GENERATE_WHERE_CLAUSE
  60.     importing
  61.       !I_KEYS type ANY
  62.       !I_SELECT_OPT type /IWBEP/T_MGW_SELECT_OPTION
  63.     exporting
  64.       !E_SQL_WHERE type RSDS_WHERE
  65.       !E_TABLENAME type RSDSTABS-PRIM_TAB
  66.       !E_STRUCT_DESCR type ref to CL_ABAP_STRUCTDESCR
  67.     raising
  68.       CX_SY_MOVE_CAST_ERROR .
  69.   class-methods CONVERT_SELECT_OPT_TO_WHERE
  70.     importing
  71.       !I_SELECT_OPT type /IWBEP/T_MGW_SELECT_OPTION
  72.       !I_TABLENAME type TABNAME
  73.     exporting
  74.       !E_SQL_WHERE type RSDS_WHERE .
  75.   class-methods GET_STRING_FOR_TEXTID
  76.     importing
  77.       value(IV_TDID) type TDID optional
  78.       value(IV_TDNAME) type TDOBNAME optional
  79.       value(IV_OBJECT) type TDOBJECT optional
  80.     exporting
  81.       value(EV_STRING) type STRING .
  82.   class-methods REMOVE_EMPTY_FILTERS
  83.     changing
  84.       !C_SELECT_OPT type /IWBEP/T_MGW_SELECT_OPTION .
  85.   class-methods HANDLE_SORTING
  86.     importing
  87.       value(IM_IT_ORDERBY) type /IWBEP/T_MGW_TECH_ORDER
  88.     changing
  89.       !CH_CT_TABLE type ANY TABLE .
  90.   PROTECTED SECTION.
  91. private section.
  92.  
  93.   class-methods TABLE_IS_OK
  94.     importing
  95.       !I_TABNAME type TABNAME
  96.     returning
  97.       value(R_OK) type ABAP_BOOL .
  98.  
  99.  
  100.  
  101. CLASS ZCL_ODATA_V2_HELPER IMPLEMENTATION.
  102.  
  103.  
  104. * <SIGNATURE>---------------------------------------------------------------------------------------+
  105. * | Static Public Method ZCL_ODATA_V2_HELPER=>CONVERSION_INPUT
  106. * +-------------------------------------------------------------------------------------------------+
  107. * | [--->] I_TABLENAME                    TYPE        TABNAME
  108. * | [--->] I_FIELDNAME                    TYPE        DFIES-FIELDNAME
  109. * | [--->] I_VALUE                        TYPE        ANY
  110. * | [<---] E_VALUE                        TYPE        ANY
  111. * +--------------------------------------------------------------------------------------</SIGNATURE>
  112.   METHOD conversion_input.
  113.     DATA:
  114.       lt_dfies              TYPE TABLE OF dfies,
  115.       ls_dfies              TYPE dfies,
  116.       lv_conversionexit(60) TYPE c.
  117.  
  118.  
  119. *        Convert to internal format, get the needed conversion exit from the field
  120.     CALL FUNCTION 'DDIF_FIELDINFO_GET'
  121.       EXPORTING
  122.         tabname        = i_tablename
  123.         fieldname      = i_fieldname
  124.       TABLES
  125.         dfies_tab      = lt_dfies
  126.       EXCEPTIONS
  127.         not_found      = 1
  128.         internal_error = 2
  129.         OTHERS         = 3.
  130.     IF sy-subrc = 0.
  131.       READ TABLE lt_dfies INTO ls_dfies INDEX 1.
  132.       IF ls_dfies-convexit IS NOT INITIAL.
  133.  
  134. "* Create data element with correct data type for the conversion exit output
  135.         DATA r_elemdescr TYPE REF TO cl_abap_elemdescr.
  136.         if ls_dfies-DOMNAME is not initial.
  137.           r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( ls_dfies-DOMNAME ).
  138.         else.
  139.           r_elemdescr ?= cl_abap_elemdescr=>describe_by_name( ls_dfies-FIELDNAME ).
  140.         endif.
  141.         DATA r_field TYPE REF TO data.
  142.         FIELD-SYMBOLS <fs> TYPE any.
  143.         CREATE DATA r_field TYPE HANDLE r_elemdescr.
  144.         ASSIGN r_field->* TO <fs>.
  145.  
  146.         CONCATENATE 'CONVERSION_EXIT_' ls_dfies-convexit '_INPUT' INTO lv_conversionexit.
  147.  
  148.         CALL FUNCTION lv_conversionexit
  149.           EXPORTING
  150.             input  = i_value
  151.           IMPORTING
  152.             output = <fs>.
  153.         e_value = <fs>.
  154.       else.
  155.         e_value = i_value.
  156.       ENDIF.  "* Conversion exit is not initial
  157.     ELSE.
  158.       RAISE EXCEPTION TYPE Zcx_ddic_type_not_found.
  159.     ENDIF.  "* DDIF_FIELDINFO_GET
  160.  
  161.  
  162.  
  163. * <SIGNATURE>---------------------------------------------------------------------------------------+
  164. * | Static Public Method ZCL_ODATA_V2_HELPER=>CONVERT_IMPORT_STRUC_TO_WHERE
  165. * +-------------------------------------------------------------------------------------------------+
  166. * | [--->] I_STRUCTDESCR                  TYPE REF TO CL_ABAP_STRUCTDESCR
  167. * | [--->] I_KEYS                         TYPE        ANY
  168. * | [<---] E_SQL_WHERE                    TYPE        RSDS_WHERE
  169. * +--------------------------------------------------------------------------------------</SIGNATURE>
  170.   METHOD convert_import_struc_to_where.
  171.     DATA: lt_components TYPE  cl_abap_structdescr=>component_table,
  172.           ls_components TYPE LINE OF cl_abap_structdescr=>component_table,
  173.           lv_fieldname  TYPE string,
  174.           lv_eqpart     TYPE string,
  175.           ls_where      TYPE rsdswhere,
  176.           lv_noand      TYPE boolean VALUE 'X'.
  177.  
  178.     FIELD-SYMBOLS <fs> TYPE any.
  179.  
  180.     lt_components = i_structdescr->get_components( ).
  181.     LOOP AT lt_components INTO ls_components.
  182.       CONCATENATE 'I_KEYS-' ls_components-name INTO lv_fieldname.
  183.       ASSIGN (lv_fieldname) TO <fs>.
  184.       IF <fs> IS assigned and <fs> is not initial.
  185.         CONCATENATE  'eq I_KEYS-' ls_components-name INTO lv_eqpart.
  186.         IF lv_noand = 'X'.
  187.           CONCATENATE '(' ls_components-name lv_eqpart ')' INTO ls_where SEPARATED BY space.
  188.           lv_noand = ''.
  189.         ELSE.
  190.           CONCATENATE 'AND (' ls_components-name lv_eqpart ')' INTO ls_where SEPARATED BY space.
  191.         ENDIF.
  192.         APPEND ls_where TO e_sql_where-where_tab.
  193.         unassign <fs>.
  194.       ENDIF.
  195.     ENDLOOP.
  196.  
  197.  
  198. * <SIGNATURE>---------------------------------------------------------------------------------------+
  199. * | Static Public Method ZCL_ODATA_V2_HELPER=>CONVERT_SELECT_OPT_TO_WHERE
  200. * +-------------------------------------------------------------------------------------------------+
  201. * | [--->] I_SELECT_OPT                   TYPE        /IWBEP/T_MGW_SELECT_OPTION
  202. * | [--->] I_TABLENAME                    TYPE        TABNAME
  203. * | [<---] E_SQL_WHERE                    TYPE        RSDS_WHERE
  204. * +--------------------------------------------------------------------------------------</SIGNATURE>
  205.   METHOD convert_select_opt_to_where.
  206.  
  207.     DATA: ls_select_opt         TYPE /iwbep/s_mgw_select_option.
  208.  
  209.     DATA: lt_field_ranges  TYPE rsds_trange,
  210.           ls_field_ranges  TYPE rsds_range,
  211.           ls_rsds_trange   TYPE rsds_frange,
  212.           lt_where_clauses TYPE rsds_twhere,
  213.           ls_selopt        TYPE rsdsselopt,
  214.           ls_gwsopt        TYPE /iwbep/s_cod_select_option.
  215.  
  216.     ls_field_ranges-tablename = i_tablename.
  217.     LOOP AT i_select_opt INTO ls_select_opt. "* Loop over field names
  218.       CLEAR: ls_rsds_trange-selopt_t.
  219.       ls_rsds_trange-fieldname = ls_select_opt-property.
  220.  
  221.       LOOP AT ls_select_opt-select_options INTO ls_gwsopt . "* Loop over select options for field
  222.         MOVE-CORRESPONDING ls_gwsopt  TO ls_selopt.
  223.  
  224. **        Convert to internal format, get the needed conversion exit from the field
  225.         TRY.
  226.           if ls_gwsopt-high is not initial.
  227.             CALL METHOD ZCL_ODATA_V2_HELPER=>conversion_input
  228.               EXPORTING
  229.                 i_tablename = i_tablename
  230.                 i_fieldname = ls_rsds_trange-fieldname
  231.                 i_value     = ls_gwsopt-high
  232.               IMPORTING
  233.                 e_value     = ls_selopt-high.
  234.           endif.
  235.           if ls_gwsopt-low is not initial.
  236.             CALL METHOD ZCL_ODATA_V2_HELPER=>conversion_input
  237.               EXPORTING
  238.                 i_tablename = i_tablename
  239.                 i_fieldname = ls_rsds_trange-fieldname
  240.                 i_value     = ls_gwsopt-low
  241.               IMPORTING
  242.                 e_value     = ls_selopt-low.
  243.           endif.
  244.           CATCH zcx_ddic_type_not_found.
  245. *           Just continue with the next values - or is there something else we should do?
  246.             CONTINUE.
  247.         ENDTRY.
  248.         APPEND ls_selopt TO ls_rsds_trange-selopt_t.
  249.       ENDLOOP. "* End-Loop over select options for field
  250.       APPEND ls_rsds_trange TO ls_field_ranges-frange_t.
  251.     ENDLOOP. "* End-Loop over field names
  252.  
  253.     APPEND ls_field_ranges TO lt_field_ranges.
  254.  
  255.     CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
  256.       EXPORTING
  257.         field_ranges  = lt_field_ranges
  258.       IMPORTING
  259.         where_clauses = lt_where_clauses.
  260.  
  261.     READ TABLE lt_where_clauses INTO e_sql_where INDEX 1.
  262.  
  263.  
  264.  
  265. * <SIGNATURE>---------------------------------------------------------------------------------------+
  266. * | Static Public Method ZCL_ODATA_V2_HELPER=>EXTRACT_PARAMETER
  267. * +-------------------------------------------------------------------------------------------------+
  268. * | [--->] I_KEYS                         TYPE        ANY
  269. * | [--->] I_SELECT_OPT                   TYPE        /IWBEP/T_MGW_SELECT_OPTION
  270. * | [--->] I_PARAMETER_NAME               TYPE        FIELDNAME
  271. * | [<---] E_PARAMETER                    TYPE        ANY
  272. * | [!CX!] CX_SY_MOVE_CAST_ERROR
  273. * +--------------------------------------------------------------------------------------</SIGNATURE>
  274.   METHOD extract_parameter.
  275.     DATA:
  276.       struc_ref1    TYPE REF TO cl_abap_structdescr,
  277.       lt_components TYPE  cl_abap_structdescr=>component_table,
  278.       ls_components TYPE LINE OF cl_abap_structdescr=>component_table,
  279.       lv_fieldname  TYPE string,
  280.       ls_so         TYPE /iwbep/s_cod_select_option,
  281.       ls_filter     TYPE /iwbep/s_mgw_select_option,
  282.       lv_tablename  TYPE tabname
  283.       .
  284.     FIELD-SYMBOLS <fs> TYPE any.
  285.  
  286. **  Get the type of the I_KEYS
  287.     struc_ref1 ?= cl_abap_structdescr=>describe_by_data( i_keys ).
  288.     lv_tablename = struc_ref1->get_relative_name( ).
  289.     IF ZCL_ODATA_V2_HELPER=>table_is_ok( lv_tablename ) = abap_false.
  290.       RETURN.
  291.     ENDIF.
  292.     IF i_keys IS INITIAL.
  293. ***    Try to find parameter value in select options table
  294.       READ TABLE i_select_opt WITH TABLE KEY property = i_parameter_name INTO ls_filter.
  295.       IF sy-subrc EQ 0.
  296.         LOOP AT ls_filter-select_options INTO ls_so WHERE option = 'EQ'.
  297.           TRY.
  298.               CALL METHOD ZCL_ODATA_V2_HELPER=>conversion_input
  299.                 EXPORTING
  300.                   i_tablename = lv_tablename
  301.                   i_fieldname = i_parameter_name
  302.                   i_value     = ls_so-low
  303.                 IMPORTING
  304.                   e_value     = e_parameter.
  305.             CATCH Zcx_ddic_type_not_found.
  306. *               Just continue processing other entries
  307.           ENDTRY.
  308.         ENDLOOP.
  309.       ENDIF.
  310.  
  311.     ELSE.
  312. ***    Try to find parameter in IMPORT structure
  313.       lt_components = struc_ref1->get_components( ).
  314.       LOOP AT lt_components INTO ls_components WHERE name = i_parameter_name.
  315.         CONCATENATE 'I_KEYS-' ls_components-name INTO lv_fieldname.
  316.         ASSIGN (lv_fieldname) TO <fs>.
  317.         IF <fs> IS NOT INITIAL.
  318.           TRY.
  319.               CALL METHOD ZCL_ODATA_V2_HELPER=>conversion_input
  320.                 EXPORTING
  321.                   i_tablename = lv_tablename
  322.                   i_fieldname = i_parameter_name
  323.                   i_value     = <fs>
  324.                 IMPORTING
  325.                   e_value     = e_parameter.
  326.             CATCH zcx_ddic_type_not_found.
  327.           ENDTRY.
  328.         ENDIF.
  329.       ENDLOOP.
  330.     ENDIF.
  331.  
  332.  
  333.  
  334. * <SIGNATURE>---------------------------------------------------------------------------------------+
  335. * | Static Public Method ZCL_ODATA_V2_HELPER=>GENERATE_WHERE_CLAUSE
  336. * +-------------------------------------------------------------------------------------------------+
  337. * | [--->] I_KEYS                         TYPE        ANY
  338. * | [--->] I_SELECT_OPT                   TYPE        /IWBEP/T_MGW_SELECT_OPTION
  339. * | [<---] E_SQL_WHERE                    TYPE        RSDS_WHERE
  340. * | [<---] E_TABLENAME                    TYPE        RSDSTABS-PRIM_TAB
  341. * | [<---] E_STRUCT_DESCR                 TYPE REF TO CL_ABAP_STRUCTDESCR
  342. * | [!CX!] CX_SY_MOVE_CAST_ERROR
  343. * +--------------------------------------------------------------------------------------</SIGNATURE>
  344.   METHOD generate_where_clause.
  345.  
  346.     DATA: ls_where_clauses TYPE rsds_where,
  347.           struc_ref1       TYPE REF TO cl_abap_structdescr,
  348.           lt_where type rsds_where
  349.           .
  350.  
  351. **  Get the type of the OUT_TABLE. Throws cx_sy_move_cast_error if i_keys is invalid.
  352.         struc_ref1 ?= cl_abap_structdescr=>describe_by_data( i_keys ).
  353.         e_struct_descr = struc_ref1.
  354.         e_tablename = struc_ref1->get_relative_name( ).
  355.  
  356.       IF ZCL_ODATA_V2_HELPER=>table_is_ok( e_tablename ) = abap_false.
  357. * Throw custom exception?
  358.         RETURN.
  359.       ENDIF.
  360.  
  361.  
  362. ** Convert select options to where clause
  363.       CALL METHOD ZCL_ODATA_V2_HELPER=>convert_select_opt_to_where
  364.         EXPORTING
  365.           i_select_opt = i_select_opt
  366.           i_tablename  = e_tablename
  367.         IMPORTING
  368.           e_sql_where  = e_sql_where.
  369.       if e_sql_where is not initial and i_keys is not initial.
  370. **    Create where clause based on Import parameter I_KEYS
  371. **        Append to structure since both filter and primery key were given.
  372. **      This case should only happen with malformed primarykeys, ie. manually added key in function module
  373.         CALL METHOD ZCL_ODATA_V2_HELPER=>convert_import_struc_to_where
  374.           EXPORTING
  375.             i_structdescr = struc_ref1
  376.             i_keys        = i_keys
  377.           IMPORTING
  378.             e_sql_where   = lt_where.
  379.         field-symbols <fs> type RSDSWHERE.
  380.         read table lt_where-WHERE_TAB ASSIGNING <fs> index 1.
  381.         if <fs> is ASSIGNED.
  382.             concatenate 'AND' <fs> into <fs> SEPARATED BY space.
  383.             append LINES OF lt_where-WHERE_TAB to e_sql_where-WHERE_TAB.
  384.         endif.
  385.       elseif i_keys is not initial.
  386.  
  387. **        Read only by primary key (I_KEYS)
  388.         CALL METHOD ZCL_ODATA_V2_HELPER=>convert_import_struc_to_where
  389.           EXPORTING
  390.             i_structdescr = struc_ref1
  391.             i_keys        = i_keys
  392.           IMPORTING
  393.             e_sql_where   = e_sql_where.
  394.       endif.
  395.  
  396.  
  397.  
  398.  
  399. * <SIGNATURE>---------------------------------------------------------------------------------------+
  400. * | Static Public Method ZCL_ODATA_V2_HELPER=>GET_DATA_FROM_VIEW
  401. * +-------------------------------------------------------------------------------------------------+
  402. * | [--->] I_KEYS                         TYPE        ANY
  403. * | [--->] I_SELECT_OPT                   TYPE        /IWBEP/T_MGW_SELECT_OPTION
  404. * | [--->] I_ORDER                        TYPE        /IWBEP/T_MGW_TECH_ORDER(optional)
  405. * | [--->] I_TOP                          TYPE        INT4(optional)
  406. * | [--->] I_SELECT_FIELDS                TYPE        /IWBEP/T_MGW_TECH_FIELD_NAMES(optional)
  407. * | [--->] I_PAGING                       TYPE        /IWBEP/S_MGW_PAGING(optional)
  408. * | [<-->] C_DATA                         TYPE        ANY TABLE
  409. * +--------------------------------------------------------------------------------------</SIGNATURE>
  410.   method GET_DATA_FROM_VIEW.
  411.  
  412.       DATA:
  413.         ls_where_clauses TYPE rsds_where,
  414.         lv_tablename     type TABNAME,
  415.         lv_lang          TYPE dml_text_langu_ui,
  416.         ls_orderby       type string,
  417.         ls_select        type string value '*',
  418.         struc_ref1       TYPE REF TO cl_abap_structdescr,
  419.         lv_top           type int4
  420.         .
  421.  
  422.  
  423.       CALL METHOD zcl_odata_v2_helper=>generate_where_clause
  424.         EXPORTING
  425.           i_keys       = i_keys
  426.           i_select_opt = i_select_opt
  427.         IMPORTING
  428.           e_sql_where  = ls_where_clauses
  429.           e_tablename  = lv_tablename
  430.           e_struct_descr = struc_ref1.
  431.  
  432.       CALL METHOD ZCL_ODATA_V2_HELPER=>PARSE_ORDERBY
  433.         EXPORTING
  434.           I_ORDER   = i_order
  435.         IMPORTING
  436.           E_ORDERBY = ls_orderby
  437.           .
  438.  
  439.       CALL METHOD ZCL_ODATA_V2_HELPER=>PARSE_SELECT_FIELDS
  440.         EXPORTING
  441.           I_SELECT        = i_select_fields
  442.           I_STRUCT_DESCR  = struc_ref1
  443.         RECEIVING
  444.           R_SELECT_FIELDS = ls_select
  445.           .
  446.  
  447.  
  448. *   Determine top
  449.       if i_paging is initial.
  450.         lv_top = i_top.
  451.       else.
  452.         if i_paging-top is initial.
  453.           clear lv_top.
  454.         else.
  455.           lv_top = i_paging-top + i_paging-skip.
  456.         endif.
  457.       endif.
  458.  
  459.  
  460. *  * Select data from table (lv_tablename)
  461.         if ls_orderby is initial.
  462.           SELECT (ls_select) FROM  (lv_tablename)
  463.             up to lv_top rows
  464.              INTO CORRESPONDING FIELDS OF table c_data
  465.                   WHERE (ls_where_clauses-where_tab).
  466.         else.
  467.           SELECT (ls_select) FROM  (lv_tablename)
  468.             INTO CORRESPONDING FIELDS OF table c_data
  469.             up to lv_top rows
  470.                  WHERE (ls_where_clauses-where_tab)
  471.            order by (ls_orderby).
  472.         endif.
  473.  
  474.  
  475. * <SIGNATURE>---------------------------------------------------------------------------------------+
  476. * | Static Public Method ZCL_ODATA_V2_HELPER=>GET_STRING_FOR_TEXTID
  477. * +-------------------------------------------------------------------------------------------------+
  478. * | [--->] IV_TDID                        TYPE        TDID(optional)
  479. * | [--->] IV_TDNAME                      TYPE        TDOBNAME(optional)
  480. * | [--->] IV_OBJECT                      TYPE        TDOBJECT(optional)
  481. * | [<---] EV_STRING                      TYPE        STRING
  482. * +--------------------------------------------------------------------------------------</SIGNATURE>
  483.   METHOD get_string_for_textid.
  484.  
  485. * Use this method to read select text element and convert it to string.
  486.  
  487.     DATA lv_tdid     TYPE tdid.
  488.     DATA lv_tdname   TYPE tdobname.
  489.     DATA lv_object   TYPE tdobject.
  490.     DATA lv_langu    TYPE spras VALUE 'E'.
  491.     DATA lt_tlines   TYPE STANDARD TABLE OF tline.
  492.  
  493.     CLEAR ev_string.
  494.  
  495.     lv_tdid   = iv_tdid.
  496.     lv_tdname = iv_tdname.
  497.     lv_object = iv_object.
  498.  
  499. *   Check that all key fields are filled
  500.     CHECK lv_tdid     IS NOT INITIAL.
  501.     CHECK lv_tdname   IS NOT INITIAL.
  502.     CHECK lv_object   IS NOT INITIAL.
  503.  
  504.  
  505.     CALL FUNCTION 'READ_TEXT'
  506.       EXPORTING
  507.         id                      = lv_tdid
  508.         language                = lv_langu
  509.         name                    = lv_tdname
  510.         object                  = lv_object
  511.       TABLES
  512.         lines                   = lt_tlines
  513.       EXCEPTIONS
  514.         id                      = 1
  515.         language                = 2
  516.         name                    = 3
  517.         not_found               = 4
  518.         object                  = 5
  519.         reference_check         = 6
  520.         wrong_access_to_archive = 7
  521.         OTHERS                  = 8.
  522.  
  523.     IF sy-subrc NE 0.
  524.       RETURN.
  525.     ENDIF.
  526.  
  527.     IF lt_tlines[] IS INITIAL.
  528.       RETURN.
  529.     ENDIF.
  530.  
  531.     CALL FUNCTION 'Z_TEXT_TO_STRING'
  532.       EXPORTING
  533.         it_tline       = lt_tlines
  534.       IMPORTING
  535.         ev_text_string = ev_string.
  536.     .
  537.  
  538. *    call function 'IDMX_DI_TLINE_INTO_STRING'
  539. *      exporting
  540. *        it_tline       = lt_tlines
  541. *      importing
  542. *        ev_text_string = ev_string.
  543.  
  544.  
  545.  
  546. * <SIGNATURE>---------------------------------------------------------------------------------------+
  547. * | Static Public Method ZCL_ODATA_V2_HELPER=>HANDLE_SORTING
  548. * +-------------------------------------------------------------------------------------------------+
  549. * | [--->] IM_IT_ORDERBY                  TYPE        /IWBEP/T_MGW_TECH_ORDER
  550. * | [<-->] CH_CT_TABLE                    TYPE        ANY TABLE
  551. * +--------------------------------------------------------------------------------------</SIGNATURE>
  552.   METHOD handle_sorting.
  553.  
  554.     DATA: lt_sorter  TYPE abap_sortorder_tab,
  555.           ls_sorter  TYPE abap_sortorder,
  556.           ls_orderby TYPE /iwbep/s_mgw_tech_order.
  557.  
  558.     CHECK im_it_orderby IS NOT INITIAL.
  559.  
  560. *  fill sorting table
  561. *  ----------------------------------------------------------------------------
  562.     LOOP AT im_it_orderby INTO ls_orderby.
  563.       ls_sorter-name = ls_orderby-property.
  564.  
  565.       CASE ls_orderby-order.
  566.         WHEN 'desc'.
  567.           ls_sorter-descending = abap_true.
  568.         WHEN OTHERS.
  569.           ls_sorter-descending = abap_false.
  570.       ENDCASE.
  571.  
  572.       APPEND ls_sorter TO lt_sorter.
  573.     ENDLOOP.
  574.  
  575. *  execute sort
  576. *  ----------------------------------------------------------------------------
  577.     SORT ch_ct_table BY (lt_sorter).
  578.  
  579.  
  580.  
  581. * <SIGNATURE>---------------------------------------------------------------------------------------+
  582. * | Static Public Method ZCL_ODATA_V2_HELPER=>PARSE_ORDERBY
  583. * +-------------------------------------------------------------------------------------------------+
  584. * | [--->] I_ORDER                        TYPE        /IWBEP/T_MGW_TECH_ORDER
  585. * | [<---] E_ORDERBY                      TYPE        STRING
  586. * +--------------------------------------------------------------------------------------</SIGNATURE>
  587.   method PARSE_ORDERBY.
  588.     data: ls_order like line of i_order,
  589.           lv_order type string.
  590.     loop at i_order into ls_order.
  591.       if ls_order-order = 'desc'.
  592.         lv_order = 'DESCENDING'.
  593.       else.
  594.         lv_order = 'ASCENDING'.
  595.       endif.
  596.       if e_orderby is not initial.
  597.                 CONCATENATE e_orderby ',' into e_orderby SEPARATED BY space.
  598.       endif.
  599.       CONCATENATE e_orderby ls_order-property lv_order into e_orderby SEPARATED BY space.
  600.     endloop.
  601.  
  602.  
  603.  
  604. * <SIGNATURE>---------------------------------------------------------------------------------------+
  605. * | Static Public Method ZCL_ODATA_V2_HELPER=>PARSE_SELECT_FIELDS
  606. * +-------------------------------------------------------------------------------------------------+
  607. * | [--->] I_SELECT                       TYPE        /IWBEP/T_MGW_TECH_FIELD_NAMES
  608. * | [--->] I_STRUCT_DESCR                 TYPE REF TO CL_ABAP_STRUCTDESCR
  609. * | [<-()] R_SELECT_FIELDS                TYPE        STRING
  610. * +--------------------------------------------------------------------------------------</SIGNATURE>
  611. method PARSE_SELECT_FIELDS.
  612. *  ** Method compares the selection fields with the fields in the table and adds the applicable fields to select string
  613.  
  614.   data: ls_select like line of i_select,
  615.         lt_components TYPE  cl_abap_structdescr=>component_table,
  616.         ls_components TYPE LINE OF cl_abap_structdescr=>component_table.
  617.  
  618.   if i_select is initial.
  619.     r_select_fields = '*'.
  620.   else.
  621.     lt_components = i_struct_descr->get_components( ).
  622.     loop at i_select into ls_select.
  623.       read TABLE lt_components into ls_components with key name = ls_select.
  624.       if sy-subrc = 0.
  625. **        Field found, append it to selected fields
  626.         concatenate r_select_fields ls_select into r_select_fields SEPARATED BY space.
  627.       endif.
  628.     endloop.
  629.   endif.
  630.  
  631.  
  632.  
  633. * <SIGNATURE>---------------------------------------------------------------------------------------+
  634. * | Static Public Method ZCL_ODATA_V2_HELPER=>REMOVE_EMPTY_FILTERS
  635. * +-------------------------------------------------------------------------------------------------+
  636. * | [<-->] C_SELECT_OPT                   TYPE        /IWBEP/T_MGW_SELECT_OPTION
  637. * +--------------------------------------------------------------------------------------</SIGNATURE>
  638.   method REMOVE_EMPTY_FILTERS.
  639. ****  SAP UI5 provides filters with empty value
  640.    data: ls_select_opt like line of c_select_opt,
  641.            ls_select type line of /IWBEP/T_COD_SELECT_OPTIONS.
  642.  
  643.     loop at c_select_opt into ls_select_opt.
  644.       read table ls_select_opt-select_options into ls_select index 1.
  645.       if sy-subrc = 0.
  646.         if ls_select-low is initial and ls_select-high is initial.
  647.           delete c_select_opt.
  648.         endif.
  649.       endif.
  650.     endloop.
  651.  
  652.  
  653.  
  654. * <SIGNATURE>---------------------------------------------------------------------------------------+
  655. * | Static Private Method ZCL_ODATA_V2_HELPER=>TABLE_IS_OK
  656. * +-------------------------------------------------------------------------------------------------+
  657. * | [--->] I_TABNAME                      TYPE        TABNAME
  658. * | [<-()] R_OK                           TYPE        ABAP_BOOL
  659. * +--------------------------------------------------------------------------------------</SIGNATURE>
  660.   method TABLE_IS_OK.
  661.  
  662.           DATA: tb TYPE tabname.
  663.       CHECK i_tabname IS NOT INITIAL.
  664.       SELECT  TABNAME
  665.         FROM DD02L
  666.         INTO TB
  667.         WHERE tabname = i_tabname
  668.         ORDER BY PRIMARY KEY.
  669.            EXIT.
  670.       ENDSELECT.
  671.       IF sy-subrc = 0.
  672.         r_ok = abap_true.
  673.       ENDIF.
  674.  
  675.  
  676. * <SIGNATURE>---------------------------------------------------------------------------------------+
  677. * | Static Public Method ZCL_ODATA_V2_HELPER=>TIMESTAMP_TO_DATETIME_SQL
  678. * +-------------------------------------------------------------------------------------------------+
  679. * | [--->] I_SELECT_OPT                   TYPE        /IWBEP/S_COD_SELECT_OPTION
  680. * | [--->] I_DATE_NAME                    TYPE        STRING
  681. * | [--->] I_TIME_NAME                    TYPE        STRING
  682. * | [--->] I_TIMESTAMP_NAME               TYPE        STRING(optional)
  683. * | [<-->] C_WHERE                        TYPE        RSDS_WHERE_TAB(optional)
  684. * +--------------------------------------------------------------------------------------</SIGNATURE>
  685.   method TIMESTAMP_TO_DATETIME_SQL.
  686.  
  687. *Importing
  688. *  OPTION = GT
  689. *    LOW = 20210111083234
  690.  
  691. * Exporting
  692. * ( DATE = '20210111' AND TIME >= '083234'
  693. * OR DATE > '20210111' )
  694.  
  695.     data: lv_date type dats,
  696.           lv_time type tims,
  697.           lv_timestamp type timestamp,
  698.           lv_esdate type string,
  699.           lv_estime type string,
  700.           lv_opt type c length 2,
  701.           lv_datepart type string,
  702.           lv_where like  line of c_where,
  703.           lv_and like  line of c_where value 'AND'.
  704.  
  705.   lv_opt = i_select_opt-OPTION.
  706.   TRANSLATE lv_opt to UPPER CASE.
  707.  
  708.  
  709. "" surround values with quotes
  710.   lv_timestamp = I_SELECT_OPT-low.
  711.  
  712.   convert TIME STAMP lv_timestamp TIME ZONE sy-zonlo
  713.   into date lv_date time lv_time.
  714.   CONCATENATE '''' lv_date '''' into lv_esdate.
  715.   CONCATENATE '''' lv_time '''' into lv_estime.
  716.  
  717.   case lv_opt.
  718.  
  719.     when 'EQ' OR 'NE'.
  720.       if c_where is not initial.
  721.         append lv_and to c_where.
  722.       endif.
  723.       concatenate '(' i_date_name lv_opt lv_esdate 'AND' i_time_name lv_opt lv_estime ')' into lv_where SEPARATED BY space.
  724.       append lv_where to C_where.
  725.     when 'LT' or 'LE'.
  726.       if c_where is not initial.
  727.         append lv_and to c_where.
  728.       endif.
  729.       concatenate '(' i_date_name 'LT' lv_esdate 'OR' into lv_where SEPARATED BY space.
  730.       append lv_where to c_where.
  731.       concatenate '(' i_date_name 'EQ' lv_esdate into lv_where SEPARATED BY space.
  732.       append lv_where to c_where.
  733.       concatenate 'AND' i_time_name lv_opt lv_estime ')' ')' into lv_where SEPARATED BY space.
  734.       append lv_where to c_where.
  735.     when 'GT' or 'GE'.
  736.       if c_where is not initial.
  737.         append lv_and to c_where.
  738.       endif.
  739.       concatenate '(' i_date_name 'GT' lv_esdate 'OR' into lv_where SEPARATED BY space.
  740.       append lv_where to c_where.
  741.       concatenate '(' i_date_name 'EQ' lv_esdate into lv_where SEPARATED BY space.
  742.       append lv_where to c_where.
  743.       concatenate 'AND' i_time_name lv_opt lv_estime ')' ')' into lv_where SEPARATED BY space.
  744.       append lv_where to c_where.
  745.     when 'BT'.
  746.       "" Not available through odata
  747.  
  748. "" Finally remove the previously mapped timestamp field from the where clause
  749.     delete c_where where line cs i_timestamp_name.
  750.  
GeSHi ©



Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. FUNCTION Z_TEXT_TO_STRING.
  2. *"----------------------------------------------------------------------
  3. *"*"Local Interface:
  4. *"  IMPORTING
  5. *"     REFERENCE(IT_TLINE) TYPE  TABTYPE_TLINE
  6. *"  EXPORTING
  7. *"     REFERENCE(EV_TEXT_STRING) TYPE  STRING
  8. *"----------------------------------------------------------------------
  9.  
  10.  
  11.   types begin of lty_stream.
  12.   types c(65535).
  13.   types end of lty_stream.
  14.  
  15.   data ls_stream type lty_stream.
  16.   data lt_stream type standard table of lty_stream.
  17.  
  18.  
  19.  
  20.   clear ls_stream.
  21.   clear lt_stream.
  22.   refresh lt_stream.
  23.  
  24.   call function 'CONVERT_ITF_TO_STREAM_TEXT'
  25. *   EXPORTING
  26. *     LANGUAGE          = SY-LANGU
  27.     tables
  28.       itf_text          = it_tline
  29.       text_stream       = lt_stream
  30.     .
  31.  
  32.  
  33.   clear ev_text_string.
  34.  
  35.   loop at lt_stream into ls_stream.
  36.     concatenate ev_text_string ls_stream into ev_text_string.
  37.  
GeSHi ©


Vorlage für alle GET_ENTITY und GET_ENTITY_SET basierend auf Mapping mit Function Module:
1.) Baustein Kopieren
2.) Für den zugrunde liegenden View oder Tabelle einen Tabellentyp anlegen (SE11).
3.) Den View/Tabelle unter I_KEYS ändern
4.) Den Tabellentyp unter Exportparameter O_DATA eintragen.
5.) Der Baustein in der SEGW im Mapping verwenden,


003.png
003.png (49.44 KiB) Viewed 135 times

004.png
004.png (37.36 KiB) Viewed 135 times

005.png
005.png (18.35 KiB) Viewed 135 times

Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. FUNCTION Z_ODATA_GET_SCUSTOMER.
  2. *"----------------------------------------------------------------------
  3. *"*"Local Interface:
  4. *"  IMPORTING
  5. *"     VALUE(I_KEYS) TYPE  ZCDS_SCUSTOMER OPTIONAL
  6. *"     VALUE(I_SELECT_OPT) TYPE  /IWBEP/T_MGW_SELECT_OPTION OPTIONAL
  7. *"     VALUE(I_ORDER) TYPE  /IWBEP/T_MGW_TECH_ORDER OPTIONAL
  8. *"     VALUE(I_SELECT_FIELDS) TYPE  /IWBEP/T_MGW_TECH_FIELD_NAMES
  9. *"       OPTIONAL
  10. *"     VALUE(I_PAGING) TYPE  /IWBEP/S_MGW_PAGING OPTIONAL
  11. *"  EXPORTING
  12. *"     VALUE(O_DATA) TYPE  ZCDS_SCUSTOMER_TT
  13. *"----------------------------------------------------------------------
  14.  
  15. *  if i_keys is initial and i_SELECT_OPT is initial and i_paging-top is INITIAL.
  16. *    return.
  17. *  endif.
  18.  
  19.  
  20.   ZCL_ODATA_V2_HELPER=>GET_DATA_FROM_VIEW(
  21.     EXPORTING
  22.       I_KEYS = i_keys
  23.       i_paging = i_paging
  24.       i_order = i_order
  25.       i_SELECT_OPT = i_SELECT_OPT
  26.       I_SELECT_FIELDS = I_SELECT_FIELDS
  27.     changing
  28.       c_data = o_data[]
  29.   ).
  30.  
  31.  
GeSHi ©

ZCL_ODATA_V2_HELPER.7z
(5.64 KiB) Downloaded 12 times
Tron
.....
.....
 
Posts: 1107
Joined: Sat Aug 04, 2007 10:21 pm

Return to ODATA Gateway Services

Who is online

Users browsing this forum: No registered users and 1 guest

cron