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.entity.Project;
25 import net.sourceforge.mystique.domain.repository.MilestoneRepository;
26 import net.sourceforge.mystique.domain.repository.ProjectRepository;
27 import net.sourceforge.mystique.domain.repository.RepositoryException;
28 import net.sourceforge.mystique.infrastructure.validation.MilestoneValidator;
29
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.dao.DataIntegrityViolationException;
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
45
46
47
48 @Controller
49 @RequestMapping("/project/milestone/add.xhtml")
50 @SessionAttributes("milestone")
51 public class AddMilestoneForm {
52
53 @Autowired
54 private ProjectRepository projectRepository;
55
56 @Autowired
57 private MilestoneRepository milestoneRepository;
58
59 @InitBinder
60 public void setAllowedFields(WebDataBinder binder) {
61
62 binder.setDisallowedFields(new String[] { "id" });
63
64 }
65
66 @RequestMapping(method = RequestMethod.GET)
67 public String setupForm(@RequestParam("projectId") Long projectId, Model model) throws RepositoryException {
68
69 Milestone milestone = new Milestone();
70
71 Project project = projectRepository.findById(projectId);
72 project.addMilestone(milestone);
73
74 model.addAttribute("milestone", milestone);
75
76 return "project.milestone.form";
77
78 }
79
80 @RequestMapping(method = RequestMethod.POST)
81
82 public String processSubmit(@ModelAttribute("milestone") Milestone milestone, BindingResult result,
83 SessionStatus status) throws RepositoryException {
84
85 ValidationUtils.invokeValidator(new MilestoneValidator(), milestone, result);
86 if (result.hasErrors()) {
87 return "project.milestone.form";
88 }
89
90 try {
91 milestoneRepository.store(milestone);
92 } catch (DataIntegrityViolationException e) {
93 result.reject(e.getMessage(), new Object[] { milestone.getName(), milestone.getProject().getName() }, null);
94 return "project.milestone.form";
95 }
96
97 status.setComplete();
98 Long projectId = milestone.getProject().getId();
99
100 return "redirect:list.xhtml?projectId=" + projectId;
101
102 }
103
104 }