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.messageboards.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.StringPool;
32  import com.liferay.portal.kernel.util.Validator;
33  import com.liferay.portal.model.ModelListener;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  import com.liferay.portal.util.PropsUtil;
38  
39  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
40  import com.liferay.portlet.messageboards.model.MBDiscussion;
41  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
42  import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
43  
44  import com.liferay.util.dao.hibernate.QueryUtil;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import org.hibernate.Query;
50  import org.hibernate.Session;
51  
52  import java.util.Collections;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  /**
57   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class MBDiscussionPersistenceImpl extends BasePersistence
63      implements MBDiscussionPersistence {
64      public MBDiscussion create(long discussionId) {
65          MBDiscussion mbDiscussion = new MBDiscussionImpl();
66  
67          mbDiscussion.setNew(true);
68          mbDiscussion.setPrimaryKey(discussionId);
69  
70          return mbDiscussion;
71      }
72  
73      public MBDiscussion remove(long discussionId)
74          throws NoSuchDiscussionException, SystemException {
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
81                      new Long(discussionId));
82  
83              if (mbDiscussion == null) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn("No MBDiscussion exists with the primary key " +
86                          discussionId);
87                  }
88  
89                  throw new NoSuchDiscussionException(
90                      "No MBDiscussion exists with the primary key " +
91                      discussionId);
92              }
93  
94              return remove(mbDiscussion);
95          }
96          catch (NoSuchDiscussionException nsee) {
97              throw nsee;
98          }
99          catch (Exception e) {
100             throw HibernateUtil.processException(e);
101         }
102         finally {
103             closeSession(session);
104         }
105     }
106 
107     public MBDiscussion remove(MBDiscussion mbDiscussion)
108         throws SystemException {
109         ModelListener listener = _getListener();
110 
111         if (listener != null) {
112             listener.onBeforeRemove(mbDiscussion);
113         }
114 
115         mbDiscussion = removeImpl(mbDiscussion);
116 
117         if (listener != null) {
118             listener.onAfterRemove(mbDiscussion);
119         }
120 
121         return mbDiscussion;
122     }
123 
124     protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
125         throws SystemException {
126         Session session = null;
127 
128         try {
129             session = openSession();
130 
131             session.delete(mbDiscussion);
132 
133             session.flush();
134 
135             return mbDiscussion;
136         }
137         catch (Exception e) {
138             throw HibernateUtil.processException(e);
139         }
140         finally {
141             closeSession(session);
142 
143             FinderCache.clearCache(MBDiscussion.class.getName());
144         }
145     }
146 
147     public MBDiscussion update(MBDiscussion mbDiscussion)
148         throws SystemException {
149         return update(mbDiscussion, false);
150     }
151 
152     public MBDiscussion update(MBDiscussion mbDiscussion, boolean merge)
153         throws SystemException {
154         ModelListener listener = _getListener();
155 
156         boolean isNew = mbDiscussion.isNew();
157 
158         if (listener != null) {
159             if (isNew) {
160                 listener.onBeforeCreate(mbDiscussion);
161             }
162             else {
163                 listener.onBeforeUpdate(mbDiscussion);
164             }
165         }
166 
167         mbDiscussion = updateImpl(mbDiscussion, merge);
168 
169         if (listener != null) {
170             if (isNew) {
171                 listener.onAfterCreate(mbDiscussion);
172             }
173             else {
174                 listener.onAfterUpdate(mbDiscussion);
175             }
176         }
177 
178         return mbDiscussion;
179     }
180 
181     public MBDiscussion updateImpl(
182         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
183         boolean merge) throws SystemException {
184         Session session = null;
185 
186         try {
187             session = openSession();
188 
189             if (merge) {
190                 session.merge(mbDiscussion);
191             }
192             else {
193                 if (mbDiscussion.isNew()) {
194                     session.save(mbDiscussion);
195                 }
196             }
197 
198             session.flush();
199 
200             mbDiscussion.setNew(false);
201 
202             return mbDiscussion;
203         }
204         catch (Exception e) {
205             throw HibernateUtil.processException(e);
206         }
207         finally {
208             closeSession(session);
209 
210             FinderCache.clearCache(MBDiscussion.class.getName());
211         }
212     }
213 
214     public MBDiscussion findByPrimaryKey(long discussionId)
215         throws NoSuchDiscussionException, SystemException {
216         MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
217 
218         if (mbDiscussion == null) {
219             if (_log.isWarnEnabled()) {
220                 _log.warn("No MBDiscussion exists with the primary key " +
221                     discussionId);
222             }
223 
224             throw new NoSuchDiscussionException(
225                 "No MBDiscussion exists with the primary key " + discussionId);
226         }
227 
228         return mbDiscussion;
229     }
230 
231     public MBDiscussion fetchByPrimaryKey(long discussionId)
232         throws SystemException {
233         Session session = null;
234 
235         try {
236             session = openSession();
237 
238             return (MBDiscussion)session.get(MBDiscussionImpl.class,
239                 new Long(discussionId));
240         }
241         catch (Exception e) {
242             throw HibernateUtil.processException(e);
243         }
244         finally {
245             closeSession(session);
246         }
247     }
248 
249     public MBDiscussion findByC_C(long classNameId, long classPK)
250         throws NoSuchDiscussionException, SystemException {
251         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
252 
253         if (mbDiscussion == null) {
254             StringMaker msg = new StringMaker();
255 
256             msg.append("No MBDiscussion exists with the key {");
257 
258             msg.append("classNameId=" + classNameId);
259 
260             msg.append(", ");
261             msg.append("classPK=" + classPK);
262 
263             msg.append(StringPool.CLOSE_CURLY_BRACE);
264 
265             if (_log.isWarnEnabled()) {
266                 _log.warn(msg.toString());
267             }
268 
269             throw new NoSuchDiscussionException(msg.toString());
270         }
271 
272         return mbDiscussion;
273     }
274 
275     public MBDiscussion fetchByC_C(long classNameId, long classPK)
276         throws SystemException {
277         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
278         String finderClassName = MBDiscussion.class.getName();
279         String finderMethodName = "fetchByC_C";
280         String[] finderParams = new String[] {
281                 Long.class.getName(), Long.class.getName()
282             };
283         Object[] finderArgs = new Object[] {
284                 new Long(classNameId), new Long(classPK)
285             };
286 
287         Object result = null;
288 
289         if (finderClassNameCacheEnabled) {
290             result = FinderCache.getResult(finderClassName, finderMethodName,
291                     finderParams, finderArgs, getSessionFactory());
292         }
293 
294         if (result == null) {
295             Session session = null;
296 
297             try {
298                 session = openSession();
299 
300                 StringMaker query = new StringMaker();
301 
302                 query.append(
303                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
304 
305                 query.append("classNameId = ?");
306 
307                 query.append(" AND ");
308 
309                 query.append("classPK = ?");
310 
311                 query.append(" ");
312 
313                 Query q = session.createQuery(query.toString());
314 
315                 int queryPos = 0;
316 
317                 q.setLong(queryPos++, classNameId);
318 
319                 q.setLong(queryPos++, classPK);
320 
321                 List list = q.list();
322 
323                 FinderCache.putResult(finderClassNameCacheEnabled,
324                     finderClassName, finderMethodName, finderParams,
325                     finderArgs, list);
326 
327                 if (list.size() == 0) {
328                     return null;
329                 }
330                 else {
331                     return (MBDiscussion)list.get(0);
332                 }
333             }
334             catch (Exception e) {
335                 throw HibernateUtil.processException(e);
336             }
337             finally {
338                 closeSession(session);
339             }
340         }
341         else {
342             List list = (List)result;
343 
344             if (list.size() == 0) {
345                 return null;
346             }
347             else {
348                 return (MBDiscussion)list.get(0);
349             }
350         }
351     }
352 
353     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
354         throws SystemException {
355         Session session = null;
356 
357         try {
358             session = openSession();
359 
360             DynamicQuery query = queryInitializer.initialize(session);
361 
362             return query.list();
363         }
364         catch (Exception e) {
365             throw HibernateUtil.processException(e);
366         }
367         finally {
368             closeSession(session);
369         }
370     }
371 
372     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
373         int begin, int end) throws SystemException {
374         Session session = null;
375 
376         try {
377             session = openSession();
378 
379             DynamicQuery query = queryInitializer.initialize(session);
380 
381             query.setLimit(begin, end);
382 
383             return query.list();
384         }
385         catch (Exception e) {
386             throw HibernateUtil.processException(e);
387         }
388         finally {
389             closeSession(session);
390         }
391     }
392 
393     public List findAll() throws SystemException {
394         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
395     }
396 
397     public List findAll(int begin, int end) throws SystemException {
398         return findAll(begin, end, null);
399     }
400 
401     public List findAll(int begin, int end, OrderByComparator obc)
402         throws SystemException {
403         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
404         String finderClassName = MBDiscussion.class.getName();
405         String finderMethodName = "findAll";
406         String[] finderParams = new String[] {
407                 "java.lang.Integer", "java.lang.Integer",
408                 "com.liferay.portal.kernel.util.OrderByComparator"
409             };
410         Object[] finderArgs = new Object[] {
411                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
412             };
413 
414         Object result = null;
415 
416         if (finderClassNameCacheEnabled) {
417             result = FinderCache.getResult(finderClassName, finderMethodName,
418                     finderParams, finderArgs, getSessionFactory());
419         }
420 
421         if (result == null) {
422             Session session = null;
423 
424             try {
425                 session = openSession();
426 
427                 StringMaker query = new StringMaker();
428 
429                 query.append(
430                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion ");
431 
432                 if (obc != null) {
433                     query.append("ORDER BY ");
434                     query.append(obc.getOrderBy());
435                 }
436 
437                 Query q = session.createQuery(query.toString());
438 
439                 List list = QueryUtil.list(q, getDialect(), begin, end);
440 
441                 if (obc == null) {
442                     Collections.sort(list);
443                 }
444 
445                 FinderCache.putResult(finderClassNameCacheEnabled,
446                     finderClassName, finderMethodName, finderParams,
447                     finderArgs, list);
448 
449                 return list;
450             }
451             catch (Exception e) {
452                 throw HibernateUtil.processException(e);
453             }
454             finally {
455                 closeSession(session);
456             }
457         }
458         else {
459             return (List)result;
460         }
461     }
462 
463     public void removeByC_C(long classNameId, long classPK)
464         throws NoSuchDiscussionException, SystemException {
465         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
466 
467         remove(mbDiscussion);
468     }
469 
470     public void removeAll() throws SystemException {
471         Iterator itr = findAll().iterator();
472 
473         while (itr.hasNext()) {
474             remove((MBDiscussion)itr.next());
475         }
476     }
477 
478     public int countByC_C(long classNameId, long classPK)
479         throws SystemException {
480         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
481         String finderClassName = MBDiscussion.class.getName();
482         String finderMethodName = "countByC_C";
483         String[] finderParams = new String[] {
484                 Long.class.getName(), Long.class.getName()
485             };
486         Object[] finderArgs = new Object[] {
487                 new Long(classNameId), new Long(classPK)
488             };
489 
490         Object result = null;
491 
492         if (finderClassNameCacheEnabled) {
493             result = FinderCache.getResult(finderClassName, finderMethodName,
494                     finderParams, finderArgs, getSessionFactory());
495         }
496 
497         if (result == null) {
498             Session session = null;
499 
500             try {
501                 session = openSession();
502 
503                 StringMaker query = new StringMaker();
504 
505                 query.append("SELECT COUNT(*) ");
506                 query.append(
507                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
508 
509                 query.append("classNameId = ?");
510 
511                 query.append(" AND ");
512 
513                 query.append("classPK = ?");
514 
515                 query.append(" ");
516 
517                 Query q = session.createQuery(query.toString());
518 
519                 int queryPos = 0;
520 
521                 q.setLong(queryPos++, classNameId);
522 
523                 q.setLong(queryPos++, classPK);
524 
525                 Long count = null;
526 
527                 Iterator itr = q.list().iterator();
528 
529                 if (itr.hasNext()) {
530                     count = (Long)itr.next();
531                 }
532 
533                 if (count == null) {
534                     count = new Long(0);
535                 }
536 
537                 FinderCache.putResult(finderClassNameCacheEnabled,
538                     finderClassName, finderMethodName, finderParams,
539                     finderArgs, count);
540 
541                 return count.intValue();
542             }
543             catch (Exception e) {
544                 throw HibernateUtil.processException(e);
545             }
546             finally {
547                 closeSession(session);
548             }
549         }
550         else {
551             return ((Long)result).intValue();
552         }
553     }
554 
555     public int countAll() throws SystemException {
556         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
557         String finderClassName = MBDiscussion.class.getName();
558         String finderMethodName = "countAll";
559         String[] finderParams = new String[] {  };
560         Object[] finderArgs = new Object[] {  };
561 
562         Object result = null;
563 
564         if (finderClassNameCacheEnabled) {
565             result = FinderCache.getResult(finderClassName, finderMethodName,
566                     finderParams, finderArgs, getSessionFactory());
567         }
568 
569         if (result == null) {
570             Session session = null;
571 
572             try {
573                 session = openSession();
574 
575                 Query q = session.createQuery(
576                         "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBDiscussion");
577 
578                 Long count = null;
579 
580                 Iterator itr = q.list().iterator();
581 
582                 if (itr.hasNext()) {
583                     count = (Long)itr.next();
584                 }
585 
586                 if (count == null) {
587                     count = new Long(0);
588                 }
589 
590                 FinderCache.putResult(finderClassNameCacheEnabled,
591                     finderClassName, finderMethodName, finderParams,
592                     finderArgs, count);
593 
594                 return count.intValue();
595             }
596             catch (Exception e) {
597                 throw HibernateUtil.processException(e);
598             }
599             finally {
600                 closeSession(session);
601             }
602         }
603         else {
604             return ((Long)result).intValue();
605         }
606     }
607 
608     protected void initDao() {
609     }
610 
611     private static ModelListener _getListener() {
612         if (Validator.isNotNull(_LISTENER)) {
613             try {
614                 return (ModelListener)Class.forName(_LISTENER).newInstance();
615             }
616             catch (Exception e) {
617                 _log.error(e);
618             }
619         }
620 
621         return null;
622     }
623 
624     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
625                 "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion"));
626     private static Log _log = LogFactory.getLog(MBDiscussionPersistenceImpl.class);
627 }