1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.persistence.impl;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.kernel.dao.orm.Dialect;
19  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
20  import com.liferay.portal.kernel.dao.orm.ORMException;
21  import com.liferay.portal.kernel.dao.orm.Session;
22  import com.liferay.portal.kernel.dao.orm.SessionFactory;
23  import com.liferay.portal.kernel.exception.SystemException;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.ListUtil;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.kernel.util.StringBundler;
29  import com.liferay.portal.model.BaseModel;
30  import com.liferay.portal.model.ModelListener;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  
33  import java.io.Serializable;
34  
35  import java.sql.Connection;
36  
37  import java.util.List;
38  
39  import javax.sql.DataSource;
40  
41  /**
42   * <a href="BasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class BasePersistenceImpl<T extends BaseModel<T>>
47      implements BasePersistence<T>, 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      @SuppressWarnings("unused")
59      public T findByPrimaryKey(Serializable primaryKey)
60          throws NoSuchModelException, SystemException {
61  
62          throw new UnsupportedOperationException();
63      }
64  
65      @SuppressWarnings("unused")
66      public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
67          throws SystemException {
68  
69          throw new UnsupportedOperationException();
70      }
71  
72      @SuppressWarnings("unused")
73      public List<Object> findWithDynamicQuery(
74              DynamicQuery dynamicQuery, int start, int end)
75          throws SystemException {
76  
77          throw new UnsupportedOperationException();
78      }
79  
80      @SuppressWarnings("unused")
81      public T fetchByPrimaryKey(Serializable primaryKey) throws SystemException {
82          throw new UnsupportedOperationException();
83      }
84  
85      public DataSource getDataSource() {
86          return _dataSource;
87      }
88  
89      public Dialect getDialect() {
90          return _dialect;
91      }
92  
93      public ModelListener<T>[] getListeners() {
94          return listeners;
95      }
96  
97      public Session openNewSession(Connection connection) throws ORMException {
98          return _sessionFactory.openNewSession(connection);
99      }
100 
101     public Session openSession() throws ORMException {
102         return _sessionFactory.openSession();
103     }
104 
105     public SystemException processException(Exception e) {
106         if (!(e instanceof ORMException)) {
107             _log.error("Caught unexpected exception " + e.getClass().getName());
108         }
109 
110         if (_log.isDebugEnabled()) {
111             _log.debug(e, e);
112         }
113 
114         return new SystemException(e);
115     }
116 
117     public void registerListener(ModelListener<T> listener) {
118         List<ModelListener<T>> listenersList = ListUtil.fromArray(listeners);
119 
120         listenersList.add(listener);
121 
122         listeners = listenersList.toArray(
123             new ModelListener[listenersList.size()]);
124     }
125 
126     @SuppressWarnings("unused")
127     public T remove(Serializable primaryKey)
128         throws NoSuchModelException, SystemException {
129 
130         throw new UnsupportedOperationException();
131     }
132 
133     @SuppressWarnings("unused")
134     public T remove(T model) throws SystemException {
135         throw new UnsupportedOperationException();
136     }
137 
138     public void setDataSource(DataSource dataSource) {
139         _dataSource = dataSource;
140     }
141 
142     public void setSessionFactory(SessionFactory sessionFactory) {
143         _sessionFactory = sessionFactory;
144         _dialect = _sessionFactory.getDialect();
145     }
146 
147     public void unregisterListener(ModelListener<T> listener) {
148         List<ModelListener<T>> listenersList = ListUtil.fromArray(listeners);
149 
150         listenersList.remove(listener);
151 
152         listeners = listenersList.toArray(
153             new ModelListener[listenersList.size()]);
154     }
155 
156     /**
157      * Add, update, or merge, the model. This method also calls the model
158      * listeners to trigger the proper events associated with adding, deleting,
159      * or updating a model.
160      *
161      * @param  model the model to add, update, or merge
162      * @param  merge boolean value for whether to merge the entity. The default
163      *         value is false. Setting merge to true is more expensive and
164      *         should only be true when model is transient. See LEP-5473 for a
165      *         detailed discussion of this method.
166      * @return the model that was added, updated, or merged
167      */
168     public T update(T model, boolean merge) throws SystemException {
169         boolean isNew = model.isNew();
170 
171         for (ModelListener<T> listener : listeners) {
172             if (isNew) {
173                 listener.onBeforeCreate(model);
174             }
175             else {
176                 listener.onBeforeUpdate(model);
177             }
178         }
179 
180         model = updateImpl(model, merge);
181 
182         for (ModelListener<T> listener : listeners) {
183             if (isNew) {
184                 listener.onAfterCreate(model);
185             }
186             else {
187                 listener.onAfterUpdate(model);
188             }
189         }
190 
191         return model;
192     }
193 
194     @SuppressWarnings("unused")
195     public T updateImpl(T model, boolean merge) throws SystemException {
196         throw new UnsupportedOperationException();
197     }
198 
199     protected void appendOrderByComparator(
200         StringBundler query, String entityAlias, OrderByComparator obc) {
201 
202         query.append(ORDER_BY_CLAUSE);
203 
204         String[] orderByFields = obc.getOrderByFields();
205 
206         for (int i = 0; i < orderByFields.length; i++) {
207             query.append(entityAlias);
208             query.append(orderByFields[i]);
209 
210             if ((i + 1) < orderByFields.length) {
211                 if (obc.isAscending()) {
212                     query.append(ORDER_BY_ASC_HAS_NEXT);
213                 }
214                 else {
215                     query.append(ORDER_BY_DESC_HAS_NEXT);
216                 }
217             }
218             else {
219                 if (obc.isAscending()) {
220                     query.append(ORDER_BY_ASC);
221                 }
222                 else {
223                     query.append(ORDER_BY_DESC);
224                 }
225             }
226         }
227     }
228 
229     protected ModelListener<T>[] listeners = new ModelListener[0];
230 
231     protected static final String ORDER_BY_ASC = " ASC";
232 
233     protected static final String ORDER_BY_ASC_HAS_NEXT = " ASC, ";
234 
235     protected static final String ORDER_BY_CLAUSE = " ORDER BY ";
236 
237     protected static final String ORDER_BY_DESC = " DESC";
238 
239     protected static final String ORDER_BY_DESC_HAS_NEXT = " DESC, ";
240 
241     private static Log _log = LogFactoryUtil.getLog(BasePersistenceImpl.class);
242 
243     private DataSource _dataSource;
244     private Dialect _dialect;
245     private SessionFactory _sessionFactory;
246 
247 }