1 package fr.ifremer.tutti.ui.swing.util.attachment.actions;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import fr.ifremer.tutti.persistence.entities.data.Attachment;
28 import fr.ifremer.tutti.persistence.entities.data.Attachments;
29 import fr.ifremer.tutti.ui.swing.util.actions.SimpleActionSupport;
30 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentEditorUI;
31 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentEditorUIHandler;
32 import fr.ifremer.tutti.ui.swing.util.attachment.AttachmentModelAware;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import java.io.File;
38
39
40
41
42
43
44
45 public class AddAttachmentAction extends SimpleActionSupport<AttachmentEditorUI> {
46
47
48 private static final Log log = LogFactory.getLog(AddAttachmentAction.class);
49
50 private static final long serialVersionUID = 1L;
51
52 public AddAttachmentAction(AttachmentEditorUI ui) {
53 super(ui);
54 }
55
56 @Override
57 protected void onActionPerformed(AttachmentEditorUI ui) {
58
59 File file = ui.getFile().getSelectedFile();
60 if (file != null) {
61 String name = ui.getFileName().getText();
62 if (StringUtils.isEmpty(name)) {
63 name = file.getName();
64 }
65
66 if (log.isInfoEnabled()) {
67 log.info("Add attachment: " + name);
68 }
69
70 AttachmentModelAware bean = ui.getBean();
71 Attachment attachment = Attachments.newAttachment();
72
73 attachment.setObjectType(bean.getObjectType());
74 attachment.setObjectId(bean.getObjectId());
75 attachment.setName(name);
76 attachment.setComment(ui.getFileComment().getText());
77
78 AttachmentEditorUIHandler handler = ui.getHandler();
79
80 if (bean.isCreate()) {
81 attachment.setPath(file.getPath());
82 } else {
83 attachment = handler.getPersistenceService().createAttachment(attachment, file);
84 }
85 bean.addAttachment(attachment);
86
87 handler.resetFields();
88 handler.addAttachment(attachment);
89
90 ui.pack();
91
92 }
93
94 }
95
96 }