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.
C:/timetracker
.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
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.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.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.
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.
protectedjava.lang.Long handleCreatePerson(org.andromda.timetracker.vo.PersonVO personVO)throwsjava.lang.Exception { Person person = Person.Factory.newInstance(); getPersonDao().personVOToEntity(personVO, person,true); getPersonDao().create(person);returnperson.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.
protectedorg.andromda.timetracker.vo.PersonVO handleGetPerson(java.lang.Long id)throwsjava.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.
protectedorg.andromda.timetracker.vo.PersonVO[] handleGetAllPeople()throwsjava.lang.Exception { Collection people = getPersonDao().loadAll(PersonDao.TRANSFORM_PERSONVO);return(PersonVO[])people.toArray(newPersonVO[people.size()]); }
Add the required import statements to the file after the package statement as shown below.
packageorg.andromda.timetracker.service;importjava.util.Collection;importorg.andromda.timetracker.domain.Person;importorg.andromda.timetracker.domain.PersonDao;importorg.andromda.timetracker.vo.PersonVO;
After implementing the code show above, save the file and compile the application.
C:/timetracker
.maven -o install
. Make sure you get a
BUILD SUCCESSFUL
message.
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.