001
014
015 package com.liferay.portal.dao.orm.hibernate;
016
017 import com.liferay.portal.kernel.dao.orm.Criterion;
018 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019 import com.liferay.portal.kernel.dao.orm.Order;
020 import com.liferay.portal.kernel.dao.orm.Projection;
021 import com.liferay.portal.kernel.dao.orm.QueryUtil;
022 import com.liferay.portal.kernel.dao.orm.Session;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.UnmodifiableList;
025
026 import java.util.List;
027
028 import org.hibernate.Criteria;
029 import org.hibernate.criterion.DetachedCriteria;
030
031
034 public class DynamicQueryImpl implements DynamicQuery {
035
036 public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
037 _detachedCriteria = detachedCriteria;
038 }
039
040 public DynamicQuery add(Criterion criterion) {
041 CriterionImpl criterionImpl = (CriterionImpl)criterion;
042
043 _detachedCriteria.add(criterionImpl.getWrappedCriterion());
044
045 return this;
046 }
047
048 public DynamicQuery addOrder(Order order) {
049 OrderImpl orderImpl = (OrderImpl)order;
050
051 _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
052
053 return this;
054 }
055
056 public void compile(Session session) {
057 org.hibernate.Session hibernateSession =
058 (org.hibernate.Session)session.getWrappedSession();
059
060 _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
061
062 if ((_start == null) || (_end == null)) {
063 return;
064 }
065
066 int start = _start.intValue();
067 int end = _end.intValue();
068
069 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
070 return;
071 }
072
073 _criteria = _criteria.setFirstResult(start);
074 _criteria = _criteria.setMaxResults(end - start);
075 }
076
077 public DetachedCriteria getDetachedCriteria() {
078 return _detachedCriteria;
079 }
080
081 @SuppressWarnings("rawtypes")
082 public List list() {
083 return list(true);
084 }
085
086 @SuppressWarnings("rawtypes")
087 public List list(boolean unmodifiable) {
088 List list = _criteria.list();
089
090 if (unmodifiable) {
091 return new UnmodifiableList(list);
092 }
093 else {
094 return ListUtil.copy(list);
095 }
096 }
097
098 public void setLimit(int start, int end) {
099 _start = Integer.valueOf(start);
100 _end = Integer.valueOf(end);
101 }
102
103 public DynamicQuery setProjection(Projection projection) {
104 ProjectionImpl projectionImpl = (ProjectionImpl)projection;
105
106 _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
107
108 return this;
109 }
110
111 private DetachedCriteria _detachedCriteria;
112 private Criteria _criteria;
113 private Integer _start;
114 private Integer _end;
115
116 }