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