[ABAP] Transport von Kopien aus Transportauftrag anlegen und Aufgaben/Objekte übernehmen

* Siehe Typgruppe SCTSC (Konstanten CTS)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl2 FOR FIELD p_tar.
  PARAMETERS: p_tar TYPE trheader-tarsystem OBLIGATORY VALUE CHECK DEFAULT 'XYZ'.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.
  lbl2 = 'Transportziel:'.

START-OF-SELECTION.

  DATA: lv_req TYPE trwbo_request.

* (Nach-)Lesen von Einzelbestandteilen eines Auftrags
  CALL FUNCTION 'TR_READ_REQUEST'
    EXPORTING
      iv_read_e070     = abap_true
      iv_read_e07t     = abap_true
      iv_trkorr        = p_trkorr
    CHANGING
      cs_request       = lv_req
    EXCEPTIONS
      error_occured    = 1
      no_authorization = 2
      OTHERS           = 3.

  IF sy-subrc = 0.

    WRITE: / 'Kopfdaten des Auftrages', p_trkorr, 'gelesen.'.

* Text übernehmen
    DATA(lv_as4text) = CONV as4text( |TvK:{ lv_req-h-as4text }| ).
    DATA: lv_tvk TYPE trwbo_request_header.

* Auftrag mit Aufgaben anlegen (ohne Dialog)
    CALL FUNCTION 'TR_INSERT_REQUEST_WITH_TASKS'
      EXPORTING
        iv_type            = sctsc_type_transport " TvK anlegen
        iv_text            = lv_as4text
        iv_owner           = sy-uname
        iv_target          = p_tar
        iv_with_badi_check = abap_true
      IMPORTING
        es_request_header  = lv_tvk
      EXCEPTIONS
        insert_failed      = 1
        enqueue_failed     = 2
        OTHERS             = 3.

    IF sy-subrc = 0.

      WRITE: / 'Ziel-Auftrag', lv_tvk-trkorr, 'angelegt.'.

* Quell-Auftrag kopieren
* Kopieren von Aufträgen/Aufgaben
      CALL FUNCTION 'TR_COPY_COMM'
        EXPORTING
          wi_dialog                = abap_false
          wi_trkorr_from           = p_trkorr
          wi_trkorr_to             = lv_tvk-trkorr
          wi_without_documentation = abap_true
        EXCEPTIONS
          db_access_error          = 1
          trkorr_from_not_exist    = 2
          trkorr_to_is_repair      = 3
          trkorr_to_locked         = 4
          trkorr_to_not_exist      = 5
          trkorr_to_released       = 6
          user_not_owner           = 7
          no_authorization         = 8
          wrong_client             = 9
          wrong_category           = 10
          object_not_patchable     = 11
          OTHERS                   = 12.

      IF sy-subrc = 0.

        WRITE: / 'Objekte des Quell-Auftrages', p_trkorr, 'nach', lv_tvk-trkorr, 'kopiert'.

        DATA(lv_ok) = abap_true.

* Quell-Aufgaben kopieren
        SELECT FROM e070
          FIELDS trkorr
          WHERE strkorr = @p_trkorr
          INTO TABLE @DATA(it_trkorr_aufg).

        WRITE: / lines( it_trkorr_aufg ), 'Aufgaben des Quell-Auftrages', p_trkorr, 'gelesen.'.

* alle Quell-Aufgaben des Quell-Auftrages
        LOOP AT it_trkorr_aufg ASSIGNING FIELD-SYMBOL(<a>).
* Kopieren von Aufträgen/Aufgaben
          CALL FUNCTION 'TR_COPY_COMM'
            EXPORTING
              wi_dialog                = abap_false
              wi_trkorr_from           = <a>-trkorr
              wi_trkorr_to             = lv_tvk-trkorr
              wi_without_documentation = abap_true
            EXCEPTIONS
              db_access_error          = 1
              trkorr_from_not_exist    = 2
              trkorr_to_is_repair      = 3
              trkorr_to_locked         = 4
              trkorr_to_not_exist      = 5
              trkorr_to_released       = 6
              user_not_owner           = 7
              no_authorization         = 8
              wrong_client             = 9
              wrong_category           = 10
              object_not_patchable     = 11
              OTHERS                   = 12.
          IF sy-subrc = 0.
            WRITE: / 'Aufgabe',<a>-trkorr,'des Quell-Auftrages', p_trkorr, 'nach', lv_tvk-trkorr, 'kopiert'.
          ELSE.
            lv_ok = abap_false.
            WRITE: / 'Fehler (', sy-subrc, ') beim Kopieren der Quell-Aufgabe:', <a>-trkorr.
            EXIT.
          ENDIF.
        ENDLOOP.

        IF lv_ok = abap_true.
