Skip to content
This repository was archived by the owner on Apr 23, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
dist: trusty
sudo: true
group: beta
language: scala
scala:
- 2.11.11
- 2.12.3
- 2.12.7
jdk:
- oraclejdk8
- oraclejdk8
- openjdk11
env:
matrix:
- SCRIPT=scripts/test-sbt
script:
- $SCRIPT
cache:
directories:
- "$HOME/.ivy2/cache"
- "$HOME/.sbt/launchers"
- "$HOME/.ivy2/cache"
before_cache:
- rm -rf $HOME/.ivy2/cache/com.typesafe.play/*
- rm -rf $HOME/.ivy2/cache/scala_*/sbt_*/com.typesafe.play/*
- find $HOME/.ivy2/cache -name "ivydata-*.properties" -print0 | xargs -n10 -0 rm
- rm -rf $HOME/.ivy2/cache/com.typesafe.play/*
- rm -rf $HOME/.ivy2/cache/scala_*/sbt_*/com.typesafe.play/*
- find $HOME/.ivy2/cache -name "ivydata-*.properties" -print0 | xargs -n10 -0 rm

# Exclude some combinations from build matrix. See:
# https://docs.travis-ci.com/user/customizing-the-build/#Build-Matrix
matrix:
fast_finish: true
allow_failures:
# Java 11 is still not fully supported. It is good that we are already
# testing our sample applications to better discover possible problems
# but we can allow failures here too.
- jdk: openjdk11

# See https://blog.travis-ci.com/2014-03-13-slack-notifications/
# created with travis encrypt command line tool
notifications:
slack:
secure: K6HWTI6zJpQfxS7sH5ZQ1jEK5TkkUl5GtcGinNecHMBqvfS4IXAnU23lz/kLqCqMVPIFaRx1g6UwgJgMvR4XWeIhpzLOzAnOOcmv+kQzv7A8vEJBM20z1HNzDcxzvuNNO2BHn8EjXh5VD65vXMcA+lKzUxASey/Rs+CBReQWE7M=
45 changes: 25 additions & 20 deletions app/controllers/HomeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import models.Computer;
import play.data.Form;
import play.data.FormFactory;
import play.i18n.MessagesApi;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Http;
import play.mvc.Result;
import play.mvc.Results;
import repository.CompanyRepository;
Expand All @@ -24,23 +26,26 @@ public class HomeController extends Controller {
private final CompanyRepository companyRepository;
private final FormFactory formFactory;
private final HttpExecutionContext httpExecutionContext;
private final MessagesApi messagesApi;

@Inject
public HomeController(FormFactory formFactory,
ComputerRepository computerRepository,
CompanyRepository companyRepository,
HttpExecutionContext httpExecutionContext) {
HttpExecutionContext httpExecutionContext,
MessagesApi messagesApi) {
this.computerRepository = computerRepository;
this.formFactory = formFactory;
this.companyRepository = companyRepository;
this.httpExecutionContext = httpExecutionContext;
this.messagesApi = messagesApi;
}

/**
* This result directly redirect to application home.
*/
private Result GO_HOME = Results.redirect(
routes.HomeController.list(0, "name", "asc", "")
routes.HomeController.list(0, "name", "asc", "")
);

