001    /**
002     * Copyright (c) 2000-2010 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.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    /**
032     * @author Brian Wing Shun Chan
033     */
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    }