AbstractQuery.java 3.5 KB
Newer Older
T
tombaeyens 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/* 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.
 */
13
package org.activiti.engine.impl;
T
tombaeyens 已提交
14

15 16 17 18 19 20
import java.util.List;

import org.activiti.engine.ActivitiException;
import org.activiti.engine.Page;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
21
import org.activiti.engine.impl.interceptor.CommandExecutor;
T
tombaeyens 已提交
22 23 24 25 26 27 28


/**
 * Abstract superclass for all query types.
 *  
 * @author Joram Barrez
 */
29 30
public abstract class AbstractQuery<T> implements Command<Object>{
  
31 32 33
  protected static final String SORTORDER_ASC = "asc";
  protected static final String SORTORDER_DESC = "desc";
  
34
  private static enum ResultType {
35
    LIST, LIST_PAGE, SINGLE_RESULT, COUNT
36
  }
T
tombaeyens 已提交
37 38
    
  protected CommandExecutor commandExecutor;
39
  protected String orderBy;
40
  
41 42
  protected int firstResult;
  protected int maxResults;
43
  protected ResultType resultType;
T
tombaeyens 已提交
44
 
45 46 47 48
  protected AbstractQuery() {
  }

  protected AbstractQuery(CommandExecutor commandExecutor) {
T
tombaeyens 已提交
49 50 51
    this.commandExecutor = commandExecutor;
  }

52
  @SuppressWarnings("unchecked")
53
  public T listPage() {
54 55
    this.resultType = ResultType.SINGLE_RESULT;
    return (T) commandExecutor.execute(this);
56 57
  }

58
  @SuppressWarnings("unchecked")
59
  public List<T> list() {
60 61
    this.resultType = ResultType.LIST;
    return (List) commandExecutor.execute(this);
62 63
  }
  
64
  @SuppressWarnings("unchecked")
65 66 67 68
  public List<T> listPage(int firstResult, int maxResults) {
    this.firstResult = firstResult;
    this.maxResults = maxResults;
    this.resultType = ResultType.LIST_PAGE;
69
    return (List) commandExecutor.execute(this);
70 71 72
  }
  
  public long count() {
73 74 75 76 77 78 79 80 81
    this.resultType = ResultType.COUNT;
    return (Long) commandExecutor.execute(this);
  }
  
  public Object execute(CommandContext commandContext) {
    if (resultType==ResultType.LIST) {
      return executeList(commandContext, null);
    } else if (resultType==ResultType.SINGLE_RESULT) {
      return executeSingleResult(commandContext);
82 83
    } else if (resultType==ResultType.LIST_PAGE) {
      return executeList(commandContext, new Page(firstResult, maxResults));
84 85 86
    } else {
      return executeCount(commandContext);
    }
87 88 89 90 91 92 93 94 95 96 97
  }

  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<T> executeList(CommandContext commandContext, Page page);
  
  public T executeSingleResult(CommandContext commandContext) {
T
tombaeyens 已提交
98
    List<T> results = executeList(commandContext, null);
99 100 101 102 103 104 105
    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;
  }
106

107 108 109 110 111
  protected void addOrder(String column, String sortOrder) {
    if (orderBy==null) {
      orderBy = "";
    } else {
      orderBy = orderBy+", ";
112
    }
113
    orderBy = orderBy+column+" "+sortOrder;
114 115
  }

116 117
  public String getOrderBy() {
    return orderBy;
118
  }
T
tombaeyens 已提交
119
}