/connect-web/src/main/java/org/osforce/connect/web/module/knowledge/AnswerWidget.java

http://focus-sns.googlecode.com/ · Java · 107 lines · 85 code · 11 blank · 11 comment · 4 complexity · d53e02e63eb2462a8468748cf2897963 MD5 · raw file

  1. package org.osforce.connect.web.module.knowledge;
  2. import javax.validation.Valid;
  3. import org.osforce.connect.entity.commons.VoteRecord;
  4. import org.osforce.connect.entity.knowledge.Answer;
  5. import org.osforce.connect.entity.system.Project;
  6. import org.osforce.connect.entity.system.ProjectFeature;
  7. import org.osforce.connect.entity.system.User;
  8. import org.osforce.connect.service.commons.VoteRecordService;
  9. import org.osforce.connect.service.knowledge.AnswerService;
  10. import org.osforce.connect.web.AttributeKeys;
  11. import org.osforce.connect.web.security.annotation.Permission;
  12. import org.osforce.spring4me.dao.Page;
  13. import org.osforce.spring4me.web.bind.annotation.RequestAttr;
  14. import org.osforce.spring4me.web.bind.annotation.SessionParam;
  15. import org.osforce.spring4me.web.stereotype.Widget;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.ui.Model;
  18. import org.springframework.validation.BindingResult;
  19. import org.springframework.web.bind.annotation.ModelAttribute;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestMethod;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. /**
  24. *
  25. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  26. * @since 1.1.0
  27. * @create May 20, 2011 - 6:46:37 PM
  28. * <a href="http://www.opensourceforce.org">????</a>
  29. */
  30. @Widget
  31. @RequestMapping("/knowledge/answer")
  32. public class AnswerWidget {
  33. private AnswerService answerService;
  34. private VoteRecordService voteRecordService;
  35. public AnswerWidget() {
  36. }
  37. @Autowired
  38. public void setAnswerService(AnswerService answerService) {
  39. this.answerService = answerService;
  40. }
  41. @Autowired
  42. public void setVoteRecordService(VoteRecordService voteRecordService) {
  43. this.voteRecordService = voteRecordService;
  44. }
  45. @RequestMapping("/list-view")
  46. @Permission({"answer-view"})
  47. public String doListView(@RequestParam Long questionId,
  48. @SessionParam String answerOrder, Page<Answer> page, Model model) {
  49. page = answerService.getAnswerPage(page, questionId, answerOrder);
  50. // vote count
  51. for(Answer answer : page.getResult()) {
  52. // usefull
  53. Long useful = voteRecordService.countVoteRecords(
  54. VoteRecord.CODE_USEFUL, answer.getId(), Answer.NAME);
  55. answer.setUseful(useful);
  56. // useless
  57. Long useless = voteRecordService.countVoteRecords(
  58. VoteRecord.CODE_USELESS, answer.getId(), Answer.NAME);
  59. answer.setUseless(useless);
  60. }
  61. model.addAttribute(AttributeKeys.PAGE_KEY_READABLE, page);
  62. return "knowledge/answer-list";
  63. }
  64. @RequestMapping("/form-view")
  65. @Permission(value={"answer-add", "answer-edit"}, userRequired=true, projectRequired=true)
  66. public String doFormView(
  67. @RequestParam(required=false) Long answerId,
  68. @RequestParam(required=false) Long questionId,
  69. @ModelAttribute @Valid Answer answer, BindingResult result,
  70. @RequestAttr Project project, @RequestAttr User user, Model model, Boolean showErrors) {
  71. if(!showErrors) {
  72. answer.setQuestionId(questionId);
  73. answer.setEnteredBy(user);
  74. answer.setModifiedBy(user);
  75. if(answerId!=null) {
  76. answer = answerService.getAnswer(answerId);
  77. }
  78. model.addAttribute(AttributeKeys.ANSWER_KEY_READABLE, answer);
  79. }
  80. return "knowledge/answer-form";
  81. }
  82. @RequestMapping(value="/form-action", method=RequestMethod.POST)
  83. @Permission(value={"answer-add", "answer-edit"}, userRequired=true, projectRequired=true)
  84. public String doFormAction(@ModelAttribute @Valid Answer answer,
  85. BindingResult result, Model model, @RequestAttr Project project) {
  86. if(result.hasErrors()) {
  87. model.addAttribute(AttributeKeys.SHOW_ERRORS_KEY_READABLE, true);
  88. model.addAttribute(AttributeKeys.FEATURE_CODE_KEY_READABLE, ProjectFeature.FEATURE_KNOWLEDGE);
  89. return "page:/knowledge/question-detail";
  90. }
  91. //
  92. answerService.createAnswer(answer);
  93. return String.format("redirect:/%s/knowledge/question/detail?questionId=%s",
  94. project.getUniqueId(), answer.getQuestionId());
  95. }
  96. }