View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.report.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.service.report.ReportGenerationRequest;
28  import fr.ifremer.tutti.service.report.ReportGenerationResult;
29  import fr.ifremer.tutti.service.report.ReportGenerationService;
30  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
31  import fr.ifremer.tutti.ui.swing.content.report.ReportUI;
32  import fr.ifremer.tutti.ui.swing.content.report.ReportUIHandler;
33  import fr.ifremer.tutti.ui.swing.content.report.ReportUIModel;
34  
35  import static org.nuiton.i18n.I18n.t;
36  
37  /**
38   * To generate the selected report.
39   *
40   * @author Tony Chemit - chemit@codelutin.com
41   * @since 2.9
42   */
43  public class GenerateReportAction extends LongActionSupport<ReportUIModel, ReportUI, ReportUIHandler> {
44  
45      protected ReportGenerationResult reportGenerationResult;
46  
47      public GenerateReportAction(ReportUIHandler handler) {
48          super(handler, true);
49      }
50  
51      @Override
52      public boolean prepareAction() throws Exception {
53          boolean doAction = super.prepareAction();
54  
55          if (doAction) {
56  
57              getModel().setReportGenerationResult(null);
58  
59          }
60  
61          return doAction;
62      }
63  
64      @Override
65      public void doAction() throws Exception {
66          Preconditions.checkState(getModel().isValid());
67  
68          createProgressionModelIfRequired(1);
69  
70          ProgressionModel progressionModel = getProgressionModel();
71  
72          progressionModel.increments(t("tutti.generateReport.action.computeNbSteps"));
73  
74          ReportGenerationRequest reportGenerationRequest = getModel().toEntity();
75  
76          ReportGenerationService reportGenerationService = getContext().getReportGenerationService();
77  
78          int nbSteps = reportGenerationService.getNbSteps(reportGenerationRequest);
79          progressionModel.adaptTotal(nbSteps);
80  
81          reportGenerationResult = reportGenerationService.generateReport(reportGenerationRequest, progressionModel);
82  
83      }
84  
85      @Override
86      public void postSuccessAction() {
87  
88          super.postSuccessAction();
89          getModel().setReportGenerationResult(reportGenerationResult);
90          sendMessage(t("tutti.report.generated"));
91  
92      }
93  
94      @Override
95      protected void releaseAction() {
96          super.releaseAction();
97          reportGenerationResult = null;
98      }
99  }