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