Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_pt
CreateActions
CreateDAO
CreateDAO_sp
CreateDAOiBATIS
CreateDAOiBATIS_ko
CreateManager_es
JSFBeans
SpringControllers
...and 3 more




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateManager


This is version 16. It is not the current version, and thus it cannot be edited.
[Back to current version]   [Restore this version]


Part II: Creating new Managers - A HowTo for creating Business Delegates that talk to the database tier (DAOs) and the web tier (Struts Actions).

About this Tutorial

This tutorial will show you how to create a Business Delegate class (and a JUnit) to talk to the DAO we created in Part I of this tutorial.

In the context of AppFuse, this is called a Manager class. It's main responsibility is converting backend data (POJOs) into front-end data (Struts ActionForms). The main reason I even use Managers, rather than just calling the DAOs directly is testability. It's nice to be able to populate a Form manually (in the test) and call the DAO to persist it, and verify the database gets the proper results. The Business Delegate pattern from Sun says that these objects are usefull for de-coupling your presentation layer from your database layer. Managers should also be where you put any business logic for your application.

I will tell you how I do stuff in the Real World in text like this.

Let's get started on creating a new ManagerTest and Manager in AppFuse's architecture.

Table of Contents

  • [1] Create a new ManagerTest to run JUnit tests on the Manager
  • [2] Create a new Manager to talk to the DAO
  • [3] Run the ManagerTest

Create a new ManagerTest to run JUnit tests on the Manager [#1]

Since we're continuing from Part I, let's create a JUnit test for the PersonManager. Create PersonManagerTest in the test/web/**/service directory. We'll want to test the same basic methods (get, save, remove) that our DAO has. This may seem redundant (why all the tests!), but these tests are GREAT to have 6 months down the road. This class should extend BaseManagerTestCase, which already exists in the service package. The parent class (BaseManagerTestCase) serves the same functionality as the BaseDaoTestCase - to load a properties file that has the same name as your *Test.class.
I usually copy (open → save as) an existing test (i.e. UserManagerTest.java) and find/replace [Uu]ser with [Pp]erson, or whatever the name of my object is.

The code below is what we need for a basic JUnit test of our Managers. It created and destroys our PersonManager. The "conn" object is initialized (and obtains a connection) in the BaseManagerTestCase class.

package org.appfuse.webapp.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.appfuse.webapp.form.PersonForm;

public class PersonManagerTest extends BaseManagerTestCase {
    //~ Instance fields ========================================================

    private PersonManager mgr = null;
    private Log log = LogFactory.getLog(PersonManagerTest.class);
    private PersonForm personForm;

    //~ Constructors ===========================================================

    public PersonManagerTest(String name) {
        super(name);
    }

    //~ Methods ================================================================

    protected void setUp() throws Exception {
        super.setUp();
        mgr = new PersonManagerImpl(conn);
    }

    protected void tearDown() throws Exception {
        super.tearDown();
        mgr = null;
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(PersonManagerTest.class);
    }
}
Now that we have the JUnit framework down for this class, let's add the meat: the test methods to make sure everything works in our Manager. Here's a snippet from the DAO Tutorial tutorial to help you understand what we're about to do.
...we create methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our <junit> task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous.

Add the following methods to your PersonManagerTest.java file:

    public void testGetPerson() throws Exception {
        personForm = (PersonForm) mgr.getPerson("1");

        if (log.isDebugEnabled()) {
            log.debug(personForm);
        }

        assertTrue(personForm != null);
        assertTrue(personForm.getFirstName() != null);
    }

    public void testSavePerson() throws Exception {
        personForm = (PersonForm) mgr.getPerson("1");
        personForm.setFirstName("Joe");

        if (log.isDebugEnabled()) {
            log.debug("saving person with updated firstName: " + personForm);
        }

        personForm = (PersonForm) mgr.savePerson(personForm);
        assertTrue(personForm.getFirstName().equals("Joe"));
    }

    public void testAddAndRemovePerson() throws Exception {
        personForm = new PersonForm();

        // call populate method in super class to populate test data
        // from a properties file matching this class name
        personForm = (PersonForm) populate(personForm);

        personForm = (PersonForm) mgr.savePerson(personForm);
        assertTrue(personForm.getLastName().equals("Raible"));

        if (log.isDebugEnabled()) {
            log.debug("removing person...");
        }

        mgr.removePerson(personForm);

        try {
            personForm = (PersonForm) mgr.getPerson("1");
            fail("Expected 'Exception' not thrown");
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug(e);
            }

            assertTrue(e != null);
        }
    }
This class won't compile at this point because we have not created our PersonManager nor the PersonManagerImpl. Unlike the DAO class, I'm not using a Factory pattern. Rather, I'm instantiating new Managers using:
Manager mgr = new ManagerImp(conn);
If I followed recommended patterns a bit more, I'd do:
Manager mgr = ManagerFactory.getInstance(conn, PersonManager.class);
I don't have any reason for not doing the factory pattern on Managers, I just simply haven't done it. I am creating Interfaces for the Managers - in case I decide I need new ManagerImpls someday. Then it'd be easy to create a Factory and different implementations.
I think it's funny how I've followed so many patterns to allow extendibility in AppFuse. In reality, on most projects I've been on - I learn so much in a year that I don't want to extend the architecture - I want to rewrite it. Hopefully by keeping AppFuse up to date with my perceived best practices, this won't happen as much. Each year will just be an upgrade to the latest AppFuse, rather than a re-write. ;-)

