public class Application extends Controller { static Form<Task> taskForm = Form.form(Task.class); public static Result index() { return redirect(routes.Application.tasks()); } public static Result tasks() { return ok(views.html.index.render(Task.all(), taskForm)); } public static Result newTask() { Form<Task> filledForm = taskForm.bindFromRequest(); if (filledForm.hasErrors()) { return badRequest(views.html.index.render(Task.all(), filledForm)); } else { Task.create(filledForm.get()); return redirect(routes.Application.tasks()); } } public static Result deleteTask(Long id) { Task.delete(id); return redirect(routes.Application.tasks()); } }
@With(DetectUser.class) @Authenticated(UserAuthenticator.class) public class TaskController extends Controller { static Form<Task> taskForm = Form.form(Task.class); public static User loadUser() { return User.fetch(session().get("username")); } public static Result tasks() { User user = loadUser(); return ok(main.render("Tasks", null, tasks.render(user.tasks, taskForm))); } public static Result newTask() { User user = loadUser(); Form<Task> filledForm = taskForm.bindFromRequest(); if (filledForm.hasErrors()) { flash("error", "The task could not be saved."); return badRequest(main.render("Tasks", null, tasks.render(user.tasks, filledForm))); } else { Task task = filledForm.get(); task.owner = user; task.save(); return created(main.render("Tasks", null, tasks.render(user.tasks, taskForm))); } } public static Result deleteTask(Long id) { Task.delete(id); return redirect(routes.TaskController.tasks()); } }
// src: 1 = viewAdvertisements // src: 2 = viewMyOwnAdvertisements @Security.Authenticated(Secured.class) public static Result changeStudAdvertisementForm(Long adId, Long src) { Form<StudentAdvertisementForm> adForm = Form.form(StudentAdvertisementForm.class).bindFromRequest(); String description = adForm.get().description; String studies = adForm.get().studies; boolean testAd = adForm.get().test; StudentAdvertisement.create( Student.find.byId(request().username()), studies, description, adId, testAd); if (adForm.hasErrors()) { return badRequest( changeStudentAdvertisement.render( Student.find.byId(request().username()), adForm, null, src)); } else { if (src == 1) { return ok( viewAdvertisements.render( Student.find.byId(request().username()), StudentAdvertisement.find.all(), TutorAdvertisement.find.all())); } else { return ok( viewOwnAdvertisements.render( Student.find.byId(request().username()), StudentAdvertisement.find.all(), TutorAdvertisement.find.all())); } } }
public static Result registerNewUser() { Form<Register> regForm = Form.form(Register.class).bindFromRequest(); if (regForm.hasErrors()) { return badRequest(register.render(regForm)); } else { return redirect(routes.Application.login()); } }
public static Result authenticate() { Form<Login> loginForm = Form.form(Login.class).bindFromRequest(); if (loginForm.hasErrors()) { return badRequest(login.render(loginForm)); } else { session().clear(); session("email", loginForm.get().email); return redirect(routes.Application.start()); } }
@Security.Authenticated(Secured.class) public static Result addNewMessageForm(Long id) { Form<MessageForm> mesForm = Form.form(MessageForm.class).bindFromRequest(); if (mesForm.hasErrors()) { return badRequest(postNewMessage.render(mesForm, id)); } else { Message m = new Message( mesForm.field("text").value().toString(), Student.find.byId(request().username())); Conversation c = null; c = Conversation.find.byId(id.toString()); c.messages.add(m); c.save(); return redirect(routes.Application.viewMyConversation(id)); } }
@Security.Authenticated(Secured.class) public static Result addNewStudAdvertisementForm(Long src) { Form<StudentAdvertisementForm> adForm = Form.form(StudentAdvertisementForm.class).bindFromRequest(); if (adForm.hasErrors()) { System.out.println("ERRORS"); System.out.println(adForm.errorsAsJson()); System.out.println(adForm.errors().entrySet()); return badRequest( postNewStudentAdvertisement.render( Student.find.byId(request().username()), adForm, null, src)); } else { String description = adForm.get().description; String studies = adForm.get().studies; boolean testAd = adForm.get().test; if (testAd) { System.out.println("CREATED TESTAD"); } StudentAdvertisement.create( Student.find.byId(request().username()), studies, description, testAd); if (src == 1) { return ok( viewAdvertisements.render( Student.find.byId(request().username()), StudentAdvertisement.find.all(), TutorAdvertisement.find.all())); } else { return ok( viewOwnAdvertisements.render( Student.find.byId(request().username()), StudentAdvertisement.find.all(), TutorAdvertisement.find.all())); } } }
@Security.Authenticated(Secured.class) public static Result addNewTutAdvertisementForm(Long src) { Form<TutorAdvertisementForm> adForm = Form.form(TutorAdvertisementForm.class).bindFromRequest(); if (adForm.hasErrors()) { return badRequest( postNewTutorAdvertisement.render( Student.find.byId(request().username()), adForm, null, src)); } else { if (src == 1) { return ok( viewAdvertisements.render( Student.find.byId(request().username()), StudentAdvertisement.find.all(), TutorAdvertisement.find.all())); } else { return ok( viewOwnAdvertisements.render( Student.find.byId(request().username()), StudentAdvertisement.find.all(), TutorAdvertisement.find.all())); } } }
public static Result login() { return ok(login.render(Form.form(Login.class))); }
public class Application extends Controller { static final Form<User> userForm = Form.form(User.class); public static class Login { public String email; public String password; public String validate() { ObjectNode jsonData = Json.newObject(); jsonData.put("email", email); jsonData.put("password", password); // POST Climate Service JSON data JsonNode response = RESTfulCalls.postAPI( Constants.URL_HOST + Constants.CMU_BACKEND_PORT + Constants.IS_USER_VALID, jsonData); if (response.get("success") == null) { return "Invalid user or password"; } return null; } } public static Result home() { return ok(home.render()); } public static Result login() { return ok(login.render(Form.form(Login.class))); } public static Result logout() { session().clear(); flash("success", "You've been logged out"); return redirect(routes.Application.home()); } public static Result createSuccess() { return ok(createSuccess.render()); } public static Result authenticate() { Form<Login> loginForm = Form.form(Login.class).bindFromRequest(); if (loginForm.hasErrors()) { return badRequest(login.render(loginForm)); } else { session().clear(); session("email", loginForm.get().email); return redirect(routes.Application.home()); } } public static void flashMsg(JsonNode jsonNode) { Iterator<Entry<String, JsonNode>> it = jsonNode.fields(); while (it.hasNext()) { Entry<String, JsonNode> field = it.next(); flash(field.getKey(), field.getValue().asText()); } } public static Result signup() { return ok(signup.render(userForm)); } public static Result createNewUser() { Form<User> nu = userForm.bindFromRequest(); ObjectNode jsonData = Json.newObject(); String userName = null; try { userName = nu.field("firstName").value() + " " + (nu.field("middleInitial")).value() + " " + (nu.field("lastName")).value(); jsonData.put("userName", userName); jsonData.put("firstName", nu.get().getFirstName()); jsonData.put("middleInitial", nu.get().getMiddleInitial()); jsonData.put("lastName", nu.get().getLastName()); jsonData.put("password", nu.get().getPassword()); jsonData.put("affiliation", nu.get().getAffiliation()); jsonData.put("title", nu.get().getTitle()); jsonData.put("email", nu.get().getEmail()); jsonData.put("mailingAddress", nu.get().getMailingAddress()); jsonData.put("phoneNumber", nu.get().getPhoneNumber()); jsonData.put("faxNumber", nu.get().getFaxNumber()); jsonData.put("researchFields", nu.get().getResearchFields()); jsonData.put("highestDegree", nu.get().getHighestDegree()); JsonNode response = RESTfulCalls.postAPI( Constants.URL_HOST + Constants.CMU_BACKEND_PORT + Constants.ADD_USER, jsonData); // flash the response message Application.flashMsg(response); return redirect(routes.Application.createSuccess()); } catch (IllegalStateException e) { e.printStackTrace(); Application.flashMsg(RESTfulCalls.createResponse(ResponseType.CONVERSIONERROR)); } catch (Exception e) { e.printStackTrace(); Application.flashMsg(RESTfulCalls.createResponse(ResponseType.UNKNOWN)); } return ok(signup.render(nu)); } public static Result isEmailExisted() { JsonNode json = request().body().asJson(); String email = json.path("email").asText(); ObjectNode jsonData = Json.newObject(); JsonNode response = null; try { jsonData.put("email", email); response = RESTfulCalls.postAPI( Constants.URL_HOST + Constants.CMU_BACKEND_PORT + Constants.IS_EMAIL_EXISTED, jsonData); Application.flashMsg(response); } catch (IllegalStateException e) { e.printStackTrace(); Application.flashMsg(RESTfulCalls.createResponse(ResponseType.CONVERSIONERROR)); } catch (Exception e) { e.printStackTrace(); Application.flashMsg(RESTfulCalls.createResponse(ResponseType.UNKNOWN)); } return ok(response); } }