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.StringUtil;
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.ArrayList;
52  import java.util.Collections;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  /**
57   * <a href="TagsSourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class TagsSourcePersistenceImpl extends BasePersistence
63      implements TagsSourcePersistence {
64      public TagsSource create(long sourceId) {
65          TagsSource tagsSource = new TagsSourceImpl();
66  
67          tagsSource.setNew(true);
68          tagsSource.setPrimaryKey(sourceId);
69  
70          return tagsSource;
71      }
72  
73      public TagsSource remove(long sourceId)
74          throws NoSuchSourceException, SystemException {
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              TagsSource tagsSource = (TagsSource)session.get(TagsSourceImpl.class,
81                      new Long(sourceId));
82  
83              if (tagsSource == null) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn("No TagsSource exists with the primary key " +
86                          sourceId);
87                  }
88  
89                  throw new NoSuchSourceException(
90                      "No TagsSource exists with the primary key " + sourceId);
91              }
92  
93              return remove(tagsSource);
94          }
95          catch (NoSuchSourceException nsee) {
96              throw nsee;
97          }
98          catch (Exception e) {
99              throw HibernateUtil.processException(e);
100         }
101         finally {
102             closeSession(session);
103         }
104     }
105 
106     public TagsSource remove(TagsSource tagsSource) throws SystemException {
107         if (_listeners != null) {
108             for (ModelListener listener : _listeners) {
109                 listener.onBeforeRemove(tagsSource);
110             }
111         }
112 
113         tagsSource = removeImpl(tagsSource);
114 
115         if (_listeners != null) {
116             for (ModelListener listener : _listeners) {
117                 listener.onAfterRemove(tagsSource);
118             }
119         }
120 
121         return tagsSource;
122     }
123 
124     protected TagsSource removeImpl(TagsSource tagsSource)
125         throws SystemException {
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             session.delete(tagsSource);
132 
133             session.flush();
134 
135             return tagsSource;
136         }
137         catch (Exception e) {
138             throw HibernateUtil.processException(e);
139         }
140         finally {
141             closeSession(session);
142 
143             FinderCache.clearCache(TagsSource.class.getName());
144         }
145     }
146 
147     /**
148      * @deprecated Use <code>update(TagsSource tagsSource, boolean merge)</code>.
149      */
150     public TagsSource update(TagsSource tagsSource) throws SystemException {
151         if (_log.isWarnEnabled()) {
152             _log.warn(
153                 "Using the deprecated update(TagsSource tagsSource) method. Use update(TagsSource tagsSource, boolean merge) instead.");
154         }
155 
156         return update(tagsSource, false);
157     }
158 
159     /**
160      * Add, update, or merge, the entity. This method also calls the model
161      * listeners to trigger the proper events associated with adding, deleting,
162      * or updating an entity.
163      *
164      * @param        tagsSource the entity to add, update, or merge
165      * @param        merge boolean value for whether to merge the entity. The
166      *                default value is false. Setting merge to true is more
167      *                expensive and should only be true when tagsSource is
168      *                transient. See LEP-5473 for a detailed discussion of this
169      *                method.
170      * @return        true if the portlet can be displayed via Ajax
171      */
172     public TagsSource update(TagsSource tagsSource, boolean merge)
173         throws SystemException {
174         boolean isNew = tagsSource.isNew();
175 
176         if (_listeners != null) {
177             for (ModelListener listener : _listeners) {
178                 if (isNew) {
179                     listener.onBeforeCreate(tagsSource);
180                 }
181                 else {
182                     listener.onBeforeUpdate(tagsSource);
183                 }
184             }
185         }
186 
187         tagsSource = updateImpl(tagsSource, merge);
188 
189         if (_listeners != null) {
190             for (ModelListener listener : _listeners) {
191                 if (isNew) {
192                     listener.onAfterCreate(tagsSource);
193                 }
194                 else {
195                     listener.onAfterUpdate(tagsSource);
196                 }
197             }
198         }
199 
200         return tagsSource;
201     }
202 
203     public TagsSource updateImpl(
204         com.liferay.portlet.tags.model.TagsSource tagsSource, boolean merge)
205         throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             if (merge) {
212                 session.merge(tagsSource);
213             }
214             else {
215                 if (tagsSource.isNew()) {
216                     session.save(tagsSource);
217                 }
218             }
219 
220             session.flush();
221 
222             tagsSource.setNew(false);
223 
224             return tagsSource;
225         }
226         catch (Exception e) {
227             throw HibernateUtil.processException(e);
228         }
229         finally {
230             closeSession(session);
231 
232             FinderCache.clearCache(TagsSource.class.getName());
233         }
234     }
235 
236     public TagsSource findByPrimaryKey(long sourceId)
237         throws NoSuchSourceException, SystemException {
238         TagsSource tagsSource = fetchByPrimaryKey(sourceId);
239 
240         if (tagsSource == null) {
241             if (_log.isWarnEnabled()) {
242                 _log.warn("No TagsSource exists with the primary key " +
243                     sourceId);
244             }
245 
246             throw new NoSuchSourceException(
247                 "No TagsSource exists with the primary key " + sourceId);
248         }
249 
250         return tagsSource;
251     }
252 
253     public TagsSource fetchByPrimaryKey(long sourceId)
254         throws SystemException {
255         Session session = null;
256 
257         try {
258             session = openSession();
259 
260             return (TagsSource)session.get(TagsSourceImpl.class,
261                 new Long(sourceId));
262         }
263         catch (Exception e) {
264             throw HibernateUtil.processException(e);
265         }
266         finally {
267             closeSession(session);
268         }
269     }
270 
271     public List<TagsSource> findWithDynamicQuery(
272         DynamicQueryInitializer queryInitializer) throws SystemException {
273         Session session = null;
274 
275         try {
276             session = openSession();
277 
278             DynamicQuery query = queryInitializer.initialize(session);
279 
280             return query.list();
281         }
282         catch (Exception e) {
283             throw HibernateUtil.processException(e);
284         }
285         finally {
286             closeSession(session);
287         }
288     }
289 
290     public List<TagsSource> findWithDynamicQuery(
291         DynamicQueryInitializer queryInitializer, int begin, int end)
292         throws SystemException {
293         Session session = null;
294 
295         try {
296             session = openSession();
297 
298             DynamicQuery query = queryInitializer.initialize(session);
299 
300             query.setLimit(begin, end);
301 
302             return query.list();
303         }
304         catch (Exception e) {
305             throw HibernateUtil.processException(e);
306         }
307         finally {
308             closeSession(session);
309         }
310     }
311 
312     public List<TagsSource> findAll() throws SystemException {
313         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
314     }
315 
316     public List<TagsSource> findAll(int begin, int end)
317         throws SystemException {
318         return findAll(begin, end, null);
319     }
320 
321     public List<TagsSource> findAll(int begin, int end, OrderByComparator obc)
322         throws SystemException {
323         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
324         String finderClassName = TagsSource.class.getName();
325         String finderMethodName = "findAll";
326         String[] finderParams = new String[] {
327                 "java.lang.Integer", "java.lang.Integer",
328                 "com.liferay.portal.kernel.util.OrderByComparator"
329             };
330         Object[] finderArgs = new Object[] {
331                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
332             };
333 
334         Object result = null;
335 
336         if (finderClassNameCacheEnabled) {
337             result = FinderCache.getResult(finderClassName, finderMethodName,
338                     finderParams, finderArgs, getSessionFactory());
339         }
340 
341         if (result == null) {
342             Session session = null;
343 
344             try {
345                 session = openSession();
346 
347                 StringMaker query = new StringMaker();
348 
349                 query.append("FROM com.liferay.portlet.tags.model.TagsSource ");
350 
351                 if (obc != null) {
352                     query.append("ORDER BY ");
353                     query.append(obc.getOrderBy());
354                 }
355 
356                 Query q = session.createQuery(query.toString());
357 
358                 List<TagsSource> list = (List<TagsSource>)QueryUtil.list(q,
359                         getDialect(), begin, end);
360 
361                 if (obc == null) {
362                     Collections.sort(list);
363                 }
364 
365                 FinderCache.putResult(finderClassNameCacheEnabled,
366                     finderClassName, finderMethodName, finderParams,
367                     finderArgs, list);
368 
369                 return list;
370             }
371             catch (Exception e) {
372                 throw HibernateUtil.processException(e);
373             }
374             finally {
375                 closeSession(session);
376             }
377         }
378         else {
379             return (List<TagsSource>)result;
380         }
381     }
382 
383     public void removeAll() throws SystemException {
384         for (TagsSource tagsSource : findAll()) {
385             remove(tagsSource);
386         }
387     }
388 
389     public int countAll() throws SystemException {
390         boolean finderClassNameCacheEnabled = TagsSourceModelImpl.CACHE_ENABLED;
391         String finderClassName = TagsSource.class.getName();
392         String finderMethodName = "countAll";
393         String[] finderParams = new String[] {  };
394         Object[] finderArgs = new Object[] {  };
395 
396         Object result = null;
397 
398         if (finderClassNameCacheEnabled) {
399             result = FinderCache.getResult(finderClassName, finderMethodName,
400                     finderParams, finderArgs, getSessionFactory());
401         }
402 
403         if (result == null) {
404             Session session = null;
405 
406             try {
407                 session = openSession();
408 
409                 Query q = session.createQuery(
410                         "SELECT COUNT(*) FROM com.liferay.portlet.tags.model.TagsSource");
411 
412                 Long count = null;
413 
414                 Iterator<Long> itr = q.list().iterator();
415 
416                 if (itr.hasNext()) {
417                     count = itr.next();
418                 }
419 
420                 if (count == null) {
421                     count = new Long(0);
422                 }
423 
424                 FinderCache.putResult(finderClassNameCacheEnabled,
425                     finderClassName, finderMethodName, finderParams,
426                     finderArgs, count);
427 
428                 return count.intValue();
429             }
430             catch (Exception e) {
431                 throw HibernateUtil.processException(e);
432             }
433             finally {
434                 closeSession(session);
435             }
436         }
437         else {
438             return ((Long)result).intValue();
439         }
440     }
441 
442     protected void initDao() {
443         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
444                     PropsUtil.get(
445                         "value.object.listener.com.liferay.portlet.tags.model.TagsSource")));
446 
447         if (listenerClassNames.length > 0) {
448             try {
449                 List<ModelListener> listeners = new ArrayList<ModelListener>();
450 
451                 for (String listenerClassName : listenerClassNames) {
452                     listeners.add((ModelListener)Class.forName(
453                             listenerClassName).newInstance());
454                 }
455 
456                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
457             }
458             catch (Exception e) {
459                 _log.error(e);
460             }
461         }
462     }
463 
464     private static Log _log = LogFactory.getLog(TagsSourcePersistenceImpl.class);
465     private ModelListener[] _listeners;
466 }