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   */
44  public class DynamicQueryImpl implements DynamicQuery {
45  
46      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
47          _detachedCriteria = detachedCriteria;
48      }
49  
50      public DynamicQuery add(Criterion criterion) {
51          CriterionImpl criterionImpl = (CriterionImpl)criterion;
52  
53          _detachedCriteria.add(criterionImpl.getWrappedCriterion());
54  
55          return this;
56      }
57  
58      public DynamicQuery addOrder(Order order) {
59          OrderImpl orderImpl = (OrderImpl)order;
60  
61          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
62  
63          return this;
64      }
65  
66      public void compile(Session session) {
67          SessionImpl sessionImpl = (SessionImpl)session;
68  
69          org.hibernate.Session hibernateSession =
70              sessionImpl.getWrappedSession();
71  
72          if (hibernateSession instanceof LiferaySession) {
73              hibernateSession =
74                  ((LiferaySession)hibernateSession).getHibernateSession();
75          }
76  
77          _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
78  
79          if ((_start != null) && (_end != null)) {
80              _criteria = _criteria.setFirstResult(_start.intValue());
81              _criteria = _criteria.setMaxResults(
82                  _end.intValue() - _start.intValue());
83          }
84      }
85  
86      public DetachedCriteria getDetachedCriteria() {
87          return _detachedCriteria;
88      }
89  
90      public List list() {
91          return list(true);
92      }
93  
94      public List list(boolean unmodifiable) {
95          List list = _criteria.list();
96  
97          if (unmodifiable) {
98              return new UnmodifiableList(list);
99          }
100         else {
101             return ListUtil.copy(list);
102         }
103     }
104 
105     public void setLimit(int start, int end) {
106         _start = Integer.valueOf(start);
107         _end = Integer.valueOf(end);
108     }
109 
110     public DynamicQuery setProjection(Projection projection) {
111         ProjectionImpl projectionImpl = (ProjectionImpl)projection;
112 
113         _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
114 
115         return this;
116     }
117 
118     private DetachedCriteria _detachedCriteria;
119     private Criteria _criteria;
120     private Integer _start;
121     private Integer _end;
122 
123 }