Complete the TimeTracker Model

In this section we will add the remaining functionality to the TimeTracker model. Since you now understand most of the important modeling concepts, we will give detailed instructions only when necessary. So roll up your sleeves and let's get started.

Add Remaining Entities

Open the Domain Objects diagram and add the remaining detail to it as shown below.

  • TimeAllocation represents the period of time for which a person worked on a specific task. This time period is specified using an EmbeddedValue of type TimePeriod. An EmbeddedValue is specified by marking the class with the EmbeddedValue stereotype. Unlike entities, embedded values do not have a separate identity in the database. They are embedded inside entity tables and are considered to be part of the entity. Note that you must create the TimePeriod embedded value before you can specify it as a type inside the TimeAllocation class.
  • Timecards may contain several TimeAllocations. A Task can also be associated with many TimeAllocations.
  • The Timecard end of the first association is marked as a composite. This means that a TimeAllocation cannot exist without a Timecard.
  • Since TimeAllocation is so tightly connected to a Timecard we would like to use this fact to simplify our code. When a timecard is saved, all the associated allocations should be saved automatically. Similarly when an allocation is removed from a timecard, it should be automatically deleted form the database. This is specified by setting the andromda-hibernate-cascade tagged value to all-delete-orphan. Follow the appropriate link below to learn how to add a tagged value to the association end.

    ArgoUML MagicDraw

Add Custom Behavior to Entities

So far we have relied on the default behavior of entities as provided by AndroMDA. Every entity is generated as a Plain Old Java Object (POJO) along with a Data Access Object (DAO). Both these objects have default behaviors as implemented by the auto-generated methods. Let us see how we can instruct AndroMDA to provide hooks for injecting custom behavior. Create a new class diagram under the domain package and call it "Domain Objects - Behavior". We like to add behavioral details to classes in a separate diagram. This way class diagrams that show purely structural detail, such as the diagram shown above, are kept simple and easy to understand. Add the custom methods shown below in the new diagram you just created. Since Person and Timecard entities already exist in our model, they can be simply dragged on to this diagram. Then add the methods to the two entities.

  • The findByUsername() method finds a person given their username. The "@" sign in front of the method indicates that this is a query method. Query methods are applicable when you are trying to find one or more entities based on their attributes. The good news is that AndroMDA can completely implement query methods without your help. It will generate the appropriate Hibernate query and return the result as one or more objects depending on the return type you specify.
  • As you know the association between Timecard and TimeAllocation is navigable both ways. So each object must keep a reference to the other. addTimeAllocation() is a convenience method that does just that - all in one shot. AndroMDA will generate a blank implementation of this method in TimecardImpl. We will fill in the details.

Now generate code for your modified model by executing the command maven -o clean install. Open the file PersonDaoBase.java under core/target/src/org/andromda/timetracker/domain. You will find that AndroMDA has completely implemented the findByUsername() method in this file. The Hibernate query that it generates is shown below.

public
Object findByUsername(
final
int
transform,
final
java.lang.String username) {
return
this
.findByUsername( transform,
"from org.andromda.timetracker.domain.Person as person where person.username = :username"
, username); }

Now let us complete the implementation of the addTimeAllocation() method. Open the file TimecardImpl.java under the src tree (core/src/java/org/andromda/timetracker/domain). Locate the dummy implementation of addTimeAllocation() and replace it with the one below. This implementation makes sure that Timecard and TimeAllocation both have references to each other.

public
void
addTimeAllocation(org.andromda.timetracker.domain.TimeAllocation timeAllocation) { getAllocations().add(timeAllocation); timeAllocation.setTimecard(
this
); }

Make sure your code compiles. All you need to do is to build the core package. You can do this by executing the command maven -o core.

Add Remaining Value Objects

Open the Value Objects diagram and add the remaining detail to it as shown below.

There is really nothing new here except that we are providing two views of the Timecard entity. The TimecardSummaryVO is very light and can be used to display lists of timecards. The TimecardVO is relatively heavier because it includes the contained TimeAllocations in addition to the TimecardSummaryVO attributes. This value object will be used when we want to show the details of an individual timecard. Note that TimecardVO inherits from TimecardSummaryVO, thus inheriting all of its attributes.

Now generate code for your modified model by executing the command maven -o clean install. Make sure you get a BUILD SUCCESSFUL message.

Add TimeTrackingService

We will now add TimeTrackingService to the model. This service supports all time tracking operations that a front-end might use. Open the Services diagram and add TimeTrackingService to it as shown below.

Now generate code for your modified model by executing the command maven -o clean install. Make sure you get a BUILD SUCCESSFUL message.

Create the Schema

Now that the structure of our entities is pretty much finalized, we can create the corresponding schema in the database. As you may remember, AndroMDA automatically generates DDL for you. This DDL is located at C:/timetracker/core/target/schema-create.sql. Open this file and review its contents. You should see the SQL to create all the needed tables and constraints. Execute the command maven -o create-schema to create this schema in your database.

Note that PostgreSQL does not like the first statement in schema-create.sql (drop sequence hibernate_sequence). Please comment out this line for PostgreSQL.

What's Next?

We will now enhance the console application to create and read timecards using the TimeTrackingService. Click here to enhance the console application.