Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 36d6fa0a3afee8f06ac3adac67d132393da4db61 @retroryan retroryan committed Jan 2, 2012
@@ -0,0 +1,5 @@
+logs
+project/project
+project/target
+target
+tmp
@@ -0,0 +1 @@
+web: target/start
12 README
@@ -0,0 +1,12 @@
+This is a re-write of James Ward tutorial - Play Framework, JPA, JSON, jQuery, & Heroku http://www.jamesward.com/2011/12/11/tutorial-play-framework-jpa-json-jquery-heroku
+
+Using Play 2.0 Beta. It seems JSON support was only recently added to the play framework and so it is not in the play framework jar file. Instead I copied the JSON class out of the play source code. I added the dependencies to the project/Build.scala
+
+
+
+TODO
+
+- Move the Script block into a variable in the index
+- Basic Code Documentation
+- How do you setup a production database configuration?
+- What needs to be done to deploy to Heroku?
@@ -0,0 +1,43 @@
+import com.avaje.ebean.Ebean;
+import models.Bar;
+import play.Application;
+import play.GlobalSettings;
+import play.api.db.evolutions.OfflineEvolutions;
+import play.libs.Yaml;
+
+import java.io.File;
+import java.util.List;
+import java.util.Map;
+
+public class Global extends GlobalSettings {
+
+ public void beforeStart(Application app) {
+ File applicationPath = app.path();
+ OfflineEvolutions.applyScript(applicationPath, app.classloader(), "default");
+
+ }
+
+ public void onStart(Application app) {
+ InitialData.insert(app);
+ }
+
+ static class InitialData {
+
+ public static void insert(Application app) {
+
+ if (Ebean.find(Bar.class).findRowCount() == 0) {
+
+ Map<String, List<Object>> all = (Map<String, List<Object>>) Yaml.load(
+ app.resourceAsStream("conf/initial-data.yml"),
+ app.classloader()
+ );
+
+ List<Object> bars = all.get("bars");
+ Ebean.save(bars);
+
+ }
+ }
+
+ }
+
+}
@@ -0,0 +1,33 @@
+package controllers;
+
+import models.Bar;
+import play.data.Form;
+import play.mvc.Controller;
+import play.mvc.Result;
+import views.html.index;
+
+import static libs.Json.toJson;
+
+public class Application extends Controller {
+
+ public static Result index() {
+ return ok(index.render("Enter the name of your new bar:", form(Bar.class)));
+ }
+
+ public static Result addBar() {
+ Form<Bar> form = form(Bar.class).bindFromRequest();
+ if (form.hasErrors()) {
+ return badRequest(index.render("Error in Bar form. Enter new Bar",form(Bar.class)));
+ } else {
+ Bar bar = form.get();
+ bar.save();
+ return ok(
+ index.render("The bar " + bar.name + " was saved. Enter a new Bar:", form(Bar.class))
+ );
+ }
+ }
+
+ public static Result listBars() {
+ return ok(toJson(Bar.findAll()));
+ }
+}
@@ -0,0 +1,47 @@
+package libs;
+
+
+import java.io.StringWriter;
+import org.codehaus.jackson.map.ObjectMapper;
+
+/**
+ * provides helpers to deal with JSON
+ */
+
+public class Json {
+
+ /**
+ * provides a simple way to serialize into JSON.
+ *
+ * usage (in a controller):
+ * public static Result index() {
+ *{{{
+ *Map<String,String> d = new HashMap<String,String>();
+ *d.put("peter","foo");
+ *d.put("yay","value");
+ * return ok(toJson(d));
+ *}
+ *}}}
+ *
+ * @param data to be serialized
+ */
+ public static play.api.mvc.Content toJson(final Object data) {
+ return new play.api.mvc.Content() {
+ public String body() {
+ ObjectMapper mapper = new ObjectMapper();
+ StringWriter w = new StringWriter();
+ try {
+ mapper.writeValue(w,data);
+ } catch (java.io.IOException ex) {
+ play.Logger.warn("could not seralize to JSON:"+data.toString());
+ }
+ return w.toString();
+ }
+
+ public String contentType() {
+ return "application/json";
+ }
+ };
+
+ }
+}
@@ -0,0 +1,24 @@
+package models;
+
+import play.db.ebean.Model;
+
+import javax.persistence.Entity;
+import java.util.List;
+
+@Entity
+public class Bar extends Model {
+
+ public String name;
+
+ // -- Queries
+ public static Model.Finder<String, Bar> find = new Model.Finder(String.class, Bar.class);
+
+ /**
+ * Retrieve all users.
+ */
+ public static List<Bar> findAll() {
+ return find.all();
+ }
+
+
+}
@@ -0,0 +1,23 @@
+@(message: String)(barForm: Form[Bar])
+
+@import helper._
+
+@main("Welcome to PlayBar 2") {
+ <h3>@message</h3>
+
+ @form(action = routes.Application.addBar, args = 'id -> "barForm") {
+
+ @inputText(
+ field = barForm("name"),
+ args = 'label -> "Name of bar?", 'placeholder -> "Foo Bar"
+ )
+
+ <p class="buttons">
+ <input type="submit">
+ <p>
+ }
+
+ <ul id="bars">
+
+ </ul>
+}
@@ -0,0 +1,27 @@
+@(title: String)(content: Html)
+
+<!DOCTYPE html>
+
+<html>
+ <head>
+ <title>@title</title>
+ <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
+ <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
+ <script src="@routes.Assets.at("javascripts/jquery-1.6.4.min.js")" type="text/javascript"></script>
+
+ <script type="text/javascript">
+ $(function() {
+ $.get("bars.json", function(data) {
+ $.each(data, function(index, item) {
+ $("#bars").append("<li>Bar " + item.name + "</li>");
+ });
+ });
+ });
+ </script>
+
+ </head>
+ <body>
+ <h1>@title</h1>
+ @content
+ </body>
+</html>
@@ -0,0 +1,44 @@
+# This is the main configuration file for the application.
+# ~~~~~
+
+# Secret key
+# ~~~~~
+# The secret key is used to secure cryptographics functions.
+# If you deploy your application to several instances be sure to use the same key!
+application.secret=S_@xwrN5NtwvYyokBG[LxJ]ti7E8\bXOK^UV=>ejd39fgGGDhDA>Cl=0v\DU5mae
+
+# Database configuration
+# ~~~~~
+# You can declare as many datasources as you want.
+# By convention, the default datasource is named `default`
+#
+
+#db.default.driver=org.h2.Driver
+#db.default.url=jdbc:h2:mem:play
+
+# The url needs to be modified with the actual url from the heroku environment variables. This needs to be changed to use environment variables
+db.default.url=jdbc:postgresql://ec2-107-21-110-190.compute-1.amazonaws.com/fjfancloqd?user=fjfancloqd&password=PZJcU_vQ-gatvF-a8zYb
+db.default.driver=org.postgresql.Driver
+
+
+# Ebean configuration
+# ~~~~~
+# You can declare as many Ebean servers as you want.
+# By convention, the default server is named `default`
+#
+ebean.default=models.*
+
+
+# Logger
+# ~~~~~
+# You can also configure logback (http://logback.qos.ch/), by providing a logger.xml file in the conf directory .
+
+# Root logger:
+logger=ERROR
+
+# Logger used by the framework:
+logger.play=INFO
+
+# Logger provided to your application:
+logger.application=DEBUG
+
@@ -0,0 +1,8 @@
+# Bars
+bars:
+ - !!models.Bar
+ name: Foo
+ - !!models.Bar
+ name: Roo
+ - !!models.Bar
+ name: Loo
@@ -0,0 +1,11 @@
+# Routes
+# This file defines all application routes (Higher priority routes first)
+# ~~~~
+
+# Home page
+GET / controllers.Application.index()
+GET /bars.json controllers.Application.listBars
+POST / controllers.Application.addBar
+
+# Map static resources from the /public folder to the /assets URL path
+GET /assets/*file controllers.Assets.at(path="/public", file)
@@ -0,0 +1,20 @@
+# --- Created by Ebean DDL
+# To stop Ebean DDL generation, remove this comment and start using Evolutions
+
+# --- !Ups
+
+create table bar (
+ name varchar(255))
+;
+
+
+
+
+# --- !Downs
+
+SET REFERENTIAL_INTEGRITY FALSE;
+
+drop table if exists bar;
+
+SET REFERENTIAL_INTEGRITY TRUE;
+
@@ -0,0 +1,9 @@
+# --- Sample dataset
+
+# --- !Ups
+
+insert into bar (name) values ('Apple Inc.');
+
+# --- !Downs
+
+delete from bar;
@@ -0,0 +1,23 @@
+import sbt._
+import Keys._
+import PlayProject._
+
+object ApplicationBuild extends Build {
+
+ val appName = "playbars2"
+ val appVersion = "1.0"
+
+ val jacksonCoreAsl = "org.codehaus.jackson" % "jackson-core-asl" % "1.9.2"
+ val jacksonMapperAsl = "org.codehaus.jackson" % "jackson-mapper-asl" % "1.9.2"
+ val postgresql = "postgresql" % "postgresql" % "9.0-801.jdbc3"
+
+ val appDependencies = Seq(
+ // Add your project dependencies here,
+ jacksonCoreAsl, jacksonMapperAsl, postgresql
+ )
+
+ val main = PlayProject(appName, appVersion, appDependencies).settings(defaultJavaSettings: _*).settings(
+ // Add your own project settings here
+ )
+
+}
@@ -0,0 +1 @@
+sbt.version=0.11.0
@@ -0,0 +1,9 @@
+resolvers ++= Seq(
+ DefaultMavenRepository,
+ Resolver.url("Play", url("http://download.playframework.org/ivy-releases/"))(Resolver.ivyStylePatterns),
+ "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"
+)
+
+libraryDependencies += "play" %% "play" % "2.0-beta"
+
+addSbtPlugin("com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.0")
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Oops, something went wrong.
No changes.

0 comments on commit 36d6fa0

Please sign in to comment.