Creating a service

Let's create a service to store a Tweet model to the datastore.

Double-click build.xml under the project.
In the Outline view, right-click gen-gwt-service task and select Run as > Ant Build(first item).
In the ant input dialog, input "TwitterService", and click OK button.

If your project is not refreshed automatically, in the Window menu, select Preferences > General > Workspace. Click "Refresh automatically" checkbox. Or refresh your project manually.

"tutorial.client.service.TwitterService", "tutorial.client.service.TwitterServiceAsync", "tutorial.server.service.TwitterServiceImpl" and "tutorial.server.service.TwitterServiceImplTest" will be created as follows:

TwitterService.java

package tutorial.client.service;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("service.s3gwt")
public interface TwitterService extends RemoteService {

}

TwitterServiceAsync.java

package tutorial.client.service;

public interface TwitterServiceAsync {

}

TwitterServiceImpl.java

package tutorial.server.service;

import tutorial.client.service.TwitterService;

public class TwitterServiceImpl implements TwitterService {

}

TwitterServiceImplTest.java

package tutorial.server.service;

import org.slim3.tester.ServletTestCase;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

public class TwitterServiceImplTest extends ServletTestCase {

    private TwitterServiceImpl service = new TwitterServiceImpl();

    @Test
    public void test() throws Exception {
        assertThat(service, is(notNullValue()));
    }
}

Run the "TwitterServiceImplTest.java". The result will be green(OK).

I want to implement a method to store a tweeted content to the datastore, so I write the test first as follows:

TwitterServiceImplTest.java

package tutorial.server.service;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.slim3.datastore.Datastore;
import org.slim3.tester.ServletTestCase;

import tutorial.shared.model.Tweet;

public class TwitterServiceImplTest extends ServletTestCase {

    private TwitterServiceImpl service = new TwitterServiceImpl();

    @Test
    public void test() throws Exception {
        assertThat(service, is(notNullValue()));
    }
   
    @Test
    public void tweet() throws Exception {
        service.tweet("Hello");
        Tweet tweet = Datastore.query(Tweet.class).asSingle();
        assertThat(tweet, is(notNullValue()));
        assertThat(tweet.getContent(), is("Hello"));
    }
}

To query a model, you use Datastore.query(Class modelClass).asSingle().
See Query Single Result.
A compile error occurs. Compiler says "The method tweet is undefined for the type TwitterServiceImpl", so let's define it.

TwitterService.java

package tutorial.client.service;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("service.s3gwt")
public interface TwitterService extends RemoteService {

    void tweet(String content);
}


TwitterServiceAsync.java

package tutorial.client.service;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface TwitterServiceAsync {

    void tweet(String content, AsyncCallback<Void> callback);

}

TwitterServiceImpl.java

package tutorial.server.service;

import org.slim3.datastore.Datastore;

import tutorial.client.service.TwitterService;
import tutorial.shared.model.Tweet;

public class TwitterServiceImpl implements TwitterService {

    public void tweet(String content) {
        Tweet tweet = new Tweet();
        tweet.setContent(content);
        Datastore.put(tweet);
    }
}

The compile error is gone, so run the "TwitterServiceImplTest.java". The result will be green(OK).
Then let's modify Main.java to call TwitterService#tweet() as follows:

Main.java

package tutorial.client;

import tutorial.client.service.TwitterService;
import tutorial.client.service.TwitterServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class Main extends Composite {

    private static MainUiBinder uiBinder = GWT.create(MainUiBinder.class);
   
    private static TwitterServiceAsync service = GWT.create(TwitterService.class);

    interface MainUiBinder extends UiBinder<Widget, Main> {
    }
   
    @UiField
    TextArea content;

    public Main() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @UiHandler("tweet")
    void handleClick(ClickEvent e) {
        tweet();
    }
   
    void tweet() {
        service.tweet(content.getText(), new AsyncCallback<Void>() {
           
            public void onSuccess(Void result) {
                Window.alert("Success!!!");
                content.setText(null);
            }
           
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }
        });
    }

}

Let's refresh the browser, and input something, and post it.

After tweeting, visit http://localhost:8888/_ah/admin. In Entity Kind list select Tweet, and click "List Entities" button. You can confirm the tweeted contents.


Next...

Continue to Listing tweets.
Comments