COBOL プログラムを作成する

  1. CobolBookReader プロジェクトの src ディレクトリを右クリックして [New > Other] を選択し、[Micro Focus COBOL] を展開して [COBOL Program] を選択します。[Next] をクリックします。
  2. book.cbl の名前を入力して、[Finish] をクリックします。
  3. ファイルの内容を次のコードに置き換えます。
          $set ilnamespace"com.microfocus.book" 
           program-id. BookLegacy.
    
           environment division.
           input-output section.
           file-control.
               select bookfile assign to filename
                   file status is ls-file-status
                   organization is indexed
                   access mode is dynamic
                   record key is b-stockno
                   alternate record key is b-title with duplicates
                   alternate record key is b-author with duplicates
                   .
    
           data division.
           file section.
           FD bookfile.
           copy "book-rec.cpy" replacing ==(prefix)== by ==b==.
    
           working-storage section.
           01 ls-file-status   pic xx.
           01 ls-call-status   pic x(2) comp-5.
    
           linkage section.
           01 lnk-filename       pic x(256).
           01 lnk-function       pic x.
               88 read-record    value "1".
               88 add-record     value "2".
               88 delete-record  value "3".
               88 next-record    value "4".
           01 lnk-file-status    pic xx.
           copy "book-rec.cpy" replacing ==(prefix)== by ==lnk-b==.
    
           procedure division using by value lnk-function
                                    by reference lnk-b-details
                                    by reference lnk-file-status.
           main section.
    
               call "CBL_TOUPPER" using lnk-b-text-details
                                  by value length lnk-b-text-details
                                  returning ls-call-status
    
               evaluate true
                   when read-record
                       perform do-read-record
    
                   when add-record
                       perform do-add-record
    
                   when delete-record
                       perform do-delete-record
    
                   when next-record
                       perform do-next-record
    
               end-evaluate
               exit program
               stop run
               .
    
           do-read-record section.
               open input bookfile
               if ls-file-status <> "00"
                   initialize lnk-b-details
                   move all '*' to lnk-b-text-details
    
                   set lnk-file-status to ls-file-status
                   exit section
               end-if
               evaluate true
                    when lnk-b-stockno <> spaces
                        set b-stockno to lnk-b-stockno
                        read bookfile
    
                    when lnk-b-title <> spaces
                        set b-title to lnk-b-title
                        read bookfile key is b-title
    
                    when lnk-b-author <> spaces
                        set b-author to lnk-b-author
                        read bookfile key is b-author
    
                    when other
          *>------------No key specified - return unsuccessful read
                         set ls-file-status to "23"
    
               end-evaluate
               set lnk-file-status to ls-file-status
               if ls-file-status = "00"
                   set lnk-b-title to b-title
                   set lnk-b-type to  b-type
                   set lnk-b-author to b-author
                   set lnk-b-stockno to b-stockno
                   set lnk-b-retail to b-retail
                   set lnk-b-onhand to b-onhand
                   set lnk-b-sold to b-sold
               else
                   initialize lnk-b-details
                   move all '*' to lnk-b-text-details
               end-if
               close bookfile
               .
    
           do-next-record section.
               open input bookfile
               if ls-file-status <> "00"
                   initialize lnk-b-details
                   move all '*' to lnk-b-text-details
    
                   set lnk-file-status to ls-file-status
                   exit section
               end-if
    
               set b-stockno to lnk-b-stockno
               start bookfile key > b-stockno
               read bookfile next
    
               set lnk-file-status to ls-file-status
               if ls-file-status = "00"
                   set lnk-b-title to b-title
                   set lnk-b-type to b-type
                   set lnk-b-author to b-author
                   set lnk-b-stockno to b-stockno
                   set lnk-b-retail to b-retail
                   set lnk-b-onhand to b-onhand
                   set lnk-b-sold to b-sold
               else
                   initialize lnk-b-details
                   move all '*' to lnk-b-text-details
               end-if
               close bookfile
               .
    
           do-add-record section.
               open i-o bookfile
               evaluate ls-file-status
                   when "05"
          *>-------File not created yet
                   when "00"
                       continue
    
                   when other
                       set lnk-file-status to ls-file-status
                       exit section
               end-evaluate
    
               set b-stockno to lnk-b-stockno
               read bookfile
               if ls-file-status = "00"
          * Record already exists - so error
                   set ls-file-status to "99"
               else
                   set b-title to lnk-b-title
                   set b-type to lnk-b-type
                   set b-author to lnk-b-author
                   set b-retail to lnk-b-retail
                   set b-onhand to lnk-b-onhand
                   set b-sold to lnk-b-sold
                   write b-details
               end-if
    
               set lnk-file-status to ls-file-status
               close bookfile
               .
    
           do-delete-record section.
               open i-o bookfile
               if ls-file-status <> "00"
                   set lnk-file-status to ls-file-status
                   exit section
               end-if
    
               evaluate true
                   when lnk-b-stockno <> spaces
                       set b-stockno to lnk-b-stockno
                       read bookfile
                       delete bookfile record
    
                   when lnk-b-title <> spaces
                       set b-title to lnk-b-title
                       read bookfile key is b-title
                       delete bookfile record
    
                   when lnk-b-author <> spaces
                       set b-author to lnk-b-author
                       read bookfile key is b-author
                       delete bookfile record
    
                   when other
          *>------------No key specified - return unsuccessful read
                       set ls-file-status to "23"
    
               end-evaluate
    
               set lnk-file-status to ls-file-status
               close bookfile
               .
           
           entry "SET_FILENAME" using lnk-filename.
               move lnk-filename to filename
               
               goback.
注:これは従来の COBOL ファイル処理プログラムですが、唯一の違いとして ilnamespace 宣言が先頭にあります。Tomcat はデフォルトのネームスペースを使用するクラスを lib ディレクトリからロードできないため、ユーザはプログラムにネームスペースを設定する必要があります。メインの BookLegacy および SET_FILENAME エントリ・ポイントは、必要に応じてブック・ファイルの設定とアクセスを行うためにラッパ・プログラムによって使用されます。