How to populate sample data when a Play Framework application starts up

If you ever need an example of how to do something when a Play Framework application starts up, check out the Zentasks application in the Play distribution samples directory. You'll find the following code in the Global.scala file in the app subdirectory of the project. It shows how to use the onStart method of the Global object to populate some sample data when the Play Framework app starts up.

Here's the source code for that class:

import play.api._

import models._
import anorm._

object Global extends GlobalSettings {
  override def onStart(app: Application) {

 * Initial set of data to be imported 
 * in the sample application.
object InitialData {
  def date(str: String) = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(str)
  def insert() = {
    if(User.findAll.isEmpty) {
        User("", "Guillaume Bort", "secret"),
        User("", "Maxime Dantec", "secret"),
        User("", "Sadek Drobi", "secret"),
        User("", "Erwan Loisant", "secret")
        Project(Id(1), "Play framework", "Play 2.0") -> Seq("", "", "", ""),
        Project(Id(2), "Play framework", "Play 1.2.4") -> Seq("", ""),
        Project(Id(3), "Play framework", "Website") -> Seq("", ""),
        Project(Id(4), "Zenexity", "Secret project") -> Seq("", "", "", ""),
        Project(Id(5), "Zenexity", "Playmate") -> Seq(""),
        Project(Id(6), "Personal", "Things to do") -> Seq(""),
        Project(Id(7), "Zenexity", "Play samples") -> Seq("", ""),
        Project(Id(8), "Personal", "Private") -> Seq(""),
        Project(Id(9), "Personal", "Private") -> Seq(""),
        Project(Id(10), "Personal", "Private") -> Seq(""),
        Project(Id(11), "Personal", "Private") -> Seq("")
      ).foreach {
        case (project,members) => Project.create(project, members)
        Task(NotAssigned, "Todo", 1, "Fix the documentation", false, None, Some("")),
        Task(NotAssigned, "Urgent", 1, "Prepare the beta release", false, Some(date("2011-11-15")), None),
        Task(NotAssigned, "Todo", 9, "Buy some milk", false, None, None),
        Task(NotAssigned, "Todo", 2, "Check 1.2.4-RC2", false, Some(date("2011-11-18")), Some("")),
        Task(NotAssigned, "Todo", 7, "Finish zentask integration", true, Some(date("2011-11-15")), Some("")),
        Task(NotAssigned, "Todo", 4, "Release the secret project", false, Some(date("2012-01-01")), Some(""))

Note that you can call your object anything you want, but the name Global is a standard convention. The important part is that it extends GlobalSettings. In this example the onStart method simply calls InitialData.insert, and that method does the work of inserting the sample data, if it's not already in the database. (See the application.conf file of the Zentasks sample project to see how the database is configured.)

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.