View Javadoc
1   package fr.ifremer.dali.ui.swing.content.synchro.changes.duplicate;
2   
3   /*
4    * #%L
5    * Dali :: 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.Predicate;
27  import com.google.common.collect.Lists;
28  import com.google.common.collect.Sets;
29  import fr.ifremer.common.synchro.service.RejectedRow;
30  import fr.ifremer.dali.dto.DaliBeanFactory;
31  import fr.ifremer.dali.dto.DaliBeans;
32  import fr.ifremer.dali.dto.system.synchronization.SynchroRowDTO;
33  import fr.ifremer.dali.dto.system.synchronization.SynchroTableDTO;
34  import fr.ifremer.dali.ui.swing.content.synchro.changes.SynchroChangesRowModel;
35  import fr.ifremer.dali.ui.swing.content.synchro.changes.SynchroChangesTableModel;
36  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableModel;
37  import fr.ifremer.dali.ui.swing.util.table.AbstractDaliTableUIHandler;
38  import fr.ifremer.quadrige3.core.dao.technical.Assert;
39  import fr.ifremer.quadrige3.synchro.service.client.vo.SynchroOperationType;
40  import fr.ifremer.quadrige3.ui.swing.ApplicationUIUtil;
41  import fr.ifremer.quadrige3.ui.swing.table.SwingTable;
42  import org.apache.commons.collections4.CollectionUtils;
43  import org.jdesktop.swingx.table.TableColumnExt;
44  import org.nuiton.jaxx.application.swing.util.Cancelable;
45  
46  import javax.swing.SortOrder;
47  import java.util.ArrayList;
48  import java.util.List;
49  
50  import static org.nuiton.i18n.I18n.t;
51  
52  /**
53   * Controleur pour la zone des programmes.
54   */
55  public class SynchroDuplicatesUIHandler extends AbstractDaliTableUIHandler<SynchroChangesRowModel, SynchroDuplicatesUIModel, SynchroDuplicatesUI> implements Cancelable {
56  
57      /** {@inheritDoc} */
58      @Override
59      public void beforeInit(final SynchroDuplicatesUI ui) {
60          super.beforeInit(ui);
61  
62          // create model and register to the JAXX context
63          final SynchroDuplicatesUIModel model = new SynchroDuplicatesUIModel();
64          ui.setContextValue(model);
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public void afterInit(SynchroDuplicatesUI ui) {
70  
71          // Init UI
72          initUI(ui);
73  
74          // Init table
75          initTable();
76          ui.getTablePanel().setVisible(false);
77          ui.pack();
78  
79          getModel().addPropertyChangeListener(SynchroDuplicatesUIModel.PROPERTY_CHANGES, evt -> populateUI());
80  
81      }
82  
83      /**
84       * Initialisation de le tableau.
85       */
86      private void initTable() {
87  
88          // Get the main table
89          final SwingTable table = getTable();
90  
91          // Column name
92          final TableColumnExt columnName = addColumn(
93                  SynchroChangesTableModel.NAME);
94          columnName.setSortable(true);
95          columnName.setEditable(false);
96          columnName.setPreferredWidth(500);
97          columnName.setWidth(500);
98  
99          // Modele de la table
100         final SynchroChangesTableModel tableModel = new SynchroChangesTableModel(getTable().getColumnModel());
101         table.setModel(tableModel);
102 
103         // Les columns obligatoire sont toujours presentes
104         columnName.setHideable(false);
105 
106         table.setEditable(true);
107 
108         // Initialisation de la table
109         initTable(table, false, true);
110 
111         // Tri par defaut
112         table.setSortOrder(SynchroChangesTableModel.NAME, SortOrder.ASCENDING);
113 
114         table.setVisibleRowCount(5);
115     }
116 
117     private void populateUI() {
118 
119         Assert.notNull(getModel().getChanges());
120 
121         // This filter is mandatory because this UI handles only one table type
122         Assert.notNull(getModel().getTableNameFilter());
123 
124         // get the change
125         SynchroTableDTO synchroTable = DaliBeans.findByProperty(getModel().getChanges().getTables(), SynchroTableDTO.PROPERTY_NAME, getModel().getTableNameFilter());
126         Assert.notNull(synchroTable);
127         getModel().setTableChange(synchroTable);
128 
129         int nbInsert = getInsertRows(synchroTable).size();
130         int nbDuplicate = getDuplicateRows(synchroTable).size();
131         boolean hasChanges = (nbInsert > 0 || nbDuplicate > 0);
132 
133         List<String> strings = new ArrayList<>();
134 
135         if (nbInsert > 0) {
136             strings.add(t("dali.synchro.duplicates.nbInsert", nbInsert, decorate(synchroTable)));
137         }
138         if (nbDuplicate > 0) {
139             strings.add(t("dali.synchro.duplicates.nbDuplicate", nbDuplicate, decorate(synchroTable)));
140             getUI().getShowDuplicatesButton().setVisible(true);
141             // populate table
142             populateTable(synchroTable);
143         } else {
144             getUI().getShowDuplicatesButton().setVisible(false);
145         }
146         getUI().getChangesLabel().setText(ApplicationUIUtil.getHtmlString(strings));
147 
148         // Display a message on survey ignored because of missing programs (mantis #37518)
149         List<SynchroRowDTO> ignoredRows = getRowsByType(synchroTable, SynchroOperationType.IGNORE);
150         if (ignoredRows.size() > 0) {
151             List<String> progCds = DaliBeans.collectProperties(ignoredRows, SynchroRowDTO.PROPERTY_NAME);
152             String message = t("dali.synchro.duplicates.nbIgnore",
153                     ignoredRows.size(),
154                     decorate(synchroTable),
155                     ApplicationUIUtil.formatHtmlList(Sets.newHashSet(progCds))); // Use a Set, to remove duplicated prog_cd
156             getUI().getIgnoreLabel().setText(ApplicationUIUtil.getHtmlString(Lists.newArrayList(message)));
157 
158             // No other changes: user can only cancel
159             if (!hasChanges) {
160                 getUI().getIgnoreHelpLabel().setText(ApplicationUIUtil.getHtmlString(t("dali.synchro.duplicates.nbIgnore.help.cancelOnly")));
161                 getUI().getConfirmBouton().setEnabled(false);
162                 getUI().getChangesLabel().setVisible(false);
163             }
164             else {
165                 getUI().getIgnoreHelpLabel().setText(ApplicationUIUtil.getHtmlString(t("dali.synchro.duplicates.nbIgnore.help")));
166             }
167         }
168         else {
169             getUI().getIgnorePanel().setVisible(false);
170         }
171     }
172 
173     /**
174      * <p>populateTable.</p>
175      *
176      * @param synchroTable a {@link SynchroTableDTO} object.
177      */
178     private void populateTable(SynchroTableDTO synchroTable) {
179 
180         TableColumnExt nameColumn = getTable().getColumnExt(SynchroChangesTableModel.NAME);
181 
182         if (synchroTable == null || CollectionUtils.isEmpty(synchroTable.getRows())) {
183 
184             nameColumn.setTitle(t("dali.synchro.duplicates.name.short.default"));
185 
186         } else {
187 
188             nameColumn.setTitle(t("dali.synchro.duplicates.name.short", decorate(synchroTable)));
189             getModel().setBeans(getDuplicateRows(synchroTable));
190         }
191 
192     }
193 
194     /**
195      * <p>showDuplicates.</p>
196      */
197     public void showDuplicates() {
198 
199         getUI().getTablePanel().setVisible(true);
200         getUI().getShowDuplicatesButton().setVisible(false);
201         getUI().pack();
202     }
203 
204     /** {@inheritDoc} */
205     @Override
206     public AbstractDaliTableModel<SynchroChangesRowModel> getTableModel() {
207         return (SynchroChangesTableModel) getTable().getModel();
208     }
209 
210     /** {@inheritDoc} */
211     @Override
212     public SwingTable getTable() {
213         return getUI().getTable();
214     }
215 
216     /** {@inheritDoc} */
217     @Override
218     public void cancel() {
219         getModel().setChangesValidated(false);
220         closeDialog();
221     }
222 
223     /**
224      * <p>confirm.</p>
225      */
226     public void confirm() {
227 
228         // remove old table from changes
229         getModel().getChanges().removeTables(getModel().getTableChange());
230 
231         // build another SynchroTableDTO
232         SynchroTableDTO synchroTable = DaliBeanFactory.newSynchroTableDTO();
233         synchroTable.setName(getModel().getTableChange().getName());
234         synchroTable.setRows(new ArrayList<>());
235 
236         // add insertRows by adding strategy
237         synchroTable.addAllRows(DaliBeans.transformCollection(getInsertRows(getModel().getTableChange()), synchroRow -> {
238                 if (synchroRow != null) {
239                     synchroRow.setStrategy(RejectedRow.ResolveStrategy.UPDATE.toString());
240                 }
241                 return synchroRow;
242         }));
243 
244         // add duplicates rows
245         synchroTable.addAllRows(DaliBeans.transformCollection(getModel().getSelectedBeans(), synchroRow -> {
246                 if (synchroRow != null) {
247                     synchroRow.setStrategy(RejectedRow.ResolveStrategy.DUPLICATE.toString());
248                 }
249                 return synchroRow;
250         }));
251 
252         getModel().getChanges().addTables(synchroTable);
253 
254         getModel().setChangesValidated(true);
255         closeDialog();
256     }
257 
258     /* -- Internal methods -- */
259 
260     private List<SynchroRowDTO> getDuplicateRows(SynchroTableDTO synchroTable) {
261         return getRowsByType(synchroTable, SynchroOperationType.DUPLICATE);
262     }
263 
264     private List<SynchroRowDTO> getInsertRows(SynchroTableDTO synchroTable) {
265         return getRowsByType(synchroTable, SynchroOperationType.INSERT);
266     }
267 
268     private List<SynchroRowDTO> getRowsByType(final SynchroTableDTO synchroTable, final SynchroOperationType operationType) {
269         final String typeStr = operationType.name();
270 
271         return DaliBeans.filterCollection(synchroTable.getRows(), (Predicate<SynchroRowDTO>) input -> input != null && typeStr.equalsIgnoreCase(input.getOperationType()));
272     }
273 
274 }