* Objekte und Schlüssel eines Auftrags sortieren
          CALL FUNCTION 'TR_SORT_AND_COMPRESS_COMM'
            EXPORTING
              iv_trkorr                      = lv_tvk-trkorr
            EXCEPTIONS
              trkorr_not_found               = 1
              order_released                 = 2
              error_while_modifying_obj_list = 3
              tr_enqueue_failed              = 4
              no_authorization               = 5
              OTHERS                         = 6.

          IF sy-subrc = 0.
            WRITE: / 'Ziel-Transportauftrag', lv_tvk-trkorr, 'erfolgreich sortiert und verdichtet.'.
          ELSE.
            WRITE: / 'Fehler (', sy-subrc, ') beim Verdichten von Transportauftrag:', lv_tvk-trkorr.
          ENDIF.

        ELSE.
          WRITE: / 'Fehler beim Kopieren der Quell-Aufgaben in Transportauftrag:', lv_tvk-trkorr.
        ENDIF.

      ELSE.
        WRITE: / 'Fehler (', sy-subrc, ') beim Lesen des Quell-Auftrages:', p_trkorr.
      ENDIF.

    ELSE.
      WRITE: / 'Fehler (', sy-subrc, ') beim Anlegen des Ziel-Auftrages'.
    ENDIF.

  ELSE.
    WRITE: / 'Fehler (', sy-subrc, ') beim Lesen des Quell-Auftrages:', p_trkorr.
  ENDIF.

[ABAP] Transportauftrag prüfen

* Siehe Typgruppe SCTSC (Konstanten CTS)

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.

  DATA: lv_req TYPE trwbo_request.

* (Nach-)Lesen von Einzelbestandteilen eines Auftrags
  CALL FUNCTION 'TR_READ_REQUEST'
    EXPORTING
      iv_read_e070     = abap_true
      iv_read_e07t     = abap_true
      iv_trkorr        = p_trkorr
    CHANGING
      cs_request       = lv_req
    EXCEPTIONS
      error_occured    = 1
      no_authorization = 2
      OTHERS           = 3.

  IF sy-subrc = 0.

    DATA: lv_messages TYPE ctsgerrmsgs.

* Prüfe kompletten Auftrag
    CALL FUNCTION 'TR_CHECK_REQUEST'
      EXPORTING
        is_request           = lv_req
        iv_check_lockability = abap_true
        iv_collect_mode      = abap_true
      IMPORTING
        et_messages          = lv_messages
      EXCEPTIONS
        header_error         = 1
        attribute_error      = 2
        obj_or_key_error     = 3
        OTHERS               = 4.

    IF sy-subrc = 0.
      cl_demo_output=>write_data( lv_messages ).
      cl_demo_output=>display( ).
    ENDIF.
  ENDIF.

[ABAP] Transportauftrag als ZIP-Datei speichern

SELECTION-SCREEN BEGIN OF LINE.
  SELECTION-SCREEN COMMENT (20) lbl1 FOR FIELD p_trkorr.
  PARAMETERS: p_trkorr LIKE e070-trkorr OBLIGATORY.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.

  lbl1 = 'Transport-Auftrag:'.

START-OF-SELECTION.
  TRY.
      DATA: lv_req TYPE trwbo_request.

* (Nach-)Lesen von Einzelbestandteilen eines Auftrags
      CALL FUNCTION 'TR_READ_REQUEST'
        EXPORTING
          iv_read_e070     = abap_true
          iv_read_e07t     = abap_true
          iv_trkorr        = p_trkorr
        CHANGING
          cs_request       = lv_req
        EXCEPTIONS
          error_occured    = 1
          no_authorization = 2
          OTHERS           = 3.

      IF sy-subrc = 0.
