mark_jsi Absent Member.
Absent Member.
1540 views

Java Calling COBOL using "Custom Record" Interface

Jump to solution

[Migrated content. Thread originally posted on 29 May 2012]

We have a fairly complex COBOL routine to perform business logic that we want to call from JAVA. The COBOL routine uses 3 parameter blocks which are defined as COBOL group item records. Some of these parameter blocks have multiple definitions (the one to use depends on what function is being requested). We have successfully created MF COBOL "Custom Record Interface" objects for one definition of each of the 3 parameter blocks and we have successfully called and passed this info into and out of our COBOL routine from JAVA. Thus far, each "custom record" has only had to describe COBOL elementary group items. However, to complete the set of interfaces, we need to be able to handle COBOL record definitions that include an array (OCCURS). The "custom record" documentation and other "calling COBOL from JAVA" documentation does not give examples or suggestions on how to handle these more complex COBOL record structures. Does anyone have any examples or know of any more detailed documentation?
0 Likes
1 Solution

Accepted Solutions
Micro Focus Expert
Micro Focus Expert

RE: Java Calling COBOL using "Custom Record" Interface

Jump to solution

I had responded to this question offline in a support incident but I thought that I would post the responswe here also for the sake of completeness.

The customrecord interface has no internal functionality to automatically handle arrays that are defined within the COBOL record.
It's function is to format elementary items within a record and pass it to COBOL as a single java byte array.

If you wanted to add array functionality to the record then you would have to layout the array within java as if it were passing each item of the array as part of the byte array that is being passed into COBOL.

So if COBOL defined something like:

linkage Section.                                          

01 customerDetails.                                        
     03 customerName       pic x(30).                        
     03 customerAddress    pic x(30).                        
     03 customerRef        pic 9(6).                          
     03 customerArray.                                        
          05 customerProducts pic x(5) occurs 3 times.          
procedure division using by reference customerDetails.    

Java would define something like this (although you would probably change code to format your java array as a separate string to include)

