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 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
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
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 }