ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.pvm.internal.query;
 
import java.io.ObjectStreamException;
import java.sql.SQLException;
import java.util.List;
 
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.jbpm.api.JbpmException;
import org.jbpm.api.cmd.Command;
import org.jbpm.api.cmd.Environment;
import org.jbpm.internal.log.Log;
import org.jbpm.pvm.internal.cmd.CommandService;
import org.jbpm.pvm.internal.env.EnvironmentImpl;
 
 
/**
 * @author Tom Baeyens
 */
public abstract class AbstractQuery implements Command<Object> {
  
  private static Log log = Log.getLog(AbstractQuery.class.getName());
 
  private static final long serialVersionUID = 1L;
  
  protected CommandService commandService;
  protected String orderByClause = null;
  protected Page page = null;
  protected boolean isWhereAdded = false;
  protected boolean count = false;
  
  protected abstract void applyParameters(Query query);
 
  public abstract String hql();
 
  /* reuse by copy and paste:
   * (return type can't be changed)
  public ConcreteQuery page(int firstResult, int maxResults) {
    this.page = new Page(firstResult, maxResults);
    return this;
  } 
  */
 
  public List untypedList() {
    if (commandService!=null) {
      return (List) commandService.execute(this);
    }
    Session session = EnvironmentImpl.getFromCurrent(Session.class);
    return (List) execute(session); 
  }
 
  protected Object untypedUniqueResult() {
    List list = untypedList();
    if (list.isEmpty()) {
      return null;
    }
    if (list.size()>1) {
      throw new JbpmException("result not unique: "+list.size());
    }
    return list.get(0);
  }
 
  public Object execute(Environment environment) throws Exception {
    Session session = environment.get(Session.class);
    return execute(session);
  }
  
  public Object execute(Session session) {
    String hql = hql();
    try {
      Query query = session.createQuery(hql);
      applyParameters(query);
      applyPage(query);
      return query.list();
    } catch (HibernateException e) {
      Throwable t = e;
      while (t!=null) {
        if (t instanceof SQLException) {
          SQLException sqlException = (SQLException) t;
          SQLException nextException = sqlException.getNextException();
          if (nextException!=null) {
            log.error("cause of "+nextException+": ", e);
          }
        }
        t = t.getCause();
      }
      throw e;
    } finally {
      resetQuery(); // reset the query member fields so the query can be reused.
    }
  }
  
  /**
   * Returns the count of the query.
   * Query types that want to use this count method should
   *   - add the operation signature to their interface
   *   - use the 'count' variable in their hql() method.
   */
  public long count() {
    count = true;
    
    // Page and count cannot be used together, because paging is applied after the query is formed
    if (page != null) {
      throw new JbpmException("page(firstResult, maxResult) and count() cannot be used together");
    }
    
    return (Long) untypedUniqueResult();
  }
  
  /**
   * Resets the query so it can be reused after an invocation.
   */
  private void resetQuery() {
    isWhereAdded = false;
    count = false;
  }
 
  protected void appendWhereClause(String whereClause, StringBuilder hql) {
    if (isWhereAdded) {
      hql.append("  and ");
    } else {
      isWhereAdded = true;
      hql.append("where ");
    }
    hql.append(whereClause);
  }
 
  protected void appendOrderByClause(StringBuilder hql) {
    if (orderByClause!=null) {
      hql.append("order by ");
      hql.append(orderByClause);
    }
  }
 
  protected void applyPage(Query query) {
    if (page!=null) {
      query.setFirstResult(page.firstResult);
      query.setMaxResults(page.maxResults);
    }
  }
 
  protected void addOrderByClause(String clause) {
    if (orderByClause==null) {
      orderByClause = clause;
    } else {
      orderByClause += ", " + clause;
    }
  }
  
  protected Object writeReplace() throws ObjectStreamException {
    this.commandService = null;
    return this;
  }
 
  public void setCommandService(CommandService commandService) {
    this.commandService = commandService;
  }
}