Intro - Part 1 - Part 2 - Part 3 - Part 4

Part 2: Kick-start the development cycle

Intro

In the first part you did the hard (and maybe confusing) stuff: setting it all up! Now comes the easy bit which is adding some functionality. Typically for a development cycle, you add a feature, then test it, and add more functionality, test again, ...

Change the .mlg file

To add a new task the following steps need to be executed:

1) get the next sequence value for the id

2) make up a a rank (sort order): add 1 to the maximum existing rank

3) insert the above values, together with the new task description into the db

Get the next sequence method

Add the method getNextSequenceValue() to the service definition in your Todo.mlg file as follows:

service TodoService
{
    ..

    int getNextSequenceValue() 
    <<
        select nextval('seq_todo')
    >>
    ..

The method to get the highest rank

Also add this method, to the same TodoService definition in Todo.mlg :


    int getMaxRank() 
    <<
        select max(rank) from t_todo 
    >>

Define the method to insert into the table

Add this method to Todo.mlg too.

 
    void insertTask( int id, String description, int rank)
    <<
        insert into t_todo(todo_id, description, rank)
        values(?, ?, ?)
    >>

The java method

The above 3 methods need to be called in 1 servercall, and that happens in this Java method:

   int addNewTask(String task)
   {{
        int newId=getNextSequenceValue();
        int newRank=1+getMaxRank();
        insertTask(newId,task,newRank);
        return newId;
   }}

So also add the above method to your Todo.mlg file. It should now look like this.

You probably noticed it already: to differentiate a method with a SQL-body from a method with a Java body you have to use the .. notation.

Why double curly braces and why not single ? Just a convention, and to remind you that it's not PURE java you are writing here, there are things lacking like the public/private/protected modifiers, exeception handler,... etc.

One final thing to do now is to have amalegeni regenerate the bean(s) and service classes.

cd ~/workspace/Todo
amalegeni -t template/gwt.stg src

After doing that, hit the F5 button in Eclipse to refresh the src directory.

Add code to Todo.java code

Change onModuleLoad()

As you have probably noticed, we had an 'Add' button, but no action attached to it. Let's correct that by adding a click-handler.

Add the following line to the onModuleLoad() method:

    addButton.addClickHandler(new ClickHandler(){
            @Override
            public void onClick(ClickEvent event)
            {
                addRandomNewTask(); 
            }});

Add new method addRandomNewTask()

This method gets a random task from method getRandomTask, defined in the next section, and contains callback code, that I copied from the cut-and-paste.txt file (and modified a bit).

    private void addRandomNewTask()
    {
        String newTask=getRandomTask();

        setStatusBarMessage("addNewTask");
        AsyncCallback<Integer> callback = new AsyncCallback<Integer>() 
        {  
            public void onFailure(Throwable caught) 
            {
                String details = caught.getMessage();
                setStatusBarErrorMessage(details);
            }

            public void onSuccess(Integer rv) 
            {
                setStatusBarMessage("added task " + rv);
                refreshTodolist();
            }
        };
        todoServiceAsync.addNewTask(newTask, callback);
    }

Add method getRandomTask

This method just returns a random task.

    private String getRandomTask( )
    {
        String[] actions= {
                 "Check the expiration date of the milk"
                ,"Wash the dishes"
                ,"Shave the dog"
                ,"Unplug all electrical appliances"
                ,"Turn down the temperature"
                ,"Memorize route"
                ,"Check out what's cooking in the pot"
                ,"Get proper insurance"
                ,"Change money"
                ,"Install security device"
                ,"Recharge batteries"
                ,"Get maps"
                ,"Read instructions"
                ,"Ask directions"
        };
        int i=Random.nextInt(actions.length);
        return actions[i];
    }

Note: when you let Eclipse sort out your import statements ensure that the Random is this one: com.google.gwt.user.client.Random

All should be ready now..

.. so fire up your Todo app, and hit that 'Add' button.

For your convenience

Here are the source files that have above changes applied:

Next step

Proceed now to part 3.

© Willem Moors, 2009 - 2013