1 package fr.ifremer.tutti.ui.swing.content.operation.catches.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.collect.Multimap;
26 import fr.ifremer.tutti.persistence.InvalidBatchModelException;
27 import fr.ifremer.tutti.persistence.entities.data.FishingOperation;
28 import fr.ifremer.tutti.service.catches.WeightCleaningService;
29 import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUI;
30 import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIHandler;
31 import fr.ifremer.tutti.ui.swing.content.operation.catches.EditCatchesUIModel;
32 import fr.ifremer.tutti.ui.swing.content.operation.catches.species.edit.SpeciesBatchUI;
33 import fr.ifremer.tutti.ui.swing.util.AbstractTuttiUIHandler;
34 import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 import javax.swing.JOptionPane;
39
40 import static org.nuiton.i18n.I18n.t;
41
42
43
44
45
46
47
48
49
50 public class CleanBatchWeightsAction extends LongActionSupport<EditCatchesUIModel, EditCatchesUI, EditCatchesUIHandler> {
51
52
53 private static final Log log =
54 LogFactory.getLog(CleanBatchWeightsAction.class);
55
56 protected final WeightCleaningService cleaningService;
57
58 protected int nbErrors;
59
60 public CleanBatchWeightsAction(EditCatchesUIHandler handler) {
61 super(handler, false);
62 cleaningService = getContext().getWeightCleaningService();
63 }
64
65 @Override
66 public boolean prepareAction() throws Exception {
67 boolean doAction = super.prepareAction();
68 if (doAction) {
69
70
71
72 FishingOperation fishingOperation = getModel().getFishingOperation();
73 Multimap<String, String> errors = cleaningService.checkFishingOperation(fishingOperation.getIdAsInt());
74
75 nbErrors = errors.size();
76
77 if (nbErrors == 0) {
78
79
80
81 sendMessage(t("tutti.editCatchBatch.action.cleanWeights.no.double.weight.detected"));
82 doAction = false;
83 } else {
84
85
86
87 String errorsStr = cleaningService.errorsToString(errors);
88 String htmlMessage = String.format(
89 AbstractTuttiUIHandler.CONFIRMATION_FORMAT,
90 t("tutti.editCatchBatch.action.cleanWeights.double.weight.detected", errorsStr),
91 t("tutti.editCatchBatch.action.cleanWeights.help"));
92
93 int answer = JOptionPane.showConfirmDialog(
94 getContext().getActionUI(),
95 htmlMessage,
96 t("tutti.editCatchBatch.action.cleanWieghts.resume.title"),
97 JOptionPane.OK_CANCEL_OPTION,
98 JOptionPane.QUESTION_MESSAGE);
99
100 doAction = (answer == JOptionPane.OK_OPTION);
101
102 }
103 }
104 return doAction;
105 }
106
107 @Override
108 public void doAction() throws Exception {
109
110 EditCatchesUIModel model = getModel();
111 FishingOperation fishingOperation = getModel().getFishingOperation();
112 model.setLoadingData(true);
113
114 cleaningService.cleanFishingOperation(fishingOperation.getIdAsInt());
115
116 try {
117
118 getSpeciesBatchUI().getHandler().selectFishingOperation(fishingOperation);
119 } catch (InvalidBatchModelException e) {
120
121
122 if (log.isDebugEnabled()) {
123 log.debug("Invalid sample category model for species batches", e);
124 }
125 }
126
127 try {
128 getBenthosBatchUI().getHandler().selectFishingOperation(fishingOperation);
129 } catch (InvalidBatchModelException e) {
130
131
132 if (log.isDebugEnabled()) {
133 log.debug("Invalid sample category model for benthos batches", e);
134 }
135 }
136 }
137
138 @Override
139 public void postSuccessAction() {
140 super.postSuccessAction();
141
142 sendMessage(t("tutti.editCatchBatch.action.cleanWeights.done", nbErrors));
143 }
144
145 private SpeciesBatchUI getBenthosBatchUI() {
146 return getUI().getBenthosTabPanel().getEditBatchesUI();
147 }
148
149 private SpeciesBatchUI getSpeciesBatchUI() {
150 return getUI().getSpeciesTabPanel().getEditBatchesUI();
151 }
152
153 }