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.getWrappedCriterion());
52  
53          return this;
54      }
55  
56      public DynamicQuery addOrder(Order order) {
57          OrderImpl orderImpl = (OrderImpl)order;
58  
59          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
60  
61          return this;
62      }
63  
64      public void compile(Session session) {
65          SessionImpl sessionImpl = (SessionImpl)session;
66  
67          org.hibernate.Session hibernateSession =
68              sessionImpl.getWrappedSession();
69  
70          if (hibernateSession instanceof LiferaySession) {
71              hibernateSession =
72                  ((LiferaySession)hibernateSession).getHibernateSession();
73          }
74  
75          _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
76  
77          if ((_start != null) && (_end != null)) {
78              _criteria = _criteria.setFirstResult(_start.intValue());
79              _criteria = _criteria.setMaxResults(
80                  _end.intValue() - _start.intValue());
81          }
82      }
83  
84      public DetachedCriteria getDetachedCriteria() {
85          return _detachedCriteria;
86      }
87  
88      public List list() {
89          return _criteria.list();
90      }
91  
92      public void setLimit(int start, int end) {
93          _start = Integer.valueOf(start);
94          _end = Integer.valueOf(end);
95      }
96  
97      public DynamicQuery setProjection(Projection projection) {
98          ProjectionImpl projectionImpl = (ProjectionImpl)projection;
99  
100         _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
101 
102         return this;
103     }
104 
105     private DetachedCriteria _detachedCriteria;
106     private Criteria _criteria;
107     private Integer _start;
108     private Integer _end;
109 
110 }