View Javadoc

1   /**
2    * Copyright (c) 2009-2010, Elias Gomes. All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5    * following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this list of conditions and the following
8    * disclaimer.
9    * 
10   * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided with the distribution.
12   * 
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
14   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
16   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20   */
21  package net.sourceforge.mystique.application;
22  
23  import net.sourceforge.mystique.domain.entity.Project;
24  import net.sourceforge.mystique.domain.entity.Version;
25  import net.sourceforge.mystique.domain.repository.ProjectRepository;
26  import net.sourceforge.mystique.domain.repository.RepositoryException;
27  import net.sourceforge.mystique.domain.repository.VersionRepository;
28  import net.sourceforge.mystique.infrastructure.validation.VersionValidator;
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   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
47   */
48  @Controller
49  @RequestMapping("/project/version/add.xhtml")
50  @SessionAttributes("version")
51  public class AddVersionForm {
52  
53  	@Autowired
54  	private ProjectRepository projectRepository;
55  
56  	@Autowired
57  	private VersionRepository versionRepository;
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  		Version version = new Version();
70  
71  		Project project = projectRepository.findById(projectId);
72  		project.addVersion(version);
73  
74  		model.addAttribute("version", version);
75  
76  		return "project.version.form";
77  
78  	}
79  
80  	@RequestMapping(method = RequestMethod.POST)
81  	// TODO: Show success or error message
82  	public String processSubmit(@ModelAttribute("version") Version version, BindingResult result, SessionStatus status)
83  			throws RepositoryException {
84  
85  		ValidationUtils.invokeValidator(new VersionValidator(), version, result);
86  		if (result.hasErrors()) {
87  			return "project.version.form";
88  		}
89  
90  		try {
91  			versionRepository.store(version);
92  		} catch (DataIntegrityViolationException e) {
93  			result.reject(e.getMessage(), new Object[] { version.getNumber(), version.getProject().getName() }, null);
94  			return "project.version.form";
95  		}
96  
97  		status.setComplete();
98  		Long projectId = version.getProject().getId();
99  
100 		return "redirect:list.xhtml?projectId=" + projectId;
101 
102 	}
103 
104 }