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.ProjectionFactoryUtil;
022 import com.liferay.portal.kernel.dao.orm.QueryUtil;
023 import com.liferay.portal.kernel.dao.orm.Session;
024 import com.liferay.portal.kernel.util.ListUtil;
025 import com.liferay.portal.kernel.util.UnmodifiableList;
026
027 import java.util.List;
028
029 import org.hibernate.Criteria;
030 import org.hibernate.criterion.DetachedCriteria;
031
032
035 public class DynamicQueryImpl implements DynamicQuery {
036
037 public DynamicQueryImpl(DetachedCriteria detachedCriteria) {
038 _detachedCriteria = detachedCriteria;
039 }
040
041 @Override
042 public DynamicQuery add(Criterion criterion) {
043 CriterionImpl criterionImpl = (CriterionImpl)criterion;
044
045 _detachedCriteria.add(criterionImpl.getWrappedCriterion());
046
047 return this;
048 }
049
050 @Override
051 public DynamicQuery addOrder(Order order) {
052 OrderImpl orderImpl = (OrderImpl)order;
053
054 _detachedCriteria.addOrder(orderImpl.getWrappedOrder());
055
056 return this;
057 }
058
059 @Override
060 public void compile(Session session) {
061 org.hibernate.Session hibernateSession =
062 (org.hibernate.Session)session.getWrappedSession();
063
064 _criteria = _detachedCriteria.getExecutableCriteria(hibernateSession);
065
066 if ((_start == null) || (_end == null)) {
067 return;
068 }
069
070 int start = _start.intValue();
071 int end = _end.intValue();
072
073 if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
074 return;
075 }
076
077 _criteria = _criteria.setFirstResult(start);
078 _criteria = _criteria.setMaxResults(end - start);
079 }
080
081 public DetachedCriteria getDetachedCriteria() {
082 return _detachedCriteria;
083 }
084
085 @Override
086 @SuppressWarnings("rawtypes")
087 public List list() {
088 return list(true);
089 }
090
091 @Override
092 @SuppressWarnings("rawtypes")
093 public List list(boolean unmodifiable) {
094 List list = _criteria.list();
095
096 if (unmodifiable) {
097 return new UnmodifiableList(list);
098 }
099 else {
100 return ListUtil.copy(list);
101 }
102 }
103
104 @Override
105 public void setLimit(int start, int end) {
106 _start = Integer.valueOf(start);
107 _end = Integer.valueOf(end);
108 }
109
110 @Override
111 public DynamicQuery setProjection(Projection projection) {
112 return setProjection(projection, true);
113 }
114
115 @Override
116 public DynamicQuery setProjection(
117 Projection projection, boolean useColumnAlias) {
118
119 if (!useColumnAlias) {
120 projection = ProjectionFactoryUtil.sqlProjection(
121 _detachedCriteria.getAlias() + "_." + projection.toString(),
122 null, null);
123 }
124
125 ProjectionImpl projectionImpl = (ProjectionImpl)projection;
126
127 _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
128
129 return this;
130 }
131
132 private Criteria _criteria;
133 private DetachedCriteria _detachedCriteria;
134 private Integer _end;
135 private Integer _start;
136
137 }