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