/
FilterQueryResolverDelegate.java
226 lines (189 loc) · 8.8 KB
/
FilterQueryResolverDelegate.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
package org.infinispan.objectfilter.impl.hql;
import org.antlr.runtime.tree.Tree;
import org.hibernate.hql.ast.common.JoinType;
import org.hibernate.hql.ast.origin.hql.resolve.path.PathedPropertyReference;
import org.hibernate.hql.ast.origin.hql.resolve.path.PathedPropertyReferenceSource;
import org.hibernate.hql.ast.origin.hql.resolve.path.PropertyPath;
import org.hibernate.hql.ast.spi.QueryResolverDelegate;
import org.infinispan.objectfilter.impl.logging.Log;
import org.infinispan.objectfilter.impl.util.StringHelper;
import org.jboss.logging.Logger;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author anistor@redhat.com
* @since 7.0
*/
final class FilterQueryResolverDelegate implements QueryResolverDelegate {
private static final Log log = Logger.getMessageLogger(Log.class, FilterQueryResolverDelegate.class.getName());
private final Map<String, String> aliasToEntityType = new HashMap<>();
private final Map<String, PropertyPath> aliasToPropertyPath = new HashMap<>();
protected final ObjectPropertyHelper propertyHelper;
private String targetType;
protected String alias;
protected enum Status {
DEFINING_SELECT, DEFINING_FROM
}
protected Status status;
FilterQueryResolverDelegate(ObjectPropertyHelper propertyHelper) {
this.propertyHelper = propertyHelper;
}
@Override
public void registerPersisterSpace(Tree entityNameTree, Tree aliasTree) {
String alias = aliasTree.getText();
String entityName = entityNameTree.getText();
String prevAlias = aliasToEntityType.put(alias, entityName);
if (prevAlias != null && !prevAlias.equalsIgnoreCase(entityName)) {
throw new UnsupportedOperationException("Alias reuse currently not supported: alias " + alias + " already assigned to type " + prevAlias);
}
if (targetType != null) {
throw new IllegalStateException("Can't target multiple types: " + targetType + " already selected before " + entityName);
}
targetType = entityName;
Class<?> targetClass = propertyHelper.getEntityNamesResolver().getClassFromName(entityName);
if (targetClass == null) {
throw log.getUnknownEntity(entityName);
}
}
@Override
public void registerJoinAlias(Tree alias, PropertyPath path) {
if (!path.getNodes().isEmpty() && !aliasToPropertyPath.containsKey(alias.getText())) {
aliasToPropertyPath.put(alias.getText(), path);
}
}
@Override
public boolean isUnqualifiedPropertyReference() {
return true;
}
@Override
public PathedPropertyReferenceSource normalizeUnqualifiedPropertyReference(Tree propertyTree) {
String property = propertyTree.getText();
if (aliasToEntityType.containsKey(property)) {
return normalizeQualifiedRoot(propertyTree);
}
return normalizeProperty(new FilterEntityTypeDescriptor(targetType, propertyHelper), Collections.emptyList(), property);
}
@Override
public boolean isPersisterReferenceAlias() {
return aliasToEntityType.containsKey(alias);
}
@Override
public PathedPropertyReferenceSource normalizeUnqualifiedRoot(Tree identifier382) {
String aliasText = identifier382.getText();
if (aliasToEntityType.containsKey(aliasText)) {
return normalizeQualifiedRoot(identifier382);
}
if (aliasToPropertyPath.containsKey(aliasText)) {
PropertyPath propertyPath = aliasToPropertyPath.get(aliasText);
if (propertyPath == null) {
throw log.getUnknownAliasException(aliasText);
}
FilterTypeDescriptor sourceType = (FilterTypeDescriptor) propertyPath.getNodes().get(0).getType();
List<String> resolveAlias = resolveAlias(propertyPath);
return new PathedPropertyReference(aliasText, new FilterEmbeddedEntityTypeDescriptor(sourceType.getEntityType(), propertyHelper, resolveAlias), true);
}
throw log.getUnknownAliasException(aliasText);
}
@Override
public PathedPropertyReferenceSource normalizeQualifiedRoot(Tree root) {
String alias = root.getText();
String entityNameForAlias = aliasToEntityType.get(alias);
if (entityNameForAlias == null) {
PropertyPath propertyPath = aliasToPropertyPath.get(alias);
if (propertyPath == null) {
throw log.getUnknownAliasException(alias);
}
return new PathedPropertyReference(StringHelper.join(propertyPath.getNodeNamesWithoutAlias()), null, false);
}
if (propertyHelper.getEntityNamesResolver().getClassFromName(entityNameForAlias) == null) {
throw log.getUnknownEntity(entityNameForAlias);
}
return new PathedPropertyReference(alias, new FilterEntityTypeDescriptor(entityNameForAlias, propertyHelper), true);
}
@Override
public PathedPropertyReferenceSource normalizePropertyPathIntermediary(PropertyPath path, Tree propertyNameTree) {
FilterTypeDescriptor sourceType = (FilterTypeDescriptor) path.getLastNode().getType();
String propertyName = propertyNameTree.getText();
if (!sourceType.hasProperty(propertyName)) {
throw log.getNoSuchPropertyException(sourceType.toString(), propertyName);
}
List<String> newPath = resolveAlias(path);
newPath.add(propertyName);
return new PathedPropertyReference(propertyName, new FilterEmbeddedEntityTypeDescriptor(sourceType.getEntityType(), propertyHelper, newPath), false);
}
private List<String> resolveAlias(PropertyPath path) {
if (path.getFirstNode().isAlias()) {
String alias = path.getFirstNode().getName();
if (aliasToEntityType.containsKey(alias)) {
// Alias for entity
return path.getNodeNamesWithoutAlias();
} else if (aliasToPropertyPath.containsKey(alias)) {
// Alias for embedded
PropertyPath propertyPath = aliasToPropertyPath.get(alias);
List<String> resolvedAlias = resolveAlias(propertyPath);
resolvedAlias.addAll(path.getNodeNamesWithoutAlias());
return resolvedAlias;
} else {
// Alias not found
throw log.getUnknownAliasException(alias);
}
}
// It does not start with an alias
return path.getNodeNamesWithoutAlias();
}
@Override
public PathedPropertyReferenceSource normalizeIntermediateIndexOperation(PathedPropertyReferenceSource propertyReferenceSource, Tree collectionProperty, Tree selector) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public void normalizeTerminalIndexOperation(PathedPropertyReferenceSource propertyReferenceSource, Tree collectionProperty, Tree selector) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public PathedPropertyReferenceSource normalizeUnqualifiedPropertyReferenceSource(Tree identifier394) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public PathedPropertyReferenceSource normalizePropertyPathTerminus(PropertyPath path, Tree propertyNameNode) {
// receives the property name on a specific entity reference _source_
return normalizeProperty((FilterTypeDescriptor) path.getLastNode().getType(), path.getNodeNamesWithoutAlias(), propertyNameNode.getText());
}
protected PathedPropertyReferenceSource normalizeProperty(FilterTypeDescriptor type, List<String> path, String propertyName) {
if (!type.hasProperty(propertyName)) {
throw log.getNoSuchPropertyException(type.toString(), propertyName);
}
FilterTypeDescriptor propType;
if (type.hasEmbeddedProperty(propertyName)) {
List<String> newPath = new LinkedList<>(path);
newPath.add(propertyName);
propType = new FilterEmbeddedEntityTypeDescriptor(type.getEntityType(), propertyHelper, newPath);
} else {
propType = new FilterPropertyTypeDescriptor();
}
return new PathedPropertyReference(propertyName, propType, false);
}
@Override
public void pushFromStrategy(JoinType joinType, Tree associationFetchTree, Tree propertyFetchTree, Tree alias) {
status = Status.DEFINING_FROM;
this.alias = alias.getText();
}
@Override
public void pushSelectStrategy() {
status = Status.DEFINING_SELECT;
}
@Override
public void popStrategy() {
status = null;
alias = null;
}
@Override
public void propertyPathCompleted(PropertyPath path) {
if (status == Status.DEFINING_SELECT && path.getLastNode().getType() instanceof FilterEmbeddedEntityTypeDescriptor) {
FilterEmbeddedEntityTypeDescriptor type = (FilterEmbeddedEntityTypeDescriptor) path.getLastNode().getType();
throw log.getProjectionOfCompleteEmbeddedEntitiesNotSupportedException(type.getEntityType(), path.asStringPathWithoutAlias());
}
}
}