package controllers;

import java.util.List;

import javax.inject.Inject;

import models.Person;
import play.data.FormFactory;
import play.db.jpa.JPAApi;
import play.db.jpa.Transactional;
import play.mvc.Controller;
import play.mvc.Result;

import static play.libs.Json.toJson;

public class CollectorController extends Controller {
	
	private final FormFactory formFactory;
	private final JPAApi jpaApi;

	@Inject
	public CollectorController(FormFactory formFactory, JPAApi jpaApi) {
		this.formFactory = formFactory;
		this.jpaApi = jpaApi;
	}

	public Result index1() {
		//return ok(views.html.index1.render());
		return ok();
	}

	@Transactional
	public Result addPerson() {
		Person person = formFactory.form(Person.class).bindFromRequest().get();
		jpaApi.em().persist(person);
		return redirect(routes.CollectorController.index());
	}

	@Transactional(readOnly=true)
	public Result getPersons() {
		List<Person> persons = (List<Person>) jpaApi.em().createQuery("select p from Person p").getResultList();
		return ok(toJson(persons));
	}

	// -----------old-------------
	public Result collect() {
		//Result notFound = notFound();
		//Result pageNotFound = notFound("<h1>Page not found</h1>").as("text/html");
		//Result badRequest = badRequest(views.html.form.render(formWithErrors));
		//Result oops = internalServerError("Oops");
		//Result anyStatus = status(488, "Strange response type");
		return ok("collected " + request() + "!");
	}

	public Result index() {
		//     return temporaryRedirect("/user/home");
		return redirect("/login");
	}
	// -----------old-------------

}

@@ -42,58 +42,194 @@
 st;%0A
-%0Aimport javax.inject.Inject;%0A%0Aimport models.Person
+import java.util.concurrent.CompletionStage;%0Aimport java.util.stream.Collectors;%0A%0Aimport javax.inject.Inject;%0A%0Aimport models.Person;%0Aimport models.PersonRepository;%0Aimport play.data.Form
 ;%0Aim
@@ -317,16 +317,66 @@
 tional;%0A
+import play.libs.concurrent.HttpExecutionContext;%0A
 import p
@@ -573,166 +573,310 @@
 nal 
-JPAApi jpaApi;%0A%0A%09@Inject%0A%09public CollectorController(FormFactory formFactory, JPAApi jpaApi) %7B%0A%09%09this.formFactory = formFactory;%0A%09%09this.jpaApi = jpaApi
+PersonRepository personRepository;%0A%09private final HttpExecutionContext ec;%0A%0A%09@Inject%0A%09public CollectorController(FormFactory formFactory, PersonRepository personRepository, HttpExecutionContext ec) %7B%0A%09%09this.formFactory = formFactory;%0A%09%09this.personRepository = personRepository;%0A%09%09this.ec = ec
 ;%0A%09%7D%0A%0A
-%09
+    
 publ
@@ -894,18 +894,78 @@
 ndex
-1
 () %7B%0A
-%09%09//
+    %09Form%3CPerson%3E form = formFactory.form(Person.class);%0A        
 retu
@@ -990,17 +990,16 @@
 ndex
-1
 .render(
 ));%0A
@@ -998,61 +998,54 @@
 der(
-));%0A%09%09return ok();%0A%09%7D%0A%0A%09@Transactional%0A%09public 
+form));%0A    %7D%0A%0A%09public CompletionStage%3C
 Result
+%3E
  add
@@ -1135,37 +1135,67 @@
 ;%0A%09%09
-jpaApi.em().persist(person);%0A
+return personRepository.add(person).thenApplyAsync(p -%3E %7B%0A%09
 %09%09re
@@ -1250,56 +1250,62 @@
 );%0A%09
-%7D%0A%0A%09@Transactional(readOnly=true)%0A%09public 
+%09%7D, ec.current());%0A%09%7D%0A%0A%09public CompletionStage%3C
 Result
+%3E
  get
@@ -1322,139 +1322,153 @@
 %7B%0A%09%09
-List%3CPerson%3E persons = (List%3CPerson%3E) jpaApi.em().createQuery(%22select p from Person p%22).getResultList();%0A%09%09return ok(toJson(persons
+return personRepository.list().thenApplyAsync(personStream -%3E %7B%0A%09%09%09return ok(toJson(personStream.collect(Collectors.toList())));%0A%09%09%7D, ec.current(
 ));%0A
@@ -1892,24 +1892,25 @@
 Result index
+1
 () %7B%0A%09%09//   

make jpa work