Method to check Value against fixed values and value table

A method that checks values against the fixed values or value table of a data element.

  method check_if_value_fits_to_domval.
    types: begin of ty_buffer,
             rollname type rollname,
             value    type string,
             valid    type abap_bool,
           end of ty_buffer,
           tty_buffer type hashed table of ty_buffer with unique key rollname value.

    statics: st_buffer type tty_buffer.

    data: ls_dom         type dd01v,
          lv_field_value type char10 ##needed.

    field-symbols: <ls_buffer> like line of st_buffer.

    try.
        r_does_fit = st_buffer[ rollname = rollname
                                value = value ]-valid.
        "we can exit the method if the combination was found in the buffer.
        return.
      catch cx_sy_itab_line_not_found.
        "create new buffer entry.
        insert value #( rollname = rollname
                        value = value ) into table st_buffer assigning <ls_buffer>.
        "and we assume the value is valid.
        <ls_buffer>-valid = abap_true.
    endtry.

    try.
        data(lr_elem) = cast cl_abap_elemdescr( cl_abap_typedescr=>describe_by_name( rollname ) ).
        data(lt_fixed_values) = lr_elem->get_ddic_fixed_values( ).

        "check against fixed values of the domain.
        if lt_fixed_values is not initial and not line_exists( lt_fixed_values[ low = value ] ).
          <ls_buffer>-valid = abap_false.
          r_does_fit = <ls_buffer>-valid.
          return.
        endif.

        data(l_dfies) = lr_elem->get_ddic_field( ).
        call function 'DDIF_DOMA_GET'
          exporting
            name          = l_dfies-domname
*           STATE         = 'A'
*           LANGU         = ' '
          importing
*           GOTSTATE      =
            dd01v_wa      = ls_dom
*         TABLES
*           DD07V_TAB     =
          exceptions
            illegal_input = 1
            others        = 2.
        if sy-subrc <> 0.
        endif.

        "there is a value table in the domain.
        if ls_dom-entitytab is not initial.
          data(lr_struc) = cast cl_abap_structdescr( cl_abap_typedescr=>describe_by_name( ls_dom-entitytab ) ).
          data(lt_fields) = lr_struc->get_ddic_field_list( ).

          "determine key field
          loop at lt_fields into data(ls_key_field)
            where keyflag = abap_true
              and datatype <> 'CLNT'.
            exit.
          endloop.

          "if we didn't find anything the table may have just the client as key.
          if sy-subrc <> 0.
            loop at lt_fields into ls_key_field
              where keyflag = abap_true.
              exit.
            endloop.
          endif.

          "key fields determined
          if ls_key_field is not initial.

            "create the WHERE clause
            data(l_where) = |{ ls_key_field-fieldname } = '{ value }'|.

            "and select from the DB.
            try.
                select single (ls_key_field-fieldname) into lv_field_value
                  from (ls_dom-entitytab)
                 where (l_where).

                "if we didn't find anything
                "set the valid flag and exit.
                if sy-subrc <> 0.
                  <ls_buffer>-valid = abap_false.
                  r_does_fit = <ls_buffer>-valid.
                  return.
                endif.
              catch cx_sy_dynamic_osql_error ##no_handler.
            endtry.
          endif.
        endif.
      catch cx_sy_move_cast_error ##no_handler.
    endtry.

    r_does_fit = <ls_buffer>-valid.
  endmethod.