1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.dao.orm.hibernate;
16  
17  import com.liferay.portal.dao.orm.common.SQLTransformer;
18  import com.liferay.portal.kernel.dao.orm.LockMode;
19  import com.liferay.portal.kernel.dao.orm.ORMException;
20  import com.liferay.portal.kernel.dao.orm.Query;
21  import com.liferay.portal.kernel.dao.orm.SQLQuery;
22  import com.liferay.portal.kernel.dao.orm.Session;
23  
24  import java.io.Serializable;
25  
26  import java.sql.Connection;
27  
28  import java.util.regex.Matcher;
29  import java.util.regex.Pattern;
30  
31  /**
32   * <a href="SessionImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class SessionImpl implements Session {
37  
38      public SessionImpl(org.hibernate.Session session) {
39          _session = session;
40      }
41  
42      public void clear() throws ORMException {
43          try {
44              _session.clear();
45          }
46          catch (Exception e) {
47              throw ExceptionTranslator.translate(e);
48          }
49      }
50  
51      public Connection close() throws ORMException {
52          try {
53              return _session.close();
54          }
55          catch (Exception e) {
56              throw ExceptionTranslator.translate(e);
57          }
58      }
59  
60      public boolean contains(Object object) throws ORMException {
61          try {
62              return _session.contains(object);
63          }
64          catch (Exception e) {
65              throw ExceptionTranslator.translate(e);
66          }
67      }
68  
69      public Query createQuery(String queryString) throws ORMException {
70          try {
71              queryString = SQLTransformer.transform(queryString);
72  
73              queryString = _jpqlToHql(queryString);
74  
75              return new QueryImpl(_session.createQuery(queryString));
76          }
77          catch (Exception e) {
78              throw ExceptionTranslator.translate(e);
79          }
80      }
81  
82      public SQLQuery createSQLQuery(String queryString)
83          throws ORMException {
84  
85          try {
86              queryString = SQLTransformer.transform(queryString);
87  
88              queryString = _jpqlToHql(queryString);
89  
90              return new SQLQueryImpl(_session.createSQLQuery(queryString));
91          }
92          catch (Exception e) {
93              throw ExceptionTranslator.translate(e);
94          }
95      }
96  
97      public void delete(Object object) throws ORMException {
98          try {
99              _session.delete(object);
100         }
101         catch (Exception e) {
102             throw ExceptionTranslator.translate(e);
103         }
104     }
105 
106     public void evict(Object object) throws ORMException {
107         try {
108             _session.evict(object);
109         }
110         catch (Exception e) {
111             throw ExceptionTranslator.translate(e);
112         }
113     }
114 
115     public void flush() throws ORMException {
116         try {
117             _session.flush();
118         }
119         catch (Exception e) {
120             throw ExceptionTranslator.translate(e);
121         }
122     }
123 
124     public Object get(Class<?> clazz, Serializable id) throws ORMException {
125         try {
126             return _session.get(clazz, id);
127         }
128         catch (Exception e) {
129             throw ExceptionTranslator.translate(e);
130         }
131     }
132 
133     public Object get(Class<?> clazz, Serializable id, LockMode lockMode)
134         throws ORMException {
135 
136         try {
137             return _session.get(
138                 clazz, id, LockModeTranslator.translate(lockMode));
139         }
140         catch (Exception e) {
141             throw ExceptionTranslator.translate(e);
142         }
143     }
144 
145     public Object getWrappedSession() {
146         return _session;
147     }
148 
149     public Object load(Class<?> clazz, Serializable id) throws ORMException {
150         try {
151             return _session.load(clazz, id);
152         }
153         catch (Exception e) {
154             throw ExceptionTranslator.translate(e);
155         }
156     }
157 
158     public Object merge(Object object) throws ORMException {
159         try {
160             return _session.merge(object);
161         }
162         catch (Exception e) {
163             throw ExceptionTranslator.translate(e);
164         }
165     }
166 
167     public Serializable save(Object object) throws ORMException {
168         try {
169             return _session.save(object);
170         }
171         catch (Exception e) {
172             throw ExceptionTranslator.translate(e);
173         }
174     }
175 
176     public void saveOrUpdate(Object object) throws ORMException {
177         try {
178             _session.saveOrUpdate(object);
179         }
180         catch (Exception e) {
181             throw ExceptionTranslator.translate(e);
182         }
183     }
184 
185     private String _jpqlToHql(String queryString) {
186         Matcher matcher = _jpqlCountPattern.matcher(queryString);
187 
188         if (matcher.find()) {
189             String countExpression = matcher.group(1);
190             String entityAlias = matcher.group(3);
191 
192             if (entityAlias.equals(countExpression)) {
193                 queryString = matcher.replaceFirst(_HQL_COUNT_SQL);
194             }
195         }
196 
197         return queryString;
198     }
199 
200     private static final String _HQL_COUNT_SQL = "SELECT COUNT(*) FROM $2 $3";
201 
202     private static Pattern _jpqlCountPattern = Pattern.compile(
203         "SELECT COUNT\\((\\S+)\\) FROM (\\S+) (\\S+)");
204 
205     private org.hibernate.Session _session;
206 
207 }