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 clearCache() {
48      }
49  
50      public void closeSession(Session session) {
51          _sessionFactory.closeSession(session);
52      }
53  
54      public DataSource getDataSource() {
55          return _dataSource;
56      }
57  
58      public Dialect getDialect() {
59          return _dialect;
60      }
61  
62      public ModelListener[] getListeners() {
63          return listeners;
64      }
65  
66      public Session openSession() throws ORMException {
67          return _sessionFactory.openSession();
68      }
69  
70      public void registerListener(ModelListener listener) {
71          List<ModelListener> listenersList = ListUtil.fromArray(listeners);
72  
73          listenersList.add(listener);
74  
75          listeners = listenersList.toArray(
76              new ModelListener[listenersList.size()]);
77      }
78  
79      public SystemException processException(Exception e) {
80          if (!(e instanceof ORMException)) {
81              _log.error("Caught unexpected exception " + e.getClass().getName());
82          }
83  
84          if (_log.isDebugEnabled()) {
85              _log.debug(e, e);
86          }
87  
88          return new SystemException(e);
89      }
90  
91      public void setDataSource(DataSource dataSource) {
92          _dataSource = dataSource;
93      }
94  
95      public void setSessionFactory(SessionFactory sessionFactory) {
96          _sessionFactory = sessionFactory;
97          _dialect = _sessionFactory.getDialect();
98      }
99  
100     public void unregisterListener(ModelListener listener) {
101         List<ModelListener> listenersList = ListUtil.fromArray(listeners);
102 
103         listenersList.remove(listener);
104 
105         listeners = listenersList.toArray(
106             new ModelListener[listenersList.size()]);
107     }
108 
109     protected ModelListener[] listeners = new ModelListener[0];
110 
111     private static Log _log = LogFactoryUtil.getLog(BasePersistenceImpl.class);
112 
113     private DataSource _dataSource;
114     private SessionFactory _sessionFactory;
115     private Dialect _dialect;
116 
117 }