Create a Console Application

In this section we will try out the PeopleService using a simple console application.

  1. Download the console application project and unzip it in the timetracker directory (C:/timetracker). You will get a new directory called C:/timetracker/console. This directory contains three files - project.xml, maven.xml (Maven project files) and TimeTrackerConsole.java (the console application, under src/java/org/andromda/timetracker/console).
  2. Add two new targets in C:/timetracker/maven.xml (the top-level maven.xml file) - these targets allow you to build and run the console application from the C:/timetracker directory. The two new targets are called ttconsole and run. Place these targets after the mda target as shown below:
        
    <!-- ================================================================== Runs the MDA component ================================================================== -->
    <
    goal
    name=
    "mda"
    > <
    maven:maven
    descriptor=
    "mda/project.xml"
    goals=
    "pom:install"
    /
    > <
    /goal
    >
    <!-- ================================================================== Builds the Console component ================================================================== -->
    <
    goal
    name=
    "ttconsole"
    > <
    maven:maven
    descriptor=
    "console/project.xml"
    goals=
    "jar:install"
    /
    > <
    /goal
    >
    <!-- ================================================================== Runs the Console component ================================================================== -->
    <
    goal
    name=
    "run"
    > <
    maven:maven
    descriptor=
    "console/project.xml"
    goals=
    "run"
    /
    > <
    /goal
    >
  3. Review the console application in TimeTrackerConsole.java. Key methods of this application are shown below:
    public
    static
    void
    main(String[] args) {
    // Get services
    serviceLocator = ServiceLocator.instance(); peopleService = serviceLocator.getPeopleService();
    // Create people
    PersonVO naresh = createPerson(
    "nbhatia"
    ,
    "Naresh"
    ,
    "Bhatia"
    ); PersonVO louis = createPerson(
    "lcoude"
    ,
    "Louis"
    ,
    "Coude"
    ); PersonVO john = createPerson(
    "jsmith"
    ,
    "John"
    ,
    "Smith"
    );
    // Fetch and show all objects created above
    PersonVO[] people = peopleService.getAllPeople(); showPeople(people); }
    private
    static
    PersonVO createPerson(String username, String firstName, String lastName) { PersonVO person =
    new
    PersonVO(
    null
    , username, firstName, lastName); person.setId(peopleService.createPerson(person)); System.out.println(
    "Person "
    + person.getId() +
    " created - "
    + person.getUsername());
    return
    person; }
    As you can see the logic of the console application is very simple. We first need to get a reference to the PeopleService. We do this by using the ServiceLocator, a helper class generated by AndroMDA that uses the Spring framework to locate services. Now we create three people in the database using the PeopleService. Following that we fetch all the people that exist in the database, again using the PeopleService, and display them.
  4. Build the console application by executing the ttconsole target as follows: maven -o ttconsole.
  5. Run the console application by executing the run target as follows: maven -o run. The console output is shown below.
    C:/timetracker>maven -o run
        ...
        [java] Person 1 created - nbhatia
        [java] Person 2 created - lcoude
        [java] Person 3 created - jsmith
        [java] People:
        [java] 1: nbhatia - Naresh Bhatia
        [java] 2: lcoude - Louis Coude
        [java] 3: jsmith - John Smith
        [java]
    BUILD SUCCESSFUL
    Total time: 8 seconds
    Finished at: Mon Jan 30 01:41:58 EST 2006
    C:/timetracker>
                            
  6. Drop the database schema in preparation for the following sections of the tutorial where we will add more tables to our timetracker database. To do this, execute the drop-schema target as follows: maven -o drop-schema.

What's Next?

Now that we have a running console application with 1 entity, 1 value object and 1 service, we are ready to implement more TimeTracker functionality. Click here to add a Timecard object to TimeTracker and learn how to model associations in AndroMDA. application.