1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.portlet.tags.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.ModelListener;
33  import com.liferay.portal.service.persistence.BasePersistence;
34  import com.liferay.portal.spring.hibernate.FinderCache;
35  import com.liferay.portal.spring.hibernate.HibernateUtil;
36  import com.liferay.portal.util.PropsUtil;
37  
38  import com.liferay.portlet.tags.NoSuchSourceException;
39  import com.liferay.portlet.tags.model.TagsSource;
40  import com.liferay.portlet.tags.model.impl.TagsSourceImpl;
41  import com.liferay.portlet.tags.model.impl.TagsSourceModelImpl;
42  
43  import com.liferay.util.dao.hibernate.QueryUtil;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  import org.hibernate.Query;
49  import org.hibernate.Session;
50  
51  import java.util.Collections;
52  import java.util.Iterator;
53  import java.util.List;
54  
55  /**
56   * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class TagsSourcePersistenceImpl extends BasePersistence
62      implements TagsSourcePersistence {
63      public TagsSource create(long sourceId) {
64          TagsSource tagsSource = new TagsSourceImpl();
65  
66          tagsSource.setNew(true);
67          tagsSource.setPrimaryKey(sourceId);
68  
69          return tagsSource;
70      }
71  
72      public TagsSource remove(long sourceId)
73          throws NoSuchSourceException, SystemException {
74          Session session = null;
75  
76          try {
77              session = openSession();
78  
79              TagsSource tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
80                      new Long(sourceId));
81  
82              if (tagsSource == null) {
83                  if (_log.isWarnEnabled()) {
84                      _log.warn("No TagsSource exists with the primary key " +
85                          sourceId);
86                  }
87  
88                  throw new NoSuchSourceException(
89                      "No TagsSource exists with the primary key " + sourceId);
90              }
91  
92              return remove(tagsSource);
93          }
94          catch (NoSuchSourceException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw HibernateUtil.processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public TagsSource remove(TagsSource tagsSource) throws SystemException {
106         ModelListener listener = _getListener();
107 
108         if (listener != null) {
109             listener.onBeforeRemove(tagsSource);
110         }
111 
112         tagsSource = removeImpl(tagsSource);
113 
114         if (listener != null) {
115             listener.onAfterRemove(tagsSource);
116         }
117 
118         return tagsSource;
119     }
120 
121     protected TagsSource removeImpl(TagsSource tagsSource)
122         throws SystemException {
123         Session session = null;
124 
125         try {
126             session = openSession();
127 
128             session.delete(tagsSource);
129 
130             session.flush();
131 
132             return tagsSource;
133         }
134         catch (Exception e) {
135             throw HibernateUtil.processException(e);
136         }
137         finally {
138             closeSession(session);
139 
140             FinderCache.clearCache(TagsSource.class.getName());
141         }
142     }
143 
144     public TagsSource update(TagsSource tagsSource) throws SystemException {
145         return update(tagsSource, false);
146     }
147 
148     public TagsSource update(TagsSource tagsSource, boolean merge)
149         throws SystemException {
150         ModelListener listener = _getListener();
151 
152         boolean isNew = tagsSource.isNew();
153 
154         if (listener != null) {
155             if (isNew) {
156                 listener.onBeforeCreate(tagsSource);
157             }
158             else {
159                 listener.onBeforeUpdate(tagsSource);
160             }
161         }
162 
163         tagsSource = updateImpl(tagsSource, merge);
164 
165         if (listener != null) {
166             if (isNew) {
167                 listener.onAfterCreate(tagsSource);
168             }
169             else {
170                 listener.onAfterUpdate(tagsSource);
171             }
172         }
173 
174         return tagsSource;
175     }
176 
177     public TagsSource updateImpl(
178         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
179         throws SystemException {
180         Session session = null;
181 
182         try {
183             session = openSession();
184 
185             if (merge) {
186                 session.merge(tagsSource);
187             }
188             else {
189                 if (tagsSource.isNew()) {
190                     session.save(tagsSource);
191                 }
192             }
193 
194             session.flush();
195 
196             tagsSource.setNew(false);
197 
198             return tagsSource;
199         }
200         catch (Exception e) {
201             throw HibernateUtil.processException(e);
202         }
203         finally {
204             closeSession(session);
205 
206             FinderCache.clearCache(TagsSource.class.getName());
207         }
208     }
209 
210     public TagsSource findByPrimaryKey(long sourceId)
211         throws NoSuchSourceException, SystemException {
212         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
213 
214         if (tagsSource == null) {
215             if (_log.isWarnEnabled()) {
216                 _log.warn("No TagsSource exists with the primary key " +
217                     sourceId);
218             }
219 
220             throw new NoSuchSourceException(
221                 "No TagsSource exists with the primary key " + sourceId);
222         }
223 
224         return tagsSource;
225     }
226 
227     public TagsSource fetchByPrimaryKey(long sourceId)
228         throws SystemException {
229         Session session = null;
230 
231         try {
232             session = openSession();
233 
234             return (TagsSource)session.get(TagsSourceImpl.class,
235                 new Long(sourceId));
236         }
237         catch (Exception e) {
238             throw HibernateUtil.processException(e);
239         }
240         finally {
241             closeSession(session);
242         }
243     }
244 
245     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
246         throws SystemException {
247         Session session = null;
248 
249         try {
250             session = openSession();
251 
252             DynamicQuery query = queryInitializer.initialize(session);
253 
254             return query.list();
255         }
256         catch (Exception e) {
257             throw HibernateUtil.processException(e);
258         }
259         finally {
260             closeSession(session);
261         }
262     }
263 
264     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
265         int begin, int end) throws SystemException {
266         Session session = null;
267 
268         try {
269             session = openSession();
270 
271             DynamicQuery query = queryInitializer.initialize(session);
272 
273             query.setLimit(begin, end);
274 
275             return query.list();
276         }
277         catch (Exception e) {
278             throw HibernateUtil.processException(e);
279         }
280         finally {
281             closeSession(session);
282         }
283     }
284 
285     public List findAll() throws SystemException {
286         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
287     }
288 
289     public List findAll(int begin, int end) throws SystemException {
290         return findAll(begin, end, null);
291     }
292 
293     public List findAll(int begin, int end, OrderByComparator obc)
294         throws SystemException {
295         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
296         String finderClassName = TagsSource.class.getName();
297         String finderMethodName = "findAll";
298         String[] finderParams = new String[] {
299                 "java.lang.Integer", "java.lang.Integer",
300                 "com.liferay.portal.kernel.util.OrderByComparator"
301             };
302         Object[] finderArgs = new Object[] {
303                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
304             };
305 
306         Object result = null;
307 
308         if (finderClassNameCacheEnabled) {
309             result = FinderCache.getResult(finderClassName, finderMethodName,
310                     finderParams, finderArgs, getSessionFactory());
311         }
312 
313         if (result == null) {
314             Session session = null;
315 
316             try {
317                 session = openSession();
318 
319                 StringMaker query = new StringMaker();
320 
321                 query.append("FROM com.liferay.portlet.tags.model.TagsSource ");
322 
323                 if (obc != null) {
324                     query.append("ORDER BY ");
325                     query.append(obc.getOrderBy());
326                 }
327 
328                 Query q = session.createQuery(query.toString());
329 
330                 List list = QueryUtil.list(q, getDialect(), begin, end);
331 
332                 if (obc == null) {
333                     Collections.sort(list);
334                 }
335 
336                 FinderCache.putResult(finderClassNameCacheEnabled,
337                     finderClassName, finderMethodName, finderParams,
338                     finderArgs, list);
339 
340                 return list;
341             }
342             catch (Exception e) {
343                 throw HibernateUtil.processException(e);
344             }
345             finally {
346                 closeSession(session);
347             }
348         }
349         else {
350             return (List)result;
351         }
352     }
353 
354     public void removeAll() throws SystemException {
355         Iterator itr = findAll().iterator();
356 
357         while (itr.hasNext()) {
358             remove((TagsSource)itr.next());
359         }
360     }
361 
362     public int countAll() throws SystemException {
363         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
364         String finderClassName = TagsSource.class.getName();
365         String finderMethodName = "countAll";
366         String[] finderParams = new String[] {  };
367         Object[] finderArgs = new Object[] {  };
368 
369         Object result = null;
370 
371         if (finderClassNameCacheEnabled) {
372             result = FinderCache.getResult(finderClassName, finderMethodName,
373                     finderParams, finderArgs, getSessionFactory());
374         }
375 
376         if (result == null) {
377             Session session = null;
378 
379             try {
380                 session = openSession();
381 
382                 Query q = session.createQuery(
383                         "SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsSource");
384 
385                 Long count = null;
386 
387                 Iterator itr = q.list().iterator();
388 
389                 if (itr.hasNext()) {
390                     count = (Long)itr.next();
391                 }
392 
393                 if (count == null) {
394                     count = new Long(0);
395                 }
396 
397                 FinderCache.putResult(finderClassNameCacheEnabled,
398                     finderClassName, finderMethodName, finderParams,
399                     finderArgs, count);
400 
401                 return count.intValue();
402             }
403             catch (Exception e) {
404                 throw HibernateUtil.processException(e);
405             }
406             finally {
407                 closeSession(session);
408             }
409         }
410         else {
411             return ((Long)result).intValue();
412         }
413     }
414 
415     protected void initDao() {
416     }
417 
418     private static ModelListener _getListener() {
419         if (Validator.isNotNull(_LISTENER)) {
420             try {
421                 return (ModelListener)Class.forName(_LISTENER).newInstance();
422             }
423             catch (Exception e) {
424                 _log.error(e);
425             }
426         }
427 
428         return null;
429     }
430 
431     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
432                 "value.object.listener.com.liferay.portlet.tags.model.TagsSource"));
433     private static Log _log = LogFactory.getLog(TagsSourcePersistenceImpl.class);
434 }