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.CacheMode;
018    import com.liferay.portal.kernel.dao.orm.ORMException;
019    import com.liferay.portal.kernel.dao.orm.Query;
020    import com.liferay.portal.kernel.dao.orm.ScrollableResults;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.UnmodifiableList;
023    
024    import java.io.Serializable;
025    
026    import java.sql.Timestamp;
027    
028    import java.util.Iterator;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class QueryImpl implements Query {
035    
036            public QueryImpl(org.hibernate.Query query) {
037                    _query = query;
038            }
039    
040            public int executeUpdate() throws ORMException {
041                    try {
042                            return _query.executeUpdate();
043                    }
044                    catch (Exception e) {
045                            throw ExceptionTranslator.translate(e);
046                    }
047            }
048    
049            public Iterator<?> iterate() throws ORMException {
050                    return iterate(true);
051            }
052    
053            public Iterator<?> iterate(boolean unmodifiable) throws ORMException {
054                    try {
055                            return list(unmodifiable).iterator();
056                    }
057                    catch (Exception e) {
058                            throw ExceptionTranslator.translate(e);
059                    }
060            }
061    
062            public List<?> list() throws ORMException {
063                    return list(true);
064            }
065    
066            public List<?> list(boolean unmodifiable) throws ORMException {
067                    try {
068                            List<?> list = _query.list();
069    
070                            if (unmodifiable) {
071                                    return new UnmodifiableList<Object>(list);
072                            }
073                            else {
074                                    return ListUtil.copy(list);
075                            }
076                    }
077                    catch (Exception e) {
078                            throw ExceptionTranslator.translate(e);
079                    }
080            }
081    
082            public ScrollableResults scroll() throws ORMException {
083                    try {
084                            return new ScrollableResultsImpl(_query.scroll());
085                    }
086                    catch (Exception e) {
087                            throw ExceptionTranslator.translate(e);
088                    }
089            }
090    
091            public Query setBoolean(int pos, boolean value) {
092                    _query.setBoolean(pos, value);
093    
094                    return this;
095            }
096    
097            public Query setCacheable(boolean cacheable) {
098                    _query.setCacheable(cacheable);
099    
100                    return this;
101            }
102    
103            public Query setCacheMode(CacheMode cacheMode) {
104                    _query.setCacheMode(CacheModeTranslator.translate(cacheMode));
105    
106                    return this;
107            }
108    
109            public Query setCacheRegion(String cacheRegion) {
110                    _query.setCacheRegion(cacheRegion);
111    
112                    return this;
113            }
114    
115            public Query setDouble(int pos, double value) {
116                    _query.setDouble(pos, value);
117    
118                    return this;
119            }
120    
121            public Query setFirstResult(int firstResult) {
122                    _query.setFirstResult(firstResult);
123    
124                    return this;
125            }
126    
127            public Query setFloat(int pos, float value) {
128                    _query.setFloat(pos, value);
129    
130                    return this;
131            }
132    
133            public Query setInteger(int pos, int value) {
134                    _query.setInteger(pos, value);
135    
136                    return this;
137            }
138    
139            public Query setLong(int pos, long value) {
140                    _query.setLong(pos, value);
141    
142                    return this;
143            }
144    
145            public Query setMaxResults(int maxResults) {
146                    _query.setMaxResults(maxResults);
147    
148                    return this;
149            }
150    
151            public Query setSerializable(int pos, Serializable value) {
152                    _query.setSerializable(pos, value);
153    
154                    return this;
155            }
156    
157            public Query setShort(int pos, short value) {
158                    _query.setShort(pos, value);
159    
160                    return this;
161            }
162    
163            public Query setString(int pos, String value) {
164                    _query.setString(pos, value);
165    
166                    return this;
167            }
168    
169            public Query setTimestamp(int pos, Timestamp value) {
170                    _query.setTimestamp(pos, value);
171    
172                    return this;
173            }
174    
175            public Object uniqueResult() throws ORMException {
176                    try {
177                            return _query.uniqueResult();
178                    }
179                    catch (Exception e) {
180                            throw ExceptionTranslator.translate(e);
181                    }
182            }
183    
184            private org.hibernate.Query _query;
185    
186    }