1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.dao.orm.hibernate;
24  
25  import com.liferay.portal.kernel.dao.orm.Criterion;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.Order;
28  import com.liferay.portal.kernel.dao.orm.Projection;
29  import com.liferay.portal.kernel.dao.orm.Session;
30  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.kernel.util.UnmodifiableList;
32  import com.liferay.portal.spring.hibernate.SessionInvocationHandler;
33  
34  import java.lang.reflect.Proxy;
35  
36  import java.util.List;
37  
38  import org.hibernate.Criteria;
39  import org.hibernate.criterion.DetachedCriteria;
40  
41  /**
42   * <a href="DynamicQueryImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class DynamicQueryImpl implements DynamicQuery {
47  
48      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
49          _detachedCriteria = detachedCriteria;
50      }
51  
52      public DynamicQuery add(Criterion criterion) {
53          CriterionImpl criterionImpl = (CriterionImpl)criterion;
54  
55          _detachedCriteria.add(criterionImpl.getWrappedCriterion());
56  
57          return this;
58      }
59  
60      public DynamicQuery addOrder(Order order) {
61          OrderImpl orderImpl = (OrderImpl)order;
62  
63          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
64  
65          return this;
66      }
67  
68      public void compile(Session session) {
69          org.hibernate.Session hibernateSession =
70              (org.hibernate.Session)session.getWrappedSession();
71  
72          SessionInvocationHandler sessionInvocationHandler =
73              (SessionInvocationHandler)Proxy.getInvocationHandler(
74                  hibernateSession);
75  
76          hibernateSession = sessionInvocationHandler.getSession();
77  
78          _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
79  
80          if ((_start != null) && (_end != null)) {
81              _criteria = _criteria.setFirstResult(_start.intValue());
82              _criteria = _criteria.setMaxResults(
83                  _end.intValue() - _start.intValue());
84          }
85      }
86  
87      public DetachedCriteria getDetachedCriteria() {
88          return _detachedCriteria;
89      }
90  
91      @SuppressWarnings("unchecked")
92      public List list() {
93          return list(true);
94      }
95  
96      @SuppressWarnings("unchecked")
97      public List list(boolean unmodifiable) {
98          List list = _criteria.list();
99  
100         if (unmodifiable) {
101             return new UnmodifiableList(list);
102         }
103         else {
104             return ListUtil.copy(list);
105         }
106     }
107 
108     public void setLimit(int start, int end) {
109         _start = Integer.valueOf(start);
110         _end = Integer.valueOf(end);
111     }
112 
113     public DynamicQuery setProjection(Projection projection) {
114         ProjectionImpl projectionImpl = (ProjectionImpl)projection;
115 
116         _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
117 
118         return this;
119     }
120 
121     private DetachedCriteria _detachedCriteria;
122     private Criteria _criteria;
123     private Integer _start;
124     private Integer _end;
125 
126 }