Creating a model

Double-click build.xml under the project.
In the Outline view, right-click gen-model task and select Run as > Ant Build(first item).
In the ant input dialog, input "Tweet", 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.shared.model.Tweet", "tutorial.shared.model.TweetTest" will be created as follows:

Tweet.java

package tutorial.shared.model;

import java.io.Serializable;

import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;

@Model(schemaVersion = 1)
public class Tweet implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }
}

TweetTest.java

package tutorial.shared.model;

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

public class TweetTest {

    private Tweet model = new Tweet();

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

If "Tweet.java" and "TweetTest.java" are created in "tutorial.model" package, check if GWTServiceServlet and the mapping are uncommented out in web.xml.

Run the "TweetTest.java". The result will be green(OK).
Then, let's add content property and createdDate property to the Tweet model as follows:

Tweet.java

package tutorial.shared.model;

import java.io.Serializable;
import java.util.Date;

import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;

@Model(schemaVersion = 1)
public class Tweet implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(version = true)
    private Long version;
   
    private String content;
   
    private Date createdDate = new Date();

    /**
     * Returns the key.
     *
     * @return the key
     */
    public Key getKey() {
        return key;
    }

    /**
     * Sets the key.
     *
     * @param key
     *            the key
     */
    public void setKey(Key key) {
        this.key = key;
    }

    /**
     * Returns the version.
     *
     * @return the version
     */
    public Long getVersion() {
        return version;
    }

    /**
     * Sets the version.
     *
     * @param version
     *            the version
     */
    public void setVersion(Long version) {
        this.version = version;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
}

Run the "TweetTest.java". The result will be green(OK).
The Tweet model is ready.

Models have two roles:Data Model and Service. A main role of Data Model is to store the data. A main role of Service is to perform a use case. Of course Data Model can have some logics for the data, too. "gen-model" creates a Data Model.

Next...

Continue to Creating a service.
Comments