View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.home.actions;
2   
3   /*
4    * #%L
5    * Tutti :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2016 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.ProgressionModel;
29  import fr.ifremer.tutti.persistence.entities.data.Cruise;
30  import fr.ifremer.tutti.service.TuttiDataContext;
31  import fr.ifremer.tutti.service.export.cps.CalcifiedPiecesSamplingExportService;
32  import fr.ifremer.tutti.service.cruise.CruiseCacheLoader;
33  import fr.ifremer.tutti.ui.swing.TuttiUIContext;
34  import fr.ifremer.tutti.ui.swing.content.MainUIHandler;
35  import fr.ifremer.tutti.ui.swing.content.actions.AbstractMainUITuttiAction;
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  import org.nuiton.util.DateUtil;
39  
40  import java.io.File;
41  import java.util.Date;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * @author Kevin Morin (Code Lutin)
47   * @since 4.5
48   */
49  public class CalcifiedPiecesSamplingReportAction extends AbstractMainUITuttiAction {
50  
51      /** Logger. */
52      private static final Log log = LogFactory.getLog(CalcifiedPiecesSamplingReportAction.class);
53  
54      protected File file;
55  
56      public CalcifiedPiecesSamplingReportAction(MainUIHandler handler) {
57          super(handler, false);
58      }
59  
60      @Override
61      public boolean prepareAction() throws Exception {
62  
63          boolean doAction = super.prepareAction();
64  
65          if (doAction && !getDataContext().isProtocolFilled()) {
66              displayErrorMessage(
67                      t("tutti.exportCpsCsv.title.missing.protocol"),
68                      t("tutti.exportCpsCsv.message.missing.protocol")
69              );
70              doAction = false;
71          }
72  
73          if (doAction && !getDataContext().getProtocol().isUseCalcifiedPieceSampling()) {
74              displayErrorMessage(
75                      t("tutti.exportCpsCsv.title.sampling.notActivated"),
76                      t("tutti.exportCpsCsv.message.sampling.notActivated")
77              );
78              doAction = false;
79          }
80  
81          if (doAction) {
82  
83              String date = DateUtil.formatDate(new Date(), "dd-MM-yyyy");
84              String exportFilename = t("tutti.exportCpsCsv.fileName", getDataContext().getCruise().getName(), date);
85  
86              // choose file to export
87              file = saveFile(
88                      exportFilename,
89                      "csv",
90                      t("tutti.exportCpsCsv.title.choose.exportFile"),
91                      t("tutti.exportCpsCsv.action.chooseFile"),
92                      "^.+\\.csv$", t("tutti.common.file.csv")
93              );
94              doAction = file != null;
95          }
96          return doAction;
97      }
98  
99      @Override
100     public void doAction() throws Exception {
101 
102         TuttiDataContext dataContext = getDataContext();
103         Cruise cruise = dataContext.getCruise();
104         Preconditions.checkNotNull(cruise);
105         Preconditions.checkNotNull(file);
106 
107         if (log.isInfoEnabled()) {
108             log.info("Will export cps for cruise " + cruise.getId() + " to file: " + file);
109         }
110         ProgressionModel pm = new ProgressionModel();
111         setProgressionModel(pm);
112 
113         long cruiseFishingOperationIds = dataContext.getCruiseFishingOperationIds().size();
114         pm.setTotal((int) (1 + cruiseFishingOperationIds));
115 
116         TuttiUIContext context = getContext();
117         if (!dataContext.isCruiseCacheLoaded() || !dataContext.isCruiseCacheUpToDate()) {
118 
119             // load (or reload) cache
120             CruiseCacheLoader cruiseCacheLoader = context.createCruiseCacheLoader(getProgressionModel());
121             dataContext.loadCruiseCache(cruiseCacheLoader);
122         }
123 
124 
125         // export sampling report
126 
127         CalcifiedPiecesSamplingExportService service = context.getCalcifiedPiecesSamplingExportService();
128         service.exportCruiseCalcifiedPiecesSamplingsReport(file, pm);
129     }
130 
131     @Override
132     public void postSuccessAction() {
133         super.postSuccessAction();
134         sendMessage(t("tutti.exportCpsCsv.action.success", file));
135     }
136 
137     @Override
138     public void releaseAction() {
139         file = null;
140         super.releaseAction();
141     }
142 
143 }