public class RecordData implements DataType                                                          
{                                                                                                    
   private String customerName, customerAddress, customerProd1, customerProd2, customerProd3;      
   private int customerRef;                                                                        
   private byte[] bytes;                                                                            
   RecordData(String name, String address, int ref, String prod1, String prod2, String prod3)      
   {                                                                                                
       customerName = name;                                                                        
       customerAddress = address;                                                                  
       customerRef = ref;                                                                          
       customerProd1 = prod1;                                                                      
       customerProd2 = prod2;                                                                      
       customerProd3 = prod3;                                                                      
   }                                                                                                
   public void synchronizeData()                                                                    
   {                                                                                                
       // This method is called from COBOL prior to return from a method.                          
       //                                                                                          
       // We use it here to update each field to contain the                                        
       // data returned by COBOL in the updated byte array.                                        
       customerName = new String(bytes, 0, 30);                                                    
       customerAddress = new String(bytes, 30, 30);                                                
       customerRef = Integer.parseInt(new String(bytes, 60, 6));                                    
       customerProd1 = new String(bytes, 66, 5);                                                    
       customerProd2 = new String(bytes, 71, 5);                                                    
       customerProd3 = new String(bytes, 76, 5);                                                    
   }                                                                                                
   public byte[] getBytes()                                                                        
   {                                                                                                
       // This method is called from COBOL on entry to a method to obtain                          
       // the data to be moved into the COBOL data type.                                            
       // To pass the information, a temporary string is created from the                          
       // fields in the class, to allow them to be passed to COBOL as a                            
       // byte array.                                                                              
       int i;                                                                                      
       StringBuffer tmpCustomerName = new StringBuffer(customerName);                              
       // The COBOL program is expecting 30 character strings, so space fill.                      
       for (i = tmpCustomerName.length(); i < 30; i++)                                              
            tmpCustomerName.append(" ");                                                            
       StringBuffer tmpCustomerAddress = new StringBuffer(customerAddress);                        
       for (i = tmpCustomerAddress.length(); i < 30; i++)                                          
            tmpCustomerAddress.append(" ");                                                        
       // Format the reference number to match PIC 9(6)                                            
       DecimalFormat pic9display = new DecimalFormat("000000");                                    
       StringBuffer tmpCustomerRef = pic9display.format(customerRef,                                
                                                        new StringBuffer(""),                      
                                                        new FieldPosition(0));                      
       StringBuffer tmpCustomerProd1 = new StringBuffer(customerProd1);                            
       for (i = tmpCustomerProd1.length(); i < 5; i++)                                              
          tmpCustomerProd1.append(" ");                                                            
       StringBuffer tmpCustomerProd2 = new StringBuffer(customerProd2);                            
       for (i = tmpCustomerProd2.length(); i < 5; i++)                                              
          tmpCustomerProd2.append(" ");                                                            
       StringBuffer tmpCustomerProd3 = new StringBuffer(customerProd3);                            
       for (i = tmpCustomerProd3.length(); i < 5; i++)                                              
          tmpCustomerProd3.append(" ");                                                            
       String tmpString = new String(tmpCustomerName.toString() +                                  
                                     tmpCustomerAddress.toString() +                                
                                     tmpCustomerRef.toString() +                                    
                                     tmpCustomerProd1.toString() +                                  
                                     tmpCustomerProd2.toString() +                                  
                                     tmpCustomerProd3.toString());                                  
       bytes = tmpString.getBytes();                                                                
       return bytes;                                                                                
   }                                                                                                
   public String getCustomerName()                                                                  
   {                                                                                                
     return this.customerName;                                                                      
   }                                                                                                
   public String getCustomerAddress()                                                              
   {                                                                                                
     return this.customerAddress;                                                                  
   }                                                                                                
   public int getCustomerRef()                                                                      
   {                                                                                                
     return this.customerRef;                                                                      
   }                                                                                                
   public String getCustomerProd1()                                                                
   {                                                                                                
     return this.customerProd1;                                                                    
   }                                                                                                
   public String getCustomerProd2()                                                                
   {                                                                                                
     return this.customerProd2;                                                                    
   }                                                                                                
   public String getCustomerProd3()                                                                
   {                                                                                                
     return this.customerProd3;                                                                    
   }                                                                                                
   public void showRef()                                                                            
   {                                                                                                
       // Method used to illustrate that the customerRef field was updated.                        
       System.out.println("Updated customerRef is : " + customerRef);                              
   }                                                                                                
   public String toString()                                                                        
   {                                                                                                
       return "Customer Name    : "+this.customerName+"\n"+                                        
              "Customer Address : "+this.customerAddress+"\n"+                                      
              "Customer Ref     : "+this.customerRef+"\n"+                                          
              "Customer Prod1   : "+this.customerProd1+"\n"+                                        
              "Customer Prod2   : "+this.customerProd2+"\n"+                                        
              "Customer Prod3   : "+this.customerProd3+"\n";                                        
   }                                                                                                
}                                                                                                    

0 Likes
1 Reply
Micro Focus Expert
Micro Focus Expert

RE: Java Calling COBOL using "Custom Record" Interface

Jump to solution

I had responded to this question offline in a support incident but I thought that I would post the responswe here also for the sake of completeness.

The customrecord interface has no internal functionality to automatically handle arrays that are defined within the COBOL record.
It's function is to format elementary items within a record and pass it to COBOL as a single java byte array.

If you wanted to add array functionality to the record then you would have to layout the array within java as if it were passing each item of the array as part of the byte array that is being passed into COBOL.

So if COBOL defined something like:

linkage Section.                                          

01 customerDetails.                                        
     03 customerName       pic x(30).                        
     03 customerAddress    pic x(30).                        
     03 customerRef        pic 9(6).                          
     03 customerArray.                                        
          05 customerProducts pic x(5) occurs 3 times.          
procedure division using by reference customerDetails.    

Java would define something like this (although you would probably change code to format your java array as a separate string to include)

