View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.synchro.changes.duplicate;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import com.google.common.base.Function;
27  import com.google.common.base.Predicate;
28  import com.google.common.collect.Lists;
29  import com.google.common.collect.Sets;
30  import fr.ifremer.common.synchro.service.RejectedRow;
31  import fr.ifremer.quadrige3.core.dao.technical.Assert;
32  import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroOperationType;
33  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
34  import fr.ifremer.reefdb.dto.ReefDbBeanFactory;
35  import fr.ifremer.reefdb.dto.ReefDbBeans;
36  import fr.ifremer.reefdb.dto.system.synchronization.SynchroRowDTO;
37  import fr.ifremer.reefdb.dto.system.synchronization.SynchroTableDTO;
38  import fr.ifremer.reefdb.ui.swing.content.synchro.changes.SynchroChangesRowModel;
39  import fr.ifremer.reefdb.ui.swing.content.synchro.changes.SynchroChangesTableModel;
40  import fr.ifremer.reefdb.ui.swing.util.ReefDbUIs;
41  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableModel;
42  import fr.ifremer.reefdb.ui.swing.util.table.AbstractReefDbTableUIHandler;
43  import org.apache.commons.collections4.CollectionUtils;
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.jdesktop.swingx.table.TableColumnExt;
47  import org.nuiton.jaxx.application.swing.util.Cancelable;
48  
49  import javax.annotation.Nullable;
50  import javax.swing.SortOrder;
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  import static org.nuiton.i18n.I18n.t;
55  
56  /**
57   * Controleur pour la zone des programmes.
58   */
59  public class SynchroDuplicatesUIHandler extends AbstractReefDbTableUIHandler<SynchroChangesRowModel, SynchroDuplicatesUIModel, SynchroDuplicatesUI> implements Cancelable {
60  
61      /**
62       * Logger.
63       */
64      private static final Log LOG = LogFactory.getLog(SynchroDuplicatesUIHandler.class);
65  
66      /** {@inheritDoc} */
67      @Override
68      public void beforeInit(final SynchroDuplicatesUI ui) {
69          super.beforeInit(ui);
70  
71          // create model and register to the JAXX context
72          final SynchroDuplicatesUIModel model = new SynchroDuplicatesUIModel();
73          ui.setContextValue(model);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public void afterInit(SynchroDuplicatesUI ui) {
79  
80          // Init UI
81          initUI(ui);
82  
83          // Init table
84          initTable();
85          ui.getTablePanel().setVisible(false);
86          ui.pack();
87  
88          getModel().addPropertyChangeListener(SynchroDuplicatesUIModel.PROPERTY_CHANGES, evt -> populateUI());
89  
90      }
91  
92      /**
93       * Initialisation de le tableau.
94       */
95      private void initTable() {
96  
97          // Column name
98          final TableColumnExt columnName = addColumn(
99                  SynchroChangesTableModel.NAME);
100         columnName.setSortable(true);
101         columnName.setEditable(false);
102         columnName.setPreferredWidth(500);
103         columnName.setWidth(500);
104 
105         // Modele de la table
106         final SynchroChangesTableModel tableModel = new SynchroChangesTableModel(getTable().getColumnModel());
107         getTable().setModel(tableModel);
108 
109         // Les columns obligatoire sont toujours presentes
110         columnName.setHideable(false);
111 
112         getTable().setEditable(true);
113 
114         // Initialisation de la table
115         initTable(getTable(), false, true);
116 
117         // Tri par defaut
118         getTable().setSortOrder(SynchroChangesTableModel.NAME, SortOrder.ASCENDING);
119 
120         getTable().setVisibleRowCount(5);
121     }
122 
123     private void populateUI() {
124 
125         Assert.notNull(getModel().getChanges());
126 
127         // This filter is mandatory because this UI handles only one table type
128         Assert.notNull(getModel().getTableNameFilter());
129 
130         // get the change
131         SynchroTableDTO synchroTable = ReefDbBeans.findByProperty(getModel().getChanges().getTables(), SynchroTableDTO.PROPERTY_NAME, getModel().getTableNameFilter());
132         Assert.notNull(synchroTable);
133         getModel().setTableChange(synchroTable);
134 
135         int nbInsert = getInsertRows(synchroTable).size();
136         int nbDuplicate = getDuplicateRows(synchroTable).size();
137         boolean hasChanges = (nbInsert > 0 || nbDuplicate > 0);
138 
139         List<String> strings = new ArrayList<>();
140 
141         if (nbInsert > 0) {
142             strings.add(t("reefdb.synchro.duplicates.nbInsert", nbInsert, decorate(synchroTable)));
143         }
144         if (nbDuplicate > 0) {
145             strings.add(t("reefdb.synchro.duplicates.nbDuplicate", nbDuplicate, decorate(synchroTable)));
146             getUI().getShowDuplicatesButton().setVisible(true);
147             // populate table
148             populateTable(synchroTable);
149         } else {
150             getUI().getShowDuplicatesButton().setVisible(false);
151         }
152         getUI().getChangesLabel().setText(ReefDbUIs.getHtmlString(strings));
153 
154 
155         // Display a message on survey ignored because of missing programs (mantis #37518)
156         List<SynchroRowDTO> ignoredRows = getRowsByType(synchroTable, SynchroOperationType.IGNORE);
157         if (ignoredRows.size() > 0) {
158             List<String> progCds = ReefDbBeans.collectProperties(ignoredRows, SynchroRowDTO.PROPERTY_NAME);
159             String message = t("reefdb.synchro.duplicates.nbIgnore",
160                     ignoredRows.size(),
161                     decorate(synchroTable),
162                     ReefDbUIs.formatHtmlList(Sets.newHashSet(progCds))); // Use a Set, to remove duplicated prog_cd
163             getUI().getIgnoreLabel().setText(ReefDbUIs.getHtmlString(Lists.newArrayList(message)));
164 
165             // No other changes: user can only cancel
166             if (!hasChanges) {
167                 getUI().getIgnoreHelpLabel().setText(ReefDbUIs.getHtmlString(t("reefdb.synchro.duplicates.nbIgnore.help.cancelOnly")));
168                 getUI().getConfirmBouton().setEnabled(false);
169                 getUI().getChangesLabel().setVisible(false);
170             }
171             else {
172                 getUI().getIgnoreHelpLabel().setText(ReefDbUIs.getHtmlString(t("reefdb.synchro.duplicates.nbIgnore.help")));
173             }
174         }
175         else {
176             getUI().getIgnorePanel().setVisible(false);
177         }
178 
179 
180     }
181 
182     /**
183      * <p>populateTable.</p>
184      *
185      * @param synchroTable a {@link fr.ifremer.reefdb.dto.system.synchronization.SynchroTableDTO} object.
186      */
187     protected void populateTable(SynchroTableDTO synchroTable) {
188 
189         TableColumnExt nameColumn = getTable().getColumnExt(SynchroChangesTableModel.NAME);
190 
191         if (synchroTable == null || CollectionUtils.isEmpty(synchroTable.getRows())) {
192 
193             nameColumn.setTitle(t("reefdb.synchro.duplicates.name.short.default"));
194 
195         } else {
196 
197             nameColumn.setTitle(t("reefdb.synchro.duplicates.name.short", decorate(synchroTable)));
198             getModel().setBeans(getDuplicateRows(synchroTable));
199         }
200 
201     }
202 
203     /**
204      * <p>showDuplicates.</p>
205      */
206     public void showDuplicates() {
207 
208         getUI().getTablePanel().setVisible(true);
209         getUI().getShowDuplicatesButton().setVisible(false);
210         getUI().pack();
211     }
212 
213     /** {@inheritDoc} */
214     @Override
215     public AbstractReefDbTableModel<SynchroChangesRowModel> getTableModel() {
216         return (SynchroChangesTableModel) getTable().getModel();
217     }
218 
219     /** {@inheritDoc} */
220     @Override
221     public SwingTable getTable() {
222         return getUI().getTable();
223     }
224 
225     /** {@inheritDoc} */
226     @Override
227     public void cancel() {
228         getModel().setChangesValidated(false);
229         closeDialog();
230     }
231 
232     /**
233      * <p>confirm.</p>
234      */
235     public void confirm() {
236 
237         // remove old table from changes
238         getModel().getChanges().removeTables(getModel().getTableChange());
239 
240         // build another SynchroTableDTO
241         SynchroTableDTO synchroTable = ReefDbBeanFactory.newSynchroTableDTO();
242         synchroTable.setName(getModel().getTableChange().getName());
243         synchroTable.setRows(new ArrayList<>());
244 
245         // add insertRows by adding strategy
246         synchroTable.addAllRows(ReefDbBeans.transformCollection(getInsertRows(getModel().getTableChange()), new Function<SynchroRowDTO, SynchroRowDTO>() {
247             @Nullable
248             @Override
249             public SynchroRowDTO apply(@Nullable SynchroRowDTO input) {
250                 if (input != null) {
251                     input.setStrategy(RejectedRow.ResolveStrategy.UPDATE.toString());
252                 }
253                 return input;
254             }
255         }));
256 
257         // add duplicates rows
258         synchroTable.addAllRows(ReefDbBeans.transformCollection(getModel().getSelectedBeans(), new Function<SynchroRowDTO, SynchroRowDTO>() {
259             @Nullable
260             @Override
261             public SynchroRowDTO apply(@Nullable SynchroRowDTO input) {
262                 if (input != null) {
263                     input.setStrategy(RejectedRow.ResolveStrategy.DUPLICATE.toString());
264                 }
265                 return input;
266             }
267         }));
268 
269         getModel().getChanges().addTables(synchroTable);
270 
271         getModel().setChangesValidated(true);
272         closeDialog();
273     }
274 
275     /* -- Internal methods -- */
276 
277     private List<SynchroRowDTO> getDuplicateRows(SynchroTableDTO synchroTable) {
278         return getRowsByType(synchroTable, SynchroOperationType.DUPLICATE);
279     }
280 
281     private List<SynchroRowDTO> getInsertRows(SynchroTableDTO synchroTable) {
282         return getRowsByType(synchroTable, SynchroOperationType.INSERT);
283     }
284 
285     private List<SynchroRowDTO> getRowsByType(final SynchroTableDTO synchroTable, final SynchroOperationType operationType) {
286         final String typeStr = operationType.name();
287 
288         return ReefDbBeans.filterCollection(synchroTable.getRows(), (Predicate<SynchroRowDTO>) input -> input != null && typeStr.equalsIgnoreCase(input.getOperationType()));
289     }
290 }