View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.validation.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.PersistenceService;
28  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
29  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUI;
30  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUIHandler;
31  import fr.ifremer.tutti.ui.swing.content.validation.ValidateCruiseUIModel;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import javax.swing.JOptionPane;
36  import java.util.List;
37  
38  import static org.nuiton.i18n.I18n.t;
39  
40  /**
41   * Created on 4/24/14.
42   *
43   * @author Tony Chemit - chemit@codelutin.com
44   * @since 3.5
45   */
46  public class SaveCruiseToReadyToSynchAction extends LongActionSupport<ValidateCruiseUIModel, ValidateCruiseUI, ValidateCruiseUIHandler> {
47  
48      /** Logger. */
49      private static final Log log = LogFactory.getLog(SaveCruiseToReadyToSynchAction.class);
50  
51      public SaveCruiseToReadyToSynchAction(ValidateCruiseUIHandler handler) {
52          super(handler, false);
53          setActionDescription(t("tutti.validationCruise.action.saveCruiseToReadyToSynch.tip"));
54      }
55  
56      @Override
57      public boolean prepareAction() throws Exception {
58          boolean doAction = super.prepareAction();
59          if (doAction) {
60              int answer = JOptionPane.showConfirmDialog(getContext().getActionUI(),
61                                                         t("tutti.validationCruise.action.saveCruiseToReadyToSynch.message"),
62                                                         t("tutti.validationCruise.action.saveCruiseToReadyToSynch.title"),
63                                                         JOptionPane.YES_NO_OPTION);
64              doAction = answer == JOptionPane.YES_OPTION;
65          }
66  
67          PersistenceService persistenceService = getContext().getPersistenceService();
68          int fishingOperationCount = persistenceService.getFishingOperationCount(getModel().getCruise().getIdAsInt());
69          createProgressionModelIfRequired(fishingOperationCount + 1);
70  
71          return doAction;
72      }
73  
74      @Override
75      public void doAction() throws Exception {
76  
77          Preconditions.checkState(getContext().isProgramFilled());
78          Preconditions.checkState(getContext().isCruiseFilled());
79  
80          PersistenceService persistenceService = getContext().getPersistenceService();
81  
82          ProgressionModel progressionModel = getProgressionModel();
83  
84          List<Integer> allFishingOperation = persistenceService.getAllFishingOperationIds(getModel().getCruise().getIdAsInt());
85  
86          for (Integer fishingOperationId : allFishingOperation) {
87  
88              if (log.isInfoEnabled()) {
89                  log.info("Recompute sample ratios for operation " + fishingOperationId);
90              }
91              persistenceService.recomputeCatchBatchSampleRatios(fishingOperationId);
92  
93              progressionModel.increments("Recalcul des poids référents pour l'opération " + fishingOperationId);
94          }
95  
96          Integer cruiseId = getModel().getCruise().getIdAsInt();
97  
98          if (log.isInfoEnabled()) {
99              log.info("Pass synchronizationStatus to *ready to synch* for cruise: " + cruiseId);
100         }
101 
102         progressionModel.increments("Mise en place des données synchronisables");
103 
104         persistenceService.setCruiseReadyToSynch(cruiseId);
105 
106     }
107 
108     @Override
109     public void postSuccessAction() {
110         super.postSuccessAction();
111 
112         // reload cruise in data context
113         getDataContext().reloadCruise();
114 
115         // reload it in our model
116         getModel().setCruise(getDataContext().getCruise());
117     }
118 }