< Previous | Next >

Lesson 3: Create a Java method

Lesson 3 leads you through the creation of a Java™ method.
Before you begin, you must complete Lesson 2: Set up a web project and Java interface and implementations.
In this lesson, you
  1. You now create a Java method that uses the COBOL importer to map the data types between the COBOL source and the data in your Java method. Open the Snippets view by clicking Window > Show View > Snippets. In the Snippets view, click J2C.
  2. Right click Add Java method to J2C Java bean and select Insert.
  3. In the New Java Method page, click Add.
  4. In the Name field, type getCustomerInfo.
  5. In this step, you import the taderc25.cbl (COBOL) file that is needed to create your application. The taderc25.cbl file is located in <installdir>IBM/IBMIMShared/plugins/com.ibm.j2c.cheatsheet.content/Samples/CICS/taderc25, where <installdir> is the directory where this product is installed. The COBOL file contains the program that runs on the CICS® server. It has the definition of the structure to be passed to the CICS server via the communications area (COMMAREA). This structure represents the customer records being returned from the CICS application. Before you can work with a file, you must import it from the file system into the workbench. Beside the Input type field, click New.
  6. In the Data Import page, ensure that the Choose mapping field is COBOL_TO_JAVA. Click Browse beside the COBOL file
  7. Locate the taderc25.cbl file in the file system, and click Open.
    taderc25.cbl
          identification division.
           program-id. TADERC25.
           environment division.
           data division.
           working-storage section.
           01 tmp pic a(40).
           01  ICOMMAREA.
               02  ICustNo    PIC X(5).
               02  Ifiller    PIC X(11).
           01  GENCUST.
               02  GCUSTCODE PIC X(4).
               02  GFILLER PIC X(40).
           01  PREFCUST.
               02  PCUSTCODE PIC X(4).
               02  PCUSTNO    PIC X(5).
               02  ASSETS  PIC S9(6)V99.
           01  REGCUST.
               02  RCUSTCODE PIC X(4).
               02  RCUSTNO    PIC X(5).
               02  ACCOUNTNAME PIC A(10).
               02  BALANCE PIC S9(6)V99.
           01  BADCUST.
               02  BCUSTCODE PIC X(4).
               02  BCUSTNO    PIC X(5).
               02  DAYSOVERDUE PIC X(4).
               02  AMOUNT PIC S9(6)V99.
           LINKAGE SECTION.
           01 DFHCOMMAREA.
              02 inputfield pic x(50).
           procedure division.
           start-para.
               move DFHCOMMAREA to ICOMMAREA.
               IF ICustNo EQUAL '12345'
                  move 'PREC' to PCUSTCODE
                  move ICustNo to PCUSTNO
                  move 43456.33 to ASSETS
                  move PREFCUST TO DFHCOMMAREA
               ELSE IF ICustNo EQUAL '34567'
                       move 'REGC' to RCUSTCODE
                       move ICustNo to RCUSTNO
                       move 'SAVINGS' TO ACCOUNTNAME
                       move 11456.33 to BALANCE
                       move REGCUST TO DFHCOMMAREA
                    ELSE
                       move 'BADC' to BCUSTCODE
                       move ICustNo to BCUSTNO
                       move '132' to DAYSOVERDUE
                       move -8965.33 to AMOUNT
                       move BADCUST TO DFHCOMMAREA
          *         END-IF.
               END-IF.
               EXEC CICS RETURN
               END-EXEC.
    
  8. Click Next.
  9. In the COBOL Importer page, select a communication data structure:
    1. Select Win32 for Platform Name.
    2. Select ISO-8859-1 for Code page.
    3. Click Query.
    4. Select ICOMMAREA for Data structures.
  10. Click Next.
  11. In the Saving properties page:
    1. Select Default for Generation Style.
    2. Click Browse to choose the web project Taderc25Sample.
    3. In the Package Name field, type sample.cics.data
    4. In the Class Name field, the default value is ICOMMAREA; replace it with InputComm.
  12. Click Finish.
  13. In the Specify the input/output type field in the Java Method page, click New beside the Output type area.
  14. In the Data Import page, ensure that the Choose mapping field is COBOL_MPO_TO_JAVA.
  15. Click New beside the multiple possible output area.
  16. Click Browse beside the Cobol file name field, and locate the location of the taderc25.cbl file. Click Open.
  17. Click Next.
  18. In the COBOL Importer page, select a communication data structure:
    1. Select Win32 for Platform Name.
    2. Select ISO-8859-1 for Code page.
    3. Click Query.
    4. Select PREFCUST, REGCUST, and BADCUST for Data structures.
  19. Click Finish. In the Specify data import configuration properties page, you see the three data types listed.
  20. Click Next.
  21. In the Saving Properties page, you can see default values set for each of the customer type record. Ensure that Taderc25Sample appears in the Project Name field. Click Browse and choose the web project Taderc25Sample.
    1. In the Specify the Saving properties page, highlight COBOL MPO Container Type.
      • Type sample.cics.data in the Package Name field
      • Type OutputComm in the Class Name field.
      • You can select Overwrite existing class.
    2. Expand COBOL MPO to Java Save Properties. The three data binding elements appear.
    3. Highlight COBOL To Java Save Properties For "PREFCUST" in File taderc25.cbl
      • For Generation Style, select Default.
      • Type sample.cics.data in the Package Name field
      • Type PrefCust in the Class Name field.
      • You can select Overwrite existing class.
    4. Highlight COBOL To Java Save Properties For "REGCUST" in File taderc25.cbl.
      • Type sample.cics.data in the Package Name field
      • Type RegCust in the Class Name field.
      • You can select Overwrite existing class.
    5. Highlight COBOL To Java Save Properties For "BADCUST" in File taderc25.cbl.
      • Type sample.cics.data in the Package Name field
      • Type BadCust in the Class Name field.
      • You can select Overwrite existing class.
  22. Click Finish. Expand OutputComm, and you can see that it contains PrefCust, RegCust and BadCust in the Output type field.
  23. On the Java Method page, click Finish.
  24. In the Java methods page:
    1. Type TADERC25 (the COBOL program ID) in the functionName field.
    2. Select Show Advanced.
    3. Select SYNC_SEND_RECEIVE(1) in the interactionVerb field.
    4. Type -1 in the replyLength field.
  25. Click Finish.
  26. Since the output coming back can be any one of the data types, the only way to match it is to have some pattern predefined in the data stream. The match method checks the recognition pattern.

    add doclet tag for recognition pattern

    1. To add the recognition pattern for PrefCust:
      • Open the PrefCust.java file in a Java editor.
      • Navigate to the getPcustcode() method. The best way to do this is to open the Outline view and scroll down until you find the desired method.
      • In the method comment area, add the tag @type-descriptor.recognition-desc pattern="PREC" or you can use the content assist by pressing CTRL-space and navigate down the list to find the tag and then enter "PREC" as the pattern.
      • Save the changes and the code PrefCust.java is regenerated.
      • Navigate to the match method to ensure that the change is there:
        /**
        	 * @generated
        	 
        	 */
        	public boolean match(Object obj) {
        		if (obj == null)
        			return (false);
        		if (obj.getClass().isArray()) {
        			byte[] currBytes = buffer_;
        			try {
        				byte[] objByteArray = (byte[]) obj;
        				buffer_ = objByteArray;
        				if (!("PREC".equals(getPcustcode().toString())))
        					return (false);
        			} catch (ClassCastException exc) {
        				return (false);
        			} finally {
        				buffer_ = currBytes;
        			}
        		} else
        			return (false);
        		return (true);
        	}
        

    2. To add the recognition pattern for RegCust:
      • Open the RegCust.java file in a Java editor.
      • Navigate to the getRcustcode() method. Again, the best way to do this is to open the Outline view and scroll down until you find the desired method.
      • In the method comment area, add the tag @type-descriptor.recognition-desc pattern="REGC" or you can use the content assist by pressing CTRL-space and navigate down the list to find the tag and then enter "REGC" as the pattern.
      • Save the changes and the code RegCust.java is regenerated.
      • Navigate to the match method to make sure that the change is there:
        /**
        	 * @generated
        	 
        	 */
        	public boolean match(Object obj) {
        		if (obj == null)
        			return (false);
        		if (obj.getClass().isArray()) {
        			byte[] currBytes = buffer_;
        			try {
        				byte[] objByteArray = (byte[]) obj;
        				buffer_ = objByteArray;
        				if (!("REGC".equals(getRcustcode().toString())))
        					return (false);
        			} catch (ClassCastException exc) {
        				return (false);
        			} finally {
        				buffer_ = currBytes;
        			}
        		} else
        			return (false);
        		return (true);
        	}
        
    3. To add the recognition pattern for BadCust:
      • Open the BadCust.java file in a Java editor.
      • Navigate to the getBcustcode() method. Again, the best way to do this is to open the Outline view and scroll down until you find the desired method.
      • In the method comment area, add the tag @type-descriptor.recognition-desc pattern="BADC" or you can use the content assist by pressing CTRL-space and navigate down the list to find the tag and then enter "BADC" as the pattern.
      • Save the changes and the code BadCust.java if regenerated.
      • Navigate to the match method to make sure that the change is there:
        /**
        	 * @generated
        	 
        	 */
        	public boolean match(Object obj) {
        		if (obj == null)
        			return (false);
        		if (obj.getClass().isArray()) {
        			byte[] currBytes = buffer_;
        			try {
        				byte[] objByteArray = (byte[]) obj;
        				buffer_ = objByteArray;
        				if (!("BADC".equals(getBcustcode().toString())))
        					return (false);
        			} catch (ClassCastException exc) {
        				return (false);
        			} finally {
        				buffer_ = currBytes;
        			}
        		} else
        			return (false);
        		return (true);
        	}
        
< Previous | Next >

Feedback