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