At line 8 changed 1 line. |
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|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] pattern from Sun says that these objects are useful for de-coupling your presentation layer from your database layer (i.e. for Swing apps). Managers should also be where you put any business logic for your application. |
In the context of [AppFuse], this is called a Manager class. It's main responsibility to act as a bridge between the persistence (DAO) layer and the |
web layer. The [Business Delegate|http://java.sun.com/blueprints/corej2eepatterns/Patterns/BusinessDelegate.html] pattern from Sun says that these objects are useful for de-coupling your presentation layer from your database layer (i.e. for Swing apps). Managers should also be where you put any business logic for your application. |
At line 18 changed 1 line. |
* [3] Run the ManagerTest |
* [4] Run the ManagerTest |
At line 38 changed 1 line. |
import org.appfuse.webapp.form.PersonForm; |
import org.appfuse.model.Person; |
At line 44 changed 1 line. |
private PersonForm personForm; |
private Person person; |
At line 77 changed 1 line. |
personForm = (PersonForm) mgr.getPerson("1"); |
person = (Person) mgr.getPerson("1"); |
At line 80 changed 1 line. |
personForm.getFirstName() != null); |
person.getFirstName() != null); |
At line 84 changed 3 lines. |
personForm = (PersonForm) mgr.getPerson("1"); |
String name = personForm.getFirstName(); |
personForm.setFirstName("test"); |
person = (Person) mgr.getPerson("1"); |
String name = person.getFirstName(); |
person.setFirstName("test"); |
At line 88 changed 2 lines. |
personForm = (PersonForm) mgr.savePerson(personForm); |
assertTrue("name updated", personForm.getFirstName().equals("test")); |
person = (Person) mgr.savePerson(person); |
assertTrue("name updated", person.getFirstName().equals("test")); |
At line 91 changed 2 lines. |
personForm.setFirstName(name); |
mgr.savePerson(personForm); |
person.setFirstName(name); |
mgr.savePerson(person); |
At line 96 changed 2 lines. |
personForm = new PersonForm(); |
personForm = (PersonForm) populate(personForm); |
person = new Person(); |
person = (Person) populate(person); |
At line 99 changed 3 lines. |
personForm = (PersonForm) mgr.savePerson(personForm); |
assertTrue(personForm.getFirstName().equals("Abbie")); |
assertTrue(personForm.getId() != null); |
person = (Person) mgr.savePerson(person); |
assertTrue(person.getFirstName().equals("Abbie")); |
assertTrue(person.getId() != null); |
At line 104 changed 2 lines. |
log.debug("removing personForm, personId: " + |
personForm.getId()); |
log.debug("removing person, personId: " + |
person.getId()); |
At line 108 changed 9 lines. |
mgr.removePerson(personForm); |
|
try { |
personForm = (PersonForm) mgr.getPerson(personForm.getId()); |
fail("Expected 'DAOException' not thrown"); |
} catch (DAOException e) { |
if (log.isDebugEnabled()) { |
log.debug(e); |
} |
mgr.removePerson(person); |
At line 118 changed 2 lines. |
assertTrue(e != null); |
} |
assertNull(mgr.getPerson(person.getId().toString())); |
At line 143 changed 1 line. |
public void removePerson(Object Person) throws Exception; |
public void removePerson(String id) throws Exception; |
At line 147 changed 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/service/**/service and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager. |
Now let's create a PersonManagerImpl class that implements the methods in PersonManager. To do this, create a new class in src/service/**/service and name it PersonManagerImpl.java. It should extend BaseManager and implement PersonManager. |
At line 150 removed 4 lines. |
__IMPORTANT:__ If you're using AppFuse 1.4, make sure that the defaultLong variable in BaseManager.java is equal to null. This [was changed|https://appfuse.dev.java.net/source/browse/appfuse/src/service/org/appfuse/service/BaseManager.java?r1=1.1&r2=1.2] after the 1.4 release and is necessary for inserts to work. |
|
;:''The [BaseManager|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/service/BaseManager.java.html] is for registering different Converters (i.e. [DateConverter|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/util/DateConverter.java.html]) so that BeanUtils.copyProperties knows how to convert Strings → Objects. It also provides a {{convert(Object)}} method that converts POJOs -> Forms and vise-versa. If you have Lists on your POJOs (i.e. for parent-child relationships), you will need to manually convert those if you want to work with indexed properties (see UserManagerImpl.java and the conversion of Roles for an example).'' |
|
At line 174 changed 3 lines. |
Person person = dao.getPerson(Long.valueOf(id)); |
|
return convert(person); |
return dao.getPerson(Long.valueOf(id)); |
At line 180 changed 1 line. |
Person person = (Person) convert(obj); |
Person person = (Person) obj; |
At line 183 changed 1 line. |
return convert(person); |
return person; |
At line 187 changed 1 line. |
Person person = (Person) convert(obj); |
Person person = (Person) obj; |
At line 196 removed 18 lines. |
__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): |
|
[{Java2HtmlPlugin |
|
* @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-service" should work just fine. |
|