1 package fr.ifremer.reefdb.ui.swing.content.synchro.changes.duplicate;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
58
59 public class SynchroDuplicatesUIHandler extends AbstractReefDbTableUIHandler<SynchroChangesRowModel, SynchroDuplicatesUIModel, SynchroDuplicatesUI> implements Cancelable {
60
61
62
63
64 private static final Log LOG = LogFactory.getLog(SynchroDuplicatesUIHandler.class);
65
66
67 @Override
68 public void beforeInit(final SynchroDuplicatesUI ui) {
69 super.beforeInit(ui);
70
71
72 final SynchroDuplicatesUIModel model = new SynchroDuplicatesUIModel();
73 ui.setContextValue(model);
74 }
75
76
77 @Override
78 public void afterInit(SynchroDuplicatesUI ui) {
79
80
81 initUI(ui);
82
83
84 initTable();
85 ui.getTablePanel().setVisible(false);
86 ui.pack();
87
88 getModel().addPropertyChangeListener(SynchroDuplicatesUIModel.PROPERTY_CHANGES, evt -> populateUI());
89
90 }
91
92
93
94
95 private void initTable() {
96
97
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
106 final SynchroChangesTableModel tableModel = new SynchroChangesTableModel(getTable().getColumnModel());
107 getTable().setModel(tableModel);
108
109
110 columnName.setHideable(false);
111
112 getTable().setEditable(true);
113
114
115 initTable(getTable(), false, true);
116
117
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
128 Assert.notNull(getModel().getTableNameFilter());
129
130
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
148 populateTable(synchroTable);
149 } else {
150 getUI().getShowDuplicatesButton().setVisible(false);
151 }
152 getUI().getChangesLabel().setText(ReefDbUIs.getHtmlString(strings));
153
154
155
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)));
163 getUI().getIgnoreLabel().setText(ReefDbUIs.getHtmlString(Lists.newArrayList(message)));
164
165
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
184
185
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
205
206 public void showDuplicates() {
207
208 getUI().getTablePanel().setVisible(true);
209 getUI().getShowDuplicatesButton().setVisible(false);
210 getUI().pack();
211 }
212
213
214 @Override
215 public AbstractReefDbTableModel<SynchroChangesRowModel> getTableModel() {
216 return (SynchroChangesTableModel) getTable().getModel();
217 }
218
219
220 @Override
221 public SwingTable getTable() {
222 return getUI().getTable();
223 }
224
225
226 @Override
227 public void cancel() {
228 getModel().setChangesValidated(false);
229 closeDialog();
230 }
231
232
233
234
235 public void confirm() {
236
237
238 getModel().getChanges().removeTables(getModel().getTableChange());
239
240
241 SynchroTableDTO synchroTable = ReefDbBeanFactory.newSynchroTableDTO();
242 synchroTable.setName(getModel().getTableChange().getName());
243 synchroTable.setRows(new ArrayList<>());
244
245
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
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
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 }