Example 2 - nsmulti with Two Parameters (Key + List)

For this example, we will slightly modify the functionality of the previous example.

Instead of returning all transactions for less than one dollar, in the whole system, it will return only the transactions for one account which were less than one dollar.

Another parameter is required to specify the account number we are interested in. Since nsmulti is a database operation and database operations require all parameters to be structs, we must use a struct for our account number parameter even though the struct will have only one field.

Note that the account number field appears in various guises - txAccountNumber, txAccountNum, theAccountID. Unlike the other database operations, the names of attributes do not have to correspond when used in ns or nsmulti operations, the handcrafted SQL can reference the different field names as appropriate.

This struct can now be used as an input argument to the nsmulti operation getMinorTransactions(theAccountID : AccountNoWrapper), returning an instance of MinorTxDetails for the entity below:

The SQL for the operation is shown in Example 2 - nsmulti with Two Parameters (Key + List):

Figure 1. SQL for nsmulti with a key and list parameters
SELECT txAccountNumber, txDate, txAmount
INTO
  :txAccountNumber,
  :txDate,
  :txAmount
FROM    BankAccount
WHERE   (txAmount < 1)
  AND   (txAccountNumber = :txAccountNum);

This is all that has to be provided by the developer, the remainder is produced by the generator and is shown below for illustrative purposes.

The following pseudo-code Example 2 - nsmulti with Two Parameters (Key + List) describes the structs used in this operation (The actual Java structs corresponding to the structs defined in the model are produced by the code generator.):

Figure 2. Pseudocode for generated structs for use by nsmulti with key and list parameters
struct MinorTxDetails {
  txDate;
  txAccountNumber;
  txAmount;
};

// this is a generated list wrapper:
struct MinorTxDetailsList {
  sequence <MinorTxDetails> dtls;
};

struct AccountNoWrapper {
  txAccountNum;
}

// this is the standard details struct for the entity
// just to show where its attributes are kept:
struct BankAccountDtls {
  txAccountNumber;
  txDate;
  txAmount;
  txTellerNumber;
}

The Java interface for this entity class - complete with the nsmulti operation - is produced by the code generator and would look like the following:

Figure 3. Generated Java interface for nsmulti operation with key and list parameters
          public interface BankAccount {

  // This is our "nsmulti" operation. Note how the
  // generator has transformed the return value of this
  // function from "MinorTxDetails" to a
  // "MinorTxDetails
           List 
          "
  public MinorTxDetailsList getMinorTransactions
                              (AccountNoWrapper   theAccountID)
    throws AppException, InformationalException;
};

Example 2 - nsmulti with Two Parameters (Key + List) demonstrates how the developer would write handcrafted Java code to call this method and to iterate through each element returned by the method:

Figure 4. Calling a nsmulti operation from handcrafted Java code (two parameters)
<ProjectPackage>.intf.BankAccount bankAccount
  = <ProjectPackage>.fact.BankAccountFactory.newInstance();

AccountNoWrapper accNoWrapper = new AccountNoWrapper;

accNoWrapper.txAccountNum = "57033186";

double theTotalAmount = 0;

// Call the operation:
MinorTxDetailsList  txList
  = bankAccount.getMinorTransactions(accNoWrapper);

// iterate through the set of results.
for (int i = 0; i < txList.dtls.size(); i++) {
  MinorTxDetails currentTx = txList.dtls.item(i);

  theTotalAmount += currentTx.txAmount;
}