001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    
026    import java.util.Collections;
027    import java.util.List;
028    
029    import org.hibernate.Criteria;
030    import org.hibernate.criterion.DetachedCriteria;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
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 = QueryUtil.ALL_POS;
071    
072                    if (_start != null) {
073                            start = _start.intValue();
074                    }
075    
076                    int end = QueryUtil.ALL_POS;
077    
078                    if (_end != null) {
079                            end = _end.intValue();
080                    }
081    
082                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
083                            return;
084                    }
085                    else if ((start < QueryUtil.ALL_POS) && (end < QueryUtil.ALL_POS)) {
086                            _criteria = _criteria.setFirstResult(0);
087                            _criteria = _criteria.setMaxResults(0);
088                    }
089    
090                    if (start < 0) {
091                            start = 0;
092                    }
093    
094                    if (start <= end) {
095                            end = end - start;
096                    }
097                    else if (((start > end) && (end > 0)) || (end < QueryUtil.ALL_POS)) {
098                            end = 0;
099                    }
100    
101                    _criteria = _criteria.setFirstResult(start);
102    
103                    if (end >= 0) {
104                            _criteria = _criteria.setMaxResults(end);
105                    }
106            }
107    
108            public DetachedCriteria getDetachedCriteria() {
109                    return _detachedCriteria;
110            }
111    
112            @Override
113            @SuppressWarnings("rawtypes")
114            public List list() {
115                    return list(true);
116            }
117    
118            @Override
119            @SuppressWarnings("rawtypes")
120            public List list(boolean unmodifiable) {
121                    List list = _criteria.list();
122    
123                    if (unmodifiable) {
124                            return Collections.unmodifiableList(list);
125                    }
126                    else {
127                            return ListUtil.copy(list);
128                    }
129            }
130    
131            @Override
132            public void setLimit(int start, int end) {
133                    _start = Integer.valueOf(start);
134                    _end = Integer.valueOf(end);
135            }
136    
137            @Override
138            public DynamicQuery setProjection(Projection projection) {
139                    return setProjection(projection, true);
140            }
141    
142            @Override
143            public DynamicQuery setProjection(
144                    Projection projection, boolean useColumnAlias) {
145    
146                    if (!useColumnAlias) {
147                            projection = ProjectionFactoryUtil.sqlProjection(
148                                    _detachedCriteria.getAlias() + "_." + projection.toString(),
149                                    null, null);
150                    }
151    
152                    ProjectionImpl projectionImpl = (ProjectionImpl)projection;
153    
154                    _detachedCriteria.setProjection(projectionImpl.getWrappedProjection());
155    
156                    return this;
157            }
158    
159            @Override
160            public String toString() {
161                    if (_criteria != null) {
162                            return _criteria.toString();
163                    }
164    
165                    return super.toString();
166            }
167    
168            private Criteria _criteria;
169            private final DetachedCriteria _detachedCriteria;
170            private Integer _end;
171            private Integer _start;
172    
173    }