Listing tweets

I want to show tweeted contents, so I define the test first as follows:

TwitterServiceImplTest.java

package tutorial.server.service;

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

import java.util.List;

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 getTweetList() throws Exception {
        Tweet tweet = new Tweet();
        tweet.setContent("Hello");
        Datastore.put(tweet);
        List<Tweet> tweetList = service.getTweetList();
        assertThat(tweetList.size(), is(1));
        assertThat(tweetList.get(0).getContent(), is("Hello"));
    }
}


A compile error occurs. Compiler says "The method getTweetList is undefined for the type TwitterServiceImpl", so let's define it.

TwitterService.java

package tutorial.client.service;

import java.util.List;

import tutorial.shared.model.Tweet;

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

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

    ...
   
    List<Tweet> getTweetList();
}

TwitterServiceAsync.java

package tutorial.client.service;

import java.util.List;

import tutorial.shared.model.Tweet;

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

public interface TwitterServiceAsync {

   ...

    void getTweetList(AsyncCallback<List<Tweet>> callback);

}

TwitterServiceImpl.java

package tutorial.server.service;

import java.util.List;

import org.slim3.datastore.Datastore;

import tutorial.client.service.TwitterService;
import tutorial.server.meta.TweetMeta;
import tutorial.shared.model.Tweet;

public class TwitterServiceImpl implements TwitterService {

    private TweetMeta t = new TweetMeta();
   
    ...

    public List<Tweet> getTweetList() {
        return Datastore.query(t).sort(t.createdDate.desc).asList();
    }
}


TweetMeta is created by Annotation Processor Tool automatically.
See Meta data of model.
You can query using a meta data of a model.
See Introducing Queries.

The compile error of "TwitterServiceImplTest.java" is gone, so run the "TwitterServiceImplTest.java". The result will be green(OK).
Then let's modify "Main.ui.xml" and "Main.java".

Main.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">
    <ui:style>
       
    </ui:style>
    <g:HTMLPanel>
    What's happening?<br />
    <g:TextArea ui:field="content"></g:TextArea><br />
    <g:Button ui:field="tweet">Tweet</g:Button>
    <g:Button ui:field="refresh">Refresh</g:Button><br />
    <g:FlexTable ui:field="table"></g:FlexTable>
    </g:HTMLPanel>
</ui:UiBinder>

Main.java

package tutorial.client;

import java.util.List;

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

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.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
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;
   
    @UiField
    FlexTable table;
   
    @UiField
    Button tweet;

   
    @UiField
    Button refresh;


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

    @UiHandler({"tweet", "refresh"})
    void handleClick(ClickEvent e) {
        if (e.getSource() == tweet) {
            tweet();
        } else if (e.getSource() == refresh) {
            getTweetList();
        }
    }

   
    void tweet() {
        service.tweet(content.getText(), new AsyncCallback<Void>() {
           
            public void onSuccess(Void result) {
                content.setText(null);
                getTweetList();
            }
           
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }
        });
    }
   
    void getTweetList() {
        service.getTweetList(new AsyncCallback<List<Tweet>>() {
           
            public void onSuccess(List<Tweet> result) {
                table.clear();
                for (int i = 0; i < result.size(); i++) {
                    table.setHTML(i, 0, result.get(i).getContent());
                }
            }
           
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }
        });
    }

}

Let's refresh the browser, and input something, and post it. You can confirm the tweeted contents.

Congratulations!

You have completed this tutorial.

Next...

See Slim3 Datastore.
Comments