1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence.impl;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.Dialect;
27  import com.liferay.portal.kernel.dao.orm.ORMException;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.dao.orm.SessionFactory;
30  import com.liferay.portal.kernel.log.Log;
31  import com.liferay.portal.kernel.log.LogFactoryUtil;
32  import com.liferay.portal.kernel.util.ListUtil;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  
36  import java.sql.Connection;
37  
38  import java.util.List;
39  
40  import javax.sql.DataSource;
41  
42  /**
43   * <a href="BasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class BasePersistenceImpl implements BasePersistence, SessionFactory {
48  
49      public static final String COUNT_COLUMN_NAME = "COUNT_VALUE";
50  
51      public void clearCache() {
52      }
53  
54      public void closeSession(Session session) {
55          _sessionFactory.closeSession(session);
56      }
57  
58      public DataSource getDataSource() {
59          return _dataSource;
60      }
61  
62      public Dialect getDialect() {
63          return _dialect;
64      }
65  
66      public ModelListener[] getListeners() {
67          return listeners;
68      }
69  
70      public Session openSession() throws ORMException {
71          return _sessionFactory.openSession();
72      }
73  
74      public Session openNewSession(Connection connection) throws ORMException {
75          return _sessionFactory.openNewSession(connection);
76      }
77  
78      public void registerListener(ModelListener listener) {
79          List<ModelListener> listenersList = ListUtil.fromArray(listeners);
80  
81          listenersList.add(listener);
82  
83          listeners = listenersList.toArray(
84              new ModelListener[listenersList.size()]);
85      }
86  
87      public SystemException processException(Exception e) {
88          if (!(e instanceof ORMException)) {
89              _log.error("Caught unexpected exception " + e.getClass().getName());
90          }
91  
92          if (_log.isDebugEnabled()) {
93              _log.debug(e, e);
94          }
95  
96          return new SystemException(e);
97      }
98  
99      public void setDataSource(DataSource dataSource) {
100         _dataSource = dataSource;
101     }
102 
103     public void setSessionFactory(SessionFactory sessionFactory) {
104         _sessionFactory = sessionFactory;
105         _dialect = _sessionFactory.getDialect();
106     }
107 
108     public void unregisterListener(ModelListener listener) {
109         List<ModelListener> listenersList = ListUtil.fromArray(listeners);
110 
111         listenersList.remove(listener);
112 
113         listeners = listenersList.toArray(
114             new ModelListener[listenersList.size()]);
115     }
116 
117     protected ModelListener[] listeners = new ModelListener[0];
118 
119     private static Log _log = LogFactoryUtil.getLog(BasePersistenceImpl.class);
120 
121     private DataSource _dataSource;
122     private SessionFactory _sessionFactory;
123     private Dialect _dialect;
124 
125 }