/
AbstractQuery.java
253 lines (204 loc) · 8.17 KB
/
AbstractQuery.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
/* 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 org.activiti.engine.impl;
import java.io.Serializable;
import java.util.List;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.db.ListQueryParameterObject;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.interceptor.CommandExecutor;
import org.activiti.engine.query.Query;
import org.activiti.engine.query.QueryProperty;
/**
* Abstract superclass for all query types.
*
* @author Joram Barrez
*/
public abstract class AbstractQuery<T extends Query<?,?>, U> extends ListQueryParameterObject implements Command<Object>, Query<T,U>, Serializable {
private static final long serialVersionUID = 1L;
public static final String SORTORDER_ASC = "asc";
public static final String SORTORDER_DESC = "desc";
private static enum ResultType {
LIST, LIST_PAGE, SINGLE_RESULT, COUNT
}
protected transient CommandExecutor commandExecutor;
protected transient CommandContext commandContext;
protected String databaseType;
protected String orderBy;
protected ResultType resultType;
protected QueryProperty orderProperty;
public static enum NullHandlingOnOrder {
NULLS_FIRST, NULLS_LAST
}
protected NullHandlingOnOrder nullHandlingOnOrder;
protected AbstractQuery() {
parameter = this;
}
protected AbstractQuery(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
}
public AbstractQuery(CommandContext commandContext) {
this.commandContext = commandContext;
}
public AbstractQuery<T, U> setCommandExecutor(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
return this;
}
@SuppressWarnings("unchecked")
public T orderBy(QueryProperty property) {
this.orderProperty = property;
return (T) this;
}
@SuppressWarnings("unchecked")
public T orderBy(QueryProperty property, NullHandlingOnOrder nullHandlingOnOrder) {
orderBy(property);
this.nullHandlingOnOrder = nullHandlingOnOrder;
return (T) this;
}
public T asc() {
return direction(Direction.ASCENDING);
}
public T desc() {
return direction(Direction.DESCENDING);
}
@SuppressWarnings("unchecked")
public T direction(Direction direction) {
if (orderProperty==null) {
throw new ActivitiIllegalArgumentException("You should call any of the orderBy methods first before specifying a direction");
}
addOrder(orderProperty.getName(), direction.getName(), nullHandlingOnOrder);
orderProperty = null;
nullHandlingOnOrder = null;
return (T) this;
}
protected void checkQueryOk() {
if (orderProperty != null) {
throw new ActivitiIllegalArgumentException("Invalid query: call asc() or desc() after using orderByXX()");
}
}
@SuppressWarnings("unchecked")
public U singleResult() {
this.resultType = ResultType.SINGLE_RESULT;
if (commandExecutor!=null) {
return (U) commandExecutor.execute(this);
}
return executeSingleResult(Context.getCommandContext());
}
@SuppressWarnings("unchecked")
public List<U> list() {
this.resultType = ResultType.LIST;
if (commandExecutor!=null) {
return (List<U>) commandExecutor.execute(this);
}
return executeList(Context.getCommandContext(), null);
}
@SuppressWarnings("unchecked")
public List<U> listPage(int firstResult, int maxResults) {
this.firstResult = firstResult;
this.maxResults = maxResults;
this.resultType = ResultType.LIST_PAGE;
if (commandExecutor!=null) {
return (List<U>) commandExecutor.execute(this);
}
return executeList(Context.getCommandContext(), new Page(firstResult, maxResults));
}
public long count() {
this.resultType = ResultType.COUNT;
if (commandExecutor!=null) {
return (Long) commandExecutor.execute(this);
}
return executeCount(Context.getCommandContext());
}
public Object execute(CommandContext commandContext) {
if (resultType==ResultType.LIST) {
return executeList(commandContext, null);
} else if (resultType==ResultType.SINGLE_RESULT) {
return executeSingleResult(commandContext);
} else if (resultType==ResultType.LIST_PAGE) {
return executeList(commandContext, null);
} else {
return executeCount(commandContext);
}
}
public abstract long executeCount(CommandContext commandContext);
/**
* Executes the actual query to retrieve the list of results.
* @param page used if the results must be paged. If null, no paging will be applied.
*/
public abstract List<U> executeList(CommandContext commandContext, Page page);
public U executeSingleResult(CommandContext commandContext) {
List<U> results = executeList(commandContext, null);
if (results.size() == 1) {
return results.get(0);
} else if (results.size() > 1) {
throw new ActivitiException("Query return "+results.size()+" results instead of max 1");
}
return null;
}
protected void addOrder(String column, String sortOrder, NullHandlingOnOrder nullHandlingOnOrder) {
if (orderBy==null) {
orderBy = "";
} else {
orderBy = orderBy+", ";
}
String defaultOrderByClause = column + " "+sortOrder;
if (nullHandlingOnOrder != null) {
if (nullHandlingOnOrder.equals(NullHandlingOnOrder.NULLS_FIRST)) {
if (ProcessEngineConfigurationImpl.DATABASE_TYPE_H2.equals(databaseType)
|| ProcessEngineConfigurationImpl.DATABASE_TYPE_POSTGRES.equals(databaseType)
|| ProcessEngineConfigurationImpl.DATABASE_TYPE_ORACLE.equals(databaseType)) {
orderBy = orderBy + defaultOrderByClause + " NULLS FIRST";
} else if (ProcessEngineConfigurationImpl.DATABASE_TYPE_MYSQL.equals(databaseType)) {
orderBy = orderBy + "isnull(" + column +") desc," + defaultOrderByClause;
} else if (ProcessEngineConfigurationImpl.DATABASE_TYPE_DB2.equals(databaseType)
|| ProcessEngineConfigurationImpl.DATABASE_TYPE_MSSQL.equals(databaseType)) {
orderBy = orderBy + "case when " + column + " is null then 1 else 0 end," + defaultOrderByClause;
} else {
orderBy = orderBy + defaultOrderByClause;
}
} else if (nullHandlingOnOrder.equals(NullHandlingOnOrder.NULLS_LAST)) {
if (ProcessEngineConfigurationImpl.DATABASE_TYPE_H2.equals(databaseType)
|| ProcessEngineConfigurationImpl.DATABASE_TYPE_POSTGRES.equals(databaseType)
|| ProcessEngineConfigurationImpl.DATABASE_TYPE_ORACLE.equals(databaseType)) {
orderBy = orderBy + column + " "+sortOrder + " NULLS LAST";
} else if (ProcessEngineConfigurationImpl.DATABASE_TYPE_MYSQL.equals(databaseType)) {
orderBy = orderBy + "order by isnull(" + column +") asc," + defaultOrderByClause;
} else if (ProcessEngineConfigurationImpl.DATABASE_TYPE_DB2.equals(databaseType)
|| ProcessEngineConfigurationImpl.DATABASE_TYPE_MSSQL.equals(databaseType)) {
orderBy = orderBy + "case when " + column + " is null then 0 else 1 end," + defaultOrderByClause;
} else {
orderBy = orderBy + defaultOrderByClause;
}
}
} else {
orderBy = orderBy + defaultOrderByClause;
}
}
public String getOrderBy() {
if(orderBy == null) {
return super.getOrderBy();
} else {
return orderBy;
}
}
public String getDatabaseType() {
return databaseType;
}
public void setDatabaseType(String databaseType) {
this.databaseType = databaseType;
}
}