public class RecordData implements DataType                                                          
{                                                                                                    
   private String customerName, customerAddress, customerProd1, customerProd2, customerProd3;      
   private int customerRef;                                                                        
   private byte[] bytes;                                                                            
   RecordData(String name, String address, int ref, String prod1, String prod2, String prod3)      
   {                                                                                                
       customerName = name;                                                                        
       customerAddress = address;                                                                  
       customerRef = ref;                                                                          
       customerProd1 = prod1;                                                                      
       customerProd2 = prod2;                                                                      
       customerProd3 = prod3;                                                                      
   }                                                                                                
   public void synchronizeData()                                                                    
   {                                                                                                
       // This method is called from COBOL prior to return from a method.                          
       //                                                                                          
       // We use it here to update each field to contain the                                        
       // data returned by COBOL in the updated byte array.                                        
       customerName = new String(bytes, 0, 30);                                                    
       customerAddress = new String(bytes, 30, 30);                                                
       customerRef = Integer.parseInt(new String(bytes, 60, 6));                                    
       customerProd1 = new String(bytes, 66, 5);                                                    
       customerProd2 = new String(bytes, 71, 5);                                                    
       customerProd3 = new String(bytes, 76, 5);                                                    
   }                                                                                                
   public byte[] getBytes()                                                                        
   {                                                                                                
       // This method is called from COBOL on entry to a method to obtain                          
       // the data to be moved into the COBOL data type.                                            
       // To pass the information, a temporary string is created from the                          
       // fields in the class, to allow them to be passed to COBOL as a                            
       // byte array.                                                                              
       int i;                                                                                      
       StringBuffer tmpCustomerName = new StringBuffer(customerName);                              
       // The COBOL program is expecting 30 character strings, so space fill.                      
       for (i = tmpCustomerName.length(); i < 30; i++)                                              
            tmpCustomerName.append(" ");                                                            
       StringBuffer tmpCustomerAddress = new StringBuffer(customerAddress);                        
       for (i = tmpCustomerAddress.length(); i < 30; i++)                                          
            tmpCustomerAddress.append(" ");                                                        
       // Format the reference number to match PIC 9(6)                                            
       DecimalFormat pic9display = new DecimalFormat("000000");                                    
       StringBuffer tmpCustomerRef = pic9display.format(customerRef,                                
                                                        new StringBuffer(""),                      
                                                        new FieldPosition(0));                      
       StringBuffer tmpCustomerProd1 = new StringBuffer(customerProd1);                            
       for (i = tmpCustomerProd1.length(); i < 5; i++)                                              
          tmpCustomerProd1.append(" ");                                                            
       StringBuffer tmpCustomerProd2 = new StringBuffer(customerProd2);                            
       for (i = tmpCustomerProd2.length(); i < 5; i++)                                              
          tmpCustomerProd2.append(" ");                                                            
       StringBuffer tmpCustomerProd3 = new StringBuffer(customerProd3);                            
       for (i = tmpCustomerProd3.length(); i < 5; i++)                                              
          tmpCustomerProd3.append(" ");                                                            
       String tmpString = new String(tmpCustomerName.toString() +                                  
                                     tmpCustomerAddress.toString() +                                
                                     tmpCustomerRef.toString() +                                    
                                     tmpCustomerProd1.toString() +                                  
                                     tmpCustomerProd2.toString() +                                  
                                     tmpCustomerProd3.toString());                                  
       bytes = tmpString.getBytes();                                                                
       return bytes;                                                                                
   }                                                                                                
   public String getCustomerName()                                                                  
   {                                                                                                
     return this.customerName;                                                                      
   }                                                                                                
   public String getCustomerAddress()                                                              
   {                                                                                                
     return this.customerAddress;                                                                  
   }                                                                                                
   public int getCustomerRef()                                                                      
   {                                                                                                
     return this.customerRef;                                                                      
   }                                                                                                
   public String getCustomerProd1()                                                                
   {                                                                                                
     return this.customerProd1;                                                                    
   }                                                                                                
   public String getCustomerProd2()                                                                
   {                                                                                                
     return this.customerProd2;                                                                    
   }                                                                                                
   public String getCustomerProd3()                                                                
   {                                                                                                
     return this.customerProd3;                                                                    
   }                                                                                                
   public void showRef()                                                                            
   {                                                                                                
       // Method used to illustrate that the customerRef field was updated.                        
       System.out.println("Updated customerRef is : " + customerRef);                              
   }                                                                                                
   public String toString()                                                                        
   {                                                                                                
       return "Customer Name    : "+this.customerName+"\n"+                                        
              "Customer Address : "+this.customerAddress+"\n"+                                      
              "Customer Ref     : "+this.customerRef+"\n"+                                          
              "Customer Prod1   : "+this.customerProd1+"\n"+                                        
              "Customer Prod2   : "+this.customerProd2+"\n"+                                        
              "Customer Prod3   : "+this.customerProd3+"\n";                                        
   }                                                                                                
}                                                                                                    

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.