Oracle9i Application Server Application Developer's Guide Release 2 (9.0.2) Part Number A95101-01 |
|
Previous chapters describe the JSP client files and the business logic objects. This chapter describes how these objects interact with each other: it shows how the JSP files access objects and retrieve data from the objects.
Contents of this chapter:
Although some methods in the business tier objects are declared public, client tier objects (that is, the JSP files) should access only some of these objects and methods. The methods are declared public so that other business tier objects can invoke them.
JSP files do not invoke methods on the Employee bean or the BenefitCatalog bean directly. Instead, the files invoke methods on an EmployeeManager bean, and these methods invoke methods on the Employee or BenefitCatalog objects. The EmployeeManager class has methods to execute the business logic operations. See Section 4.7, "EmployeeManager (Stateless Session Bean)" for details.
To get a reference to the EmployeeManager bean, the JSP files reference the SessionHelper class, which is a "regular" Java class. The SessionHelper class contains the getEmployeeManager static method which returns an instance of EmployeeManager. The SessionHelper class instantiates and stores the session bean in an attribute of HttpSession class. For example:
// from addBenefitToEmployee.jsp <% int empId = Integer.parseInt(request.getParameter( SessionHelper.EMP_ID_PARAMETER)); EmployeeManager mgr = SessionHelper.getEmployeeManager(request); Collection unelected = mgr.getUnelectedBenefitItems(empId); ... %>
Typically, the user accesses the application through a link on an external page. The link's URL looks like this:
http://<host>/empbft/controller?action=queryEmployee
The user then sees the ID page (Figure 2-1).
Figure 6-1 diagrams the query operation. The numbers in the figure correspond to the steps that follow the figure. This figure covers requests from browsers only. See Section 7.5.1, "Query Operation" for requests from wireless clients.
B1: The Controller servlet handles the request to the application.
B2: The value of the action parameter is queryEmployee
, so the Controller invokes the performAction method in the QueryEmployee class.
B3: The performAction method forwards the request to the queryEmployee.jsp file, which displays an ID page (Figure 2-1).
B4: The user then enters an employee ID and clicks Query. The request still has the same value in the action parameter (queryEmployee
), but it also has an employee ID parameter. The request is again handled by the QueryEmployee class.
B5: The performAction method in the QueryEmployee class and the queryEmployee.jsp file validate the employee ID entered by the user.
B6: For valid employee IDs, the JSP file queries the database to retrieve data for the specified employee ID.
To get employee details, queryEmployee.jsp invokes the getEmployeeDetails(employeeId) method in EmployeeManager. The method returns an EmployeeModel object, which contains the data. The JSP then retrieves values from the EmployeeModel object to display the employee data.
// from queryEmployee.jsp <% ... int id = Integer.parseInt(empId); EmployeeManager mgr = SessionHelper.getEmployeeManager(request); EmployeeModel emp = mgr.getEmployeeDetails(id); ... %> ... <h4>Employee Details</h4> <table> <tr><td>Employee ID: </td><td colspan=3><b><%=id%></b></td></tr> <tr><td>First Name: </td><td><b><%=emp.getFirstName()%></b></td><td>Last Name: </td><td><b><%=emp.getLastName()%></b></td></tr> <tr><td>Email: </td><td><b><%=emp.getEmail()%></b></td><td>Phone Number: </td><td><b><%=emp.getPhoneNumber()%></b></td></tr> <tr><td>Hire Date: </td><td><b><%=emp.getHireDate().toString()%></b></td><td>Job: </td><td><b><%=emp.getJobId()%></b></td></tr> </table>
The getEmployeeDetails method in EmployeeManager starts off the following sequence:
The EmployeeBean class implements the ejbFindByPrimaryKey(int empId) method. This method calls the EmployeeDAOImpl class to retrieve data from the database.
// from EmployeeBean.java public Integer ejbFindByPrimaryKey(int empId) throws FinderException { try { if (m_dao == null) m_dao = new EmployeeDAOImpl(); Integer findReturn = m_dao.findByPrimaryKey(empId); return findReturn; } catch (Exception e) { throw new FinderException ("\nSQL Exception in find by primary key.\n" + e.getMessage()); } }
In the EmployeeDAOImpl class the findByPrimaryKey(int id) method queries the database for the specified employee ID. It executes a SELECT statement on the database and returns the employee ID if it finds an employee. If it does not find an employee, it throws an exception.
For benefit data, where a user can have more than one benefit, the application iterates over the Collection.
// from queryEmployee.jsp <h4>Elected Benefits</h4> <table> <% Collection benefits = emp.getBenefits(); if (benefits == null || benefits.size() == 0) { %> <tr><td>None</td></tr> <% } else { Iterator it = benefits.iterator(); while (it.hasNext()) { BenefitItem item = (BenefitItem)it.next(); %> <tr><td><%=item.getName()%></td></tr> <% } // end of while } // end of if %> </table>
For the add and remove operations, the JSPs send which benefit to add or remove, plus the employee ID, to the EmployeeManager. The EmployeeManager adds or removes the benefit and returns the status of the operation.
The add and remove benefits operations follow similar sequences in presenting a list of benefits to the user, and executing the add or remove operation on the database.
<a href="/empbft/controller?empID=<%=id%>&action=addBenefitToEmployee"> Add benefits to the employee</a>
<a href="/empbft/controller?empID=<%=id%>&action=removeBenefitFromEmployee"> Remove benefits from the employee</a>
See the following sections for details on the add and remove operations.
Figure 6-4 shows the events that occur when a user selects the add benefit option:
addBenefitToEmployee
) and invokes the performAction method in the corresponding class, AddBenefitToEmployee.
addBenefitToEmployee
), but this time, it has a benefits parameter that specifies which benefits to add.
To show a list of benefits that the user can add, the addBenefitToEmployee.jsp page gets a list of benefits that the user does not have. The JSP file gets an instance of EmployeeManager, then invokes the getUnelectedBenefitItems method.
// from addBenefitToEmployee.jsp <% int empId = Integer.parseInt(request.getParameter( SessionHelper.EMP_ID_PARAMETER)); EmployeeManager mgr = SessionHelper.getEmployeeManager(request); Collection unelected = mgr.getUnelectedBenefitItems(empId); ... %>
The getUnelectedBenefitItems method gets the master list of all benefits from BenefitCatalog, then it gets a list of benefits for the employee. It compares the two lists and returns a list of benefits that the employee does not have.
// from EmployeeManagerBean.java public Collection getUnelectedBenefitItems(int id) throws RemoteException { Collection allBenefits = null; InitialContext initial = new InitialContext(); Object objref = initial.lookup(AppJNDINames.BENEFIT_CATALOG_EJBHOME); BenefitCatalogHome home = (BenefitCatalogHome) PortableRemoteObject.narrow(objref, BenefitCatalogHome.class); BenefitCatalog catalog = home.create(); allBenefits = catalog.getBenefits(); // ... exceptions omitted ... Collection unelected = new ArrayList(); EmployeeModel emp = this.getEmployeeDetails(id); ArrayList eb = (ArrayList) emp.getBenefits(); if (eb != null && !eb.isEmpty()) { Iterator i = allBenefits.iterator(); while (i.hasNext()) { BenefitModel b = (BenefitModel)i.next(); if (Collections.binarySearch(eb, b) < 0) unelected.add(b); } return unelected; } return allBenefits; }
To add the benefits the user selected, the AddBenefitToEmployee object gets the Employee object and executes the addBenefits method:
// from AddBenefitToEmployee.java String benefits[] = req.getParameterValues(SessionHelper.BENEFIT_PARAMETER); ... int benefitIDs[] = new int[benefits.length]; for (int i = 0; i < benefits.length; i++) { benefitIDs[i] = Integer.parseInt(benefits[i]); } int empId = Integer.parseInt(req.getParameter(SessionHelper.EMP_ID_PARAMETER)); EmployeeManager mgr = SessionHelper.getEmployeeManager(req); try { Employee emp = mgr.getEmployee(empId); emp.addBenefits(benefitIDs); } catch (RemoteException e) { throw new ServletException ( "\nRemote exception while getting employee and adding benefits.\n" + e.getMessage()); } forward(req, res, wireless ? "/successWireless.jsp" : "/success.jsp");
The addBenefits method in the Employee object uses the EmployeeDAOImpl class to connect to the database.
// from EmployeeBean.java public void addBenefits(int benefits[]) { try{ if (m_dao == null) m_dao = new EmployeeDAOImpl(); m_dao.addBenefits(m_emp.getId(), benefits); ejbLoad(); } catch (Exception e) { throw new EJBException ("\nData access exception in adding benefits.\n" + e.getMessage()); } }
After adding the benefits in the database, the addBenefits method calls the ejbLoad method to synchronize the Employee bean with the data in the database.
The addBenefits method in EmployeeDAOImpl connects to the database and sends an INSERT statement.
Figure 6-6 shows the events that occur when a user selects some benefits to remove and clicks the Submit button.
removeBenefitFromEmployee
) and invokes the performAction method in the corresponding class, RemoveBenefitFromEmployee.
removeBenefitFromEmployee
), but this time, it has a benefits parameter that specifies which benefits to remove.
To get a list of benefits that the user can remove, the removeBenefitFromEmployee.jsp gets an EmployeeModel, which contains all the data for an employee, then it calls the getBenefits method in EmployeeModel. It then iterates through the list to display each benefit.
<% int empId = Integer.parseInt(request.getParameter( SessionHelper.EMP_ID_PARAMETER)); EmployeeManager mgr = SessionHelper.getEmployeeManager(request); Collection elected = mgr.getEmployeeDetails(empId).getBenefits(); if (elected == null || elected.size() == 0) { %> <h4>No Benefits to Remove</h4> <p>The employee has not elected any benefits.</p> <h4>Actions</h4> <a href="./controller?action=queryEmployee&empID=<%=empId%>">Query the same employee</a><br> <a href="./controller?action=queryEmployee">Query other employee</a><br> <a href="./">Home</a><br> <% } else { %> <h4>Select Elected Benefits</h4> <% Iterator i = elected.iterator(); while (i.hasNext()) { BenefitItem b = (BenefitItem) i.next(); %> <input type=checkbox name=benefits value=<%=b.getId()%>><%=b.getName()%><br> <% } // End of while %> <h4>Actions</h4> <input type=submit value="Remove Selected Benefits"> <input type=hidden name=empID value=<%=empId%>> <input type=hidden name=action value=<%=SessionHelper.ACTION_REMOVE_BENEFIT_FROM_EMPLOYEE%>> <% } // End of if %>
To remove the benefits the user selected, the RemoveBenefitFromEmployee object gets the Employee object and executes the removeBenefits method:
// from RemoveBenefitFromEmployee.java String benefits[] = req.getParameterValues(SessionHelper.BENEFIT_PARAMETER); String client = req.getParameter(SessionHelper.CLIENT_TYPE_PARAMETER); boolean wireless = client != null && client.equals(SessionHelper.CLIENT_TYPE_WIRELESS); if(benefits == null) { forward(req, res, wireless ? "/removeBenefitFromEmployeeWireless.jsp" : "/removeBenefitFromEmployee.jsp"); } else { int benefitIDs[] = new int[benefits.length]; for (int i = 0; i < benefits.length; i++) { benefitIDs[i] = Integer.parseInt(benefits[i]); } int empId = Integer.parseInt(req.getParameter( SessionHelper.EMP_ID_PARAMETER)); EmployeeManager mgr = SessionHelper.getEmployeeManager(req); try { Employee emp = mgr.getEmployee(empId); emp.removeBenefits(benefitIDs); } catch (RemoteException e) { throw new ServletException ( "Remote exception while getting employee and removing his/her benefits." + e.getMessage()); } forward(req, res, wireless ? "/successWireless.jsp" : "/success.jsp"); }
The removeBenefits method in the Employee object uses the EmployeeDAOImpl class to connect to the database.
// from EmployeeBean.java public void removeBenefits(int benefits[]) { try { if (m_dao == null) m_dao = new EmployeeDAOImpl(); m_dao.removeBenefits(m_emp.getId(), benefits); ejbLoad(); } catch (Exception e) { throw new EJBException ("\nData access exception in removing benefits.\n" + e.getMessage()); } }
After removing the benefits from the database, the removeBenefits method calls the ejbLoad method to synchronize the Employee bean with the data in the database.
The removeBenefits method in EmployeeDAOImpl connects to the database and sends a DELETE statement.
|
Copyright © 2002 Oracle Corporation. All Rights Reserved. |
|