1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.dao.orm.hibernate;
21  
22  import com.liferay.portal.kernel.dao.orm.Criterion;
23  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
24  import com.liferay.portal.kernel.dao.orm.Order;
25  import com.liferay.portal.kernel.dao.orm.Projection;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.kernel.util.UnmodifiableList;
29  
30  import java.util.List;
31  
32  import org.hibernate.Criteria;
33  import org.hibernate.criterion.DetachedCriteria;
34  
35  /**
36   * <a href="DynamicQueryImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class DynamicQueryImpl implements DynamicQuery {
42  
43      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
44          _detachedCriteria = detachedCriteria;
45      }
46  
47      public DynamicQuery add(Criterion criterion) {
48          CriterionImpl criterionImpl = (CriterionImpl)criterion;
49  
50          _detachedCriteria.add(criterionImpl.getWrappedCriterion());
51  
52          return this;
53      }
54  
55      public DynamicQuery addOrder(Order order) {
56          OrderImpl orderImpl = (OrderImpl)order;
57  
58          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
59  
60          return this;
61      }
62  
63      public void compile(Session session) {
64          SessionImpl sessionImpl = (SessionImpl)session;
65  
66          org.hibernate.Session hibernateSession =
67              sessionImpl.getWrappedSession();
68  
69          if (hibernateSession instanceof LiferaySession) {
70              hibernateSession =
71                  ((LiferaySession)hibernateSession).getHibernateSession();
72          }
73  
74          _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
75  
76          if ((_start != null) && (_end != null)) {
77              _criteria = _criteria.setFirstResult(_start.intValue());
78              _criteria = _criteria.setMaxResults(
79                  _end.intValue() - _start.intValue());
80          }
81      }
82  
83      public DetachedCriteria getDetachedCriteria() {
84          return _detachedCriteria;
85      }
86  
87      public List list() {
88          return list(true);
89      }
90  
91      public List list(boolean unmodifiable) {
92          List list = _criteria.list();
93  
94          if (unmodifiable) {
95              return new UnmodifiableList(list);
96          }
97          else {
98              return ListUtil.copy(list);
99          }
100     }
101 
102     public void setLimit(int start, int end) {
103         _start = Integer.valueOf(start);
104         _end = Integer.valueOf(end);
105     }
106 
107     public DynamicQuery setProjection(Projection projection) {
108         ProjectionImpl projectionImpl = (ProjectionImpl)projection;
109 
110         _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
111 
112         return this;
113     }
114 
115     private DetachedCriteria _detachedCriteria;
116     private Criteria _criteria;
117     private Integer _start;
118     private Integer _end;
119 
120 }