/
ContainerableListPanel.java
208 lines (174 loc) · 7.72 KB
/
ContainerableListPanel.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
/*
* Copyright (c) 2010-2019 Evolveum et al. and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.gui.api.component;
import com.evolveum.midpoint.gui.api.model.LoadableModel;
import com.evolveum.midpoint.gui.api.prism.wrapper.PrismContainerValueWrapper;
import com.evolveum.midpoint.prism.Containerable;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectPaging;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.data.BoxedTablePanel;
import com.evolveum.midpoint.web.component.search.Search;
import com.evolveum.midpoint.web.component.search.SearchFactory;
import com.evolveum.midpoint.web.component.search.SearchFormPanel;
import com.evolveum.midpoint.web.component.util.ContainerListDataProvider;
import com.evolveum.midpoint.web.component.util.VisibleBehaviour;
import com.evolveum.midpoint.web.session.PageStorage;
import com.evolveum.midpoint.web.session.UserProfileStorage;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.markup.html.WebMarkupContainer;
import java.util.Collection;
import java.util.List;
/**
* Created by honchar
*/
public abstract class ContainerableListPanel<C extends Containerable> extends BasePanel<C>{
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(ContainerableListPanel.class);
private static final String ID_TABLE = "table";
private LoadableModel<Search> searchModel = null;
UserProfileStorage.TableId tableId;
ContainerListDataProvider provider = null;
public ContainerableListPanel(String id, UserProfileStorage.TableId tableId){
super(id);
this.tableId = tableId;
}
@Override
protected void onInitialize(){
super.onInitialize();
initSearchModel();
initLayout();
}
private void initSearchModel() {
searchModel = new LoadableModel<Search>(false) {
private static final long serialVersionUID = 1L;
@Override
public Search load() {
Search search = null;
PageStorage storage = getPageStorage();
if (storage != null) {
search = storage.getSearch();
}
if (search == null) {
search = SearchFactory.createContainerSearch(getType(), ContainerableListPanel.this.getPageBase());
}
return search;
}
};
}
private void initLayout() {
initProvider();
BoxedTablePanel<PrismContainerValueWrapper<C>> table =
new BoxedTablePanel<PrismContainerValueWrapper<C>>(ID_TABLE, provider, initColumns(),
tableId, (int)getItemsPerPage()) {
private static final long serialVersionUID = 1L;
@Override
protected WebMarkupContainer createHeader(String headerId) {
SearchFormPanel searchPanel = new SearchFormPanel(headerId, searchModel) {
private static final long serialVersionUID = 1L;
@Override
protected void searchPerformed(ObjectQuery query, AjaxRequestTarget target) {
ContainerableListPanel.this.searchPerformed(target);
}
@Override
protected void saveSearch(Search search, AjaxRequestTarget target) {
if (getPageStorage() != null) {
getPageStorage().setSearch(search);
}
}
};
searchPanel.add(new VisibleBehaviour(() -> isSearchVisible()));
return searchPanel;
}
@Override
protected boolean hideFooterIfSinglePage(){
return ContainerableListPanel.this.hideFooterIfSinglePage();
}
};
table.setShowPaging(true);
table.setOutputMarkupId(true);
add(table);
}
private void initProvider() {
provider = new ContainerListDataProvider<C>(this,
getType(), getQueryOptions()) {
private static final long serialVersionUID = 1L;
@Override
protected void saveProviderPaging(ObjectQuery query, ObjectPaging paging) {
getPageStorage().setPaging(paging);
}
@Override
public ObjectQuery getQuery() {
try {
// TODO this seems to throw now checked exception (see unused throws lower)
return createQuery();
} catch (SchemaException | ObjectNotFoundException | ExpressionEvaluationException | CommunicationException
| ConfigurationException | SecurityViolationException e) {
LOGGER.error("Couldn't create query for type " + getType().getSimpleName(), e.getLocalizedMessage());
}
return null;
}
};
setDefaultSorting(provider);
}
protected abstract Class<C> getType();
protected void setDefaultSorting(ContainerListDataProvider<C> provider){
//should be overridden if needed
}
public long getItemsPerPage() {
UserProfileStorage userProfile = getPageBase().getSessionStorage().getUserProfile();
return userProfile.getPagingSize(tableId);
}
protected Collection<SelectorOptions<GetOperationOptions>> getQueryOptions() {
return null;
}
protected abstract <PS extends PageStorage> PS getPageStorage();
private ObjectQuery createQuery() throws SchemaException, ObjectNotFoundException, ExpressionEvaluationException, CommunicationException, ConfigurationException, SecurityViolationException {
Search search = searchModel.getObject();
ObjectQuery query = search != null ? search.createObjectQuery(getPageBase().getPrismContext()) :
getPrismContext().queryFor(getType()).build();
ObjectFilter customFilter = getCustomFilter();
if (customFilter != null){
if (query == null){
query = getPrismContext().queryFor(getType()).build();
}
query.addFilter(customFilter);
}
return query;
}
protected ObjectFilter getCustomFilter(){
return null;
}
private void searchPerformed(AjaxRequestTarget target){
PageStorage storage = getPageStorage();
if (storage != null) {
storage.setSearch(searchModel.getObject());
storage.setPaging(null);
}
BoxedTablePanel table = (BoxedTablePanel) get(ID_TABLE);
table.setCurrentPage(null);
target.add((Component) table);
target.add(getPageBase().getFeedbackPanel());
}
protected abstract List<IColumn<PrismContainerValueWrapper<C>, String>> initColumns();
protected boolean hideFooterIfSinglePage(){
return false;
}
protected boolean isSearchVisible(){
return true;
}
public ContainerListDataProvider getProvider(){
return provider;
}
}