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.CacheMode;
018    import com.liferay.portal.kernel.dao.orm.LockMode;
019    import com.liferay.portal.kernel.dao.orm.ORMException;
020    import com.liferay.portal.kernel.dao.orm.Query;
021    import com.liferay.portal.kernel.dao.orm.ScrollableResults;
022    import com.liferay.portal.kernel.util.ListUtil;
023    import com.liferay.portal.kernel.util.UnmodifiableList;
024    
025    import java.io.Serializable;
026    
027    import java.sql.Timestamp;
028    
029    import java.util.Arrays;
030    import java.util.Iterator;
031    import java.util.List;
032    
033    import org.hibernate.LockOptions;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Shuyang Zhou
038     */
039    public class QueryImpl implements Query {
040    
041            public QueryImpl(org.hibernate.Query query, boolean strictName) {
042                    _query = query;
043                    _strictName = strictName;
044    
045                    if (!_strictName) {
046                            _names = query.getNamedParameters();
047    
048                            Arrays.sort(_names);
049                    }
050            }
051    
052            public int executeUpdate() throws ORMException {
053                    try {
054                            return _query.executeUpdate();
055                    }
056                    catch (Exception e) {
057                            throw ExceptionTranslator.translate(e);
058                    }
059            }
060    
061            public Iterator<?> iterate() throws ORMException {
062                    return iterate(true);
063            }
064    
065            public Iterator<?> iterate(boolean unmodifiable) throws ORMException {
066                    try {
067                            return list(unmodifiable).iterator();
068                    }
069                    catch (Exception e) {
070                            throw ExceptionTranslator.translate(e);
071                    }
072            }
073    
074            public Object iterateNext() throws ORMException {
075                    Iterator<?> iterator = iterate(false);
076    
077                    if (iterator.hasNext()) {
078                            return iterator.next();
079                    }
080    
081                    return null;
082            }
083    
084            public List<?> list() throws ORMException {
085                    return list(false, false);
086            }
087    
088            public List<?> list(boolean unmodifiable) throws ORMException {
089                    return list(true, unmodifiable);
090            }
091    
092            public List<?> list(boolean copy, boolean unmodifiable)
093                    throws ORMException {
094    
095                    try {
096                            List<?> list = _query.list();
097    
098                            if (unmodifiable) {
099                                    list = new UnmodifiableList<Object>(list);
100                            }
101                            else if (copy) {
102                                    list = ListUtil.copy(list);
103                            }
104    
105                            return list;
106                    }
107                    catch (Exception e) {
108                            throw ExceptionTranslator.translate(e);
109                    }
110            }
111    
112            public ScrollableResults scroll() throws ORMException {
113                    try {
114                            return new ScrollableResultsImpl(_query.scroll());
115                    }
116                    catch (Exception e) {
117                            throw ExceptionTranslator.translate(e);
118                    }
119            }
120    
121            public Query setBoolean(int pos, boolean value) {
122                    _query.setBoolean(pos, value);
123    
124                    return this;
125            }
126    
127            public Query setBoolean(String name, boolean value) {
128                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
129                            return this;
130                    }
131    
132                    _query.setBoolean(name, value);
133    
134                    return this;
135            }
136    
137            public Query setCacheable(boolean cacheable) {
138                    _query.setCacheable(cacheable);
139    
140                    return this;
141            }
142    
143            public Query setCacheMode(CacheMode cacheMode) {
144                    _query.setCacheMode(CacheModeTranslator.translate(cacheMode));
145    
146                    return this;
147            }
148    
149            public Query setCacheRegion(String cacheRegion) {
150                    _query.setCacheRegion(cacheRegion);
151    
152                    return this;
153            }
154    
155            public Query setDouble(int pos, double value) {
156                    _query.setDouble(pos, value);
157    
158                    return this;
159            }
160    
161            public Query setDouble(String name, double value) {
162                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
163                            return this;
164                    }
165    
166                    _query.setDouble(name, value);
167    
168                    return this;
169            }
170    
171            public Query setFirstResult(int firstResult) {
172                    _query.setFirstResult(firstResult);
173    
174                    return this;
175            }
176    
177            public Query setFloat(int pos, float value) {
178                    _query.setFloat(pos, value);
179    
180                    return this;
181            }
182    
183            public Query setFloat(String name, float value) {
184                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
185                            return this;
186                    }
187    
188                    _query.setFloat(name, value);
189    
190                    return this;
191            }
192    
193            public Query setInteger(int pos, int value) {
194                    _query.setInteger(pos, value);
195    
196                    return this;
197            }
198    
199            public Query setInteger(String name, int value) {
200                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
201                            return this;
202                    }
203    
204                    _query.setInteger(name, value);
205    
206                    return this;
207            }
208    
209            public Query setLockMode(String alias, LockMode lockMode) {
210                    org.hibernate.LockMode hibernateLockMode = LockModeTranslator.translate(
211                            lockMode);
212    
213                    LockOptions lockOptions = new LockOptions(hibernateLockMode);
214    
215                    lockOptions.setAliasSpecificLockMode(alias, hibernateLockMode);
216    
217                    _query.setLockOptions(lockOptions);
218    
219                    return this;
220            }
221    
222            public Query setLong(int pos, long value) {
223                    _query.setLong(pos, value);
224    
225                    return this;
226            }
227    
228            public Query setLong(String name, long value) {
229                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
230                            return this;
231                    }
232    
233                    _query.setLong(name, value);
234    
235                    return this;
236            }
237    
238            public Query setMaxResults(int maxResults) {
239                    _query.setMaxResults(maxResults);
240    
241                    return this;
242            }
243    
244            public Query setSerializable(int pos, Serializable value) {
245                    _query.setSerializable(pos, value);
246    
247                    return this;
248            }
249    
250            public Query setSerializable(String name, Serializable value) {
251                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
252                            return this;
253                    }
254    
255                    _query.setSerializable(name, value);
256    
257                    return this;
258            }
259    
260            public Query setShort(int pos, short value) {
261                    _query.setShort(pos, value);
262    
263                    return this;
264            }
265    
266            public Query setShort(String name, short value) {
267                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
268                            return this;
269                    }
270    
271                    _query.setShort(name, value);
272    
273                    return this;
274            }
275    
276            public Query setString(int pos, String value) {
277                    _query.setString(pos, value);
278    
279                    return this;
280            }
281    
282            public Query setString(String name, String value) {
283                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
284                            return this;
285                    }
286    
287                    _query.setString(name, value);
288    
289                    return this;
290            }
291    
292            public Query setTimestamp(int pos, Timestamp value) {
293                    _query.setTimestamp(pos, value);
294    
295                    return this;
296            }
297    
298            public Query setTimestamp(String name, Timestamp value) {
299                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
300                            return this;
301                    }
302    
303                    _query.setTimestamp(name, value);
304    
305                    return this;
306            }
307    
308            public Object uniqueResult() throws ORMException {
309                    try {
310                            return _query.uniqueResult();
311                    }
312                    catch (Exception e) {
313                            throw ExceptionTranslator.translate(e);
314                    }
315            }
316    
317            private String[] _names;
318            private org.hibernate.Query _query;
319            private boolean _strictName;
320    
321    }