-
Notifications
You must be signed in to change notification settings - Fork 188
/
HibernateQuery.java
283 lines (231 loc) · 8.77 KB
/
HibernateQuery.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
279
280
281
282
283
/*
* Copyright (c) 2010-2015 Evolveum 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.repo.sql.query.hqm;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.jetbrains.annotations.NotNull;
import com.evolveum.midpoint.prism.query.OrderDirection;
import com.evolveum.midpoint.repo.sql.data.common.RObject;
import com.evolveum.midpoint.repo.sql.query.QueryException;
import com.evolveum.midpoint.repo.sql.query.definition.JpaEntityDefinition;
import com.evolveum.midpoint.repo.sql.query.definition.JpaLinkDefinition;
import com.evolveum.midpoint.repo.sql.query.hqm.condition.Condition;
import com.evolveum.midpoint.repo.sql.util.ClassMapper;
/**
* Query in HQL that is being created.
*
* @author mederly
*/
public abstract class HibernateQuery {
private static final String INDENT_STRING = " ";
// projection elements - i.e. select projectionElement1, projectionElement2, ..., projectionElementN from ...
private List<ProjectionElement> projectionElements = new ArrayList<>();
/**
* Primary entity for this query, along with joined entities.
* For example,
* RUser u
* Or,
* RUser u
* left join u.assignments a with ...
* <p>
* (originally, we thought about cross-joins with other entities, hence "primary entity")
*/
private EntityReference primaryEntity; // not null
/**
* List of conditions in the "where" clause. They are to be interpreted as a conjunction.
*/
private List<Condition> conditions = new ArrayList<>();
public static class Ordering {
@NotNull private final String byProperty;
private final OrderDirection direction;
Ordering(@NotNull String byProperty, OrderDirection direction) {
this.byProperty = byProperty;
this.direction = direction;
}
@NotNull
public String getByProperty() {
return byProperty;
}
public OrderDirection getDirection() {
return direction;
}
}
private List<Ordering> orderingList = new ArrayList<>();
public static class Grouping {
@NotNull private final String byProperty;
Grouping(@NotNull String byProperty) {
this.byProperty = byProperty;
}
@NotNull
public String getByProperty() {
return byProperty;
}
}
private List<Grouping> groupingList = new ArrayList<>();
public HibernateQuery(@NotNull JpaEntityDefinition primaryEntityDef) {
primaryEntity = createItemSpecification(primaryEntityDef);
}
protected HibernateQuery(EntityReference primaryEntity) {
this.primaryEntity = primaryEntity;
}
public List<ProjectionElement> getProjectionElements() {
return projectionElements;
}
public void addProjectionElement(ProjectionElement element) {
projectionElements.add(element);
}
public void addProjectionElementsFor(List<String> items) {
for (String item : items) {
addProjectionElement(new GenericProjectionElement(item));
}
}
public EntityReference getPrimaryEntity() {
return primaryEntity;
}
public void setPrimaryEntity(EntityReference primaryEntity) {
this.primaryEntity = primaryEntity;
}
public List<Condition> getConditions() {
return conditions;
}
public void addCondition(Condition condition) {
conditions.add(condition);
}
// Seems to have some side effects. Do not call twice!
public String getAsHqlText(int indent, boolean distinct) {
StringBuilder sb = new StringBuilder();
indent(sb, indent);
sb.append("select");
if (distinct) {
sb.append(" distinct");
}
sb.append("\n");
ProjectionElement.dumpToHql(sb, projectionElements, indent + 1); // we finish at the end of the last line (not at the new line)
sb.append("\n");
indent(sb, indent);
sb.append("from\n");
primaryEntity.dumpToHql(sb, indent + 1);
if (!conditions.isEmpty()) {
sb.append("\n");
indent(sb, indent);
sb.append("where\n");
Condition.dumpToHql(sb, conditions, indent + 1);
}
if (!orderingList.isEmpty()) {
sb.append("\n");
indent(sb, indent);
sb.append("order by ");
boolean first = true;
for (Ordering ordering : orderingList) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(ordering.byProperty);
if (ordering.direction != null) {
switch (ordering.direction) {
case DESCENDING:
sb.append(" desc");
break;
case ASCENDING:
sb.append(" asc");
break;
default:
throw new IllegalStateException("Unknown ordering: " + ordering.direction);
}
}
}
}
if (!groupingList.isEmpty()) {
sb.append("\n");
indent(sb, indent);
sb.append("group by ");
boolean first = true;
for (Grouping grouping : groupingList) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(grouping.byProperty);
}
}
return sb.toString();
}
public static void indent(StringBuilder sb, int indent) {
while (indent-- > 0) {
sb.append(INDENT_STRING);
}
}
public EntityReference createItemSpecification(JpaEntityDefinition entityDef) {
String alias = createAlias(entityDef);
return new EntityReference(alias, entityDef.getJpaClassName());
}
public String createAlias(JpaEntityDefinition def) {
return createAlias(def.getJpaClassName(), true);
}
public String createAlias(JpaLinkDefinition linkDefinition) {
Validate.notNull(linkDefinition.getJpaName(), "Got unnamed transition");
return createAlias(linkDefinition.getJpaName(), false);
}
private static final int LIMIT = 100;
public String createAlias(String name, boolean entity) {
String prefix;
//we want to skip 'R' prefix for entity definition names (a bit of hack)
int prefixIndex = entity ? 1 : 0;
prefix = Character.toString(name.charAt(prefixIndex)).toLowerCase();
int index = 2;
String alias = prefix;
while (hasAlias(alias)) {
alias = prefix + Integer.toString(index);
index++;
if (index > LIMIT) {
throw new IllegalStateException("Alias index for '" + name
+ "' is more than " + LIMIT + "? This probably should not happen.");
}
}
return alias;
}
private boolean hasAlias(String alias) {
if (primaryEntity != null && primaryEntity.containsAlias(alias)) {
return true;
}
return false;
}
public String getPrimaryEntityAlias() {
return getPrimaryEntity().getAlias();
}
// use with care!
public void setPrimaryEntityAlias(String alias) {
getPrimaryEntity().setAlias(alias);
}
public void addOrdering(String propertyPath, OrderDirection direction) {
orderingList.add(new Ordering(propertyPath, direction));
}
public List<Ordering> getOrderingList() {
return orderingList;
}
public void addGrouping(String propertyPath) {
groupingList.add(new Grouping(propertyPath));
}
public List<Grouping> getGroupingList() {
return groupingList;
}
public abstract RootHibernateQuery getRootQuery();
// used to narrow the primary entity e.g. from RObject to RUser (e.g. during ItemValueRestriction processing)
public void narrowPrimaryEntity(JpaEntityDefinition newDefinition) throws QueryException {
String oldEntityName = getPrimaryEntity().getName();
Class<? extends RObject> oldEntityClass = ClassMapper.getHqlClassForHqlName(oldEntityName);
Class<?> newEntityClass = newDefinition.getJpaClass();
if (!(oldEntityClass.isAssignableFrom(newEntityClass))) {
throw new QueryException("Cannot narrow primary entity definition from " + oldEntityClass + " to " + newEntityClass);
}
getPrimaryEntity().setName(newDefinition.getJpaClassName()); // alias stays the same
}
}