Skip to content

Commit 6d6f3c8

Browse files
committed
Chapter 8 sample code
0 parents  commit 6d6f3c8

37 files changed

+10281
-0
lines changed

.gitignore

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
logs
2+
project/project
3+
project/target
4+
target
5+
tmp
6+
.history
7+
dist
8+
/.idea
9+
/*.iml
10+
/out
11+
/.idea_modules
12+
/.classpath
13+
/.project
14+
/RUNNING_PID
15+
/.settings

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Chapter 8: View templates
2+
=========================
3+
4+
This is the sample code for chapter eight of [Play for Java](http://bit.ly/playjava).

app/controllers/CatchAction.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package controllers;
2+
3+
import play.mvc.*;
4+
import utils.ExceptionMailer;
5+
6+
public class CatchAction extends Action.Simple {
7+
public Result call(Http.Context ctx) {
8+
try {
9+
return delegate.call(ctx);
10+
} catch (Throwable e) {
11+
ExceptionMailer.send(e);
12+
throw new RuntimeException(e);
13+
}
14+
}
15+
}

app/controllers/Products.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package controllers;
2+
3+
import com.google.common.io.Files;
4+
import models.Product;
5+
import models.StockItem;
6+
import models.Tag;
7+
import play.data.Form;
8+
import play.mvc.Controller;
9+
import play.mvc.Result;
10+
import views.html.products.details;
11+
import views.html.products.list;
12+
13+
import java.io.File;
14+
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.Set;
18+
19+
import static play.mvc.Http.MultipartFormData;
20+
21+
public class Products extends Controller {
22+
23+
private static final Form<Product> productForm = Form.form(Product.class);
24+
25+
public static Result index() {
26+
return redirect(routes.Products.list(1));
27+
}
28+
29+
public static Result list(Integer page) {
30+
List<Product> products = Product.find().all();
31+
return ok(views.html.catalog.render(products));
32+
}
33+
34+
public static Result newProduct() {
35+
return ok(details.render(productForm));
36+
}
37+
38+
public static Result details(Long ean) {
39+
final Product product = Product.find().byId(ean);
40+
if(product == null)
41+
return notFound(String.format("Product %s does not exist.", ean));
42+
43+
Form<Product> filledForm = productForm.fill(product);
44+
return ok(details.render(filledForm));
45+
}
46+
47+
public static Result save() {
48+
MultipartFormData body = request().body().asMultipartFormData();
49+
Form<Product> boundForm = productForm.bindFromRequest();
50+
if(boundForm.hasErrors()) {
51+
flash("error", "Please correct the form below.");
52+
return badRequest(details.render(boundForm));
53+
}
54+
Product product = boundForm.get();
55+
56+
MultipartFormData.FilePart part = body.getFile("picture");
57+
if(part != null) {
58+
File picture = part.getFile();
59+
60+
try {
61+
product.picture = Files.toByteArray(picture);
62+
} catch (IOException e) {
63+
return internalServerError("Error reading file upload");
64+
}
65+
}
66+
67+
List<Tag> tags = new ArrayList<Tag>();
68+
for (Tag tag : product.tags) {
69+
if (tag.id != null) {
70+
tags.add(Tag.findById(tag.id));
71+
}
72+
}
73+
product.tags = tags;
74+
75+
StockItem item = new StockItem();
76+
item.quantity = 0L;
77+
item.product = product;
78+
79+
product.save();
80+
item.save();
81+
flash("success",
82+
String.format("Successfully added product %s", product));
83+
84+
return redirect(routes.Products.list(1));
85+
}
86+
87+
public static Result picture(Long id) {
88+
final Product product = Product.find().byId(id);
89+
if(product == null) return notFound();
90+
return ok(product.picture);
91+
}
92+
}

app/controllers/StockItems.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package controllers;
2+
3+
import models.StockItem;
4+
import play.mvc.Controller;
5+
import play.mvc.Result;
6+
7+
import java.util.List;
8+
9+
public class StockItems extends Controller {
10+
11+
public static Result index() {
12+
List<StockItem> items = StockItem.find()
13+
.where()
14+
.ge("quantity", 300)
15+
.orderBy("quantity")
16+
.setMaxRows(10)
17+
.findList();
18+
return ok(items.toString());
19+
}
20+
}

app/models/Address.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package models;
2+
3+
import play.db.ebean.Model;
4+
5+
import javax.persistence.*;
6+
7+
@Entity
8+
public class Address extends Model{
9+
10+
@Id
11+
public Long id;
12+
13+
@OneToOne(mappedBy = "address")
14+
public Warehouse warehouse;
15+
16+
public String street;
17+
public String number;
18+
public String postalCode;
19+
public String city;
20+
public String country;
21+
22+
}

app/models/Product.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package models;
2+
3+
4+
import play.data.validation.Constraints;
5+
import play.db.ebean.Model;
6+
import play.libs.F;
7+
import play.mvc.PathBindable;
8+
import play.mvc.QueryStringBindable;
9+
10+
import javax.persistence.*;
11+
import java.util.*;
12+
13+
@Entity
14+
public class Product extends Model {
15+
16+
public static class EanValidator extends Constraints.Validator<String> {
17+
18+
@Override
19+
public boolean isValid(String value) {
20+
String pattern = "^[0-9]{13}$";
21+
return value != null && value.matches(pattern);
22+
}
23+
24+
@Override
25+
public F.Tuple<String, Object[]> getErrorMessageKey() {
26+
return new F.Tuple<String, Object[]>("error.invalid.ean",
27+
new String[]{});
28+
}
29+
}
30+
31+
@Id
32+
public Long id;
33+
@Constraints.Required
34+
@Constraints.ValidateWith(EanValidator.class)
35+
public String ean;
36+
@Constraints.Required
37+
public String name;
38+
public String description;
39+
public byte[] picture;
40+
41+
@ManyToMany
42+
public List<Tag> tags = new LinkedList<>();
43+
44+
@OneToMany(mappedBy = "product")
45+
public List<StockItem> stockItems;
46+
47+
public Product() {
48+
}
49+
50+
public Product(String ean, String name, String description) {
51+
this.ean = ean;
52+
this.name = name;
53+
this.description = description;
54+
}
55+
56+
public String toString() {
57+
return String.format("%s - %s", ean, name);
58+
}
59+
60+
public static Finder<Long, Product> find() {
61+
return new Finder<>(Long.class, Product.class);
62+
}
63+
}

app/models/StockItem.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package models;
2+
3+
import play.db.ebean.Model;
4+
5+
import javax.persistence.Entity;
6+
import javax.persistence.Id;
7+
import javax.persistence.ManyToOne;
8+
9+
@Entity
10+
public class StockItem extends Model{
11+
12+
@Id
13+
public Long id;
14+
15+
@ManyToOne
16+
public Warehouse warehouse;
17+
18+
@ManyToOne
19+
public Product product;
20+
public Long quantity;
21+
22+
@Override
23+
public String toString() {
24+
return String.format("StockItem %d - %dx product %s",
25+
id, quantity, product == null ? null : product.id);
26+
}
27+
28+
public static Finder<Long, StockItem> find() {
29+
return new Finder<>(Long.class, StockItem.class);
30+
}
31+
}

app/models/Tag.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package models;
2+
3+
import play.db.ebean.Model;
4+
5+
import javax.persistence.*;
6+
import java.util.Collection;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
@Entity
11+
public class Tag extends Model {
12+
13+
public static Finder<Long, Tag> find() {
14+
return new Finder<>(Long.class, Tag.class);
15+
}
16+
17+
public static Tag findById(Long id) {
18+
return find().byId(id);
19+
}
20+
21+
@Id
22+
public Long id;
23+
public String name;
24+
25+
@ManyToMany(mappedBy = "tags")
26+
public List<Product> products;
27+
28+
29+
public Tag(){
30+
// Left empty
31+
}
32+
33+
public Tag(Long id, String name, Collection<Product> products) {
34+
this.id = id;
35+
this.name = name;
36+
this.products = new LinkedList<>(products);
37+
for (Product product : products) {
38+
product.tags.add(this);
39+
}
40+
}
41+
}

app/models/Warehouse.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package models;
2+
3+
import play.db.ebean.Model;
4+
5+
import javax.persistence.*;
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
@Entity
10+
public class Warehouse extends Model {
11+
12+
@Id
13+
public Long id;
14+
15+
public String name;
16+
17+
@OneToMany(mappedBy = "warehouse")
18+
public List<StockItem> stock = new ArrayList<>();
19+
20+
@OneToOne
21+
public Address address;
22+
23+
@Override
24+
public String toString() {
25+
return name;
26+
}
27+
}

0 commit comments

Comments
 (0)