001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
024    import java.io.Serializable;
025    
026    import java.sql.Connection;
027    
028    import java.util.regex.Matcher;
029    import java.util.regex.Pattern;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class SessionImpl implements Session {
035    
036            public SessionImpl(org.hibernate.Session session) {
037                    _session = session;
038            }
039    
040            public void clear() throws ORMException {
041                    try {
042                            _session.clear();
043                    }
044                    catch (Exception e) {
045                            throw ExceptionTranslator.translate(e);
046                    }
047            }
048    
049            public Connection close() throws ORMException {
050                    try {
051                            return _session.close();
052                    }
053                    catch (Exception e) {
054                            throw ExceptionTranslator.translate(e);
055                    }
056            }
057    
058            public boolean contains(Object object) throws ORMException {
059                    try {
060                            return _session.contains(object);
061                    }
062                    catch (Exception e) {
063                            throw ExceptionTranslator.translate(e);
064                    }
065            }
066    
067            public Query createQuery(String queryString) throws ORMException {
068                    try {
069                            queryString = SQLTransformer.transform(queryString);
070    
071                            queryString = _jpqlToHql(queryString);
072    
073                            return new QueryImpl(_session.createQuery(queryString));
074                    }
075                    catch (Exception e) {
076                            throw ExceptionTranslator.translate(e);
077                    }
078            }
079    
080            public SQLQuery createSQLQuery(String queryString)
081                    throws ORMException {
082    
083                    try {
084                            queryString = SQLTransformer.transform(queryString);
085    
086                            queryString = _jpqlToHql(queryString);
087    
088                            return new SQLQueryImpl(_session.createSQLQuery(queryString));
089                    }
090                    catch (Exception e) {
091                            throw ExceptionTranslator.translate(e);
092                    }
093            }
094    
095            public void delete(Object object) throws ORMException {
096                    try {
097                            _session.delete(object);
098                    }
099                    catch (Exception e) {
100                            throw ExceptionTranslator.translate(e);
101                    }
102            }
103    
104            public void evict(Object object) throws ORMException {
105                    try {
106                            _session.evict(object);
107                    }
108                    catch (Exception e) {
109                            throw ExceptionTranslator.translate(e);
110                    }
111            }
112    
113            public void flush() throws ORMException {
114                    try {
115                            _session.flush();
116                    }
117                    catch (Exception e) {
118                            throw ExceptionTranslator.translate(e);
119                    }
120            }
121    
122            public Object get(Class<?> clazz, Serializable id) throws ORMException {
123                    try {
124                            return _session.get(clazz, id);
125                    }
126                    catch (Exception e) {
127                            throw ExceptionTranslator.translate(e);
128                    }
129            }
130    
131            /**
132             * @deprecated
133             */
134            public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
135                    throws ORMException {
136    
137                    try {
138                            return _session.get(
139                                    clazz, id, LockModeTranslator.translate(lockMode));
140                    }
141                    catch (Exception e) {
142                            throw ExceptionTranslator.translate(e);
143                    }
144            }
145    
146            public Object getWrappedSession() {
147                    return _session;
148            }
149    
150            public Object load(Class<?> clazz, Serializable id) throws ORMException {
151                    try {
152                            return _session.load(clazz, id);
153                    }
154                    catch (Exception e) {
155                            throw ExceptionTranslator.translate(e);
156                    }
157            }
158    
159            public Object merge(Object object) throws ORMException {
160                    try {
161                            return _session.merge(object);
162                    }
163                    catch (Exception e) {
164                            throw ExceptionTranslator.translate(e);
165                    }
166            }
167    
168            public Serializable save(Object object) throws ORMException {
169                    try {
170                            return _session.save(object);
171                    }
172                    catch (Exception e) {
173                            throw ExceptionTranslator.translate(e);
174                    }
175            }
176    
177            public void saveOrUpdate(Object object) throws ORMException {
178                    try {
179                            _session.saveOrUpdate(object);
180                    }
181                    catch (Exception e) {
182                            throw ExceptionTranslator.translate(e);
183                    }
184            }
185    
186            private String _jpqlToHql(String queryString) {
187                    Matcher matcher = _jpqlCountPattern.matcher(queryString);
188    
189                    if (matcher.find()) {
190                            String countExpression = matcher.group(1);
191                            String entityAlias = matcher.group(3);
192    
193                            if (entityAlias.equals(countExpression)) {
194                                    queryString = matcher.replaceFirst(_HQL_COUNT_SQL);
195                            }
196                    }
197    
198                    return queryString;
199            }
200    
201            private static final String _HQL_COUNT_SQL = "SELECT COUNT(*) FROM $2 $3";
202    
203            private static Pattern _jpqlCountPattern = Pattern.compile(
204                    "SELECT COUNT\\((\\S+)\\) FROM (\\S+) (\\S+)");
205    
206            private org.hibernate.Session _session;
207    
208    }