1 package fr.ifremer.tutti.ui.swing.content.validation.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.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
42
43
44
45
46 public class SaveCruiseToReadyToSynchAction extends LongActionSupport<ValidateCruiseUIModel, ValidateCruiseUI, ValidateCruiseUIHandler> {
47
48
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
113 getDataContext().reloadCruise();
114
115
116 getModel().setCruise(getDataContext().getCruise());
117 }
118 }