/
MainObjectListPanel.java
259 lines (216 loc) · 11.1 KB
/
MainObjectListPanel.java
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Copyright (c) 2016 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.evolveum.midpoint.gui.api.component;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.event.IEventSink;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.CSVDataExporter;
import org.apache.wicket.extensions.markup.html.repeater.data.table.export.ExportToolbar;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.Fragment;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.component.IRequestablePage;
import org.apache.wicket.util.resource.IResourceStream;
import com.evolveum.midpoint.gui.api.component.button.CsvDownloadButtonPanel;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.model.api.ModelAuthorizationAction;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.AbstractAjaxDownloadBehavior;
import com.evolveum.midpoint.web.component.AjaxIconButton;
import com.evolveum.midpoint.web.component.data.column.CheckBoxHeaderColumn;
import com.evolveum.midpoint.web.component.data.column.ObjectNameColumn;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import com.evolveum.midpoint.web.page.admin.configuration.PageImportObject;
import com.evolveum.midpoint.web.session.UserProfileStorage.TableId;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
/**
* @author katkav
*/
public abstract class MainObjectListPanel<O extends ObjectType> extends ObjectListPanel<O> {
private static final long serialVersionUID = 1L;
private static final String ID_REFRESH = "refresh";
private static final String ID_NEW_OBJECT = "newObject";
private static final String ID_IMPORT_OBJECT = "importObject";
private static final String ID_EXPORT_DATA = "exportData";
private static final String ID_BUTTON_BAR = "buttonBar";
private static final Trace LOGGER = TraceManager.getTrace(MainObjectListPanel.class);
public MainObjectListPanel(String id, Class<O> type, TableId tableId, Collection<SelectorOptions<GetOperationOptions>> options, PageBase parentPage) {
super(id, type, tableId, options, parentPage);
}
@Override
protected IColumn<SelectableBean<O>, String> createCheckboxColumn() {
return new CheckBoxHeaderColumn<>();
}
@Override
protected IColumn<SelectableBean<O>, String> createNameColumn(IModel<String> columnNameModel, String itemPath) {
if (StringUtils.isEmpty(itemPath)) {
return new ObjectNameColumn<O>(columnNameModel == null ? createStringResource("ObjectType.name") : columnNameModel) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target, IModel<SelectableBean<O>> rowModel) {
O object = rowModel.getObject().getValue();
MainObjectListPanel.this.objectDetailsPerformed(target, object);
}
@Override
public boolean isClickable(IModel<SelectableBean<O>> rowModel) {
return MainObjectListPanel.this.isClickable(rowModel);
}
};
} else {
return new ObjectNameColumn<O>(columnNameModel == null ? createStringResource("ObjectType.name") : columnNameModel,
itemPath) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target, IModel<SelectableBean<O>> rowModel) {
O object = rowModel.getObject().getValue();
MainObjectListPanel.this.objectDetailsPerformed(target, object);
}
@Override
public boolean isClickable(IModel<SelectableBean<O>> rowModel) {
return MainObjectListPanel.this.isClickable(rowModel);
}
};
}
}
protected boolean isClickable(IModel<SelectableBean<O>> rowModel) {
return true;
}
protected abstract void objectDetailsPerformed(AjaxRequestTarget target, O object);
protected abstract void newObjectPerformed(AjaxRequestTarget target);
@Override
protected WebMarkupContainer createTableButtonToolbar(String id) {
return new ButtonBar(id, ID_BUTTON_BAR, this);
}
private static class ButtonBar extends Fragment {
private static final long serialVersionUID = 1L;
public <O extends ObjectType> ButtonBar(String id, String markupId, MainObjectListPanel<O> markupProvider) {
super(id, markupId, markupProvider);
initLayout(markupProvider);
}
private <O extends ObjectType> void initLayout(final MainObjectListPanel<O> mainObjectListPanel) {
AjaxIconButton refreshIcon = new AjaxIconButton(ID_REFRESH, new Model<>("fa fa-refresh"),
mainObjectListPanel.createStringResource("MainObjectListPanel.refresh")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
mainObjectListPanel.clearCache();
mainObjectListPanel.refreshTable((Class<O>) mainObjectListPanel.getType(), target);
target.add((Component) mainObjectListPanel.getTable());
}
};
add(refreshIcon);
AjaxIconButton newObjectIcon = new AjaxIconButton(ID_NEW_OBJECT, new Model<>("fa fa-plus"),
mainObjectListPanel.createStringResource("MainObjectListPanel.newObject")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
mainObjectListPanel.newObjectPerformed(target);
}
};
//TODO will be fixed in 3.6.1
// newObjectIcon.add(new VisibleEnableBehaviour(){
// private static final long serialVersionUID = 1L;
//
// @Override
// public boolean isVisible(){
//
// boolean isVisible = false;
// try {
// PrismObject<O> objectToCreate = mainObjectListPanel.getNewObjectListObject();
// if (objectToCreate != null) {
// mainObjectListPanel.adoptNewObject(objectToCreate);
// }
// isVisible = ((PageBase) getPage()).getSecurityEnforcer().isAuthorized(ModelAuthorizationAction.ADD.getUrl(),
// null, objectToCreate, null, null, null);
// } catch (Exception ex){
// LOGGER.error("Failed to check authorization for ADD action on new object of " + mainObjectListPanel.getType().getSimpleName()
// + " type, ", ex);
// }
// return isVisible;
// }
// });
add(newObjectIcon);
AjaxIconButton importObject = new AjaxIconButton(ID_IMPORT_OBJECT, new Model<>("fa fa-upload"),
mainObjectListPanel.createStringResource("MainObjectListPanel.import")) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
((PageBase) getPage()).navigateToNext(PageImportObject.class);
}
};
importObject.add(new VisibleEnableBehaviour(){
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible(){
boolean isVisible = false;
try {
isVisible = ((PageBase) getPage()).getSecurityEnforcer().isAuthorized(ModelAuthorizationAction.IMPORT_OBJECTS.getUrl(),
null, null, null, null, null)
&& WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_CONFIGURATION_ALL_URL,
AuthorizationConstants.AUTZ_UI_CONFIGURATION_IMPORT_URL);
} catch (Exception ex){
LOGGER.error("Failed to check authorization for IMPORT action for " + mainObjectListPanel.getType().getSimpleName()
+ " object, ", ex);
}
return isVisible;
}
});
add(importObject);
CsvDownloadButtonPanel exportDataLink = new CsvDownloadButtonPanel(ID_EXPORT_DATA) {
private static final long serialVersionUID = 1L;
@Override
protected DataTable<?, ?> getDataTable() {
return mainObjectListPanel.getTable().getDataTable();
}
@Override
protected String getFilename() {
return mainObjectListPanel.getType().getSimpleName() +
"_" + mainObjectListPanel.createStringResource("MainObjectListPanel.exportFileName").getString();
}
};
exportDataLink.add(new VisibleEnableBehaviour(){
private static final long serialVersionUID = 1L;
@Override
public boolean isVisible(){
return WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_ADMIN_CSV_EXPORT_ACTION_URI);
}
});
add(exportDataLink);
}
}
protected PrismObject<O> getNewObjectListObject(){
return null;
}
private void adoptNewObject(PrismObject<O> object) throws SchemaException{
getPageBase().getMidpointApplication().getPrismContext().adopt(object);
}
}