View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.report.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 com.google.common.base.Preconditions;
28  import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
29  import fr.ifremer.tutti.ui.swing.content.report.ReportUI;
30  import fr.ifremer.tutti.ui.swing.content.report.ReportUIHandler;
31  import fr.ifremer.tutti.ui.swing.content.report.ReportUIModel;
32  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.nuiton.decorator.Decorator;
36  import org.nuiton.jaxx.application.ApplicationIOUtil;
37  
38  import java.io.File;
39  
40  import static org.nuiton.i18n.I18n.t;
41  
42  /**
43   * Created on 3/11/15.
44   *
45   * @author Tony Chemit - chemit@codelutin.com
46   * @since 3.13.2
47   */
48  public class SaveReportAction extends LongActionSupport<ReportUIModel, ReportUI, ReportUIHandler> {
49  
50      /** Logger. */
51      private static final Log log = LogFactory.getLog(SaveReportAction.class);
52  
53      protected File file;
54  
55      public SaveReportAction(ReportUIHandler handler) {
56          super(handler, true);
57      }
58  
59      @Override
60      public boolean prepareAction() throws Exception {
61  
62          boolean doAction = super.prepareAction();
63  
64          if (doAction) {
65  
66              FishingOperation fishingOperation = getModel().getFishingOperation();
67  
68              Preconditions.checkNotNull("Can't have a null fishing operation", fishingOperation);
69              Preconditions.checkState(getModel().isReportDone(), "Result must be done");
70  
71              Decorator<FishingOperation> decorator = getDecorator(FishingOperation.class, null);
72  
73              String filename = "rapport-" + decorator.toString(fishingOperation) + ".pdf";
74              filename = filename.replaceAll("/", "_").replaceAll("\\s", "");
75  
76              if (log.isInfoEnabled()) {
77                  log.info("Default save filename: " + filename);
78              }
79  
80              file = saveFileWithStartDirectory(
81                      getConfig().getReportBackupDirectory(),
82                      false,
83                      filename,
84                      null,
85                      t("tutti.report.title.choose.saveReportFile"),
86                      t("tutti.report.action.save"),
87                      "^.+\\.pdf$", t("tutti.common.file.pdf"));
88  
89              doAction = file != null;
90  
91          }
92  
93          return doAction;
94  
95      }
96  
97      @Override
98      public void doAction() throws Exception {
99  
100         File outputFile = getModel().getOutputFile();
101         ApplicationIOUtil.copyFile(outputFile, file, "Could not save file to " + file);
102 
103     }
104 
105     @Override
106     public void postSuccessAction() {
107 
108         super.postSuccessAction();
109         sendMessage(t("tutti.report.saved", file));
110 
111     }
112 
113     @Override
114     protected void releaseAction() {
115 
116         super.releaseAction();
117         file = null;
118 
119     }
120 
121 }