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            public void clear() throws ORMException {
044                    try {
045                            _session.clear();
046                    }
047                    catch (Exception e) {
048                            throw ExceptionTranslator.translate(e);
049                    }
050            }
051    
052            @NotPrivileged
053            public Connection close() throws ORMException {
054                    try {
055                            return _session.close();
056                    }
057                    catch (Exception e) {
058                            throw ExceptionTranslator.translate(e);
059                    }
060            }
061    
062            @NotPrivileged
063            public boolean contains(Object object) throws ORMException {
064                    try {
065                            return _session.contains(object);
066                    }
067                    catch (Exception e) {
068                            throw ExceptionTranslator.translate(e);
069                    }
070            }
071    
072            public Query createQuery(String queryString) throws ORMException {
073                    return createQuery(queryString, true);
074            }
075    
076            public Query createQuery(String queryString, boolean strictName)
077                    throws ORMException {
078    
079                    try {
080                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
081    
082                            return DoPrivilegedUtil.wrap(
083                                    new QueryImpl(_session.createQuery(queryString), strictName),
084                                    true);
085                    }
086                    catch (Exception e) {
087                            throw ExceptionTranslator.translate(e);
088                    }
089            }
090    
091            public SQLQuery createSQLQuery(String queryString) throws ORMException {
092                    return createSQLQuery(queryString, true);
093            }
094    
095            public SQLQuery createSQLQuery(String queryString, boolean strictName)
096                    throws ORMException {
097    
098                    try {
099                            queryString = SQLTransformer.transformFromJpqlToHql(queryString);
100    
101                            return DoPrivilegedUtil.wrap(
102                                    new SQLQueryImpl(
103                                            _session.createSQLQuery(queryString), strictName),
104                                    true);
105                    }
106                    catch (Exception e) {
107                            throw ExceptionTranslator.translate(e);
108                    }
109            }
110    
111            @NotPrivileged
112            public void delete(Object object) throws ORMException {
113                    try {
114                            _session.delete(object);
115                    }
116                    catch (Exception e) {
117                            throw ExceptionTranslator.translate(e);
118                    }
119            }
120    
121            @NotPrivileged
122            public void evict(Object object) throws ORMException {
123                    try {
124                            _session.evict(object);
125                    }
126                    catch (Exception e) {
127                            throw ExceptionTranslator.translate(e);
128                    }
129            }
130    
131            @NotPrivileged
132            public void flush() throws ORMException {
133                    try {
134                            _session.flush();
135                    }
136                    catch (Exception e) {
137                            throw ExceptionTranslator.translate(e);
138                    }
139            }
140    
141            @NotPrivileged
142            public Object get(Class<?> clazz, Serializable id) throws ORMException {
143                    try {
144                            return _session.get(clazz, id);
145                    }
146                    catch (Exception e) {
147                            throw ExceptionTranslator.translate(e);
148                    }
149            }
150    
151            /**
152             * @deprecated As of 6.1.0
153             */
154            @NotPrivileged
155            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
156                    throws ORMException {
157    
158                    try {
159                            return _session.get(
160                                    clazz, id, LockModeTranslator.translate(lockMode));
161                    }
162                    catch (Exception e) {
163                            throw ExceptionTranslator.translate(e);
164                    }
165            }
166    
167            @NotPrivileged
168            public Object getWrappedSession() {
169                    return _session;
170            }
171    
172            @NotPrivileged
173            public Object load(Class<?> clazz, Serializable id) throws ORMException {
174                    try {
175                            return _session.load(clazz, id);
176                    }
177                    catch (Exception e) {
178                            throw ExceptionTranslator.translate(e);
179                    }
180            }
181    
182            @NotPrivileged
183            public Object merge(Object object) throws ORMException {
184                    try {
185                            return _session.merge(object);
186                    }
187                    catch (Exception e) {
188                            throw ExceptionTranslator.translate(e);
189                    }
190            }
191    
192            @NotPrivileged
193            public Serializable save(Object object) throws ORMException {
194                    try {
195                            return _session.save(object);
196                    }
197                    catch (Exception e) {
198                            throw ExceptionTranslator.translate(e);
199                    }
200            }
201    
202            @NotPrivileged
203            public void saveOrUpdate(Object object) throws ORMException {
204                    try {
205                            _session.saveOrUpdate(object);
206                    }
207                    catch (Exception e) {
208                            throw ExceptionTranslator.translate(e);
209                    }
210            }
211    
212            private org.hibernate.Session _session;
213    
214    }