View Javadoc
1   package fr.ifremer.tutti.ui.swing.util.attachment.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
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   * Created on 3/7/15.
41   *
42   * @author Tony Chemit - chemit@codelutin.com
43   * @since 3.15
44   */
45  public class AddAttachmentAction extends SimpleActionSupport<AttachmentEditorUI> {
46  
47      /** Logger. */
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  }