lyh
2025-01-13 137d008bd9b7d932160436a3a560b24512f6d1db
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
package org.activiti.engine.impl;
 
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
 
import org.activiti.engine.ActivitiException;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.ManagementService;
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;
 
import java.io.Serializable;
import java.util.List;
 
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";
    protected transient CommandExecutor commandExecutor;
    protected transient CommandContext commandContext;
    protected String databaseType;
    protected String orderBy;
    protected ResultType resultType;
    protected QueryProperty orderProperty;
    protected NullHandlingOnOrder nullHandlingOnOrder;
 
    protected AbstractQuery() {
        this.parameter = this;
    }
 
    protected AbstractQuery(CommandExecutor commandExecutor) {
        this.commandExecutor = commandExecutor;
    }
 
    public AbstractQuery(CommandContext commandContext) {
        this.commandContext = commandContext;
    }
 
    public AbstractQuery(ManagementService managementService) {
        this(((ManagementServiceImpl)managementService).getCommandExecutor());
    }
 
    public AbstractQuery<T, U> setCommandExecutor(CommandExecutor commandExecutor) {
        this.commandExecutor = commandExecutor;
        return this;
    }
 
    public T orderBy(QueryProperty property) {
        this.orderProperty = property;
        return (T) this;
    }
 
    public T orderBy(QueryProperty property, NullHandlingOnOrder nullHandlingOnOrder) {
        this.orderBy(property);
        this.nullHandlingOnOrder = nullHandlingOnOrder;
        return (T) this;
    }
 
    public T asc() {
        return this.direction(Direction.ASCENDING);
    }
 
    public T desc() {
        return this.direction(Direction.DESCENDING);
    }
 
    public T direction(Direction direction) {
        if (this.orderProperty == null) {
            throw new ActivitiIllegalArgumentException("You should call any of the orderBy methods first before specifying a direction");
        } else {
            this.addOrder(this.orderProperty.getName(), direction.getName(), this.nullHandlingOnOrder);
            this.orderProperty = null;
            this.nullHandlingOnOrder = null;
            return (T) this;
        }
    }
 
    protected void checkQueryOk() {
        if (this.orderProperty != null) {
            throw new ActivitiIllegalArgumentException("Invalid query: call asc() or desc() after using orderByXX()");
        }
    }
 
    public U singleResult() {
        this.resultType = ResultType.SINGLE_RESULT;
        return this.commandExecutor != null ? (U) this.commandExecutor.execute(this) : this.executeSingleResult(Context.getCommandContext());
    }
 
    public List<U> list() {
        this.resultType = ResultType.LIST;
        return this.commandExecutor != null ? (List)this.commandExecutor.execute(this) : this.executeList(Context.getCommandContext(), (Page)null);
    }
 
    public List<U> listPage(int firstResult, int maxResults) {
        this.firstResult = firstResult;
        this.maxResults = maxResults;
        this.resultType = ResultType.LIST_PAGE;
        return this.commandExecutor != null ? (List)this.commandExecutor.execute(this) : this.executeList(Context.getCommandContext(), new Page(firstResult, maxResults));
    }
 
    public long count() {
        this.resultType = ResultType.COUNT;
        return this.commandExecutor != null ? (Long)this.commandExecutor.execute(this) : this.executeCount(Context.getCommandContext());
    }
 
    public Object execute(CommandContext commandContext) {
        if (this.resultType == ResultType.LIST) {
            return this.executeList(commandContext, (Page)null);
        } else if (this.resultType == ResultType.SINGLE_RESULT) {
            return this.executeSingleResult(commandContext);
        } else {
            return this.resultType == ResultType.LIST_PAGE ? this.executeList(commandContext, (Page)null) : this.executeCount(commandContext);
        }
    }
 
    public abstract long executeCount(CommandContext var1);
 
    public abstract List<U> executeList(CommandContext var1, Page var2);
 
    public U executeSingleResult(CommandContext commandContext) {
        List<U> results = this.executeList(commandContext, (Page)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");
        } else {
            return null;
        }
    }
 
    protected void addOrder(String column, String sortOrder, NullHandlingOnOrder nullHandlingOnOrder) {
        if (this.orderBy == null) {
            this.orderBy = "";
        } else {
            this.orderBy = this.orderBy + ", ";
        }
 
        String defaultOrderByClause = column + " " + sortOrder;
        if (nullHandlingOnOrder != null) {
            if (nullHandlingOnOrder.equals(NullHandlingOnOrder.NULLS_FIRST)) {
                if (!"h2".equals(this.databaseType) && !"hsql".equals(this.databaseType) && !"postgres".equals(this.databaseType) && !"oracle".equals(this.databaseType)) {
                    if ("mysql".equals(this.databaseType)) {
                        this.orderBy = this.orderBy + "isnull(" + column + ") desc," + defaultOrderByClause;
                    } else if (!"db2".equals(this.databaseType) && !"mssql".equals(this.databaseType)) {
                        this.orderBy = this.orderBy + defaultOrderByClause;
                    } else {
                        this.orderBy = this.orderBy + "case when " + column + " is null then 0 else 1 end," + defaultOrderByClause;
                    }
                } else {
                    this.orderBy = this.orderBy + defaultOrderByClause + " NULLS FIRST";
                }
            } else if (nullHandlingOnOrder.equals(NullHandlingOnOrder.NULLS_LAST)) {
                if (!"h2".equals(this.databaseType) && !"hsql".equals(this.databaseType) && !"postgres".equals(this.databaseType) && !"oracle".equals(this.databaseType)) {
                    if ("mysql".equals(this.databaseType)) {
                        this.orderBy = this.orderBy + "isnull(" + column + ") asc," + defaultOrderByClause;
                    } else if (!"db2".equals(this.databaseType) && !"mssql".equals(this.databaseType)) {
                        this.orderBy = this.orderBy + defaultOrderByClause;
                    } else {
                        this.orderBy = this.orderBy + "case when " + column + " is null then 1 else 0 end," + defaultOrderByClause;
                    }
                } else {
                    this.orderBy = this.orderBy + column + " " + sortOrder + " NULLS LAST";
                }
            }
        } else {
            this.orderBy = this.orderBy + defaultOrderByClause;
        }
 
    }
 
    public String getOrderBy() {
        return this.orderBy == null ? super.getOrderBy() : this.orderBy;
    }
 
    public String getOrderByColumns() {
        return this.getOrderBy();
    }
 
    public String getDatabaseType() {
        return this.databaseType;
    }
 
    public void setDatabaseType(String databaseType) {
        this.databaseType = databaseType;
    }
 
    public static enum NullHandlingOnOrder {
        NULLS_FIRST,
        NULLS_LAST;
 
        private NullHandlingOnOrder() {
        }
    }
 
    private static enum ResultType {
        LIST,
        LIST_PAGE,
        SINGLE_RESULT,
        COUNT;
 
        private ResultType() {
        }
    }
}