| At line 76 changed 1 line. |
| assertTrue(personForm.getRoles().size() == 1); |
| assertTrue(personForm.getFirstName() != null); |
| At line 94 changed 2 lines. |
| // call populate method in super class to populate test data |
| // from a properties file matching this class name |
| <span style="color: green">// call populate method in super class to populate test data |
| // from a properties file matching this class name</span> |
| At line 147 added 2 lines. |
| Now let's create a PersonManagerImpl class that implements the methods in PersonManager and uses [BeanUtils.copyProperties|http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/BeanUtils.html#copyProperties(java.lang.Object,%20java.lang.Object)] |
| 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. |
| At line 150 added 94 lines. |
| ;:''The [BaseManager|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/webapp/service/BaseManager.java.html] 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 { |
| <span style="color: green">//~ Instance fields ========================================================</span> |
|
| private Log log = LogFactory.getLog(PersonManagerImpl.class); |
| private PersonDao dao; |
| private Person person; |
| private PersonForm personForm; |
|
| <span style="color: green">//~ Constructors ===========================================================</span> |
|
| public PersonManagerImpl(Object conn) |
| throws Exception { |
| dao = (PersonDao) DAOFactory.getInstance(conn, PersonDao.class); |
| } |
|
| <span style="color: green">//~ Methods ================================================================</span> |
|
| public Object getPerson(String personname) throws Exception { |
| person = dao.getPerson(personname); |
|
| return convert(person); |
| } |
|
| public Object savePerson(Object obj) throws Exception { |
| if (person == null) { |
| person = new Person(); |
| } |
| personForm = (PersonForm) obj; |
| |
| <span style="color: green">// copy form properties to person</span>> |
| try { |
| BeanUtils.copyProperties(person, personForm); |
| } catch (IllegalArgumentException i) { |
| log.error("Exception occured copying properties", i); |
| throw new ConversionException(); |
| } |
|
| <span style="color: green">// 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</span> |
| if (ObjectUtils.equals(person.getId(), new Long(0))) { |
| person.setId(null); |
| } |
|
| dao.savePerson(person); |
|
| return getPerson(person.getId()); |
| } |
|
| 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. |
|