View Javadoc
1   package fr.ifremer.tutti.ui.swing.content.protocol.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 com.google.common.collect.Lists;
27  import com.google.common.collect.Sets;
28  import fr.ifremer.tutti.persistence.entities.referential.Species;
29  import fr.ifremer.tutti.persistence.entities.referential.Speciess;
30  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesRowModel;
31  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolSpeciesTableModel;
32  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUI;
33  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIHandler;
34  import fr.ifremer.tutti.ui.swing.content.protocol.EditProtocolUIModel;
35  import fr.ifremer.tutti.ui.swing.content.protocol.calcifiedpiecessampling.CalcifiedPiecesSamplingEditorRowModel;
36  import fr.ifremer.tutti.ui.swing.util.actions.LongActionSupport;
37  import jaxx.runtime.SwingUtil;
38  
39  import javax.swing.JTable;
40  import java.util.Collection;
41  import java.util.Collections;
42  import java.util.List;
43  import java.util.Objects;
44  import java.util.Set;
45  import java.util.stream.Collectors;
46  
47  import static org.nuiton.i18n.I18n.t;
48  
49  /**
50   * To remove all the selected benthos rows from protocol.
51   *
52   * @author Tony Chemit - chemit@codelutin.com
53   * @since 1.0
54   */
55  public class RemoveBenthosProtocolAction extends LongActionSupport<EditProtocolUIModel, EditProtocolUI, EditProtocolUIHandler> {
56  
57  
58      /**
59       * Set of removed species.
60       *
61       * @since 2.8
62       */
63      protected Set<Species> removedSpecies;
64  
65      /**
66       * Set of removed rows.
67       *
68       * @since 2.8
69       */
70      protected Set<EditProtocolSpeciesRowModel> removedRows;
71  
72      /**
73       * calcified pieces sampling rows to delete
74       *
75       * @since 4.5
76       */
77      protected Collection<CalcifiedPiecesSamplingEditorRowModel> cpsRowsToDelete;
78  
79      public RemoveBenthosProtocolAction(EditProtocolUIHandler handler) {
80          super(handler, false);
81      }
82  
83      @Override
84      public boolean prepareAction() throws Exception {
85          boolean result = super.prepareAction();
86  
87          if (result) {
88  
89              JTable table = handler.getBenthosTable();
90  
91              // need to have a selection
92              Preconditions.checkState(!table.getSelectionModel().isSelectionEmpty());
93  
94              EditProtocolSpeciesTableModel tableModel =
95                      (EditProtocolSpeciesTableModel) table.getModel();
96  
97              removedSpecies = Sets.newHashSet();
98              removedRows = Sets.newHashSet();
99  
100             EditProtocolUIModel model = getModel();
101 
102             for (Integer rowIndex : SwingUtil.getSelectedModelRows(table)) {
103 
104                 // get row to remove
105                 EditProtocolSpeciesRowModel selectedRow =
106                         tableModel.getEntry(rowIndex);
107 
108                 // re-add all synonym of this taxon to the species / benthos combobox
109                 Species species = selectedRow.getSpecies();
110                 removedSpecies.add(species);
111 
112                 Integer taxonId = species.getReferenceTaxonId();
113                 List<Species> allSynonyms = Lists.newArrayList(
114                         model.getAllSynonyms(String.valueOf(taxonId)));
115                 allSynonyms.remove(species);
116                 model.getAllSynonyms().addAll(allSynonyms);
117 
118                 // mark row to be removed at the very last moment
119                 removedRows.add(selectedRow);
120             }
121 
122             List<CalcifiedPiecesSamplingEditorRowModel> cpsRows = getModel().getCpsRows();
123             cpsRowsToDelete = cpsRows.stream()
124                                      .filter(r -> removedSpecies.contains(r.getProtocolSpecies().getSpecies()))
125                                      .collect(Collectors.toList());
126 
127             if (!cpsRowsToDelete.isEmpty()) {
128 
129                 result = askBeforeDelete(t("tutti.editProtocol.action.removeBenthosProtocol.removeCpsRows.title"),
130                                          t("tutti.editProtocol.action.removeBenthosProtocol.removeCpsRows.message"));
131 
132             }
133         }
134 
135         return result;
136     }
137 
138     @Override
139     public void doAction() throws Exception {
140 
141         // reorder the list by name, otherwise,
142         // all the species without a reftax code will be at the end
143         Collections.sort(getModel().getAllSynonyms(),
144                          Speciess.SPECIES_BY_NAME_COMPARATOR);
145     }
146 
147     @Override
148     public void postSuccessAction() {
149         super.postSuccessAction();
150 
151         // update comboboxes
152         getUI().getBenthosComboBox().addItems(removedSpecies);
153         getUI().getSpeciesComboBox().addItems(removedSpecies);
154 
155         // remove all rows from model
156         getModel().getBenthosRow().removeAll(removedRows);
157 
158         // remove the lengthstep pmfm of the rows from the used lengthstep pmfm
159         getModel().getLengthStepPmfmUsed().removeCaracteristics(removedRows.stream()
160                                                                            .map(EditProtocolSpeciesRowModel::getLengthStepPmfm)
161                                                                            .filter(Objects::nonNull)
162                                                                            .collect(Collectors.toList()));
163 
164         // remove the maturities of the rows from the used maturities
165         getModel().getMaturityPmfmUsed().removeCaracteristics(removedRows.stream()
166                                                                          .map(EditProtocolSpeciesRowModel::getMaturityPmfm)
167                                                                          .filter(Objects::nonNull)
168                                                                          .collect(Collectors.toList()));
169 
170         // remove the protocolSpecies from the cps table or combobox
171         if (!cpsRowsToDelete.isEmpty()) {
172             handler.deleteCpsRows(cpsRowsToDelete);
173         }
174 
175         // fire table data changed
176         handler.getBenthosTableModel().fireTableDataChanged();
177 
178         // clear table selection
179         handler.getBenthosTable().clearSelection();
180 
181         // notify user
182         sendMessage(t("tutti.flash.info.benthos.remove.from.protocol"));
183     }
184 
185 }