/
SelectorOptions.java
278 lines (240 loc) · 8.89 KB
/
SelectorOptions.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
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2010-2013 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.schema;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.path.ItemPath;
import com.evolveum.midpoint.prism.path.ItemPathSegment;
import com.evolveum.midpoint.prism.path.NameItemPathSegment;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
/**
* @author semancik
*
*/
public class SelectorOptions<T> implements Serializable {
private ObjectSelector selector;
private T options;
//region Construction
public SelectorOptions(ObjectSelector selector, T options) {
super();
this.selector = selector;
this.options = options;
}
public SelectorOptions(T options) {
super();
this.selector = null;
this.options = options;
}
public static <T> SelectorOptions<T> create(ItemPath path, T options) {
return new SelectorOptions<T>(new ObjectSelector(path), options);
}
public static <T> SelectorOptions<T> create(QName pathQName, T options) {
return new SelectorOptions<T>(new ObjectSelector(new ItemPath(pathQName)), options);
}
public static <T> Collection<SelectorOptions<T>> createCollection(ItemPath path, T options) {
Collection<SelectorOptions<T>> optionsCollection = new ArrayList<SelectorOptions<T>>(1);
optionsCollection.add(create(path, options));
return optionsCollection;
}
public static <T> Collection<SelectorOptions<T>> createCollection(QName pathQName, T options) {
Collection<SelectorOptions<T>> optionsCollection = new ArrayList<SelectorOptions<T>>(1);
optionsCollection.add(create(pathQName, options));
return optionsCollection;
}
public static <T> Collection<SelectorOptions<T>> createCollection(T options) {
Collection<SelectorOptions<T>> optionsCollection = new ArrayList<SelectorOptions<T>>(1);
optionsCollection.add(new SelectorOptions<T>(options));
return optionsCollection;
}
public static <T> Collection<SelectorOptions<T>> createCollection(T options, ItemPath... paths) {
Collection<SelectorOptions<T>> optionsCollection = new ArrayList<SelectorOptions<T>>(paths.length);
for (ItemPath path: paths) {
optionsCollection.add(create(path, options));
}
return optionsCollection;
}
public static <T> Collection<SelectorOptions<T>> createCollection(T options, QName... pathQNames) {
Collection<SelectorOptions<T>> optionsCollection = new ArrayList<SelectorOptions<T>>(pathQNames.length);
for (QName qname: pathQNames) {
optionsCollection.add(create(qname, options));
}
return optionsCollection;
}
//endregion
//region Simple getters
public ObjectSelector getSelector() {
return selector;
}
public T getOptions() {
return options;
}
//endregion
//region Methods for accessing content (findRoot, hasToLoadPath, ...)
/**
* Returns options that apply to the "root" object. I.e. options that have null selector, null path, empty path, ...
*/
public static <T> T findRootOptions(Collection<SelectorOptions<T>> options) {
if (options == null) {
return null;
}
for (SelectorOptions<T> oooption: options) {
if (oooption.isRoot()) {
return oooption.getOptions();
}
}
return null;
}
public boolean isRoot() {
if (selector == null) {
return true;
}
if (selector.getPath() == null || selector.getPath().isEmpty()) {
return true;
}
return false;
}
public static boolean hasToLoadPath(QName itemName, Collection<SelectorOptions<GetOperationOptions>> options) {
return hasToLoadPath(new ItemPath(itemName), options);
}
// TODO find a better way to specify this
private static final Set<ItemPath> PATHS_NOT_RETURNED_BY_DEFAULT = new HashSet<>(Arrays.asList(
new ItemPath(UserType.F_JPEG_PHOTO),
new ItemPath(TaskType.F_SUBTASK),
new ItemPath(TaskType.F_NODE_AS_OBSERVED),
new ItemPath(TaskType.F_NEXT_RUN_START_TIMESTAMP),
new ItemPath(TaskType.F_WORKFLOW_CONTEXT, WfContextType.F_WORK_ITEM),
new ItemPath(LookupTableType.F_ROW),
new ItemPath(AccessCertificationCampaignType.F_CASE)));
public static boolean hasToLoadPath(ItemPath path, Collection<SelectorOptions<GetOperationOptions>> options) {
List<SelectorOptions<GetOperationOptions>> retrieveOptions = filterRetrieveOptions(options);
if (retrieveOptions.isEmpty()) {
return !ItemPath.containsEquivalent(PATHS_NOT_RETURNED_BY_DEFAULT, path);
}
for (SelectorOptions<GetOperationOptions> option : retrieveOptions) {
ObjectSelector selector = option.getSelector();
if (selector != null) {
ItemPath selected = selector.getPath();
if (!isPathInSelected(path, selected)) {
continue;
}
}
RetrieveOption retrieveOption = option.getOptions().getRetrieve();
for (ItemPath notByDefault : PATHS_NOT_RETURNED_BY_DEFAULT) {
if (notByDefault.equivalent(path)) {
//this one is not retrieved by default
switch (retrieveOption) {
case INCLUDE:
return true;
case EXCLUDE:
case DEFAULT:
default:
return false;
}
}
}
switch (retrieveOption) {
case EXCLUDE:
case DEFAULT:
return false;
case INCLUDE:
default:
return true;
}
}
return false;
}
private static boolean isPathInSelected(ItemPath path, ItemPath selected) {
if (selected == null || path == null) {
return false;
}
if (path.isEmpty()) {
if (selected.isEmpty()) {
return true;
}
} else {
List<ItemPathSegment> pSegments = path.getSegments();
List<ItemPathSegment> sSegments = selected.getSegments();
for (int i = 0; i < pSegments.size(); i++) {
if (sSegments.size() <= i) {
return true;
}
NameItemPathSegment pSegment = (NameItemPathSegment) pSegments.get(i);
NameItemPathSegment sSegment = (NameItemPathSegment) sSegments.get(i);
if (!pSegment.equivalent(sSegment)) {
return false;
}
}
return true;
}
return false;
}
public static List<SelectorOptions<GetOperationOptions>> filterRetrieveOptions(
Collection<SelectorOptions<GetOperationOptions>> options) {
List<SelectorOptions<GetOperationOptions>> retrieveOptions = new ArrayList<>();
if (options == null) {
return retrieveOptions;
}
for (SelectorOptions<GetOperationOptions> option : options) {
if (option.getOptions() == null || option.getOptions().getRetrieve() == null) {
continue;
}
retrieveOptions.add(option);
}
return retrieveOptions;
}
//endregion
//region hashCode, equals, toString
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((options == null) ? 0 : options.hashCode());
result = prime * result + ((selector == null) ? 0 : selector.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
SelectorOptions other = (SelectorOptions) obj;
if (options == null) {
if (other.options != null)
return false;
} else if (!options.equals(other.options))
return false;
if (selector == null) {
if (other.selector != null)
return false;
} else if (!selector.equals(other.selector))
return false;
return true;
}
@Override
public String toString() {
return "ObjectOperationOptions(" + selector + ": " + options + ")";
}
//endregion
}