1 package fr.ifremer.tutti.ui.swing.content.report.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 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
44
45
46
47
48 public class SaveReportAction extends LongActionSupport<ReportUIModel, ReportUI, ReportUIHandler> {
49
50
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 }