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