Modifying the Enterprise Bean

DRAFT


Previous Next Contents

Modifying the Enterprise Bean

DukesBirthdayBean is a stateless session bean. Stateless session beans are enterprise beans that do not maintain a conversational state with a client. With stateless session beans, the client makes isolated requests that do not depend on any previous state or requests. If an application requires conversational state, use stateful session beans.

DukesBirthdayBean is a local enterprise bean that uses a no-interface view:

  • A local enterprise bean is visible only within the application in which it is deployed.

  • Enterprise beans with a no-interface view do not need a separate business interface that the enterprise bean class implements. The enterprise bean class is the only coding artifact needed to create a local, no-interface enterprise bean.

DukesBirthdayBean will be packaged within the same WAR file as the Facelets web front end.

Implement a Business Method to DukesBirthdayBean that Gets the Average Age Difference of firstcup-war Users

Add code to a business method to the DukesBirthdayBean session bean to call the findAverageAgeDifferenceOfAllFirstcupUsers named query in FirstcupUser that returns the average age difference of all users.

  1. Expand the Source Packages node, expand the firstcup.ejb node, then double-click the DukesBirthdayBean.java file to open it in the editor window.

  2. Find the business method called getAverageAgeDifference and add the following code in bold by copying and pasting:

        public Double getAverageAgeDifference() {
            Double avgAgeDiff = (Double)
                em.createNamedQuery("findAverageAgeDifferenceOfAllFirstcupUsers")
                                    .getSingleResult();
            logger.log(Level.INFO, "Average age difference is: {0}",  avgAgeDiff);
            return avgAgeDiff;
        }

    The named query in FirstcupUser is called by using the createNamedQuery method in EntityManager. Because this query returns a single number, the getSingleResult method is called on the returned Query object. The query returns a Double.

  3. Right-click in the editor window and select Format.

Implement a Business Method for Calculating the Age Difference Between Duke and the User

Add code to a business method that calculates the difference in age in years between Duke and the user and creates a new FirstcupUser entity.

  1. Find the getAgeDifference business method and add the following code in bold:

        public int getAgeDifference(Date date) {
            int ageDifference;
    
            Calendar theirBirthday = new GregorianCalendar();
            Calendar dukesBirthday = new GregorianCalendar(1995, Calendar.MAY, 23);
    
            // Set the Calendar object to the passed-in Date
            theirBirthday.setTime(date);
    
            // Subtract the user's age from Duke's age
            ageDifference = dukesBirthday.get(Calendar.YEAR)
                    - theirBirthday.get(Calendar.YEAR);
            logger.log(Level.INFO, "Raw ageDifference is: {0}",  ageDifference);
            // Check to see if Duke's birthday occurs before the user's. If so,
            // subtract one from the age difference
            if (dukesBirthday.before(theirBirthday) && (ageDifference> 0)) {
                ageDifference--;
            }
    
            // Create and store the user's birthday in the database
            FirstcupUser user = new FirstcupUser(date, ageDifference);
            em.persist(user);
    
            logger.log(Level.INFO, "Final ageDifference is: {0}",  ageDifference);
    
            return ageDifference;
        }

    This method creates the Calendar objects used to calculate the difference in age between the user and Duke and performs the actual calculation of the difference in age.

    Similar to the DukesAgeResource.getText code, getAgeDifference subtracts Duke’s birthday year from the user’s birthday year to get a raw age difference. If Duke’s birthday falls before the user’s, and the raw difference is more than 0, it subtracts one year from the age difference.

    A new FirstcupUser entity is created with the user’s birthday and age difference, then stored in Derby by calling the persist method in EntityManager.

    The final age difference is returned as an int.

  2. Right-click in the editor window and select Format.

  3. From the File menu, choose Save.


Previous Next Contents
Eclipse Foundation Logo  Copyright © 2019, Oracle and/or its affiliates. All rights reserved.

DRAFT