View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.operation.catches.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.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   * To remove weight in double for species and benthos rows.
44   *
45   * Created on 9/27/13.
46   *
47   * @author Tony Chemit - chemit@codelutin.com
48   * @since 2.6
49   */
50  public class CleanBatchWeightsAction extends LongActionSupport<EditCatchesUIModel, EditCatchesUI, EditCatchesUIHandler> {
51  
52      /** Logger. */
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              // do a check
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                  // no errors
80  
81                  sendMessage(t("tutti.editCatchBatch.action.cleanWeights.no.double.weight.detected"));
82                  doAction = false;
83              } else {
84  
85                  // show errors to user and ask him to apply change
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             // invalid sample category model for species batches
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             // invalid sample category model for benthos batches
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 }