In this section we will create the PeopleService. This service exposes three methods to manage information about people working at Northwind. The model for PeopleService is shown below.
Note that we have added a dependency from PeopleService to the Person entity. This will make sure that the PeopleService has access to the PersonDao.
Now add PeopleService to your model. To do this, click the link below for your UML modeling tool.
Follow the steps below to generate code.
Just like data access objects, a service is also generated as a trio of classes: an interface, an abstract base class and a concrete implementation. Here are the 3 classes generated for PeopleService
This section shows how to implement the 3 "handle" methods in PeopleServiceImpl. Open the file PeopleServiceImpl.java under C:/timetracker/core/src/java/org/andromda/timetracker/service and follow the instructions below.
This service method accepts a PersonVO and creates the person in the database. It returns the id of the newly created person. Fill in the implementation of this method as shown bolow.
protected java.lang.Long handleCreatePerson(org.andromda.timetracker.vo.PersonVO personVO)
throws java.lang.Exception
{
Person person = Person.Factory.newInstance();
getPersonDao().personVOToEntity(personVO, person, true);
getPersonDao().create(person);
return person.getId();
}
We first create a new instance of a person in memory using the Person factory. We then initialize the person from the supplied value object - the default implementation of personVOToEntity() works well for this. Now we ask PersonDao to create the person in the database. Finally we return the id of the newly created person to the caller.
This service method accepts a person's id and returns the associated PersonVO. It is implemented using a direct DAO call that retrieves the Person, converts it to a PersonVO and returns it.
protected org.andromda.timetracker.vo.PersonVO handleGetPerson(java.lang.Long id)
throws java.lang.Exception
{
return (PersonVO)getPersonDao().load(PersonDao.TRANSFORM_PERSONVO, id);
}
This service method returns all people in the TimeTracker database as an array of PersonVO objects. It is implemented as a direct call to PersonDao.loadAll(), which returns a collection of PersonVO objects. This collection is converted to an array and returned.
protected org.andromda.timetracker.vo.PersonVO[] handleGetAllPeople()
throws java.lang.Exception
{
Collection people = getPersonDao().loadAll(PersonDao.TRANSFORM_PERSONVO);
return (PersonVO[])people.toArray(new PersonVO[people.size()]);
}
Add the required import statements to the file after the package statement as shown below.
package org.andromda.timetracker.service; import java.util.Collection; import org.andromda.timetracker.domain.Person; import org.andromda.timetracker.domain.PersonDao; import org.andromda.timetracker.vo.PersonVO;
After implementing the code show above, save the file and compile the application.
Now that we have modeled the PeopleService, it is finally time to see all our effort working in a client application. Click here to create a console client application.