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.NoSuchModelException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.orm.Dialect;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.ORMException;
30  import com.liferay.portal.kernel.dao.orm.Session;
31  import com.liferay.portal.kernel.dao.orm.SessionFactory;
32  import com.liferay.portal.kernel.log.Log;
33  import com.liferay.portal.kernel.log.LogFactoryUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringBundler;
37  import com.liferay.portal.model.BaseModel;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.BasePersistence;
40  
41  import java.io.Serializable;
42  
43  import java.sql.Connection;
44  
45  import java.util.List;
46  
47  import javax.sql.DataSource;
48  
49  /**
50   * <a href="BasePersistenceImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   */
54  public class BasePersistenceImpl<T extends BaseModel<T>>
55      implements BasePersistence<T>, SessionFactory {
56  
57      public static final String COUNT_COLUMN_NAME = "COUNT_VALUE";
58  
59      public void clearCache() {
60      }
61  
62      public void closeSession(Session session) {
63          _sessionFactory.closeSession(session);
64      }
65  
66      @SuppressWarnings("unused")
67      public T findByPrimaryKey(Serializable primaryKey)
68          throws NoSuchModelException, SystemException {
69  
70          throw new UnsupportedOperationException();
71      }
72  
73      @SuppressWarnings("unused")
74      public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
75          throws SystemException {
76  
77          throw new UnsupportedOperationException();
78      }
79  
80      @SuppressWarnings("unused")
81      public List<Object> findWithDynamicQuery(
82              DynamicQuery dynamicQuery, int start, int end)
83          throws SystemException {
84  
85          throw new UnsupportedOperationException();
86      }
87  
88      @SuppressWarnings("unused")
89      public T fetchByPrimaryKey(Serializable primaryKey) throws SystemException {
90          throw new UnsupportedOperationException();
91      }
92  
93      public DataSource getDataSource() {
94          return _dataSource;
95      }
96  
97      public Dialect getDialect() {
98          return _dialect;
99      }
100 
101     public ModelListener<T>[] getListeners() {
102         return listeners;
103     }
104 
105     public Session openNewSession(Connection connection) throws ORMException {
106         return _sessionFactory.openNewSession(connection);
107     }
108 
109     public Session openSession() throws ORMException {
110         return _sessionFactory.openSession();
111     }
112 
113     public SystemException processException(Exception e) {
114         if (!(e instanceof ORMException)) {
115             _log.error("Caught unexpected exception " + e.getClass().getName());
116         }
117 
118         if (_log.isDebugEnabled()) {
119             _log.debug(e, e);
120         }
121 
122         return new SystemException(e);
123     }
124 
125     public void registerListener(ModelListener<T> listener) {
126         List<ModelListener<T>> listenersList = ListUtil.fromArray(listeners);
127 
128         listenersList.add(listener);
129 
130         listeners = listenersList.toArray(
131             new ModelListener[listenersList.size()]);
132     }
133 
134     @SuppressWarnings("unused")
135     public T remove(Serializable primaryKey)
136         throws NoSuchModelException, SystemException {
137 
138         throw new UnsupportedOperationException();
139     }
140 
141     @SuppressWarnings("unused")
142     public T remove(T model) throws SystemException {
143         throw new UnsupportedOperationException();
144     }
145 
146     public void setDataSource(DataSource dataSource) {
147         _dataSource = dataSource;
148     }
149 
150     public void setSessionFactory(SessionFactory sessionFactory) {
151         _sessionFactory = sessionFactory;
152         _dialect = _sessionFactory.getDialect();
153     }
154 
155     public void unregisterListener(ModelListener<T> listener) {
156         List<ModelListener<T>> listenersList = ListUtil.fromArray(listeners);
157 
158         listenersList.remove(listener);
159 
160         listeners = listenersList.toArray(
161             new ModelListener[listenersList.size()]);
162     }
163 
164     /**
165      * Add, update, or merge, the model. This method also calls the model
166      * listeners to trigger the proper events associated with adding, deleting,
167      * or updating a model.
168      *
169      * @param  model the model to add, update, or merge
170      * @param  merge boolean value for whether to merge the entity. The default
171      *         value is false. Setting merge to true is more expensive and
172      *         should only be true when model is transient. See LEP-5473 for a
173      *         detailed discussion of this method.
174      * @return the model that was added, updated, or merged
175      */
176     public T update(T model, boolean merge) throws SystemException {
177         boolean isNew = model.isNew();
178 
179         for (ModelListener<T> listener : listeners) {
180             if (isNew) {
181                 listener.onBeforeCreate(model);
182             }
183             else {
184                 listener.onBeforeUpdate(model);
185             }
186         }
187 
188         model = updateImpl(model, merge);
189 
190         for (ModelListener<T> listener : listeners) {
191             if (isNew) {
192                 listener.onAfterCreate(model);
193             }
194             else {
195                 listener.onAfterUpdate(model);
196             }
197         }
198 
199         return model;
200     }
201 
202     @SuppressWarnings("unused")
203     public T updateImpl(T model, boolean merge) throws SystemException {
204         throw new UnsupportedOperationException();
205     }
206 
207     protected void appendOrderByComparator(
208         StringBundler query, String entityAlias, OrderByComparator obc) {
209 
210         query.append(ORDER_BY_CLAUSE);
211 
212         String[] orderByFields = obc.getOrderByFields();
213 
214         for (int i = 0; i < orderByFields.length; i++) {
215             query.append(entityAlias);
216             query.append(orderByFields[i]);
217 
218             if ((i + 1) < orderByFields.length) {
219                 if (obc.isAscending()) {
220                     query.append(ORDER_BY_ASC_HAS_NEXT);
221                 }
222                 else {
223                     query.append(ORDER_BY_DESC_HAS_NEXT);
224                 }
225             }
226             else {
227                 if (obc.isAscending()) {
228                     query.append(ORDER_BY_ASC);
229                 }
230                 else {
231                     query.append(ORDER_BY_DESC);
232                 }
233             }
234         }
235     }
236 
237     protected ModelListener<T>[] listeners = new ModelListener[0];
238 
239     protected static final String ORDER_BY_ASC = " ASC";
240 
241     protected static final String ORDER_BY_ASC_HAS_NEXT = " ASC, ";
242 
243     protected static final String ORDER_BY_CLAUSE = " ORDER BY ";
244 
245     protected static final String ORDER_BY_DESC = " DESC";
246 
247     protected static final String ORDER_BY_DESC_HAS_NEXT = " DESC, ";
248 
249     private static Log _log = LogFactoryUtil.getLog(BasePersistenceImpl.class);
250 
251     private DataSource _dataSource;
252     private Dialect _dialect;
253     private SessionFactory _sessionFactory;
254 
255 }