< Previous | Next >

Lesson 1.3: Create a Java method

Lesson 1.3 leads you through the creation of a Java™ method.

Before you begin, you must complete Lesson 1.2: Setting up the Web project and Java Interface and Implementations. In this lesson you will:
  1. First you will create a Java method: You will now create a Java method that will use the COBOL importer to map the data types between the COBOL source and the data in your Java method.
  2. Open the Snippets view by clicking on Window > Show View > Snippets. In the Snippets view, click on J2C.
  3. Right click Add Java method to J2C Java bean and select Insert.
  4. In the New Java Method page, click Add.
  5. In the Java method name field, type getCustomerInfo for the name of the operation. Click Next.
  6. Next you will create the input parameter data mapping: In this step, you will import the taderc25.cbl (COBOL) file that is needed to create your application. The taderc25.cbl file is located in <installdir>\IBM\SDP70Shared\plugins\com.ibm.j2c.cheatsheet.content_7.0.0\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. In the Specify the input/output type field of the Java Method page, click New.
  7. In the Data Import page, ensure that the Choose mapping field is COBOL_TO_JAVA. Click Browse beside the COBOL file
  8. Locate the taderc25.cbl file in the file system, and click Open.
  9. Click Next.
  10. 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.
  11. Click Next.
  12. 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.
  13. Click Finish.
  14. Next you will create the multiple possible outputs for the output parameter: In the Specify the input/output type field in the Java Method page, click New beside the Output type area.
  15. In the Data Import page, ensure that the Choose mapping field is COBOL_MPO_TO_JAVA.
  16. Click New beside the multiple possible output area.
  17. Click Browse beside the Cobol file name field, and locate the location of the taderc25.cbl file. Click Open.
  18. Click Next.
  19. 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.
  20. Click Finish. In the Specify data import configuration properties page, you will see the three data types listed.
  21. Click Next.
  22. Next you will specify the saving properties: In the Saving Properties page, you will 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 to Java Save Properties.
      • Type sample.cics.data in the Package Name field
      • TypeOutputComm in the Class Name field.
      • You can select Overwrite existing class .
    2. Expand COBOL MPO to Java Save Properties. The three data binding elements should 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
      • TypePrefCust 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
      • TypeRegCust 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
      • TypeBadCust in the Class Name field.
      • You can select Overwrite existing class .
  23. Click Finish. Expand OutputComm, and you will see that it contains PrefCust, RegCust and BadCust in the Output type field.
  24. On the Java Method page, click Finish.
  25. 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.
  26. Click Finish.
  27. Now you will Add the recognition pattern tag to the generate Java output data mapping file: 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 will be 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 will be regenerated.
      • Navigate to the match method to make sure 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 will be regenerated.
      • Navigate to the match method to make sure 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);
        	}