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.ProgressionModel;
27 import fr.ifremer.tutti.persistence.entities.data.Cruise;
28 import fr.ifremer.tutti.service.export.sumatra.CatchesSumatraExportService;
29 import fr.ifremer.tutti.service.export.sumatra.SumatraExportResult;
30 import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
31 import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.nuiton.util.DateUtil;
35
36 import java.io.File;
37 import java.util.Date;
38
39 import static org.nuiton.i18n.I18n.t;
40
41
42
43
44
45
46
47 public class ExportCruiseForSumatraAction extends AbstractMainUITuttiAction {
48
49
50 private static final Log log =
51 LogFactory.getLog(ExportCruiseForSumatraAction.class);
52
53 private static final String EXPORT_FILE_NAME = "sumatra_%s_%s";
54
55 protected File file;
56
57 protected SumatraExportResult sumatraExportResult;
58
59 public ExportCruiseForSumatraAction(MainUIHandler handler) {
60 super(handler, false);
61 }
62
63 @Override
64 public boolean prepareAction() throws Exception {
65
66 boolean doAction = super.prepareAction();
67
68 if (doAction) {
69
70 if (!getDataContext().isProtocolFilled()) {
71 displayWarningMessage(
72 t("tutti.exportCruiseCsv.title.missing.protocol"),
73 t("tutti.exportCruiseCsv.message.missing.protocol")
74 );
75 }
76 }
77
78 if (doAction) {
79
80 String date = DateUtil.formatDate(new Date(), "dd-MM-yyyy");
81 String exportFilename = String.format(EXPORT_FILE_NAME, getDataContext().getCruise().getName(), date);
82
83
84 file = saveFile(
85 exportFilename,
86 "csv",
87 t("tutti.exportCruiseCsv.title.choose.exportFile"),
88 t("tutti.exportCruiseCsv.action.chooseFile"),
89 "^.+\\.csv$", t("tutti.common.file.csv")
90 );
91 doAction = file != null;
92 }
93 return doAction;
94 }
95
96 @Override
97 public void releaseAction() {
98 file = null;
99 sumatraExportResult = null;
100 super.releaseAction();
101 }
102
103 @Override
104 public void doAction() throws Exception {
105 Cruise cruise = getDataContext().getCruise();
106 Preconditions.checkNotNull(cruise);
107 Preconditions.checkNotNull(file);
108
109 if (log.isInfoEnabled()) {
110 log.info("Will export cruise " + cruise.getId() +
111 " to file: " + file);
112 }
113 ProgressionModel pm = new ProgressionModel();
114 pm.setTotal(3);
115 setProgressionModel(pm);
116
117
118 CatchesSumatraExportService service =
119 getContext().getCatchesSumatraExportService();
120 sumatraExportResult = service.exportCruiseForSumatra(file, cruise.getIdAsInt(), pm);
121
122 }
123
124 @Override
125 public void postSuccessAction() {
126 super.postSuccessAction();
127
128 if (sumatraExportResult.withBadSpecies()) {
129
130 StringBuilder badSpeciesList = new StringBuilder();
131 for (String s : sumatraExportResult.getBadSpecies()) {
132 badSpeciesList.append("<li>").append(s);
133 }
134
135 displayWarningMessage(
136 t("tutti.exportCruiseCsv.title.badSpecies"),
137 t("tutti.exportCruiseCsv.message.badSpecies", badSpeciesList.toString()));
138 }
139
140 if (sumatraExportResult.withBadBenthos()) {
141
142 StringBuilder badBenthosList = new StringBuilder();
143 for (String s : sumatraExportResult.getBadBenthos()) {
144 badBenthosList.append("<li>").append(s);
145 }
146
147 displayWarningMessage(
148 t("tutti.exportCruiseCsv.title.badBenthos"),
149 t("tutti.exportCruiseCsv.message.badBenthos", badBenthosList.toString()));
150 }
151
152 sendMessage(t("tutti.exportCruiseCsv.action.success", file));
153 }
154 }