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 List<?> list() throws ORMException {
075                    return list(false, false);
076            }
077    
078            public List<?> list(boolean unmodifiable) throws ORMException {
079                    return list(true, unmodifiable);
080            }
081    
082            public List<?> list(boolean copy, boolean unmodifiable)
083                    throws ORMException {
084    
085                    try {
086                            List<?> list = _query.list();
087    
088                            if (unmodifiable) {
089                                    list = new UnmodifiableList<Object>(list);
090                            }
091                            else if (copy) {
092                                    list = ListUtil.copy(list);
093                            }
094    
095                            return list;
096                    }
097                    catch (Exception e) {
098                            throw ExceptionTranslator.translate(e);
099                    }
100            }
101    
102            public ScrollableResults scroll() throws ORMException {
103                    try {
104                            return new ScrollableResultsImpl(_query.scroll());
105                    }
106                    catch (Exception e) {
107                            throw ExceptionTranslator.translate(e);
108                    }
109            }
110    
111            public Query setBoolean(int pos, boolean value) {
112                    _query.setBoolean(pos, value);
113    
114                    return this;
115            }
116    
117            public Query setBoolean(String name, boolean value) {
118                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
119                            return this;
120                    }
121    
122                    _query.setBoolean(name, value);
123    
124                    return this;
125            }
126    
127            public Query setCacheable(boolean cacheable) {
128                    _query.setCacheable(cacheable);
129    
130                    return this;
131            }
132    
133            public Query setCacheMode(CacheMode cacheMode) {
134                    _query.setCacheMode(CacheModeTranslator.translate(cacheMode));
135    
136                    return this;
137            }
138    
139            public Query setCacheRegion(String cacheRegion) {
140                    _query.setCacheRegion(cacheRegion);
141    
142                    return this;
143            }
144    
145            public Query setDouble(int pos, double value) {
146                    _query.setDouble(pos, value);
147    
148                    return this;
149            }
150    
151            public Query setDouble(String name, double value) {
152                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
153                            return this;
154                    }
155    
156                    _query.setDouble(name, value);
157    
158                    return this;
159            }
160    
161            public Query setFirstResult(int firstResult) {
162                    _query.setFirstResult(firstResult);
163    
164                    return this;
165            }
166    
167            public Query setFloat(int pos, float value) {
168                    _query.setFloat(pos, value);
169    
170                    return this;
171            }
172    
173            public Query setFloat(String name, float value) {
174                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
175                            return this;
176                    }
177    
178                    _query.setFloat(name, value);
179    
180                    return this;
181            }
182    
183            public Query setInteger(int pos, int value) {
184                    _query.setInteger(pos, value);
185    
186                    return this;
187            }
188    
189            public Query setInteger(String name, int value) {
190                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
191                            return this;
192                    }
193    
194                    _query.setInteger(name, value);
195    
196                    return this;
197            }
198    
199            public Query setLockMode(String alias, LockMode lockMode) {
200                    org.hibernate.LockMode hibernateLockMode = LockModeTranslator.translate(
201                            lockMode);
202    
203                    LockOptions lockOptions = new LockOptions(hibernateLockMode);
204    
205                    lockOptions.setAliasSpecificLockMode(alias, hibernateLockMode);
206    
207                    _query.setLockOptions(lockOptions);
208    
209                    return this;
210            }
211    
212            public Query setLong(int pos, long value) {
213                    _query.setLong(pos, value);
214    
215                    return this;
216            }
217    
218            public Query setLong(String name, long value) {
219                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
220                            return this;
221                    }
222    
223                    _query.setLong(name, value);
224    
225                    return this;
226            }
227    
228            public Query setMaxResults(int maxResults) {
229                    _query.setMaxResults(maxResults);
230    
231                    return this;
232            }
233    
234            public Query setSerializable(int pos, Serializable value) {
235                    _query.setSerializable(pos, value);
236    
237                    return this;
238            }
239    
240            public Query setSerializable(String name, Serializable value) {
241                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
242                            return this;
243                    }
244    
245                    _query.setSerializable(name, value);
246    
247                    return this;
248            }
249    
250            public Query setShort(int pos, short value) {
251                    _query.setShort(pos, value);
252    
253                    return this;
254            }
255    
256            public Query setShort(String name, short value) {
257                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
258                            return this;
259                    }
260    
261                    _query.setShort(name, value);
262    
263                    return this;
264            }
265    
266            public Query setString(int pos, String value) {
267                    _query.setString(pos, value);
268    
269                    return this;
270            }
271    
272            public Query setString(String name, String value) {
273                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
274                            return this;
275                    }
276    
277                    _query.setString(name, value);
278    
279                    return this;
280            }
281    
282            public Query setTimestamp(int pos, Timestamp value) {
283                    _query.setTimestamp(pos, value);
284    
285                    return this;
286            }
287    
288            public Query setTimestamp(String name, Timestamp value) {
289                    if (!_strictName && (Arrays.binarySearch(_names, name) < 0)) {
290                            return this;
291                    }
292    
293                    _query.setTimestamp(name, value);
294    
295                    return this;
296            }
297    
298            public Object uniqueResult() throws ORMException {
299                    try {
300                            return _query.uniqueResult();
301                    }
302                    catch (Exception e) {
303                            throw ExceptionTranslator.translate(e);
304                    }
305            }
306    
307            private String[] _names;
308            private org.hibernate.Query _query;
309            private boolean _strictName;
310    
311    }