PageRenderTime 45ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/tmm/nosql/mongodb/controller/ProfileController.java

https://github.com/robhinds/SpringMongoIntegrationExample
Java | 101 lines | 60 code | 20 blank | 21 comment | 1 complexity | ca380249f268c0f371dbde2b0a9fd839 MD5 | raw file
  1. package com.tmm.nosql.mongodb.controller;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.apache.commons.lang.WordUtils;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.web.servlet.ModelAndView;
  10. import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
  11. import com.google.gson.JsonObject;
  12. import com.tmm.nosql.mongodb.domain.Resume;
  13. import com.tmm.nosql.mongodb.domain.ResumePage;
  14. import com.tmm.nosql.mongodb.helper.JsonHelper;
  15. import com.tmm.nosql.mongodb.service.ProfileService;
  16. public class ProfileController extends MultiActionController {
  17. @Autowired
  18. private ProfileService profileService;
  19. public void setProfileService(ProfileService profileService) {
  20. this.profileService = profileService;
  21. }
  22. /**
  23. * Default handler to handle calls to view a resume.
  24. * If no resume exists for user then it automatically
  25. * creates a blank resume to allow user to begin
  26. * creating.
  27. *
  28. * @param request
  29. * @param response
  30. * @return
  31. * @throws Exception
  32. */
  33. public ModelAndView view(HttpServletRequest request, HttpServletResponse response) throws Exception {
  34. String userName = request.getParameter("userName");
  35. Resume r = profileService.get(userName);
  36. if (r==null){
  37. r = new Resume();
  38. r.setId(userName.toLowerCase());
  39. ResumePage p = new ResumePage();
  40. p.setTitle("Introduction");
  41. ResumePage p2 = new ResumePage();
  42. p2.setTitle("Experience");
  43. ResumePage p3 = new ResumePage();
  44. p3.setTitle("Education");
  45. ResumePage p4 = new ResumePage();
  46. p4.setTitle("References");
  47. r.addPage(p);
  48. r.addPage(p2);
  49. r.addPage(p3);
  50. r.addPage(p4);
  51. profileService.createResume(r);
  52. }
  53. JsonObject res = JsonHelper.buildJson(r);
  54. Map<String, Object> model = new HashMap<String, Object>();
  55. model.put("resume", res);
  56. return new ModelAndView("usereditpage", model);
  57. }
  58. /**
  59. * Controller method to handle requests
  60. * to add a new section to an existing resume.
  61. * returns ajax data to calling form to refresh details.
  62. *
  63. * @param request
  64. * @param response
  65. * @return
  66. * @throws Exception
  67. */
  68. public ModelAndView createSection(HttpServletRequest request, HttpServletResponse response) throws Exception {
  69. Map<String, Object> model = new HashMap<String, Object>();
  70. String sectionTitle = request.getParameter("sectionTitle");
  71. String sectionDetails = request.getParameter("sectionDetails");
  72. String resumeId = request.getParameter("resume");
  73. String pageId = request.getParameter("page");
  74. profileService.addSection(resumeId, pageId, sectionTitle, sectionDetails);
  75. JsonObject section = new JsonObject();
  76. section.addProperty("title", WordUtils.capitalize(sectionTitle));
  77. section.addProperty("details", StringUtils.capitalize(sectionDetails));
  78. model.put("section", section.toString());
  79. return new ModelAndView("ajax_useredit", model);
  80. }
  81. }