001
014
015 package com.liferay.portal.dao.orm.jpa;
016
017 import com.liferay.portal.dao.orm.common.SQLTransformer;
018 import com.liferay.portal.kernel.dao.orm.CacheMode;
019 import com.liferay.portal.kernel.dao.orm.LockMode;
020 import com.liferay.portal.kernel.dao.orm.ORMException;
021 import com.liferay.portal.kernel.dao.orm.Query;
022 import com.liferay.portal.kernel.dao.orm.ScrollableResults;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.UnmodifiableList;
025
026 import java.io.Serializable;
027
028 import java.sql.Timestamp;
029
030 import java.util.Date;
031 import java.util.HashMap;
032 import java.util.Iterator;
033 import java.util.List;
034 import java.util.Map;
035
036 import javax.persistence.FlushModeType;
037 import javax.persistence.LockModeType;
038
039
044 public class QueryImpl implements Query {
045
046 public QueryImpl(
047 SessionImpl sessionImpl, String queryString, boolean strictName) {
048
049 this.sessionImpl = sessionImpl;
050 this.queryString = SQLTransformer.transformFromHqlToJpql(queryString);
051 this.strictName = strictName;
052 }
053
054 public int executeUpdate() throws ORMException {
055 try {
056 return sessionImpl.executeUpdate(
057 queryString, positionalParameterMap, namedParameterMap,
058 strictName, firstResult, maxResults, flushModeType,
059 lockModeType, sqlQuery, entityClass);
060 }
061 catch (Exception e) {
062 throw ExceptionTranslator.translate(e);
063 }
064 }
065
066 public Iterator<?> iterate() throws ORMException {
067 return iterate(true);
068 }
069
070 public Iterator<?> iterate(boolean unmodifiable) throws ORMException {
071 try {
072 return list(unmodifiable).iterator();
073 }
074 catch (Exception e) {
075 throw ExceptionTranslator.translate(e);
076 }
077 }
078
079 public Object iterateNext() throws ORMException {
080 Iterator<?> iterator = iterate(false);
081
082 if (iterator.hasNext()) {
083 return iterator.next();
084 }
085
086 return null;
087 }
088
089 public List<?> list() throws ORMException {
090 return list(false, false);
091 }
092
093 public List<?> list(boolean unmodifiable) throws ORMException {
094 return list(true, unmodifiable);
095 }
096
097 public List<?> list(boolean copy, boolean unmodifiable)
098 throws ORMException {
099
100 try {
101 List<?> list = sessionImpl.list(
102 queryString, positionalParameterMap, namedParameterMap,
103 strictName, firstResult, maxResults, flushModeType,
104 lockModeType, sqlQuery, entityClass);
105
106 if (unmodifiable) {
107 list = new UnmodifiableList<Object>(list);
108 }
109 else if (copy) {
110 list = ListUtil.copy(list);
111 }
112
113 return list;
114 }
115 catch (Exception e) {
116 throw ExceptionTranslator.translate(e);
117 }
118 }
119
120 public ScrollableResults scroll() throws ORMException {
121 try {
122 return new ScrollableResultsImpl(list());
123 }
124 catch (Exception e) {
125 throw ExceptionTranslator.translate(e);
126 }
127 }
128
129 public Query setBoolean(int pos, boolean value) {
130 positionalParameterMap.put(pos, value);
131
132 return this;
133 }
134
135 public Query setBoolean(String name, boolean value) {
136 namedParameterMap.put(name, value);
137
138 return this;
139 }
140
141 public Query setCacheable(boolean cacheable) {
142 return this;
143 }
144
145 public Query setCacheMode(CacheMode cacheMode) {
146 return this;
147 }
148
149 public Query setCacheRegion(String cacheRegion) {
150 return this;
151 }
152
153 public Query setDouble(int pos, double value) {
154 positionalParameterMap.put(pos, Double.valueOf(value));
155
156 return this;
157 }
158
159 public Query setDouble(String name, double value) {
160 namedParameterMap.put(name, Double.valueOf(value));
161
162 return this;
163 }
164
165 public Query setFirstResult(int firstResult) {
166 this.firstResult = firstResult;
167
168 return this;
169 }
170
171 public Query setFloat(int pos, float value) {
172 positionalParameterMap.put(pos, Float.valueOf(value));
173
174 return this;
175 }
176
177 public Query setFloat(String name, float value) {
178 namedParameterMap.put(name, Float.valueOf(value));
179
180 return this;
181 }
182
183 public Query setFlushMode(FlushModeType flushModeType) {
184 this.flushModeType = flushModeType;
185
186 return this;
187 }
188
189 public Query setInteger(int pos, int value) {
190 positionalParameterMap.put(pos, Integer.valueOf(value));
191
192 return this;
193 }
194
195 public Query setInteger(String name, int value) {
196 namedParameterMap.put(name, Integer.valueOf(value));
197
198 return this;
199 }
200
201 public Query setLockMode(String alias, LockMode lockMode) {
202 lockModeType = LockModeTranslator.translate(lockMode);
203
204 return this;
205 }
206
207 public Query setLong(int pos, long value) {
208 positionalParameterMap.put(pos, Long.valueOf(value));
209
210 return this;
211 }
212
213 public Query setLong(String name, long value) {
214 namedParameterMap.put(name, Long.valueOf(value));
215
216 return this;
217 }
218
219 public Query setMaxResults(int maxResults) {
220 this.maxResults = maxResults;
221
222 return this;
223 }
224
225 public Query setSerializable(int pos, Serializable value) {
226 positionalParameterMap.put(pos, value);
227
228 return this;
229 }
230
231 public Query setSerializable(String name, Serializable value) {
232 namedParameterMap.put(name, value);
233
234 return this;
235 }
236
237 public Query setShort(int pos, short value) {
238 positionalParameterMap.put(pos, Short.valueOf(value));
239
240 return this;
241 }
242
243 public Query setShort(String name, short value) {
244 namedParameterMap.put(name, Short.valueOf(value));
245
246 return this;
247 }
248
249 public Query setString(int pos, String value) {
250 positionalParameterMap.put(pos, value);
251
252 return this;
253 }
254
255 public Query setString(String name, String value) {
256 namedParameterMap.put(name, value);
257
258 return this;
259 }
260
261 public Query setTimestamp(int pos, Timestamp value) {
262 Date date = null;
263
264 if (value != null) {
265 date = new Date(value.getTime());
266 }
267
268 positionalParameterMap.put(pos, date);
269
270 return this;
271 }
272
273 public Query setTimestamp(String name, Timestamp value) {
274 Date date = null;
275
276 if (value != null) {
277 date = new Date(value.getTime());
278 }
279
280 namedParameterMap.put(name, date);
281
282 return this;
283 }
284
285 public Object uniqueResult() throws ORMException {
286 try {
287 return sessionImpl.uniqueResult(
288 queryString, positionalParameterMap, namedParameterMap,
289 strictName, firstResult, maxResults, flushModeType,
290 lockModeType, sqlQuery, entityClass);
291 }
292 catch (Exception e) {
293 throw ExceptionTranslator.translate(e);
294 }
295 }
296
297 protected Class<?> entityClass;
298 protected int firstResult = -1;
299 protected FlushModeType flushModeType;
300 protected LockModeType lockModeType;
301 protected int maxResults = -1;
302 protected Map<String, Object> namedParameterMap =
303 new HashMap<String, Object>();
304 protected Map<Integer, Object> positionalParameterMap =
305 new HashMap<Integer, Object>();
306 protected String queryString;
307 protected SessionImpl sessionImpl;
308 protected boolean sqlQuery;
309 protected boolean strictName = true;
310
311 }