1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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  
31  import java.util.List;
32  
33  import org.hibernate.Criteria;
34  import org.hibernate.criterion.DetachedCriteria;
35  
36  /**
37   * <a href="DynamicQueryImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class DynamicQueryImpl implements DynamicQuery {
43  
44      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
45          _detachedCriteria = detachedCriteria;
46      }
47  
48      public DynamicQuery add(Criterion criterion) {
49          CriterionImpl criterionImpl = (CriterionImpl)criterion;
50  
51          _detachedCriteria.add(criterionImpl.getCriterion());
52  
53          return this;
54      }
55  
56      public DynamicQuery addOrder(Order order) {
57          OrderImpl orderImpl = (OrderImpl)order;
58  
59          _detachedCriteria.addOrder(orderImpl.getOrder());
60  
61          return this;
62      }
63  
64      public void compile(Session session) {
65          SessionImpl sessionImpl = (SessionImpl)session;
66  
67          org.hibernate.Session hibernateSession = sessionImpl.getSession();
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 _criteria.list();
89      }
90  
91      public void setLimit(int start, int end) {
92          _start = Integer.valueOf(start);
93          _end = Integer.valueOf(end);
94      }
95  
96      public DynamicQuery setProjection(Projection projection) {
97          ProjectionImpl projectionImpl = (ProjectionImpl)projection;
98  
99          _detachedCriteria.setProjection(projectionImpl.getProjection());
100 
101         return this;
102     }
103 
104     private DetachedCriteria _detachedCriteria;
105     private Criteria _criteria;
106     private Integer _start;
107     private Integer _end;
108 
109 }