# Step 5: Add an EJB Control

In this task you will add an Enterprise Java Bean control to the Investigate Java control. This control provides accesses to an EJB (already deployed on the server as part of the Workshop samples domain) that calculates an approval rating on credit applicants. The tasks in this step are:

- To add an EJB control
- To add code that requests credit validation
- To test the Investigate Java control

To Add an EJB Control

The ValidateCredit EJB you will be accessing is designed to take the credit score and return a response about the applicant's credit worthiness. To access the ValidateCredit bean, you will add an EJB control.

Note: In order for you to add an EJB control to a web service project, the compiled home and remote interfaces for the EJB must be in the project also. The JAR file containing the home and remote interfaces is already within the Libraries folder of the application.

1. On the Application tab, double-click InvestigateImpl.jcs and click the Design View tab.
2. From the Insert menu, choose Controls -->EJB Control. The Insert Control - EJB Control dialog appears.
3. Enter values as shown in the following illustration. Use the Browse server EJBs button to fill in the jndi-name values required in Step 3 of the dialog. When you select financial.ValidateCredit from the list provided and click Select, the home interface and bean interface values will be added automatically.

4. Click Create.

WebLogic Workshop adds an EJB control to your design, as shown below. The control shows that the EJB exposes two methods: create and validate. The validate method is the one you will use (all EJBs expose a create method that is used by WebLogic Server; you will not need to call this).

Now you need to connect the score received through the creditScore JMS control to the validateCredit EJB control. You will do this by updating the JMS control's callback handler. The code you add will send the credit score to the EJB for validation.

To Add Code that Requests Credit Validation

1. Confirm that InvestigateImpl.jcs is displayed in the main work area.
2. Click the Design View tab.
3. Click the link text of the receiveTextMessage callback handler. Its source code appears in Source View.
4. Edit the creditScoreJMS_receiveTextMessage callback handler so that it appears as follows. Code to edit appears in red. Make sure to delete the code shown crossed out.

           public void creditScoreJMS_receiveTextMessage(java.lang.String payload)
           {
               try
               {
                   /*
                    * Extract the the credit score from the JMS response string
                    * and store it in m_currentApplicant.
                    */
                   ScoreResponseDocument scoreInfo = ScoreResponseDocument.Factory.parse(payload);
                   m_currentApplicant.creditScore = scoreInfo.getScoreResponse().getCalculatedScore();

                   /*
                    * Send the complete report back to the client.
                    */
                   // callback.onCreditReportDone(m_currentApplicant);
               }
               catch(XmlException e)
               {
                   m_currentApplicant.message = "There was a problem retrieving the credit score.  Please call (555) 555-5555.";

                   /*
                    * Send the the error message back to the client.
                    */
                   callback.onCreditReportDone(m_currentApplicant);
               }

               try
               {
                   /*
                    * Pass the credit score to the EJB's validate method. Store the value returned
                    * with other applicant data.
                    */
                   m_currentApplicant.approvalLevel = validateCreditEJB.validate(m_currentApplicant.creditScore);

                   /*
                    * Send the complete report back to the client.
                    */
                   callback.onCreditReportDone(m_currentApplicant);
               }
               catch(java.rmi.RemoteException e)
               {
                   m_currentApplicant.message = "There was a problem retrieving the approval level. Please call (555) 555-5555.";

                   /*
                    * Send an error message back to the client.
                    */
                   callback.onCreditReportDone(m_currentApplicant);
               }
           }

5. Press Ctrl+S to save your work.

To Test the Investigate Java control

1. On the Application tab, double-click InvestigateTest.jws to display the file.
2. Click the Start button, shown here:

   Workshop builds InvestigateTest.jws and launches the Workshop Test Browser.
3. In the Workshop Test Browser, in the taxID field, enter the 9 digit number 555555555 and click the requestCreditReport button.

   Note: Use one of the following (9 digit) taxID's to test your Java control throughout the tutorial:

   123456789, 111111111, 222222222, 333333333, 444444444, and 555555555.
4. Click Refresh until callback.onCreditReportDone appears in the Message Log.
5. When callback.onCreditReportDone appears, click it. Your screen should resemble the following:

Notice the new element in the applicant profile <approvalLevel>Applicant is a low risk.</approvalLevel>. This element was provided by the EJB credit validating application.

Related Topics

EJB Control

Click one of the following arrows to navigate through the tutorial: