package controllers;

import play.data.*;
import play.mvc.*;

import views.html.*;

import static play.data.Form.*;


public class Application extends Controller {

    public static Result home() {
        return ok(home.render("Hi! This is Ode.", form(Registration.class)));
    }

    public static Result register() {
        Form<Registration> registrationForm = form(Registration.class).
            bindFromRequest();
        if (registrationForm.hasErrors()) {
            return badRequest(
                home.render("Hi! This is Ode.", registrationForm));
        } else {
            flash("success", "You've registered successfully.");
            return redirect(routes.Application.home());
        }
    }

    public static Result login() {
        return ok(login.render("Hi! Use the form below to log in.",
                               form(Login.class)));
    }

    public static Result authenticate() {
        Form<Login> loginForm = form(Login.class).bindFromRequest();
        if (loginForm.hasErrors()) {
            return badRequest(login.render("Do I know you?", loginForm));
        } else {
            session().clear();
            session("email", loginForm.get().email);
            return redirect(routes.Application.home());
        }
    }

    @Security.Authenticated(Secured.class)
    public static Result logout() {
        session().clear();
        flash("success", "Alright. See you around.");
        return redirect(routes.Application.login());
    }

    @Security.Authenticated(Secured.class)
    public static Result rules() {
        return ok(rules.render("Hi! This is Ode's Rule Browser."));
    }

    @Security.Authenticated(Secured.class)
    public static Result rule(int id) {
        return ok(rule.render("Hi! You are looking at rule " + id + "."));
    }

    @Security.Authenticated(Secured.class)
    public static Result search() {
        return ok(search.render("Hi! This is Ode's Search Interface."));
    }

    @Security.Authenticated(Secured.class)
    public static Result features() {
        return ok(features.render("Hi! This is Ode's Feature Editor."));
    }

    // Forms

    public static class Login {
        public String email;
        public String password;

        public String validate() {
            if (email == "" || password == "") {
                return "You must provide input for all fields.";
            }
            return null;
        }
    }

    public static class Registration extends Login {}

}

@@ -26,38 +26,284 @@
 ort 
-play.data.*;%0Aimport play.mvc.*
+com.fasterxml.jackson.databind.node.ObjectNode;%0A%0Aimport play.data.*;%0Aimport play.libs.Json;%0Aimport play.libs.WS;%0Aimport play.libs.F.Function;%0Aimport play.libs.F.Function0;%0Aimport play.libs.F.Promise;%0Aimport play.mvc.Controller;%0Aimport play.mvc.Result;%0Aimport play.mvc.Security
 ;%0A%0Ai
@@ -541,22 +541,31 @@
  static 
+Promise%3C
 Result
+%3E
  registe
@@ -574,24 +574,30 @@
 ) %7B%0A        
+final 
 Form%3CRegistr
@@ -617,24 +617,36 @@
 rationForm =
+%0A           
  form(Regist
@@ -659,29 +659,16 @@
 .class).
-%0A            
 bindFrom
@@ -738,55 +738,224 @@
     
-return badRequest(%0A                home.render(
+Promise%3CResult%3E errorResult = Promise.promise(%0A                new Function0%3CResult%3E() %7B%0A                    public Result apply() %7B%0A                        return badRequest(home.render(%0A                            
 %22Hi!
@@ -994,33 +994,602 @@
 ));%0A        
+      
-%7D else %7B%0A
+  %7D%0A            %7D);%0A            return errorResult;%0A        %7D%0A        ObjectNode nodeProperties = Json.newObject();%0A        nodeProperties.put(%22username%22, registrationForm.get().email);%0A        nodeProperties.put(%22password%22, registrationForm.get().password);%0A        Promise%3CWS.Response%3E neo4j = WS.url(%0A            %22http://localhost:7474/db/data/node%22).setContentType(%0A                %22application/json%22).post(nodeProperties);%0A        return neo4j.map(%0A            new Function%3CWS.Response, Result%3E() %7B%0A                public Result apply(WS.Response response) %7B%0A        
             
@@ -1637,24 +1637,32 @@
 ssfully.%22);%0A
+        
             
@@ -1705,33 +1705,66 @@
 ome());%0A        
-%7D
+        %7D%0A            %7D%0A        );
 %0A    %7D%0A%0A    publ

Store username and password in DB upon registration.