1   /**
2    * Copyright (c) 2000-2009 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  import com.liferay.portal.kernel.util.ListUtil;
31  import com.liferay.portal.kernel.util.UnmodifiableList;
32  
33  import java.util.List;
34  
35  import org.hibernate.Criteria;
36  import org.hibernate.criterion.DetachedCriteria;
37  
38  /**
39   * <a href="DynamicQueryImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class DynamicQueryImpl implements DynamicQuery {
44  
45      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
46          _detachedCriteria = detachedCriteria;
47      }
48  
49      public DynamicQuery add(Criterion criterion) {
50          CriterionImpl criterionImpl = (CriterionImpl)criterion;
51  
52          _detachedCriteria.add(criterionImpl.getWrappedCriterion());
53  
54          return this;
55      }
56  
57      public DynamicQuery addOrder(Order order) {
58          OrderImpl orderImpl = (OrderImpl)order;
59  
60          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
61  
62          return this;
63      }
64  
65      public void compile(Session session) {
66  
67          org.hibernate.Session hibernateSession =
68              (org.hibernate.Session)session.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 list(true);
90      }
91  
92      public List list(boolean unmodifiable) {
93          List list = _criteria.list();
94  
95          if (unmodifiable) {
96              return new UnmodifiableList(list);
97          }
98          else {
99              return ListUtil.copy(list);
100         }
101     }
102 
103     public void setLimit(int start, int end) {
104         _start = Integer.valueOf(start);
105         _end = Integer.valueOf(end);
106     }
107 
108     public DynamicQuery setProjection(Projection projection) {
109         ProjectionImpl projectionImpl = (ProjectionImpl)projection;
110 
111         _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
112 
113         return this;
114     }
115 
116     private DetachedCriteria _detachedCriteria;
117     private Criteria _criteria;
118     private Integer _start;
119     private Integer _end;
120 
121 }