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