-
Notifications
You must be signed in to change notification settings - Fork 188
/
SelectableBeanObjectDataProvider.java
267 lines (224 loc) · 9.4 KB
/
SelectableBeanObjectDataProvider.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
260
261
262
263
264
265
266
267
/*
* Copyright (c) 2010-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.web.component.data;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang.Validate;
import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseException;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.prism.PrismObject;
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.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.component.util.SelectableBean;
import com.evolveum.midpoint.web.page.error.PageError;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ResourceType;
/**
* @author lazyman
* @author semancik
*/
public class SelectableBeanObjectDataProvider<O extends ObjectType> extends BaseSortableDataProvider<SelectableBean<O>> {
private static final long serialVersionUID = 1L;
private static final Trace LOGGER = TraceManager.getTrace(SelectableBeanObjectDataProvider.class);
private static final String DOT_CLASS = SelectableBeanObjectDataProvider.class.getName() + ".";
private static final String OPERATION_SEARCH_OBJECTS = DOT_CLASS + "searchObjects";
private static final String OPERATION_COUNT_OBJECTS = DOT_CLASS + "countObjects";
private Set<? extends O> selected = new HashSet<>();
private boolean emptyListOnNullQuery = false;
private boolean useObjectCounting = true;
private int size = Integer.MAX_VALUE;
private Class<? extends O> type;
private Collection<SelectorOptions<GetOperationOptions>> options;
public SelectableBeanObjectDataProvider(Component component, Class<? extends O> type) {
super(component, true, true);
Validate.notNull(type);
this.type = type;
}
public void clearSelectedObjects(){
selected.clear();
}
public List<O> getSelectedData() {
preprocessSelectedDataInternal();
for (SelectableBean<O> s : super.getAvailableData()){
SelectableBean<O> selectable = (SelectableBean<O>) s;
if (selectable.isSelected()){
((Set)selected).add(selectable.getValue());
}
}
List<O> allSelected = new ArrayList<>();
allSelected.addAll(selected);
return allSelected;
}
private void preprocessSelectedData(){
preprocessSelectedDataInternal();
getAvailableData().clear();
}
private void preprocessSelectedDataInternal(){
for (SelectableBean<O> available : getAvailableData()){
SelectableBean<O> selectableBean = (SelectableBean<O>) available;
if (selectableBean.isSelected()){
((Set)selected).add(selectableBean.getValue());
}
}
for (SelectableBean<O> available : getAvailableData()){
SelectableBean<O> selectableBean = (SelectableBean<O>) available;
if (!selectableBean.isSelected()){
if (selected.contains(selectableBean.getValue())){
selected.remove(selectableBean.getValue());
}
}
}
}
@Override
public Iterator<SelectableBean<O>> internalIterator(long first, long count) {
LOGGER.trace("begin::iterator() from {} count {}.", new Object[]{first, count});
preprocessSelectedData();
OperationResult result = new OperationResult(OPERATION_SEARCH_OBJECTS);
try {
ObjectPaging paging = createPaging(first, count);
Task task = getPage().createSimpleTask(OPERATION_SEARCH_OBJECTS);
ObjectQuery query = getQuery();
if (query == null){
if (emptyListOnNullQuery){
return new ArrayList<SelectableBean<O>>().iterator();
}
query = new ObjectQuery();
}
query.setPaging(paging);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Query {} with {}", type.getSimpleName(), query.debugDump());
}
if (ResourceType.class.equals(type) && (options == null || options.isEmpty())){
options = SelectorOptions.createCollection(GetOperationOptions.createNoFetch());
}
List<PrismObject<? extends O>> list = (List)getModel().searchObjects(type, query, options, task, result);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Query {} resulted in {} objects", type.getSimpleName(), list.size());
}
for (PrismObject<? extends O> object : list) {
getAvailableData().add(createDataObjectWrapper(object.asObjectable()));
}
// result.recordSuccess();
} catch (Exception ex) {
result.recordFatalError("Couldn't list objects.", ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't list objects", ex);
return handleNotSuccessOrHandledErrorInIterator(result);
} finally {
result.computeStatusIfUnknown();
}
LOGGER.trace("end::iterator() {}", result);
return getAvailableData().iterator();
}
protected Iterator<SelectableBean<O>> handleNotSuccessOrHandledErrorInIterator(OperationResult result) {
LOGGER.trace("handling non-success result {}", result);
// page.showResult() will not work here. We are too deep in the rendering now.
// Also do NOT re-throw not redirect to to error page. That will break the page.
// Just return a SelectableBean that indicates the error.
List<SelectableBean<O>> errorList = new ArrayList<>(1);
SelectableBean<O> bean = new SelectableBean<>();
bean.setResult(result);
errorList.add(bean);
return errorList.iterator();
}
public SelectableBean<O> createDataObjectWrapper(O obj) {
SelectableBean<O> selectable = new SelectableBean<O>(obj);
if (!WebComponentUtil.isSuccessOrHandledError(obj.getFetchResult())){
selectable.setResult(obj.getFetchResult());
}
for (O s : selected){
if (s.getOid().equals(obj.getOid())){
selectable.setSelected(true);
}
}
return selectable;
}
@Override
protected int internalSize() {
LOGGER.trace("begin::internalSize()");
if (!isUseObjectCounting()) {
return size;
}
int count = 0;
OperationResult result = new OperationResult(OPERATION_COUNT_OBJECTS);
try {
Task task = getPage().createSimpleTask(OPERATION_COUNT_OBJECTS);
Integer counted = getModel().countObjects(type, getQuery(), options, task, result);
count = counted == null ? 0 : counted.intValue();
} catch (Exception ex) {
result.recordFatalError("Couldn't count objects.", ex);
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't count objects", ex);
} finally {
result.computeStatusIfUnknown();
}
if (!WebComponentUtil.isSuccessOrHandledError(result)) {
getPage().showResult(result);
throw new RestartResponseException(PageError.class);
}
LOGGER.trace("end::internalSize(): {}", count);
return count;
}
@Override
protected CachedSize getCachedSize(Map<Serializable, CachedSize> cache) {
return cache.get(new TypedCacheKey(getQuery(), type));
}
@Override
protected void addCachedSize(Map<Serializable, CachedSize> cache, CachedSize newSize) {
cache.put(new TypedCacheKey(getQuery(), type), newSize);
}
public void setType(Class<O> type) {
Validate.notNull(type, "Class must not be null.");
this.type = type;
clearCache();
}
public boolean isUseObjectCounting(){
return useObjectCounting;
}
public void setUseObjectCounting(boolean useCounting) {
this.useObjectCounting = useCounting;
}
public Collection<SelectorOptions<GetOperationOptions>> getOptions() {
return options;
}
public void setOptions(Collection<SelectorOptions<GetOperationOptions>> options) {
this.options = options;
}
public boolean isEmptyListOnNullQuery() {
return emptyListOnNullQuery;
}
public void setEmptyListOnNullQuery(boolean emptyListOnNullQuery) {
this.emptyListOnNullQuery = emptyListOnNullQuery;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}