1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.persistence.impl;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.dao.orm.Dialect;
24  import com.liferay.portal.kernel.dao.orm.ORMException;
25  import com.liferay.portal.kernel.dao.orm.Session;
26  import com.liferay.portal.kernel.dao.orm.SessionFactory;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.util.ListUtil;
30  import com.liferay.portal.model.ModelListener;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  
33  import java.util.List;
34  
35  import javax.sql.DataSource;
36  
37  /**
38   * <a href="BasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class BasePersistenceImpl implements BasePersistence, SessionFactory {
44  
45      public static final String COUNT_COLUMN_NAME = "COUNT_VALUE";
46  
47      public void closeSession(Session session) {
48          _sessionFactory.closeSession(session);
49      }
50  
51      public DataSource getDataSource() {
52          return _dataSource;
53      }
54  
55      public Dialect getDialect() {
56          return _dialect;
57      }
58  
59      public ModelListener[] getListeners() {
60          return listeners;
61      }
62  
63      public Session openSession() throws ORMException {
64          return _sessionFactory.openSession();
65      }
66  
67      public void registerListener(ModelListener listener) {
68          List<ModelListener> listenersList = ListUtil.fromArray(listeners);
69  
70          listenersList.add(listener);
71  
72          listeners = listenersList.toArray(
73              new ModelListener[listenersList.size()]);
74      }
75  
76      public SystemException processException(Exception e) {
77          if (!(e instanceof ORMException)) {
78              _log.error("Caught unexpected exception " + e.getClass().getName());
79          }
80  
81          if (_log.isDebugEnabled()) {
82              _log.debug(e, e);
83          }
84  
85          return new SystemException(e);
86      }
87  
88      public void setDataSource(DataSource dataSource) {
89          _dataSource = dataSource;
90      }
91  
92      public void setSessionFactory(SessionFactory sessionFactory) {
93          _sessionFactory = sessionFactory;
94          _dialect = _sessionFactory.getDialect();
95      }
96  
97      public void unregisterListener(ModelListener listener) {
98          List<ModelListener> listenersList = ListUtil.fromArray(listeners);
99  
100         listenersList.remove(listener);
101 
102         listeners = listenersList.toArray(
103             new ModelListener[listenersList.size()]);
104     }
105 
106     protected ModelListener[] listeners = new ModelListener[0];
107 
108     private static Log _log = LogFactoryUtil.getLog(BasePersistenceImpl.class);
109 
110     private DataSource _dataSource;
111     private SessionFactory _sessionFactory;
112     private Dialect _dialect;
113 
114 }