1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.kernel.dao.orm.Criterion;
18  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
19  import com.liferay.portal.kernel.dao.orm.Order;
20  import com.liferay.portal.kernel.dao.orm.Projection;
21  import com.liferay.portal.kernel.dao.orm.QueryUtil;
22  import com.liferay.portal.kernel.dao.orm.Session;
23  import com.liferay.portal.kernel.util.ListUtil;
24  import com.liferay.portal.kernel.util.UnmodifiableList;
25  
26  import java.util.List;
27  
28  import org.hibernate.Criteria;
29  import org.hibernate.criterion.DetachedCriteria;
30  
31  /**
32   * <a href="DynamicQueryImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class DynamicQueryImpl implements DynamicQuery {
37  
38      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
39          _detachedCriteria = detachedCriteria;
40      }
41  
42      public DynamicQuery add(Criterion criterion) {
43          CriterionImpl criterionImpl = (CriterionImpl)criterion;
44  
45          _detachedCriteria.add(criterionImpl.getWrappedCriterion());
46  
47          return this;
48      }
49  
50      public DynamicQuery addOrder(Order order) {
51          OrderImpl orderImpl = (OrderImpl)order;
52  
53          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
54  
55          return this;
56      }
57  
58      public void compile(Session session) {
59          org.hibernate.Session hibernateSession =
60              (org.hibernate.Session)session.getWrappedSession();
61  
62          _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
63  
64          if ((_start == null) || (_end == null)) {
65              return;
66          }
67  
68          int start = _start.intValue();
69          int end = _end.intValue();
70  
71          if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
72              return;
73          }
74  
75          _criteria = _criteria.setFirstResult(start);
76          _criteria = _criteria.setMaxResults(end - start);
77      }
78  
79      public DetachedCriteria getDetachedCriteria() {
80          return _detachedCriteria;
81      }
82  
83      @SuppressWarnings("unchecked")
84      public List list() {
85          return list(true);
86      }
87  
88      @SuppressWarnings("unchecked")
89      public List list(boolean unmodifiable) {
90          List list = _criteria.list();
91  
92          if (unmodifiable) {
93              return new UnmodifiableList(list);
94          }
95          else {
96              return ListUtil.copy(list);
97          }
98      }
99  
100     public void setLimit(int start, int end) {
101         _start = Integer.valueOf(start);
102         _end = Integer.valueOf(end);
103     }
104 
105     public DynamicQuery setProjection(Projection projection) {
106         ProjectionImpl projectionImpl = (ProjectionImpl)projection;
107 
108         _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
109 
110         return this;
111     }
112 
113     private DetachedCriteria _detachedCriteria;
114     private Criteria _criteria;
115     private Integer _start;
116     private Integer _end;
117 
118 }