Create a new Manager to talk to the DAO [#2]

First off, create a PersonManager.java interface in the src/web/**/service directory and specify the basic CRUD methods for any implementation classes. I've eliminated the JavaDocs in the class below for display purposes.
As usual, I usually duplicate (open → save as) an existing file (i.e. UserManager.java).
package org.appfuse.webapp.service;

public interface PersonManager {

    public Object getPerson(String id) throws Exception;

    public Object savePerson(Object person) throws Exception;

    public void removePerson(Object Person) throws Exception;
}
Now let's create a PersonManagerImpl class that implements the methods in PersonManager and uses BeanUtils.copyProperties to convert POJOs → ActionForms and ActionForms → POJOs. To do this, create a new class in src/web/**/service and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager.
The BaseManager is for registering different Converters (i.e. DateConverter) so that BeanUtils.copyProperties knows how to convert Strings → Objects. It currently does not have any convenience methods, but does provide a nice place to add them for all your managers.
package org.appfuse.webapp.service;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.appfuse.persistence.DAOFactory;
import org.appfuse.persistence.Person;
import org.appfuse.persistence.PersonDao;
import org.appfuse.webapp.form.PersonForm;

public class PersonManagerImpl extends BaseManager implements PersonManager {
    //~ Instance fields ========================================================

    private Log log = LogFactory.getLog(PersonManagerImpl.class);
    private PersonDao dao;
    private Person person;
    private PersonForm personForm;

    //~ Constructors ===========================================================

    public PersonManagerImpl(Object conn)
    throws Exception {
        dao = (PersonDao) DAOFactory.getInstance(conn, PersonDao.class);
    }

    //~ Methods ================================================================

    public Object getPerson(String id) throws Exception {
        person = dao.getPerson(Long.valueOf(id));
        
        personForm = new PersonForm();
        BeanUtils.copyProperties(personForm, person);
        
        return personForm;
    }

    public Object savePerson(Object obj) throws Exception {
        if (person == null) {
            person = new Person();
        }
        personForm = (PersonForm) obj;
        
        // copy form properties to person
        try {
            BeanUtils.copyProperties(person, personForm);
        } catch (IllegalArgumentException i) {
            log.error("Exception occured copying properties", i);
            throw new ConversionException();
        }

        // BeanUtils.copyProperties converts empty Strings to 
        // Longs with a value of 0 to prevent a NPE.
        // Setting the defaultLong to null (in BaseManager) is another solution 
        if (ObjectUtils.equals(person.getId(), new Long(0))) {
            person.setId(null);
        }

        dao.savePerson(person);

        return getPerson(person.getId().toString());
    }

    public void removePerson(Object obj) throws Exception {
        if (person == null) {
            person = new Person();
        }
        personForm = (PersonForm) obj;
        if (log.isDebugEnabled()) {
            log.debug("removing person: " + 
                      personForm.getFirstName() + " " + 
                      personForm.getLastName());
        }

        dao.removePerson(person);
    }
You should be able to compile everything now using "ant compile-web". Doh! I guess not:
PersonManagerImpl.java:11: cannot resolve symbol
    [javac] symbol  : class PersonForm 
    [javac] location: package form
    [javac] import org.appfuse.webapp.form.PersonForm;
We need to add XDoclet tags to the Person.java Object to create our Struts ActionForm. In the JavaDoc for the Person.java file, add the following @struts.form tags (use User.java if you need an example):
* @struts.form include-all="true" extends="BaseForm"
We extend org.appfuse.webapp.form.BaseForm because it has a toString() method that allows us to call log.debug(formName) to print out a reader-friendly view of the Form object.

Now, running "ant compile-web" should work just fine.

Finally, we need to create the PersonManagerTest.properties file in test/web/**/service so that personForm = (PersonForm) populate(personForm); will work in our test.

Duplicating (and renaming) the PersonDaoTest.properties is the easy route. Alternatively, you could set the properties manually. Make sure and add an id property.
id=1
firstName=Matt
lastName=Raible

Run the ManagerTest [#3]

Save all your edited files and try running "ant test-web -Dtestcase=PersonManager" one more time.

Yeah Baby, Yeah: BUILD SUCCESSFUL
Total time: 13 seconds

You'll notice a nice long StackTrace for an ObjectNotFound error - caused by the last part of the "testAddAndRemovePerson" method in PersonManagerTest. You can remove it by removing the e.printStackTrace() in BaseDaoHibernate.retrieveObject(Session ses, Class clazz, Long id).

Next Up: Part II: Creating new Managers - A HowTo for creating Business Delegates that talk to the database tier (DAOs) and the web tier (Struts Actions).



Go to top   More info...   Attach file...
This particular version was published on 06-Nov-2006 13:52:32 MST by MattRaible.