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