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