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.hibernate;
016    
017    import com.liferay.portal.dao.orm.common.SQLTransformer;
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.SQLQuery;
022    import com.liferay.portal.kernel.dao.orm.Session;
023    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
024    import com.liferay.portal.kernel.security.pacl.NotPrivileged;
025    import com.liferay.portal.security.lang.DoPrivilegedUtil;
026    
027    import java.io.Serializable;
028    
029    import java.sql.Connection;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Shuyang Zhou
034     */
035    @DoPrivileged
036    public class SessionImpl implements Session {
037    
038            public SessionImpl(org.hibernate.Session session) {
039                    _session = session;
040            }
041    
042            @NotPrivileged
043            @Override
044            public void clear() throws ORMException {
045                    try {
046                            _session.clear();
047                    }
048                    catch (Exception e) {
049                            throw ExceptionTranslator.translate(e);
050                    }
051            }
052    
053            @NotPrivileged
054            @Override
055            public Connection close() throws ORMException {
056                    try {
057                            return _session.close();
058                    }
059                    catch (Exception e) {
060                            throw ExceptionTranslator.translate(e);
061                    }
062            }
063    
064            @NotPrivileged
065            @Override
066            public boolean contains(Object object) throws ORMException {
067                    try {
068                            return _session.contains(object);
069                    }
070                    catch (Exception e) {
071                            throw ExceptionTranslator.translate(e);
072                    }
073            }
074    
075            @Override
076            public Query createQuery(String queryString) throws ORMException {
077                    return createQuery(queryString, true);
078            }
079    
080            @Override
081            public Query createQuery(String queryString, boolean strictName)
082                    throws ORMException {
083    
084                    try {
085                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
086    
087                            return DoPrivilegedUtil.wrapWhenActive(
088                                    new QueryImpl(_session.createQuery(queryString), strictName)
089                            );
090                    }
091                    catch (Exception e) {
092                            throw ExceptionTranslator.translate(e);
093                    }
094            }
095    
096            @Override
097            public SQLQuery createSQLQuery(String queryString) throws ORMException {
098                    return createSQLQuery(queryString, true);
099            }
100    
101            @Override
102            public SQLQuery createSQLQuery(String queryString, boolean strictName)
103                    throws ORMException {
104    
105                    try {
106                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
107    
108                            return DoPrivilegedUtil.wrapWhenActive(
109                                    new SQLQueryImpl(
110                                            _session.createSQLQuery(queryString), strictName)
111                            );
112                    }
113                    catch (Exception e) {
114                            throw ExceptionTranslator.translate(e);
115                    }
116            }
117    
118            @NotPrivileged
119            @Override
120            public void delete(Object object) throws ORMException {
121                    try {
122                            _session.delete(object);
123                    }
124                    catch (Exception e) {
125                            throw ExceptionTranslator.translate(e);
126                    }
127            }
128    
129            @NotPrivileged
130            @Override
131            public void evict(Object object) throws ORMException {
132                    try {
133                            _session.evict(object);
134                    }
135                    catch (Exception e) {
136                            throw ExceptionTranslator.translate(e);
137                    }
138            }
139    
140            @NotPrivileged
141            @Override
142            public void flush() throws ORMException {
143                    try {
144                            _session.flush();
145                    }
146                    catch (Exception e) {
147                            throw ExceptionTranslator.translate(e);
148                    }
149            }
150    
151            @NotPrivileged
152            @Override
153            public Object get(Class<?> clazz, Serializable id) throws ORMException {
154                    try {
155                            return _session.get(clazz, id);
156                    }
157                    catch (Exception e) {
158                            throw ExceptionTranslator.translate(e);
159                    }
160            }
161    
162            /**
163             * @deprecated
164             */
165            @NotPrivileged
166            @Override
167            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
168                    throws ORMException {
169    
170                    try {
171                            return _session.get(
172                                    clazz, id, LockModeTranslator.translate(lockMode));
173                    }
174                    catch (Exception e) {
175                            throw ExceptionTranslator.translate(e);
176                    }
177            }
178    
179            @NotPrivileged
180            @Override
181            public Object getWrappedSession() {
182                    return _session;
183            }
184    
185            @NotPrivileged
186            @Override
187            public Object load(Class<?> clazz, Serializable id) throws ORMException {
188                    try {
189                            return _session.load(clazz, id);
190                    }
191                    catch (Exception e) {
192                            throw ExceptionTranslator.translate(e);
193                    }
194            }
195    
196            @NotPrivileged
197            @Override
198            public Object merge(Object object) throws ORMException {
199                    try {
200                            return _session.merge(object);
201                    }
202                    catch (Exception e) {
203                            throw ExceptionTranslator.translate(e);
204                    }
205            }
206    
207            @NotPrivileged
208            @Override
209            public Serializable save(Object object) throws ORMException {
210                    try {
211                            return _session.save(object);
212                    }
213                    catch (Exception e) {
214                            throw ExceptionTranslator.translate(e);
215                    }
216            }
217    
218            @NotPrivileged
219            @Override
220            public void saveOrUpdate(Object object) throws ORMException {
221                    try {
222                            _session.saveOrUpdate(object);
223                    }
224                    catch (Exception e) {
225                            throw ExceptionTranslator.translate(e);
226                    }
227            }
228    
229            private org.hibernate.Session _session;
230    
231    }