1 package fr.ifremer.dali.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.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
54
55 public class SynchroDuplicatesUIHandler extends AbstractDaliTableUIHandler<SynchroChangesRowModel, SynchroDuplicatesUIModel, SynchroDuplicatesUI> implements Cancelable {
56
57
58 @Override
59 public void beforeInit(final SynchroDuplicatesUI ui) {
60 super.beforeInit(ui);
61
62
63 final SynchroDuplicatesUIModel model = new SynchroDuplicatesUIModel();
64 ui.setContextValue(model);
65 }
66
67
68 @Override
69 public void afterInit(SynchroDuplicatesUI ui) {
70
71
72 initUI(ui);
73
74
75 initTable();
76 ui.getTablePanel().setVisible(false);
77 ui.pack();
78
79 getModel().addPropertyChangeListener(SynchroDuplicatesUIModel.PROPERTY_CHANGES, evt -> populateUI());
80
81 }
82
83
84
85
86 private void initTable() {
87
88
89 final SwingTable table = getTable();
90
91
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
100 final SynchroChangesTableModel tableModel = new SynchroChangesTableModel(getTable().getColumnModel());
101 table.setModel(tableModel);
102
103
104 columnName.setHideable(false);
105
106 table.setEditable(true);
107
108
109 initTable(table, false, true);
110
111
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
122 Assert.notNull(getModel().getTableNameFilter());
123
124
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
142 populateTable(synchroTable);
143 } else {
144 getUI().getShowDuplicatesButton().setVisible(false);
145 }
146 getUI().getChangesLabel().setText(ApplicationUIUtil.getHtmlString(strings));
147
148
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)));
156 getUI().getIgnoreLabel().setText(ApplicationUIUtil.getHtmlString(Lists.newArrayList(message)));
157
158
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
175
176
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
196
197 public void showDuplicates() {
198
199 getUI().getTablePanel().setVisible(true);
200 getUI().getShowDuplicatesButton().setVisible(false);
201 getUI().pack();
202 }
203
204
205 @Override
206 public AbstractDaliTableModel<SynchroChangesRowModel> getTableModel() {
207 return (SynchroChangesTableModel) getTable().getModel();
208 }
209
210
211 @Override
212 public SwingTable getTable() {
213 return getUI().getTable();
214 }
215
216
217 @Override
218 public void cancel() {
219 getModel().setChangesValidated(false);
220 closeDialog();
221 }
222
223
224
225
226 public void confirm() {
227
228
229 getModel().getChanges().removeTables(getModel().getTableChange());
230
231
232 SynchroTableDTO synchroTable = DaliBeanFactory.newSynchroTableDTO();
233 synchroTable.setName(getModel().getTableChange().getName());
234 synchroTable.setRows(new ArrayList<>());
235
236
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
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
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 }