Creating an Implementation of the IWrapper Interface

Create the AddressBookWrapper Class

To create an implementation of the IWrapper interface called AddressBookWrapper:

  1. In COBOL Explorer, expand addressBookWrapper > src.
  2. Right-click com.addressBookWrapper.
  3. Click New > COBOL JVM Class.
    This opens the New COBOL JVM Class dialog box.
    Note: The Source folder field is already filled with AddressBookWrapper/src and the Package is already filled with com.addressBookWrapper.
  4. In the Name field, type AddressBookWrapper.
  5. Click Finish.

    This opens the AddressBookWrapper.cbl program in a COBOL editor.

  6. Update the class declaration to acknowledge the interface inheritance:
    class-id com.addressBookWrapper.AddressBookWrapper public
                    implements type IWrapper.
  7. In the working-storage section declare a procedure pointer that points to the procedural code in the recordsProgram.cbl from the AddressBookJVM project, together with the required parameters:
           working-storage section.
           *> procedural cobol pointer
           01 pp procedure-pointer private.
           *> private values
           01 #index binary-long value 0 private.
           78 PROCEDURAL_PROGRAM value "recordsProgram" private.
           01 func                     pic 99 private.
           copy "functions.cpy".
  8. You need to create a constructor and initialize the function and the procedure pointer. The procedure function needs to accept three parameters. The first parameter is mandatory and the second two are optional, depending on the function call being made:
           method-id New protected.
           procedure division.
               initialize func
               set pp to entry PROCEDURAL_PROGRAM
               goback.
           end method.
  9. You need a method that enables you to connect with a data store file and can calculate the maximum number of records:
           method-id openConnection.
           procedure division.
               move openFile to func
               call pp using func omitted omitted
               set #index to self::maxNumberOfRecords()
               goback.
           end method.
  10. In the same way, create a close connection method using the closeFile function parameter.
  11. You need to specify a method to create a record without requiring input parameters:
    1. You can create your own implementation of a getUniqueID() method. Alternatively, you can use the code provided in the sample folder.
    2. Create a RecordDTO object and translate it to a procedural record (DTOtoRecord).
    3. Write the rec variable to the data store file using writeRecord.
    4. Return the RecordDTO object to the client.

    You need to explicitly define the rec variable in the local-storage section in the same way it is used in procedural COBOL code. Your code should look something like this:

           method-id createRecord.
           local-storage secion.
           01 rec.
              copy "records.cpy".
           procedure division returning return-value as type RecordDTO.
               declare #id as binary-long = self::getUniqueID()
               declare dto = new RecordDTO(#id)
               set rec to self::DTOtoRecord(dto)
               move writeRecord to func
               call pp using func rec omitted
               set return-value to dto
               goback.
           end method.

    Here is an example of the object record translation:

           method-id DTOtoRecord private.
           linkage section.
           01 rec.
              copy "records.cpy".
           procedure division using by value dto as type RecordDTO
                              returning rec.
               initialize rec
               set recordId of rec to dto::recordID
               set recordName of rec to dto::name
               set recordFamily of rec to dto::family
               set recordCode of rec to dto::postalCode
               set recordCity of rec to dto::city
               set recordStreet of rec to dto::street
               set recordPhone of rec to dto::phone
    
               goback.
  12. In the same way, create methods for updateRecord and deleteRecord and a private method recordToDTO() that converts the record to an DTO object.
  13. To implement a getAllRecords method:
    1. Declare an arrayParent variable to store the array of records. You will use the variable as an input parameter to the method call.
    2. Declare a rec variable to store a single record while iterating over the results of the method call.
    3. Initialize the arrayParent variable and move the readAllRecords variable to func, and then call the function.
    4. Use a perform operation to loop over the resulting array converting it into RecordDTO objects. Add the objects to a list called return-value that is returned by the method.

    Your code should look something like this:

           method-id getAllRecords.
           local-storage section.
           01 arrayParent.
               03 array occurs 0 to 1000 depending on #index.
               copy "records.cpy".
           01 rec.
               copy "records.cpy".
           procedure division returning return-value as list[type RecordDTO].
               initialize arrayParent
               move readAllRecords to func
               call pp using func omitted arrayParent
               create return-value
               declare i as binary-long.
               perform varying i from 1 by 1 until i >= #index
                   set rec to array(i)
                   if recordId of rec not= 0
                       declare dto = self::recordToDTO(rec)
                       invoke return-value::add(dto)
                   end-if
               end-perform. 
    
               goback.
           end method.
    Note: You can import a complete example of the AddressBookWrapper.cbl program from the samples folder Samples\Eclipse\jvm_cobol\AddressBookDemo\addressBookWrapper\src\com\addressBookWrapper\AddressBookWrapper.cbl
    .

Create the WrapperProvider Class

You need to create a class WrapperProvider, to handle the creation of the IWrapper implementation. It should contain a single static method to handle the operation:

  1. In the addressBookWrapper project, right-click com.addressBookWrapper.
  2. Click New > COBOL JVM Class.
    This opens the New COBOL JVM Class dialog box.
    Note: The Source folder field is already filled with AddressBookWrapper/src and the Package is already filled with com.addressBookWrapper.
  3. In the Name field, type WrapperProvider.
  4. Click Finish.

    This opens the WrapperProvider.cbl program in a COBOL editor.

  5. Copy and paste the following COBOL code into the editor:
           class-id com.addressBookWrapper.WrapperProvider public.
    
           method-id getWrapper static.
           procedure division returning return-value as type IWrapper.
               set return-value to new AddressBookWrapper()
               goback.
           end method.
    
           end class.
    
  6. Click File > Save.