Examples of Creating Java Methods for User Variables

To use attribute values captured in application documents that are not defined as existing system variables, you can create user variables that obtain values from Java methods you write based on the sample code in this topic.

This topic provides two sample methods with comments to help you write such Java methods. The sourcing of the Java variable value in these methods are different based on the database table and view object (VO).

If the Java user variable is an attribute of the Document Header VO (for example, Contract Header VO or PO Header VO) then use the first method. Use the second method if the Java user variable is an attribute on any child table of the Document Header VO.

Sample Java Method 1

This sample assumes that CurrencyCode is an attribute on the PO Header VO. This Contract Expert Java variable works even if the header information is not saved during document authoring. In this scenario, getCurrencyCode() is the method name associated with the user-defined Java variable in the variable definition page.

Note: Because Java is case sensitive, be careful when entering VO attribute names. Do not change the signature of any method or the parameter names.
  1. Using Oracle JDeveloper, create an application and a project within that application.

  2. Within the project, create a Java file with the method for the Java user variable.

  3. Create a temporary folder and copy the ContractsTermsLibraryPublicModel JAR file from the fusionapps/jlib directory to this folder.

  4. Right-click the project in Oracle JDeveloper and in the Project Properties:

    1. Select Libraries and Classpath.

    2. Add the ContractsTermsLibraryPublicModel JAR from the temporary folder.

  5. Create a JAR for the current project, by right-clicking the project and selecting Project Properties and Deployment profile.

  6. Copy this new JAR to the following directory: mw_home_standalone/user_projects/domains/fusion_domain/servers/AdminServer/upload/ContractManagementApp/V2.0/app/ContractManagementApp/APP-INF/lib

  7. Bounce the server.

The following is a sample Java class to implement Java user variables. To configure, change the class name (MyPurchaseUDV). Do not change or remove any of the import statements.

/**
*/
MyPurchaseUDV.java
package oracle.apps.contracts.termsLibrary.publicModel.Attributes.model.java;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import oracle.apps.contracts.termsLibrary.publicModel.variables.model.java.ProgrammaticUDV;


/**
This class extends the abstract class ProgrammaticUDV. 
TO CONFIGURE: Change the Class name only (MyPurchaseUDV). 
*/

public class MyPurchaseUDV extends ProgrammaticUDV {


/**
CASE 1: For achieving CASE 1 use the methods registerAttributes() and getCurrencyCode().
*/


 /**
 The following method registers the Java variable present in the Header VO. The name of the variable should be the same as the name of the attribute in the Header VO. 
TO CONFIGURE: Change only the VO attribute name of the variable (in this case CurrencyCode) to match the attribute name in the Header VO. 
Do not change the method name or scope of the method. The only thing can be changed is the VO attribute name of the user variable. 
*/
       
protected void registerAttributes() {          
  registerAttribute("CurrencyCode");
 }
      
/**
 The following method obtains the value of java variable used in the Header VO. The attribute name of the java variable used in this method is CurrencyCode. This method returns the value of the CurrencyCode. The value of the variable which we are trying to get using this method (getCurrencyCode) should be registered in the previous method registerAttributes().  
TO CONFIGURE: Change the name of the method (getCurrencyCode()). Do not change the scope of the method.
The return type can be changed. To get the value of the variable we have to use the getAttributeValue() method only. 
 */
       
       public String getCurrencyCode() throws Exception {
            
        String  retVal = null;
        
        retVal = getAttributeValue("CurrencyCode");
       
          return retVal;
      }     

Sample Java Method 2

The following method is used to get the value of Java variable through SQL queries. In this scenario, we want to add clauses to the contract terms if the contract has any sales credit. Sales credit information is stored in a different table from the contract header. To work this scenario, the document must be saved before invoking Contract Expert. Java variable used is in this case is Sales Credit. Use method getSalesCredit() if the Java user variable is an attribute on any child table of the Document Header VO.

To configure, change the name of the method getSalesCredit() and the return type of the method. The other attribute values, such as document ID and document type, which might be needed while executing the query, can be obtained from the get methods getDocumentId(), getDocumentType(), and getDocumentVersion().

The executeQuery method:

  • Will always return a scalar value which is present in the first row and first column in the result set.

  • Will always return a string value:

    • If you are expecting an integer value, then you must do a conversion before returning value.

    • No conversion is required if you are expecting a string.

In the following example, an ID value of a Yes or No value set value is returned based on whether the contract has sales credits entries or not.

*/
      
      
public int getSalesCredit() throws SQLException, Exception {
        int  retVal = 0;  
        int value = 0;              
        String s1 = null;        
        BigDecimal id = getDocumentId();                
        s1 = executeQuery("SELECT to_char(count(*)) FROM OKC_K_SALES_CREDITS where dnz_chr_id = " + id);              
        value = Integer.parseInt(s1);    
        
         if(value > 0) {
           retVal = 271230; // Value Set id for "YES"
          } else {
            retVal = 271229; // Value Set id for "NO"
          }
      
           return retVal;      
        
      }
 }


/*****************************************************
   The following file content is provided here only for reference. 
   DO NOT INCLUDE THE FOLLOWING CODE IN ANY USER METHOD.
*****************************************************/
ProgrammaticUDV.java
package oracle.apps.contracts.termsLibrary.publicModel.variables.model.java;

import java.math.BigDecimal;

import java.sql.ResultSet;

import java.sql.SQLException;
import java.sql.Statement;

import java.util.ArrayList;
import java.util.HashMap;

import oracle.jbo.server.DBTransaction;

public abstract class ProgrammaticUDV {
   
    private HashMap attributesData;
    private DBTransaction dBTransaction;
    private Statement statement;
    protected BigDecimal documentId;
    protected String documentType;
    protected BigDecimal documentVersion;
    private ArrayList<String> attributeNamesUsed = new ArrayList<String>();
   
     public ProgrammaticUDV(){
           registerAttributes();
       }
     
    protected abstract void registerAttributes();
   
    protected void registerAttribute(String attributeName) {
        attributeNamesUsed.add(attributeName);
    }
   
    protected String getAttributeValue(String attributeName) throws Exception {
    if(attributesData.get(attributeName) == null){
        throw new Exception("Attribute name '" + attributeName + "' is either invalid or not registered.");
    }
     return (String)attributesData.get(attributeName);
    }   
   
    public HashMap getAttributesData() {
        return attributesData;
    }
   
    public void setAttributesData(HashMap variableData) {
        this.attributesData = variableData;
    }
   
    public ArrayList getAttributesUsed() {
        return attributeNamesUsed;
    }

    public void setDBTransaction(DBTransaction dBTransaction) {
        this.dBTransaction = dBTransaction;
    }
    
    protected String executeQuery(String query) throws SQLException {
        ResultSet rs = null;      
        String s =null;
        if (statement != null) {
            statement.close();
        }
        statement = dBTransaction.createStatement(0);
        rs = statement.executeQuery(query);      
        if(rs.next()){
          s = rs.getString(1);
        }
        statement.close();
        return s;
    }
    
    protected void closeQuery() throws SQLException {
     if (statement != null) {
         statement.close();
         statement = null;
      }
    }
    
    public void setDocumentId(BigDecimal documentId) {
        this.documentId = documentId;
    }

    public void setDocumentType(String documentType) {
        this.documentType = documentType;
    }

    public void setDocumentVersion(BigDecimal documentVersion) {
        this.documentVersion = documentVersion;
    }

    public BigDecimal getDocumentId() {
        return documentId;
    }

    public String getDocumentType() {
        return documentType;
    }

    public BigDecimal getDocumentVersion() {
        return documentVersion;
    }
}