Book Image

Play Framework Cookbook

By : Alexander Reelsen
Book Image

Play Framework Cookbook

By: Alexander Reelsen

Overview of this book

<p>The Play framework is the new kid on the block of Java frameworks. By breaking with existing standards the play framework tries not to abstract away from HTTP as most web frameworks do, but tightly integrates with it. This means quite a shift for Java programmers. Understanding these concepts behind the play framework and its impact on web development with Java are crucial for fast development of applications.<br /><br />The Play Framework Cookbook starts where the beginner documentation ends. It shows you how to utilize advanced features of the Play framework &ndash; piece by piece and completely outlined with working applications!<br /><br />The reader will be taken through all layers of the Play Framework and provided with in-depth knowledge from as many examples and applications as possible. Leveraging the most from the Play framework means to think simple again in a java environment. Implement your own renderers, integrate tightly with HTTP, use existing code, improve site performance with caching and integrate with other web services and interfaces. Learn about non-functional issues like modularity or integration into production and testing environments. In order to provide the best learning experience during reading Play Framework Cookbook, almost every example is provided with source code, so you can start immediately to integrate recipes into your own play applications.</p>
Table of Contents (16 chapters)
Play Framework Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Further Information About the Play Framework
Index

Using fixtures to provide initial data


Fixtures are the Swiss Army knife of database independent seed data. By defining and describing your data entities in a text file it is pretty simple to load it into an arbitrary database.

This serves two purposes. First, you can make sure in your tests, that certain data exists when running the tests. Second, you can ensure that the must-have data like a first administrative account in your application exists, when deploying and starting your application in production.

How to do it...

Define a fixtures file and store it under conf/initial-data.yml:

User(alr):
        login: alr
        password: test
        email: [email protected]

Tweet(t1):
        content: Lets get ready to tweet
        postedAt: 2010-11-22T08:23:15
        login: alr

How it works...

As you can see in the preceding snippet, there are two entities defined. The first one only consists of strings, whereas the second one consists of a date and a reference to the first one, which uses the name in parentheses after the type as a reference.

There's more...

Fixtures are helpful in two cases. For one you can ensure the same test data in your unit, functional, and selenium tests. Also you can make sure, that your application is initialized with a certain set of data, when the application is loaded for the first time.

Using a bootstrap job to load seed data

If you need to initialize your application with some data, you can execute a job loading this data at application startup with the following code snippet:

@OnApplicationStart
public class Bootstrap extends Job {

    public void doJob() {
        // Check if the database is empty
        if(User.count() == 0) {
          Fixtures.load("initial-data.yml");
        }
    }

You should put the referenced initial-data.yml file into the./conf directory of your application. If you reference it with its filename only in any class like in the doJob() method that we saw some time back, it will be found and loaded in your current database by using the count() method of the User entity. Also by extending this class from Job and putting the @OnApplicationStart annotation at the top, the doJob() method is executed right at the start of the application.

More information about YAML

Play uses SnakeYAML as an internal YAML parser. You can find out more about the integration at either http://www.playframework.org/documentation/1.2/yaml or http://code.google.com/p/snakeyaml/.

Using lists in YAML

Fixtures are quite flexible, they also allow lists; for example, if the tags field is from type List<Tag>, this works:

tags:             [tag1, tag2, tag3, tag4]