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.Session;
22  import com.liferay.portal.kernel.util.ListUtil;
23  import com.liferay.portal.kernel.util.UnmodifiableList;
24  import com.liferay.portal.spring.hibernate.SessionInvocationHandler;
25  
26  import java.lang.reflect.Proxy;
27  
28  import java.util.List;
29  
30  import org.hibernate.Criteria;
31  import org.hibernate.criterion.DetachedCriteria;
32  
33  /**
34   * <a href="DynamicQueryImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class DynamicQueryImpl implements DynamicQuery {
39  
40      public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
41          _detachedCriteria = detachedCriteria;
42      }
43  
44      public DynamicQuery add(Criterion criterion) {
45          CriterionImpl criterionImpl = (CriterionImpl)criterion;
46  
47          _detachedCriteria.add(criterionImpl.getWrappedCriterion());
48  
49          return this;
50      }
51  
52      public DynamicQuery addOrder(Order order) {
53          OrderImpl orderImpl = (OrderImpl)order;
54  
55          _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
56  
57          return this;
58      }
59  
60      public void compile(Session session) {
61          org.hibernate.Session hibernateSession =
62              (org.hibernate.Session)session.getWrappedSession();
63  
64          SessionInvocationHandler sessionInvocationHandler =
65              (SessionInvocationHandler)Proxy.getInvocationHandler(
66                  hibernateSession);
67  
68          hibernateSession = sessionInvocationHandler.getSession();
69  
70          _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
71  
72          if ((_start != null) && (_end != null)) {
73              _criteria = _criteria.setFirstResult(_start.intValue());
74              _criteria = _criteria.setMaxResults(
75                  _end.intValue() - _start.intValue());
76          }
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 }