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.journal.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.journal.NoSuchArticleResourceException;
40  import com.liferay.portlet.journal.model.JournalArticleResource;
41  import com.liferay.portlet.journal.model.impl.JournalArticleResourceImpl;
42  import com.liferay.portlet.journal.model.impl.JournalArticleResourceModelImpl;
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="JournalArticleResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class JournalArticleResourcePersistenceImpl extends BasePersistence
63      implements JournalArticleResourcePersistence {
64      public JournalArticleResource create(long resourcePrimKey) {
65          JournalArticleResource journalArticleResource = new JournalArticleResourceImpl();
66  
67          journalArticleResource.setNew(true);
68          journalArticleResource.setPrimaryKey(resourcePrimKey);
69  
70          return journalArticleResource;
71      }
72  
73      public JournalArticleResource remove(long resourcePrimKey)
74          throws NoSuchArticleResourceException, SystemException {
75          Session session = null;
76  
77          try {
78              session = openSession();
79  
80              JournalArticleResource journalArticleResource = (JournalArticleResource)session.get(JournalArticleResourceImpl.class,
81                      new Long(resourcePrimKey));
82  
83              if (journalArticleResource == null) {
84                  if (_log.isWarnEnabled()) {
85                      _log.warn(
86                          "No JournalArticleResource exists with the primary key " +
87                          resourcePrimKey);
88                  }
89  
90                  throw new NoSuchArticleResourceException(
91                      "No JournalArticleResource exists with the primary key " +
92                      resourcePrimKey);
93              }
94  
95              return remove(journalArticleResource);
96          }
97          catch (NoSuchArticleResourceException nsee) {
98              throw nsee;
99          }
100         catch (Exception e) {
101             throw HibernateUtil.processException(e);
102         }
103         finally {
104             closeSession(session);
105         }
106     }
107 
108     public JournalArticleResource remove(
109         JournalArticleResource journalArticleResource)
110         throws SystemException {
111         ModelListener listener = _getListener();
112 
113         if (listener != null) {
114             listener.onBeforeRemove(journalArticleResource);
115         }
116 
117         journalArticleResource = removeImpl(journalArticleResource);
118 
119         if (listener != null) {
120             listener.onAfterRemove(journalArticleResource);
121         }
122 
123         return journalArticleResource;
124     }
125 
126     protected JournalArticleResource removeImpl(
127         JournalArticleResource journalArticleResource)
128         throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             session.delete(journalArticleResource);
135 
136             session.flush();
137 
138             return journalArticleResource;
139         }
140         catch (Exception e) {
141             throw HibernateUtil.processException(e);
142         }
143         finally {
144             closeSession(session);
145 
146             FinderCache.clearCache(JournalArticleResource.class.getName());
147         }
148     }
149 
150     public JournalArticleResource update(
151         JournalArticleResource journalArticleResource)
152         throws SystemException {
153         return update(journalArticleResource, false);
154     }
155 
156     public JournalArticleResource update(
157         JournalArticleResource journalArticleResource, boolean merge)
158         throws SystemException {
159         ModelListener listener = _getListener();
160 
161         boolean isNew = journalArticleResource.isNew();
162 
163         if (listener != null) {
164             if (isNew) {
165                 listener.onBeforeCreate(journalArticleResource);
166             }
167             else {
168                 listener.onBeforeUpdate(journalArticleResource);
169             }
170         }
171 
172         journalArticleResource = updateImpl(journalArticleResource, merge);
173 
174         if (listener != null) {
175             if (isNew) {
176                 listener.onAfterCreate(journalArticleResource);
177             }
178             else {
179                 listener.onAfterUpdate(journalArticleResource);
180             }
181         }
182 
183         return journalArticleResource;
184     }
185 
186     public JournalArticleResource updateImpl(
187         com.liferay.portlet.journal.model.JournalArticleResource journalArticleResource,
188         boolean merge) throws SystemException {
189         Session session = null;
190 
191         try {
192             session = openSession();
193 
194             if (merge) {
195                 session.merge(journalArticleResource);
196             }
197             else {
198                 if (journalArticleResource.isNew()) {
199                     session.save(journalArticleResource);
200                 }
201             }
202 
203             session.flush();
204 
205             journalArticleResource.setNew(false);
206 
207             return journalArticleResource;
208         }
209         catch (Exception e) {
210             throw HibernateUtil.processException(e);
211         }
212         finally {
213             closeSession(session);
214 
215             FinderCache.clearCache(JournalArticleResource.class.getName());
216         }
217     }
218 
219     public JournalArticleResource findByPrimaryKey(long resourcePrimKey)
220         throws NoSuchArticleResourceException, SystemException {
221         JournalArticleResource journalArticleResource = fetchByPrimaryKey(resourcePrimKey);
222 
223         if (journalArticleResource == null) {
224             if (_log.isWarnEnabled()) {
225                 _log.warn(
226                     "No JournalArticleResource exists with the primary key " +
227                     resourcePrimKey);
228             }
229 
230             throw new NoSuchArticleResourceException(
231                 "No JournalArticleResource exists with the primary key " +
232                 resourcePrimKey);
233         }
234 
235         return journalArticleResource;
236     }
237 
238     public JournalArticleResource fetchByPrimaryKey(long resourcePrimKey)
239         throws SystemException {
240         Session session = null;
241 
242         try {
243             session = openSession();
244 
245             return (JournalArticleResource)session.get(JournalArticleResourceImpl.class,
246                 new Long(resourcePrimKey));
247         }
248         catch (Exception e) {
249             throw HibernateUtil.processException(e);
250         }
251         finally {
252             closeSession(session);
253         }
254     }
255 
256     public List findByGroupId(long groupId) throws SystemException {
257         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
258         String finderClassName = JournalArticleResource.class.getName();
259         String finderMethodName = "findByGroupId";
260         String[] finderParams = new String[] { Long.class.getName() };
261         Object[] finderArgs = new Object[] { new Long(groupId) };
262 
263         Object result = null;
264 
265         if (finderClassNameCacheEnabled) {
266             result = FinderCache.getResult(finderClassName, finderMethodName,
267                     finderParams, finderArgs, getSessionFactory());
268         }
269 
270         if (result == null) {
271             Session session = null;
272 
273             try {
274                 session = openSession();
275 
276                 StringMaker query = new StringMaker();
277 
278                 query.append(
279                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
280 
281                 query.append("groupId = ?");
282 
283                 query.append(" ");
284 
285                 Query q = session.createQuery(query.toString());
286 
287                 int queryPos = 0;
288 
289                 q.setLong(queryPos++, groupId);
290 
291                 List list = q.list();
292 
293                 FinderCache.putResult(finderClassNameCacheEnabled,
294                     finderClassName, finderMethodName, finderParams,
295                     finderArgs, list);
296 
297                 return list;
298             }
299             catch (Exception e) {
300                 throw HibernateUtil.processException(e);
301             }
302             finally {
303                 closeSession(session);
304             }
305         }
306         else {
307             return (List)result;
308         }
309     }
310 
311     public List findByGroupId(long groupId, int begin, int end)
312         throws SystemException {
313         return findByGroupId(groupId, begin, end, null);
314     }
315 
316     public List findByGroupId(long groupId, int begin, int end,
317         OrderByComparator obc) throws SystemException {
318         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
319         String finderClassName = JournalArticleResource.class.getName();
320         String finderMethodName = "findByGroupId";
321         String[] finderParams = new String[] {
322                 Long.class.getName(),
323                 
324                 "java.lang.Integer", "java.lang.Integer",
325                 "com.liferay.portal.kernel.util.OrderByComparator"
326             };
327         Object[] finderArgs = new Object[] {
328                 new Long(groupId),
329                 
330                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
331             };
332 
333         Object result = null;
334 
335         if (finderClassNameCacheEnabled) {
336             result = FinderCache.getResult(finderClassName, finderMethodName,
337                     finderParams, finderArgs, getSessionFactory());
338         }
339 
340         if (result == null) {
341             Session session = null;
342 
343             try {
344                 session = openSession();
345 
346                 StringMaker query = new StringMaker();
347 
348                 query.append(
349                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
350 
351                 query.append("groupId = ?");
352 
353                 query.append(" ");
354 
355                 if (obc != null) {
356                     query.append("ORDER BY ");
357                     query.append(obc.getOrderBy());
358                 }
359 
360                 Query q = session.createQuery(query.toString());
361 
362                 int queryPos = 0;
363 
364                 q.setLong(queryPos++, groupId);
365 
366                 List list = QueryUtil.list(q, getDialect(), begin, end);
367 
368                 FinderCache.putResult(finderClassNameCacheEnabled,
369                     finderClassName, finderMethodName, finderParams,
370                     finderArgs, list);
371 
372                 return list;
373             }
374             catch (Exception e) {
375                 throw HibernateUtil.processException(e);
376             }
377             finally {
378                 closeSession(session);
379             }
380         }
381         else {
382             return (List)result;
383         }
384     }
385 
386     public JournalArticleResource findByGroupId_First(long groupId,
387         OrderByComparator obc)
388         throws NoSuchArticleResourceException, SystemException {
389         List list = findByGroupId(groupId, 0, 1, obc);
390 
391         if (list.size() == 0) {
392             StringMaker msg = new StringMaker();
393 
394             msg.append("No JournalArticleResource exists with the key {");
395 
396             msg.append("groupId=" + groupId);
397 
398             msg.append(StringPool.CLOSE_CURLY_BRACE);
399 
400             throw new NoSuchArticleResourceException(msg.toString());
401         }
402         else {
403             return (JournalArticleResource)list.get(0);
404         }
405     }
406 
407     public JournalArticleResource findByGroupId_Last(long groupId,
408         OrderByComparator obc)
409         throws NoSuchArticleResourceException, SystemException {
410         int count = countByGroupId(groupId);
411 
412         List list = findByGroupId(groupId, count - 1, count, obc);
413 
414         if (list.size() == 0) {
415             StringMaker msg = new StringMaker();
416 
417             msg.append("No JournalArticleResource exists with the key {");
418 
419             msg.append("groupId=" + groupId);
420 
421             msg.append(StringPool.CLOSE_CURLY_BRACE);
422 
423             throw new NoSuchArticleResourceException(msg.toString());
424         }
425         else {
426             return (JournalArticleResource)list.get(0);
427         }
428     }
429 
430     public JournalArticleResource[] findByGroupId_PrevAndNext(
431         long resourcePrimKey, long groupId, OrderByComparator obc)
432         throws NoSuchArticleResourceException, SystemException {
433         JournalArticleResource journalArticleResource = findByPrimaryKey(resourcePrimKey);
434 
435         int count = countByGroupId(groupId);
436 
437         Session session = null;
438 
439         try {
440             session = openSession();
441 
442             StringMaker query = new StringMaker();
443 
444             query.append(
445                 "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
446 
447             query.append("groupId = ?");
448 
449             query.append(" ");
450 
451             if (obc != null) {
452                 query.append("ORDER BY ");
453                 query.append(obc.getOrderBy());
454             }
455 
456             Query q = session.createQuery(query.toString());
457 
458             int queryPos = 0;
459 
460             q.setLong(queryPos++, groupId);
461 
462             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
463                     journalArticleResource);
464 
465             JournalArticleResource[] array = new JournalArticleResourceImpl[3];
466 
467             array[0] = (JournalArticleResource)objArray[0];
468             array[1] = (JournalArticleResource)objArray[1];
469             array[2] = (JournalArticleResource)objArray[2];
470 
471             return array;
472         }
473         catch (Exception e) {
474             throw HibernateUtil.processException(e);
475         }
476         finally {
477             closeSession(session);
478         }
479     }
480 
481     public JournalArticleResource findByG_A(long groupId, String articleId)
482         throws NoSuchArticleResourceException, SystemException {
483         JournalArticleResource journalArticleResource = fetchByG_A(groupId,
484                 articleId);
485 
486         if (journalArticleResource == null) {
487             StringMaker msg = new StringMaker();
488 
489             msg.append("No JournalArticleResource exists with the key {");
490 
491             msg.append("groupId=" + groupId);
492 
493             msg.append(", ");
494             msg.append("articleId=" + articleId);
495 
496             msg.append(StringPool.CLOSE_CURLY_BRACE);
497 
498             if (_log.isWarnEnabled()) {
499                 _log.warn(msg.toString());
500             }
501 
502             throw new NoSuchArticleResourceException(msg.toString());
503         }
504 
505         return journalArticleResource;
506     }
507 
508     public JournalArticleResource fetchByG_A(long groupId, String articleId)
509         throws SystemException {
510         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
511         String finderClassName = JournalArticleResource.class.getName();
512         String finderMethodName = "fetchByG_A";
513         String[] finderParams = new String[] {
514                 Long.class.getName(), String.class.getName()
515             };
516         Object[] finderArgs = new Object[] { new Long(groupId), articleId };
517 
518         Object result = null;
519 
520         if (finderClassNameCacheEnabled) {
521             result = FinderCache.getResult(finderClassName, finderMethodName,
522                     finderParams, finderArgs, getSessionFactory());
523         }
524 
525         if (result == null) {
526             Session session = null;
527 
528             try {
529                 session = openSession();
530 
531                 StringMaker query = new StringMaker();
532 
533                 query.append(
534                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
535 
536                 query.append("groupId = ?");
537 
538                 query.append(" AND ");
539 
540                 if (articleId == null) {
541                     query.append("articleId IS NULL");
542                 }
543                 else {
544                     query.append("articleId = ?");
545                 }
546 
547                 query.append(" ");
548 
549                 Query q = session.createQuery(query.toString());
550 
551                 int queryPos = 0;
552 
553                 q.setLong(queryPos++, groupId);
554 
555                 if (articleId != null) {
556                     q.setString(queryPos++, articleId);
557                 }
558 
559                 List list = q.list();
560 
561                 FinderCache.putResult(finderClassNameCacheEnabled,
562                     finderClassName, finderMethodName, finderParams,
563                     finderArgs, list);
564 
565                 if (list.size() == 0) {
566                     return null;
567                 }
568                 else {
569                     return (JournalArticleResource)list.get(0);
570                 }
571             }
572             catch (Exception e) {
573                 throw HibernateUtil.processException(e);
574             }
575             finally {
576                 closeSession(session);
577             }
578         }
579         else {
580             List list = (List)result;
581 
582             if (list.size() == 0) {
583                 return null;
584             }
585             else {
586                 return (JournalArticleResource)list.get(0);
587             }
588         }
589     }
590 
591     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
592         throws SystemException {
593         Session session = null;
594 
595         try {
596             session = openSession();
597 
598             DynamicQuery query = queryInitializer.initialize(session);
599 
600             return query.list();
601         }
602         catch (Exception e) {
603             throw HibernateUtil.processException(e);
604         }
605         finally {
606             closeSession(session);
607         }
608     }
609 
610     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
611         int begin, int end) throws SystemException {
612         Session session = null;
613 
614         try {
615             session = openSession();
616 
617             DynamicQuery query = queryInitializer.initialize(session);
618 
619             query.setLimit(begin, end);
620 
621             return query.list();
622         }
623         catch (Exception e) {
624             throw HibernateUtil.processException(e);
625         }
626         finally {
627             closeSession(session);
628         }
629     }
630 
631     public List findAll() throws SystemException {
632         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
633     }
634 
635     public List findAll(int begin, int end) throws SystemException {
636         return findAll(begin, end, null);
637     }
638 
639     public List findAll(int begin, int end, OrderByComparator obc)
640         throws SystemException {
641         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
642         String finderClassName = JournalArticleResource.class.getName();
643         String finderMethodName = "findAll";
644         String[] finderParams = new String[] {
645                 "java.lang.Integer", "java.lang.Integer",
646                 "com.liferay.portal.kernel.util.OrderByComparator"
647             };
648         Object[] finderArgs = new Object[] {
649                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
650             };
651 
652         Object result = null;
653 
654         if (finderClassNameCacheEnabled) {
655             result = FinderCache.getResult(finderClassName, finderMethodName,
656                     finderParams, finderArgs, getSessionFactory());
657         }
658 
659         if (result == null) {
660             Session session = null;
661 
662             try {
663                 session = openSession();
664 
665                 StringMaker query = new StringMaker();
666 
667                 query.append(
668                     "FROM com.liferay.portlet.journal.model.JournalArticleResource ");
669 
670                 if (obc != null) {
671                     query.append("ORDER BY ");
672                     query.append(obc.getOrderBy());
673                 }
674 
675                 Query q = session.createQuery(query.toString());
676 
677                 List list = QueryUtil.list(q, getDialect(), begin, end);
678 
679                 if (obc == null) {
680                     Collections.sort(list);
681                 }
682 
683                 FinderCache.putResult(finderClassNameCacheEnabled,
684                     finderClassName, finderMethodName, finderParams,
685                     finderArgs, list);
686 
687                 return list;
688             }
689             catch (Exception e) {
690                 throw HibernateUtil.processException(e);
691             }
692             finally {
693                 closeSession(session);
694             }
695         }
696         else {
697             return (List)result;
698         }
699     }
700 
701     public void removeByGroupId(long groupId) throws SystemException {
702         Iterator itr = findByGroupId(groupId).iterator();
703 
704         while (itr.hasNext()) {
705             JournalArticleResource journalArticleResource = (JournalArticleResource)itr.next();
706 
707             remove(journalArticleResource);
708         }
709     }
710 
711     public void removeByG_A(long groupId, String articleId)
712         throws NoSuchArticleResourceException, SystemException {
713         JournalArticleResource journalArticleResource = findByG_A(groupId,
714                 articleId);
715 
716         remove(journalArticleResource);
717     }
718 
719     public void removeAll() throws SystemException {
720         Iterator itr = findAll().iterator();
721 
722         while (itr.hasNext()) {
723             remove((JournalArticleResource)itr.next());
724         }
725     }
726 
727     public int countByGroupId(long groupId) throws SystemException {
728         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
729         String finderClassName = JournalArticleResource.class.getName();
730         String finderMethodName = "countByGroupId";
731         String[] finderParams = new String[] { Long.class.getName() };
732         Object[] finderArgs = new Object[] { new Long(groupId) };
733 
734         Object result = null;
735 
736         if (finderClassNameCacheEnabled) {
737             result = FinderCache.getResult(finderClassName, finderMethodName,
738                     finderParams, finderArgs, getSessionFactory());
739         }
740 
741         if (result == null) {
742             Session session = null;
743 
744             try {
745                 session = openSession();
746 
747                 StringMaker query = new StringMaker();
748 
749                 query.append("SELECT COUNT(*) ");
750                 query.append(
751                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
752 
753                 query.append("groupId = ?");
754 
755                 query.append(" ");
756 
757                 Query q = session.createQuery(query.toString());
758 
759                 int queryPos = 0;
760 
761                 q.setLong(queryPos++, groupId);
762 
763                 Long count = null;
764 
765                 Iterator itr = q.list().iterator();
766 
767                 if (itr.hasNext()) {
768                     count = (Long)itr.next();
769                 }
770 
771                 if (count == null) {
772                     count = new Long(0);
773                 }
774 
775                 FinderCache.putResult(finderClassNameCacheEnabled,
776                     finderClassName, finderMethodName, finderParams,
777                     finderArgs, count);
778 
779                 return count.intValue();
780             }
781             catch (Exception e) {
782                 throw HibernateUtil.processException(e);
783             }
784             finally {
785                 closeSession(session);
786             }
787         }
788         else {
789             return ((Long)result).intValue();
790         }
791     }
792 
793     public int countByG_A(long groupId, String articleId)
794         throws SystemException {
795         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
796         String finderClassName = JournalArticleResource.class.getName();
797         String finderMethodName = "countByG_A";
798         String[] finderParams = new String[] {
799                 Long.class.getName(), String.class.getName()
800             };
801         Object[] finderArgs = new Object[] { new Long(groupId), articleId };
802 
803         Object result = null;
804 
805         if (finderClassNameCacheEnabled) {
806             result = FinderCache.getResult(finderClassName, finderMethodName,
807                     finderParams, finderArgs, getSessionFactory());
808         }
809 
810         if (result == null) {
811             Session session = null;
812 
813             try {
814                 session = openSession();
815 
816                 StringMaker query = new StringMaker();
817 
818                 query.append("SELECT COUNT(*) ");
819                 query.append(
820                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
821 
822                 query.append("groupId = ?");
823 
824                 query.append(" AND ");
825 
826                 if (articleId == null) {
827                     query.append("articleId IS NULL");
828                 }
829                 else {
830                     query.append("articleId = ?");
831                 }
832 
833                 query.append(" ");
834 
835                 Query q = session.createQuery(query.toString());
836 
837                 int queryPos = 0;
838 
839                 q.setLong(queryPos++, groupId);
840 
841                 if (articleId != null) {
842                     q.setString(queryPos++, articleId);
843                 }
844 
845                 Long count = null;
846 
847                 Iterator itr = q.list().iterator();
848 
849                 if (itr.hasNext()) {
850                     count = (Long)itr.next();
851                 }
852 
853                 if (count == null) {
854                     count = new Long(0);
855                 }
856 
857                 FinderCache.putResult(finderClassNameCacheEnabled,
858                     finderClassName, finderMethodName, finderParams,
859                     finderArgs, count);
860 
861                 return count.intValue();
862             }
863             catch (Exception e) {
864                 throw HibernateUtil.processException(e);
865             }
866             finally {
867                 closeSession(session);
868             }
869         }
870         else {
871             return ((Long)result).intValue();
872         }
873     }
874 
875     public int countAll() throws SystemException {
876         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
877         String finderClassName = JournalArticleResource.class.getName();
878         String finderMethodName = "countAll";
879         String[] finderParams = new String[] {  };
880         Object[] finderArgs = new Object[] {  };
881 
882         Object result = null;
883 
884         if (finderClassNameCacheEnabled) {
885             result = FinderCache.getResult(finderClassName, finderMethodName,
886                     finderParams, finderArgs, getSessionFactory());
887         }
888 
889         if (result == null) {
890             Session session = null;
891 
892             try {
893                 session = openSession();
894 
895                 Query q = session.createQuery(
896                         "SELECT COUNT(*) FROM com.liferay.portlet.journal.model.JournalArticleResource");
897 
898                 Long count = null;
899 
900                 Iterator itr = q.list().iterator();
901 
902                 if (itr.hasNext()) {
903                     count = (Long)itr.next();
904                 }
905 
906                 if (count == null) {
907                     count = new Long(0);
908                 }
909 
910                 FinderCache.putResult(finderClassNameCacheEnabled,
911                     finderClassName, finderMethodName, finderParams,
912                     finderArgs, count);
913 
914                 return count.intValue();
915             }
916             catch (Exception e) {
917                 throw HibernateUtil.processException(e);
918             }
919             finally {
920                 closeSession(session);
921             }
922         }
923         else {
924             return ((Long)result).intValue();
925         }
926     }
927 
928     protected void initDao() {
929     }
930 
931     private static ModelListener _getListener() {
932         if (Validator.isNotNull(_LISTENER)) {
933             try {
934                 return (ModelListener)Class.forName(_LISTENER).newInstance();
935             }
936             catch (Exception e) {
937                 _log.error(e);
938             }
939         }
940 
941         return null;
942     }
943 
944     private static final String _LISTENER = GetterUtil.getString(PropsUtil.get(
945                 "value.object.listener.com.liferay.portlet.journal.model.JournalArticleResource"));
946     private static Log _log = LogFactory.getLog(JournalArticleResourcePersistenceImpl.class);
947 }