In the following post : . We've learned to create a new Controller with View, a new Controller without View and moving between two controllers. In this post, we will learn how to create a data Model to save  Tweet Content ( Data).

Step 1: Generate a model Class in Slim3 

  • 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.
"tutorial.model.Tweet", "tutorial.model.TweetTest" will be created like following
 package root.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;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Tweet other = (Tweet) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }
}

and tutorial.model.TweetTest
package root.model;

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

public class TweetTest extends AppEngineTestCase {

    private Tweet model = new Tweet();

    @Test
    public void test() throws Exception {
        assertThat(model, is(notNullValue()));
    }
}
You can run TweetTest in JUnit Test by right clicking on find name --> choosing RunAs/ JUnitTest

if it turn green, it means your generated model class OK ( I don't use JUnit Test like that so much ^^)

Step 2: Customize data Model 

As you can see, generated Tweet Model Class just have three attributes :
  • private static final long serialVersionUID = 1L
  • private Key key 
  • private Long version
we need to add more attribute to save message content and created time of message called:
  • private String content;
  • private Date createdDate = new Date();  
new Date() means the createdDate will be set the time which Object is initial.
and Don't forget to import : java.util.Date and create getter,setter for each attribute.

After adding more attribute, our Tweet Model Class will become like following:

package root.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;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Tweet other = (Tweet) obj;
        if (key == null) {
            if (other.key != null) {
                return false;
            }
        } else if (!key.equals(other.key)) {
            return false;
        }
        return true;
    }

    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;
    }
}

I've defined a Data Model class which will be store the message receiving from Client. In slim3 we have another Model role Class called "Service".
 In Service Class type We will write use case and logic to connect with Datastore. So, just put attribute and based function in Model Class like setter , getter, Object Compare,etc. The method which connect with Datastore should be place in Service.

Let's create a Tweet Service Class to put Message to Datastore (save Data into Database )

Create Service Class in Slim3 Framework