View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.home.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * %%
7    * Copyright (C) 2012 - 2014 Ifremer
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
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   * Opens a file chooser, exports the cruise catches into the selected file and open the default email editor.
43   *
44   * @author Kevin Morin - kmorin@codelutin.com
45   * @since 1.0
46   */
47  public class ExportCruiseForSumatraAction extends AbstractMainUITuttiAction {
48  
49      /** Logger. */
50      private static final Log log =
51              LogFactory.getLog(ExportCruiseForSumatraAction.class);
52  
53      private static final String EXPORT_FILE_NAME = "sumatra_%s_%s"; // sumatra_nom campagne - date du jour
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              // choose file to export
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); // loading cruise + loading fishing operationIds + export
115         setProgressionModel(pm);
116 
117         // export catches
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 }