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