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