/trunk/app/controllers/Application.java

https://code.google.com/ · Java · 265 lines · 128 code · 121 blank · 16 comment · 13 complexity · b5523b03e2726e4a32b1e20cf65b788e MD5 · raw file

  1. package controllers;
  2. import play.mvc.*;
  3. import play.data.validation.Required;
  4. import models.*;
  5. import com.boun.give2get.registration.RegistrationController;
  6. import com.boun.give2get.db.DAO;
  7. import com.boun.give2get.mail.MailUtil;
  8. import com.boun.give2get.exceptions.MailException;
  9. import org.apache.log4j.Logger;
  10. import java.util.List;
  11. public class Application extends Controller {
  12. private static final org.apache.log4j.Logger log = Logger.getLogger(Application.class);
  13. @Before
  14. public static void addUser() {
  15. User user = isUserConnected();
  16. if (user != null) {
  17. System.out.println("User is connected!");
  18. renderArgs.put("user", user);
  19. renderArgs.put("username", user.getRegistration().getFirstname().concat(" ").concat(user.getRegistration().getLastname()));
  20. }
  21. }
  22. public static final User isUserConnected() {
  23. // Check render Args
  24. if (renderArgs.get("user") != null) {
  25. User user = renderArgs.get("user", User.class);
  26. System.out.println("User found in render args!");
  27. return user;
  28. }
  29. // Check session
  30. String userid = session.get("userid");
  31. if (userid != null) {
  32. User user = DAO.getUser(Integer.valueOf(userid).intValue());
  33. System.out.println("User fetched from databse!");
  34. return user;
  35. }
  36. // Not Logged in
  37. return null;
  38. }
  39. public static void index() {
  40. // Recent Activities;
  41. List<Activity> recentActivities = DAO.getRecentActivities();
  42. renderArgs.put("recentActivities", recentActivities);
  43. // Top Rated Services
  44. List<TopRatedService> topRatedServices = DAO.getTopRatedServices();
  45. renderArgs.put("topRatedServices", topRatedServices);
  46. render();
  47. }
  48. public static void signup() {
  49. render();
  50. }
  51. public static void signup(String status) {
  52. render(status);
  53. }
  54. public static final void saveUser(
  55. @Required(message = "Email is required") String email,
  56. @Required(message = "Firstname is required") String firstname,
  57. @Required(message = "Lastname is required") String lastname,
  58. @Required(message = "Password is required") String password) throws MailException {
  59. if (validation.hasErrors()) {
  60. System.out.println("validation error!");
  61. params.flash();
  62. validation.keep();
  63. signup();
  64. }
  65. System.out.println("Registering...");
  66. // New Registration
  67. Registration registration = Registration.create(email, firstname, lastname, password);
  68. // Persist
  69. DAO.saveRegistration(registration);
  70. // Send Registration Email
  71. MailUtil.sendRegistrationEmail(registration);
  72. flash.success(
  73. "Thank you %s,\n\nPlease check your mail inbox for the confirmation.",
  74. registration.getFirstname().concat(" ").concat(registration.getLastname())
  75. );
  76. signup();
  77. }
  78. public static void login(String email, String password) {
  79. User user = DAO.login(email, password);
  80. if (user == null) {
  81. flash.error("No User Found! Please Check your credentials!");
  82. index();
  83. }else {
  84. session.put("userid", user.getId());
  85. session.put("username", user.getRegistration().getFirstname().concat(" ").concat(user.getRegistration().getLastname()));
  86. renderArgs.put("user", user);
  87. index();
  88. }
  89. }
  90. public static final void complete(String reg) {
  91. // Activate Registration and create User
  92. RegistrationController.activateRegistration(reg);
  93. // Say Welcome to the new user
  94. MailUtil.sayThankyouForRegistration(RegistrationController.getRegistration(reg).getEmail());
  95. flash.success(
  96. "Your account is now activated. Please feel free to login and Give 2 Get!"
  97. );
  98. render();
  99. }
  100. public static final void logout() {
  101. session.clear();
  102. index();
  103. }
  104. public static final void about() {
  105. render();
  106. }
  107. public static final void contact() {
  108. render();
  109. }
  110. public static final void terms() {
  111. render();
  112. }
  113. public static final void search() {
  114. render();
  115. }
  116. public static final void profile() {
  117. if (session.get("userid") != null) {
  118. System.out.println("profile()");
  119. int userId = Integer.valueOf(session.get("userid")).intValue();
  120. System.out.println("userId=" + userId);
  121. String username = session.get("username");
  122. System.out.println(username);
  123. // Rating Info
  124. // Basic Info
  125. User user = DAO.getUserDetailedInfo(userId);
  126. // Posted Services
  127. List<Service> userProvidedServices = DAO.getUserPostedServices(userId);
  128. // Latest Comments
  129. List<Comment> userComments = DAO.getUserLatestComments(userId);
  130. render(user, username, userProvidedServices, userComments);
  131. }
  132. index();
  133. }
  134. public static final void user(int id) {
  135. if (session.get("userid") == null) {
  136. index();
  137. }
  138. // Basic Info
  139. User user = DAO.getUserDetailedInfo(id);
  140. // Services
  141. List<Service> userServices = DAO.getUserPostedServices(id);
  142. render(user, userServices);
  143. }
  144. }