1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.console.panels;
20
21 import de.agilecoders.wicket.core.markup.html.bootstrap.dialog.Modal;
22 import java.io.Serializable;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Optional;
27 import java.util.function.BiConsumer;
28 import org.apache.syncope.client.console.batch.BatchContent;
29 import org.apache.syncope.client.console.batch.BatchModal;
30 import org.apache.syncope.client.console.pages.BasePage;
31 import org.apache.syncope.client.console.panels.DirectoryPanel.EventDataWrapper;
32 import org.apache.syncope.client.console.rest.BaseRestClient;
33 import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.AjaxFallbackDataTable;
34 import org.apache.syncope.client.console.wicket.extensions.markup.html.repeater.data.table.CheckGroupColumn;
35 import org.apache.syncope.client.console.wicket.markup.html.bootstrap.dialog.BaseModal;
36 import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
37 import org.apache.syncope.client.console.wicket.markup.html.form.ActionLinksTogglePanel;
38 import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
39 import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormChoiceComponentUpdatingBehavior;
40 import org.apache.syncope.client.ui.commons.rest.RestClient;
41 import org.apache.wicket.AttributeModifier;
42 import org.apache.wicket.PageReference;
43 import org.apache.wicket.ajax.AjaxRequestTarget;
44 import org.apache.wicket.event.Broadcast;
45 import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
46 import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
47 import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
48 import org.apache.wicket.markup.html.WebMarkupContainer;
49 import org.apache.wicket.markup.html.form.CheckGroup;
50 import org.apache.wicket.markup.html.form.CheckGroupSelector;
51 import org.apache.wicket.markup.html.form.Form;
52 import org.apache.wicket.markup.html.panel.Fragment;
53 import org.apache.wicket.model.IModel;
54 import org.apache.wicket.model.ResourceModel;
55
56 public final class AjaxDataTablePanel<T extends Serializable, S> extends DataTablePanel<T, S> {
57
58 private static final long serialVersionUID = -7264400471578272966L;
59
60 public static class Builder<T extends Serializable, S> implements Serializable {
61
62 private static final long serialVersionUID = 8876232177473972722L;
63
64 private boolean checkBoxEnabled = true;
65
66 private final List<IColumn<T, S>> columns = new ArrayList<>();
67
68 private final ISortableDataProvider<T, S> dataProvider;
69
70 private int rowsPerPage = 10;
71
72 private final Collection<ActionLink.ActionType> batches = new ArrayList<>();
73
74 private RestClient batchExecutor;
75
76 private String itemKeyField;
77
78 private final PageReference pageRef;
79
80 private WebMarkupContainer container;
81
82 private MultilevelPanel multiLevelPanel;
83
84 public Builder(final ISortableDataProvider<T, S> provider, final PageReference pageRef) {
85 this.dataProvider = provider;
86 this.pageRef = pageRef;
87 }
88
89 public AjaxDataTablePanel<T, S> build(final String id) {
90 return new AjaxDataTablePanel<>(id, this);
91 }
92
93 public Builder<T, S> setContainer(final WebMarkupContainer container) {
94 this.container = container;
95 return this;
96 }
97
98 public Builder<T, S> addBatch(final ActionLink.ActionType actionType) {
99 batches.add(actionType);
100 return this;
101 }
102
103 public Builder<T, S> setBatchExecutor(final BaseRestClient batchExecutor) {
104 this.batchExecutor = batchExecutor;
105 return this;
106 }
107
108 public Builder<T, S> setItemKeyField(final String itemKeyField) {
109 this.itemKeyField = itemKeyField;
110 return this;
111 }
112
113 public Builder<T, S> setBatches(
114 final Collection<ActionLink.ActionType> batches,
115 final RestClient batchExecutor,
116 final String itemKeyField) {
117
118 this.batches.clear();
119 if (batches != null) {
120 this.batches.addAll(batches);
121 }
122 this.batchExecutor = batchExecutor;
123 this.itemKeyField = itemKeyField;
124 return this;
125 }
126
127 public Builder<T, S> addColumn(final IColumn<T, S> column) {
128 columns.add(column);
129 return this;
130 }
131
132 public Builder<T, S> setColumns(final List<IColumn<T, S>> columns) {
133 this.columns.clear();
134 if (columns != null) {
135 this.columns.addAll(columns);
136 }
137 return this;
138 }
139
140 public Builder<T, S> setRowsPerPage(final int rowsPerPage) {
141 this.rowsPerPage = rowsPerPage;
142 return this;
143 }
144
145 public Builder<T, S> disableCheckBoxes() {
146 this.checkBoxEnabled = false;
147 return this;
148 }
149
150 private boolean isBatchEnabled() {
151 return checkBoxEnabled && batchExecutor != null && !batches.isEmpty();
152 }
153
154 public void setMultiLevelPanel(final MultilevelPanel multiLevelPanel) {
155 this.multiLevelPanel = multiLevelPanel;
156 }
157
158 protected ActionsPanel<T> getActions(final IModel<T> model) {
159 return null;
160 }
161
162 protected ActionLinksTogglePanel<T> getTogglePanel() {
163 return null;
164 }
165
166 protected BiConsumer<AjaxRequestTarget, IModel<T>> onDoubleClick() {
167 return null;
168 }
169 }
170
171 protected final BaseModal<T> batchModal;
172
173 private AjaxDataTablePanel(final String id, final Builder<T, S> builder) {
174 super(id);
175
176 batchModal = new BaseModal<>("batchModal");
177 batchModal.size(Modal.Size.Extra_large);
178 add(batchModal);
179
180 batchModal.setWindowClosedCallback(target -> {
181 batchModal.show(false);
182
183 EventDataWrapper data = new EventDataWrapper();
184 data.setTarget(target);
185 data.setRows(builder.rowsPerPage);
186
187 send(builder.pageRef.getPage(), Broadcast.BREADTH, data);
188 Optional.ofNullable((BasePage) findPage()).
189 ifPresent(page -> page.getNotificationPanel().refresh(target));
190 });
191
192 Fragment fragment = new Fragment("tablePanel", "batchAvailable", this);
193 add(fragment);
194
195 Form<T> groupForm = new Form<>("groupForm");
196 fragment.add(groupForm);
197
198 group = new CheckGroup<>("checkgroup", model);
199 group.add(new IndicatorAjaxFormChoiceComponentUpdatingBehavior() {
200
201 private static final long serialVersionUID = -151291731388673682L;
202
203 @Override
204 protected void onUpdate(final AjaxRequestTarget target) {
205 group.visitChildren(CheckGroupSelector.class, (selector, ivisit) -> {
206 target.focusComponent(selector);
207 ivisit.stop();
208 });
209 }
210 });
211 groupForm.add(group);
212
213 if (builder.checkBoxEnabled) {
214 builder.columns.addFirst(new CheckGroupColumn<>(group));
215 }
216
217 dataTable = new AjaxFallbackDataTable<>(
218 "dataTable", builder.columns, builder.dataProvider, builder.rowsPerPage, builder.container) {
219
220 private static final long serialVersionUID = -7370603907251344224L;
221
222 @Override
223 protected ActionsPanel<T> getActions(final IModel<T> model) {
224 return builder.getActions(model);
225 }
226
227 @Override
228 protected ActionLinksTogglePanel<T> getTogglePanel() {
229 return builder.getTogglePanel();
230 }
231
232 @Override
233 protected void onDoubleClick(final AjaxRequestTarget target, final IModel<T> model) {
234 Optional.ofNullable(builder.onDoubleClick()).ifPresentOrElse(
235 odc -> odc.accept(target, model),
236 () -> super.onDoubleClick(target, model));
237 }
238 };
239
240 dataTable.add(new AttributeModifier("class", "table table-bordered table-hover dataTable"));
241
242 group.add(dataTable);
243
244 fragment.add(new IndicatingAjaxButton("batchLink", groupForm) {
245
246 private static final long serialVersionUID = 382302811235019988L;
247
248 @Override
249 protected void onSubmit(final AjaxRequestTarget target) {
250
251 Optional.ofNullable(builder.getTogglePanel()).ifPresent(p -> p.close(target));
252
253 if (builder.multiLevelPanel == null) {
254 batchModal.header(new ResourceModel("batch"));
255 batchModal.changeCloseButtonLabel(getString("cancel", null, "Cancel"), target);
256
257 target.add(batchModal.setContent(new BatchModal<>(
258 batchModal,
259 builder.pageRef,
260 new ArrayList<>(group.getModelObject()),
261 builder.columns.size() == 1
262 ? builder.columns
263
264 : new ArrayList<>(builder.columns.subList(1, builder.columns.size())),
265 builder.batches,
266 builder.batchExecutor,
267 builder.itemKeyField)));
268
269 batchModal.show(true);
270 } else {
271 builder.multiLevelPanel.next(getString("batch"),
272 new BatchContent<>(
273 new ArrayList<>(group.getModelObject()),
274 builder.columns.size() == 1
275 ? builder.columns
276
277 : new ArrayList<>(builder.columns.subList(1, builder.columns.size())),
278 builder.batches,
279 builder.batchExecutor,
280 builder.itemKeyField),
281 target);
282 }
283 group.setModelObject(List.of());
284 target.add(group);
285 }
286 }.setEnabled(builder.isBatchEnabled()).setVisible(builder.isBatchEnabled()));
287 }
288 }