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 java.io.IOException;
24  
25  import net.sourceforge.mystique.domain.entity.Attachment;
26  import net.sourceforge.mystique.domain.entity.Ticket;
27  import net.sourceforge.mystique.domain.entity.User;
28  import net.sourceforge.mystique.domain.repository.AttachmentRepository;
29  import net.sourceforge.mystique.domain.repository.RepositoryException;
30  import net.sourceforge.mystique.domain.repository.TicketRepository;
31  import net.sourceforge.mystique.domain.valueobject.Configuration;
32  import net.sourceforge.mystique.infrastructure.validation.MultipartFileValidator;
33  
34  import org.springframework.beans.factory.annotation.Autowired;
35  import org.springframework.stereotype.Controller;
36  import org.springframework.ui.Model;
37  import org.springframework.validation.BindingResult;
38  import org.springframework.validation.ValidationUtils;
39  import org.springframework.web.bind.WebDataBinder;
40  import org.springframework.web.bind.annotation.InitBinder;
41  import org.springframework.web.bind.annotation.ModelAttribute;
42  import org.springframework.web.bind.annotation.RequestMapping;
43  import org.springframework.web.bind.annotation.RequestMethod;
44  import org.springframework.web.bind.annotation.RequestParam;
45  import org.springframework.web.bind.annotation.SessionAttributes;
46  import org.springframework.web.bind.support.SessionStatus;
47  import org.springframework.web.context.request.WebRequest;
48  import org.springframework.web.multipart.MultipartFile;
49  
50  /**
51   * @author Elias Gomes [eliasgomes@users.sourceforge.net]
52   */
53  @Controller
54  @RequestMapping("/project/attachment/add.xhtml")
55  @SessionAttributes("attachment")
56  public class AddAttachmentForm {
57  
58  	@Autowired
59  	private TicketRepository ticketRepository;
60  
61  	@Autowired
62  	private AttachmentRepository attachmentRepository;
63  
64  	@Autowired
65  	private Configuration configuration;
66  
67  	@InitBinder
68  	public void setAllowedFields(WebDataBinder binder) {
69  
70  		binder.setDisallowedFields(new String[] { "id" });
71  
72  	}
73  
74  	@RequestMapping(method = RequestMethod.GET)
75  	public String setupForm(@RequestParam("ticketId") Long ticketId, Model model, WebRequest request)
76  			throws RepositoryException {
77  
78  		Attachment attachment = new Attachment();
79  
80  		User owner = (User) request.getAttribute("user", WebRequest.SCOPE_SESSION);
81  		attachment.setOwner(owner);
82  
83  		Ticket ticket = ticketRepository.findById(ticketId);
84  		ticket.addAttachment(attachment);
85  
86  		model.addAttribute(attachment);
87  
88  		return "ticket.attachment.form";
89  
90  	}
91  
92  	@RequestMapping(method = RequestMethod.POST)
93  	// TODO: Show success or error message
94  	public String processSubmit(@RequestParam MultipartFile multipartFile, @ModelAttribute Attachment attachment,
95  			BindingResult result, SessionStatus status) throws RepositoryException, IOException {
96  
97  		ValidationUtils.invokeValidator(new MultipartFileValidator(configuration), multipartFile, result);
98  		if (result.hasErrors()) {
99  			return "ticket.attachment.form";
100 		}
101 
102 		attachment.setName(multipartFile.getOriginalFilename());
103 		attachment.setMimeType(multipartFile.getContentType());
104 		attachment.setLength(multipartFile.getSize());
105 
106 		attachmentRepository.store(attachment);
107 
108 		attachment.storeStream(configuration.getAttachmentsPath(), multipartFile.getInputStream());
109 
110 		status.setComplete();
111 		Long ticketId = attachment.getTicket().getId();
112 
113 		return "redirect:list.xhtml?ticketId=" + ticketId;
114 
115 	}
116 
117 }