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