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.Milestone;
24 import net.sourceforge.mystique.domain.repository.MilestoneRepository;
25 import net.sourceforge.mystique.domain.repository.RepositoryException;
26 import net.sourceforge.mystique.infrastructure.validation.MilestoneValidator;
27
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.dao.DataIntegrityViolationException;
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.RequestParam;
40 import org.springframework.web.bind.annotation.SessionAttributes;
41 import org.springframework.web.bind.support.SessionStatus;
42
43
44
45
46 @Controller
47 @RequestMapping("/project/milestone/edit.xhtml")
48 @SessionAttributes("milestone")
49 public class EditMilestoneForm {
50
51 @Autowired
52 private MilestoneRepository milestoneRepository;
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(@RequestParam("milestoneId") Long milestoneId, Model model) throws RepositoryException {
63
64 Milestone milestone = milestoneRepository.findById(milestoneId);
65 model.addAttribute("milestone", milestone);
66
67 return "project.milestone.form";
68
69 }
70
71 @RequestMapping(method = RequestMethod.POST)
72
73 public String processSubmit(@ModelAttribute("milestone") Milestone milestone, BindingResult result,
74 SessionStatus status) throws RepositoryException {
75
76 ValidationUtils.invokeValidator(new MilestoneValidator(), milestone, result);
77 if (result.hasErrors()) {
78 return "project.milestone.form";
79 }
80
81 try {
82 milestoneRepository.store(milestone);
83 } catch (DataIntegrityViolationException e) {
84 result.reject(e.getMessage(), new Object[] { milestone.getName(), milestone.getProject().getName() }, null);
85 return "project.milestone.form";
86 }
87
88 status.setComplete();
89 Long projectId = milestone.getProject().getId();
90
91 return "redirect:list.xhtml?projectId=" + projectId;
92
93 }
94
95 }