1 package fr.ifremer.tutti.ui.swing.content.home.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 import com.google.common.base.Preconditions;
26 import fr.ifremer.tutti.persistence.entities.protocol.TuttiProtocol;
27 import fr.ifremer.tutti.service.protocol.ProtocolImportExportService;
28 import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
29 import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUI;
30 import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUIHandler;
31 import fr.ifremer.tutti.ui.swing.content.home.SelectCruiseUIModel;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import java.io.File;
36
37 import static org.nuiton.i18n.I18n.t;
38
39
40
41
42
43
44
45 public class ExportProtocolAction extends LongActionSupport<SelectCruiseUIModel, SelectCruiseUI, SelectCruiseUIHandler> {
46
47
48 private static final Log log =
49 LogFactory.getLog(ExportProtocolAction.class);
50
51 protected File file;
52
53 public ExportProtocolAction(SelectCruiseUIHandler handler) {
54 super(handler, true);
55 }
56
57 @Override
58 public boolean prepareAction() throws Exception {
59
60 boolean doAction = super.prepareAction();
61
62 if (doAction) {
63
64 TuttiProtocol protocol = getModel().getProtocol();
65
66 file = saveFile(
67 "protocol-" + protocol.getName().replaceAll(" ", "_"),
68 "tuttiProtocol",
69 t("tutti.selectCruise.title.choose.exportProtocolFile"),
70 t("tutti.selectCruise.action.exportProtocol"),
71 "^.+\\.tuttiProtocol$", t("tutti.common.file.protocol")
72 );
73 doAction = file != null;
74 }
75 return doAction;
76 }
77
78 @Override
79 public void releaseAction() {
80 file = null;
81 super.releaseAction();
82 }
83
84 @Override
85 public void doAction() throws Exception {
86 TuttiProtocol protocol = getModel().getProtocol();
87 Preconditions.checkNotNull(protocol);
88 Preconditions.checkNotNull(file);
89
90 if (log.isInfoEnabled()) {
91 log.info("Will save protocol " + protocol.getId() +
92 " to file: " + file);
93 }
94
95
96 ProtocolImportExportService service =
97 getContext().getTuttiProtocolImportExportService();
98 service.exportProtocol(protocol, file);
99
100 }
101
102 @Override
103 public void postSuccessAction() {
104
105 getHandler().resetComboBoxAction(getUI().getEditProtocolComboBox());
106
107 super.postSuccessAction();
108 TuttiProtocol protocol = getModel().getProtocol();
109 sendMessage(t("tutti.exportProtocol.action.success", protocol.getName(), file.getName()));
110
111 }
112 }