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.Component;
24 import net.sourceforge.mystique.domain.repository.ComponentRepository;
25 import net.sourceforge.mystique.domain.repository.RepositoryException;
26 import net.sourceforge.mystique.infrastructure.validation.ComponentValidator;
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/component/edit.xhtml")
48 @SessionAttributes("component")
49 public class EditComponentForm {
50
51 @Autowired
52 private ComponentRepository componentRepository;
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("componentId") Long componentId, Model model) throws RepositoryException {
63
64 Component component = componentRepository.findById(componentId);
65 model.addAttribute("component", component);
66
67 return "project.component.form";
68
69 }
70
71 @RequestMapping(method = RequestMethod.POST)
72
73 public String processSubmit(@ModelAttribute("component") Component component, BindingResult result,
74 SessionStatus status) throws RepositoryException {
75
76 ValidationUtils.invokeValidator(new ComponentValidator(), component, result);
77 if (result.hasErrors()) {
78 return "project.component.form";
79 }
80
81 try {
82 componentRepository.store(component);
83 } catch (DataIntegrityViolationException e) {
84 result.reject(e.getMessage(), new Object[] { component.getName(), component.getProject().getName() }, null);
85 return "project.component.form";
86 }
87
88 status.setComplete();
89 Long projectId = component.getProject().getId();
90
91 return "redirect:list.xhtml?projectId=" + projectId;
92
93 }
94
95 }