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() {
|
}
|
}
|
}
|