*D Änderbar
*L Änderbar, geschützt
*O Freigabe gestartet
*R Freigegeben
*N Freigegeben (Importschutz für reparierte Objekte aktiv)
        IF lv_req-h-trstatus = 'R'.

          DATA: lv_trans_cofile_dir TYPE stpa-file.
          DATA: lv_trans_data_dir TYPE stpa-file.

* intern: Pfade der Transportverzeichnisse
          CALL FUNCTION 'TRINT_FI_GET_DIRECTORY_NAMES'
            EXPORTING
              iv_dir_type          = 'T'
            IMPORTING
              ev_data_dir          = lv_trans_data_dir
              ev_cofiles_dir       = lv_trans_cofile_dir
            EXCEPTIONS
              get_dir_trans_failed = 1
              build_path_failed    = 2
              OTHERS               = 3.

          IF sy-subrc = 0.
* Name
            DATA(lv_syst) = |{ p_trkorr(3) }|.
            DATA(lv_num) = |{ p_trkorr+4 }|.
            DATA(lv_name) = |{ lv_num }.{ lv_syst }|.

* Cofile lesen
            DATA: lv_xstr_cofile TYPE xstring.
            DATA(lv_cofile) = |{ lv_trans_cofile_dir }/K{ lv_name }|.

            OPEN DATASET lv_cofile FOR INPUT IN BINARY MODE.
            READ DATASET lv_cofile INTO lv_xstr_cofile.
            CLOSE DATASET lv_cofile.

* Data lesen
            DATA: lv_xstr_data TYPE xstring.
            DATA(lv_data) = |{ lv_trans_data_dir }/R{ lv_name }|.

            OPEN DATASET lv_data FOR INPUT IN BINARY MODE.
            READ DATASET lv_data INTO lv_xstr_data.
            CLOSE DATASET lv_data.

* Zip
            DATA(o_zip) = NEW cl_abap_zip( ).

            o_zip->add( name    = |K{ lv_name }|
                        content = lv_xstr_cofile ).

            o_zip->add( name    = |R{ lv_name }|
                        content = lv_xstr_data ).

            DATA(lv_xstr_zip) = o_zip->save( ).

* SOLIX
            DATA(it_raw_data) = cl_bcs_convert=>xstring_to_solix( EXPORTING iv_xstring = lv_xstr_zip ).

            DATA: lv_action TYPE i.
            DATA: lv_filename TYPE string.
            DATA: lv_fullpath TYPE string.
            DATA: lv_path TYPE string.

            DATA(lv_default) = |{ lv_req-h-as4text }_{ p_trkorr }|.

* alle Vorkommen, die nicht [a-zA-Z0-9_] entsprechen, durch '_' ersetzen
            REPLACE ALL OCCURRENCES OF REGEX '[^\w]+' IN lv_default WITH '_'.

* Save-Dialog
            cl_gui_frontend_services=>file_save_dialog( EXPORTING default_file_name = lv_default
                                                                  default_extension = 'zip'
                                                                  file_filter       = |{ cl_gui_frontend_services=>filetype_all }|
                                                        CHANGING  filename          = lv_filename
                                                                  path              = lv_path
                                                                  fullpath          = lv_fullpath
                                                                  user_action       = lv_action ).

            IF lv_action EQ cl_gui_frontend_services=>action_ok.

* Datei lokal speichern
              cl_gui_frontend_services=>gui_download( EXPORTING filename     = lv_fullpath
                                                                filetype     = 'BIN'
                                                                bin_filesize = xstrlen( lv_xstr_zip )
                                                      CHANGING  data_tab     = it_raw_data ).
            ENDIF.

          ELSE.
            WRITE: / |Systemverzeichnisse nicht gefunden.|.
          ENDIF.
        ELSE.
          WRITE: / |Auftrag { p_trkorr } nicht frei gegeben.|.
        ENDIF.

      ELSE.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 INTO DATA(lv_msg).
        WRITE: / lv_msg.
      ENDIF.
    CATCH cx_root INTO DATA(e_txt).
      WRITE: / e_txt->get_text( ).
  ENDTRY.