1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sourceforge.mystique.application;
22
23 import net.sourceforge.mystique.domain.entity.Comment;
24 import net.sourceforge.mystique.domain.entity.Ticket;
25 import net.sourceforge.mystique.domain.entity.User;
26 import net.sourceforge.mystique.domain.repository.CommentRepository;
27 import net.sourceforge.mystique.domain.repository.RepositoryException;
28 import net.sourceforge.mystique.domain.repository.TicketRepository;
29 import net.sourceforge.mystique.infrastructure.validation.CommentValidator;
30
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.ui.Model;
34 import org.springframework.validation.BindingResult;
35 import org.springframework.validation.ValidationUtils;
36 import org.springframework.web.bind.WebDataBinder;
37 import org.springframework.web.bind.annotation.InitBinder;
38 import org.springframework.web.bind.annotation.ModelAttribute;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.bind.annotation.RequestParam;
42 import org.springframework.web.bind.annotation.SessionAttributes;
43 import org.springframework.web.bind.support.SessionStatus;
44 import org.springframework.web.context.request.WebRequest;
45
46
47
48
49 @Controller
50 @RequestMapping("/project/comment/add.xhtml")
51 @SessionAttributes("comment")
52 public class AddCommentForm {
53
54 @Autowired
55 private TicketRepository ticketRepository;
56
57 @Autowired
58 private CommentRepository commentRepository;
59
60 @InitBinder
61 public void setAllowedFields(WebDataBinder binder) {
62
63 binder.setDisallowedFields(new String[] { "id" });
64
65 }
66
67 @RequestMapping(method = RequestMethod.GET)
68 public String setupForm(@RequestParam("ticketId") Long ticketId, Model model, WebRequest request)
69 throws RepositoryException {
70
71 Comment comment = new Comment();
72
73 User author = (User) request.getAttribute("user", WebRequest.SCOPE_SESSION);
74 comment.setAuthor(author);
75
76 Ticket ticket = ticketRepository.findById(ticketId);
77 ticket.addComment(comment);
78
79 model.addAttribute(comment);
80
81 return "ticket.comment.form";
82
83 }
84
85 @RequestMapping(method = RequestMethod.POST)
86
87 public String processSubmit(@ModelAttribute("comment") Comment comment, BindingResult result, SessionStatus status)
88 throws RepositoryException {
89
90 ValidationUtils.invokeValidator(new CommentValidator(), comment, result);
91 if (result.hasErrors()) {
92 return "ticket.comment.form";
93 }
94
95 commentRepository.store(comment);
96
97 status.setComplete();
98 Long ticketId = comment.getTicket().getId();
99
100 return "redirect:list.xhtml?ticketId=" + ticketId;
101
102 }
103
104 }