Create a Service

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.

ArgoUML MagicDraw

Generate Code

Follow the steps below to generate code.

  1. Open a Command Prompt and change your directory to C:/timetracker.
  2. Execute the command maven -o clean install. Make sure you get a BUILD SUCCESSFUL message.

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

  1. PeopleService.java: PeopleService is the interface that specifies the service methods. Since this interface is needed by client applications as well as the service implementation, it is generated in the target branch of the common project.
  2. PeopleServiceBase.java: PeopleServiceBase implements the methods specified by the PeopleService interface. These methods essentially do some parameter checking and then delegate the actual business functionality to "handle" methods. Handle methods are expected to be implemented manually in the PeopleServiceImpl class. PeopleServiceBase also contains references to DAOs that the service depends on. PeopleServiceBase.java is generated in the target branch of the core project.
  3. PeopleServiceImpl.java: PeopleServiceImpl is a concrete extension of the PeopleServiceBase class. This is where developers are expected to code the main business logic for the service methods. PeopleServiceImpl.java is generated in the source branch of the core project.

Implement Service Methods

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.

handleCreatePerson()

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.

handleGetPerson()

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); }

handleGetAllPeople()

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()]); }

Imports

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.

  1. Open a Command Prompt and change your directory to C:/timetracker.
  2. Execute the command maven -o install. Make sure you get a BUILD SUCCESSFUL message.

What's Next?

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.