/**
Expand All @@ -58,11 +63,11 @@ public Result index() {
* @param order Sort order (either asc or desc)
* @param filter Filter applied on computer names
*/
public CompletionStage<Result> list(int page, String sortBy, String order, String filter) {
public CompletionStage<Result> list(Http.Request request, int page, String sortBy, String order, String filter) {
// Run a db operation in another thread (using DatabaseExecutionContext)
return computerRepository.page(page, 10, sortBy, order, filter).thenApplyAsync(list -> {
// This is the HTTP rendering thread context
return ok(views.html.list.render(list, sortBy, order, filter));
return ok(views.html.list.render(list, sortBy, order, filter, request, messagesApi.preferred(request)));
}, httpExecutionContext.current());
}

Expand All @@ -71,7 +76,7 @@ public CompletionStage<Result> list(int page, String sortBy, String order, Strin
*
* @param id Id of the computer to edit
*/
public CompletionStage<Result> edit(Long id) {
public CompletionStage<Result> edit(Http.Request request,Long id) {

// Run a db operation in another thread (using DatabaseExecutionContext)
CompletionStage<Map<String, String>> companiesFuture = companyRepository.options();
Expand All @@ -81,7 +86,7 @@ public CompletionStage<Result> edit(Long id) {
// This is the HTTP rendering thread context
Computer c = computerOptional.get();
Form<Computer> computerForm = formFactory.form(Computer.class).fill(c);
return ok(views.html.editForm.render(id, computerForm, companies));
return ok(views.html.editForm.render(id, computerForm, companies, request, messagesApi.preferred(request)));
}, httpExecutionContext.current());
}

Expand All @@ -90,56 +95,56 @@ public CompletionStage<Result> edit(Long id) {
*
* @param id Id of the computer to edit
*/
public CompletionStage<Result> update(Long id) throws PersistenceException {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
public CompletionStage<Result> update(Http.Request request, Long id) throws PersistenceException {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest(request);
if (computerForm.hasErrors()) {
// Run companies db operation and then render the failure case
return companyRepository.options().thenApplyAsync(companies -> {
// This is the HTTP rendering thread context
return badRequest(views.html.editForm.render(id, computerForm, companies));
return badRequest(views.html.editForm.render(id, computerForm, companies, request, messagesApi.preferred(request)));
}, httpExecutionContext.current());
} else {
Computer newComputerData = computerForm.get();
// Run update operation and then flash and then redirect
return computerRepository.update(id, newComputerData).thenApplyAsync(data -> {
// This is the HTTP rendering thread context
flash("success", "Computer " + newComputerData.name + " has been updated");
return GO_HOME;
return GO_HOME
.flash("success", "Computer " + newComputerData.name + " has been updated");
}, httpExecutionContext.current());
}
}

/**
* Display the 'new computer form'.
*/
public CompletionStage<Result> create() {
public CompletionStage<Result> create(Http.Request request) {
Form<Computer> computerForm = formFactory.form(Computer.class);
// Run companies db operation and then render the form
return companyRepository.options().thenApplyAsync((Map<String, String> companies) -> {
// This is the HTTP rendering thread context
return ok(views.html.createForm.render(computerForm, companies));
return ok(views.html.createForm.render(computerForm, companies, request, messagesApi.preferred(request)));
}, httpExecutionContext.current());
}

/**
* Handle the 'new computer form' submission
*/
public CompletionStage<Result> save() {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest();
public CompletionStage<Result> save(Http.Request request) {
Form<Computer> computerForm = formFactory.form(Computer.class).bindFromRequest(request);
if (computerForm.hasErrors()) {
// Run companies db operation and then render the form
return companyRepository.options().thenApplyAsync(companies -> {
// This is the HTTP rendering thread context
return badRequest(views.html.createForm.render(computerForm, companies));
return badRequest(views.html.createForm.render(computerForm, companies, request, messagesApi.preferred(request)));
}, httpExecutionContext.current());
}

Computer computer = computerForm.get();
// Run insert db operation, then redirect
return computerRepository.insert(computer).thenApplyAsync(data -> {
// This is the HTTP rendering thread context
flash("success", "Computer " + computer.name + " has been created");
return GO_HOME;
return GO_HOME
.flash("success", "Computer " + computer.name + " has been created");
}, httpExecutionContext.current());
}

Expand All @@ -150,8 +155,8 @@ public CompletionStage<Result> delete(Long id) {
// Run delete db operation, then redirect
return computerRepository.delete(id).thenApplyAsync(v -> {
// This is the HTTP rendering thread context
flash("success", "Computer has been deleted");
return GO_HOME;
return GO_HOME
.flash("success", "Computer has been deleted");
}, httpExecutionContext.current());
}

Expand Down
2 changes: 1 addition & 1 deletion app/views/createForm.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(computerForm: Form[Computer], companies: Map[String, String])
@(computerForm: Form[Computer], companies: Map[String, String])(implicit request: Http.Request, messages: play.i18n.Messages)

@import helper._

Expand Down
2 changes: 1 addition & 1 deletion app/views/editForm.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@(id: Long, computerForm: Form[Computer], companies: Map[String, String])
@(id: Long, computerForm: Form[Computer], companies: Map[String, String])(implicit request: Http.Request, messages: play.i18n.Messages)

@import helper._

Expand Down
11 changes: 8 additions & 3 deletions app/views/list.scala.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
@(currentPage: io.ebean.PagedList[Computer], currentSortBy: String, currentOrder: String, currentFilter: String)
@(currentPage: io.ebean.PagedList[Computer],
currentSortBy: String,
currentOrder: String,
currentFilter: String)(
implicit request: Http.Request,
messages: play.i18n.Messages)

@****************************************
* Helper generating navigation links *
Expand Down Expand Up @@ -39,9 +44,9 @@

<h1 id="homeTitle">@Messages("computers.list.title", currentPage.getTotalCount)</h1>

@if(flash.containsKey("success")) {
@request.flash.asScala().get("success").map { successFlashValue =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an implicit Http.Request then you can use @flash("key") (also returns a Optional).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I need to review a few other samples.

<div class="alert-message warning">
<strong>Done!</strong> @flash.get("success")
<strong>Done!</strong> @successFlashValue
</div>
}

Expand Down
6 changes: 3 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ name := "play-java-ebean-example"

version := "1.0.0-SNAPSHOT"

scalaVersion := "2.12.6"

crossScalaVersions := Seq("2.11.12", "2.12.6")
scalaVersion := "2.12.7"

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)

Expand All @@ -16,3 +14,5 @@ libraryDependencies += "org.awaitility" % "awaitility" % "2.0.0" % Test
libraryDependencies += "org.assertj" % "assertj-core" % "3.6.2" % Test
libraryDependencies += "org.mockito" % "mockito-core" % "2.1.0" % Test
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v")

javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation", "-Werror")
10 changes: 5 additions & 5 deletions conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
GET / controllers.HomeController.index()

# Computers list (look at the default values for pagination parameters)
GET /computers controllers.HomeController.list(p:Int ?= 0, s ?= "name", o ?= "asc", f ?= "")
GET /computers controllers.HomeController.list(request: Request, p:Int ?= 0, s ?= "name", o ?= "asc", f ?= "")

# Add computer
GET /computers/new controllers.HomeController.create()
POST /computers controllers.HomeController.save()
GET /computers/new controllers.HomeController.create(request: Request)
POST /computers controllers.HomeController.save(request: Request)

# Edit existing computer
GET /computers/:id controllers.HomeController.edit(id:Long)
POST /computers/:id controllers.HomeController.update(id:Long)
GET /computers/:id controllers.HomeController.edit(request: Request, id:Long)
POST /computers/:id controllers.HomeController.update(request: Request, id:Long)

# Delete a computer
POST /computers/:id/delete controllers.HomeController.delete(id:Long)
Expand Down
6 changes: 2 additions & 4 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.6.20")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.0-RC8")

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "4.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "5.0.0-RC2")
6 changes: 6 additions & 0 deletions scripts/test-sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

echo "+----------------------------+"
echo "| Executing tests using sbt |"
echo "+----------------------------+"
sbt ++$TRAVIS_SCALA_VERSION test
2 changes: 1 addition & 1 deletion test/BrowserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testBrowser() {
browser.$("a", withText("Apple II")).click();
browser.$("input.danger").click();

browser.takeHtmlDump("delete.html");
browser.takeHtmlDump("target/delete.html");

assertThat(browser.$("section h1").first().text(), equalTo("573 computers found"));
assertThat(browser.$(".alert-message").first().text(), equalTo("Done! Computer has been deleted"));
Expand Down
2 changes: 1 addition & 1 deletion test/FunctionalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void createANewComputer() {

assertThat(result.status()).isEqualTo(SEE_OTHER);
assertThat(result.redirectLocation().get()).isEqualTo("/computers");
assertThat(result.flash().get("success")).isEqualTo("Computer FooBar has been created");
assertThat(result.flash().getOptional("success").get()).isEqualTo("Computer FooBar has been created");

result = route(app, controllers.routes.HomeController.list(0, "name", "asc", "FooBar"));
assertThat(result.status()).isEqualTo(OK);
Expand Down