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.Ticket;
24 import net.sourceforge.mystique.domain.entity.User;
25 import net.sourceforge.mystique.domain.repository.RepositoryException;
26 import net.sourceforge.mystique.domain.repository.TicketRepository;
27 import net.sourceforge.mystique.infrastructure.validation.TicketValidator;
28
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Controller;
31 import org.springframework.ui.Model;
32 import org.springframework.validation.BindingResult;
33 import org.springframework.validation.ValidationUtils;
34 import org.springframework.web.bind.WebDataBinder;
35 import org.springframework.web.bind.annotation.InitBinder;
36 import org.springframework.web.bind.annotation.ModelAttribute;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.RequestMethod;
39 import org.springframework.web.bind.annotation.SessionAttributes;
40 import org.springframework.web.bind.support.SessionStatus;
41 import org.springframework.web.context.request.WebRequest;
42
43
44
45
46 @Controller
47 @RequestMapping("/ticket/add.xhtml")
48 @SessionAttributes(types = Ticket.class)
49 public class AddTicketForm {
50
51 @Autowired
52 private TicketRepository ticketRepository;
53
54 @InitBinder
55 public void setAllowedFields(WebDataBinder binder) {
56
57 binder.setDisallowedFields(new String[] { "id" });
58
59 }
60
61 @RequestMapping(method = RequestMethod.GET)
62 public String setupForm(Model model, WebRequest request) {
63
64 Ticket ticket = new Ticket();
65
66 User reporter = (User) request.getAttribute("user", WebRequest.SCOPE_SESSION);
67 ticket.setReporter(reporter);
68
69 model.addAttribute(ticket);
70
71 return "ticket.form";
72
73 }
74
75 @RequestMapping(method = RequestMethod.POST)
76
77 public String processSubmit(@ModelAttribute Ticket ticket, BindingResult result, SessionStatus status)
78 throws RepositoryException {
79
80 ValidationUtils.invokeValidator(new TicketValidator(), ticket, result);
81 if (result.hasErrors()) {
82 return "ticket.form";
83 }
84
85 ticketRepository.store(ticket);
86 status.setComplete();
87
88 return "redirect:list.xhtml";
89
90 }
91
92 }