/
InOidFilter.java
215 lines (180 loc) · 5.6 KB
/
InOidFilter.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
/*
* 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.prism.query;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import com.evolveum.midpoint.prism.ExpressionWrapper;
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismObject;
import org.apache.commons.lang.StringUtils;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.util.exception.SchemaException;
public class InOidFilter extends ObjectFilter {
private Collection<String> oids;
private ExpressionWrapper expression;
private boolean considerOwner; // temporary hack (checks owner OID)
private InOidFilter(boolean considerOwner, Collection<String> oids) {
this.considerOwner = considerOwner;
this.oids = oids;
}
private InOidFilter(boolean considerOwner, ExpressionWrapper expression){
this.considerOwner = considerOwner;
this.expression = expression;
}
public static InOidFilter createInOid(boolean considerOwner, Collection<String> oids){
return new InOidFilter(considerOwner, oids);
}
public static InOidFilter createInOid(Collection<String> oids){
return new InOidFilter(false, oids);
}
public static InOidFilter createInOid(String... oids){
return new InOidFilter(false, Arrays.asList(oids));
}
public static InOidFilter createOwnerHasOidIn(Collection<String> oids){
return new InOidFilter(true, oids);
}
public static InOidFilter createOwnerHasOidIn(String... oids){
return new InOidFilter(true, Arrays.asList(oids));
}
public static InOidFilter createInOid(boolean considerOwner, ExpressionWrapper expression){
return new InOidFilter(considerOwner, expression);
}
public Collection<String> getOids() {
return oids;
}
public void setOids(Collection<String> oids) {
this.oids = oids;
}
public boolean isConsiderOwner() {
return considerOwner;
}
public ExpressionWrapper getExpression() {
return expression;
}
public void setExpression(ExpressionWrapper expression) {
this.expression = expression;
}
@Override
public void checkConsistence(boolean requireDefinitions) {
if (oids == null) {
throw new IllegalArgumentException("Null oids in "+this);
}
for (String oid: oids) {
if (StringUtils.isBlank(oid)) {
throw new IllegalArgumentException("Empty oid in "+this);
}
}
}
@Override
public String debugDump() {
return debugDump(0);
}
@Override
public String debugDump(int indent) {
StringBuilder sb = new StringBuilder();
sb.append("IN OID: ");
if (considerOwner) {
sb.append("(for owner)");
}
sb.append("VALUE:");
if (getOids() != null) {
sb.append("\n");
for (String oid : getOids()) {
DebugUtil.indentDebugDump(sb, indent+1);
sb.append(oid);
sb.append("\n");
}
} else {
sb.append(" null\n");
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("IN OID: ");
if (getOids() != null){
Iterator<String> itertor = getOids().iterator();
while (itertor.hasNext()){
String value = itertor.next();
if (value == null) {
sb.append("null");
} else {
sb.append(value);
}
sb.append("; ");
}
}
return sb.toString();
}
@Override
public InOidFilter clone() {
InOidFilter inOid = new InOidFilter(considerOwner, getOids());
inOid.setExpression(getExpression());
return inOid;
}
@Override
public boolean match(PrismContainerValue value, MatchingRuleRegistry matchingRuleRegistry) throws SchemaException {
if (value == null) {
return false; // just for sure
}
// are we a prism object?
if (value.getParent() instanceof PrismObject) {
if (considerOwner) {
return false;
}
String oid = ((PrismObject) (value.getParent())).getOid();
return StringUtils.isNotBlank(oid) && oids != null && oids.contains(oid);
}
final PrismContainerValue pcvToConsider;
if (considerOwner) {
if (!(value.getParent() instanceof PrismContainer)) {
return false;
}
PrismContainer container = (PrismContainer) value.getParent();
if (!(container.getParent() instanceof PrismContainerValue)) {
return false;
}
pcvToConsider = (PrismContainerValue) container.getParent();
} else {
pcvToConsider = value;
}
return pcvToConsider.getId() != null && oids.contains(pcvToConsider.getId());
}
@Override
public boolean equals(Object o, boolean exact) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
InOidFilter that = (InOidFilter) o;
if (considerOwner != that.considerOwner)
return false;
if (oids != null ? !oids.equals(that.oids) : that.oids != null)
return false;
return expression != null ? expression.equals(that.expression) : that.expression == null;
}
@Override
public int hashCode() {
int result = oids != null ? oids.hashCode() : 0;
result = 31 * result + (expression != null ? expression.hashCode() : 0);
result = 31 * result + (considerOwner ? 1 : 0);
return result;
}
}