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.bean.InitializingBean;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
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, InitializingBean {
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             session.delete(journalArticleResource);
135 
136             session.flush();
137 
138             return journalArticleResource;
139         }
140         catch (Exception e) {
141             throw processException(e);
142         }
143         finally {
144             closeSession(session);
145 
146             FinderCacheUtil.clearCache(JournalArticleResource.class.getName());
147         }
148     }
149 
150     /**
151      * @deprecated Use <code>update(JournalArticleResource journalArticleResource, boolean merge)</code>.
152      */
153     public JournalArticleResource update(
154         JournalArticleResource journalArticleResource)
155         throws SystemException {
156         if (_log.isWarnEnabled()) {
157             _log.warn(
158                 "Using the deprecated update(JournalArticleResource journalArticleResource) method. Use update(JournalArticleResource journalArticleResource, boolean merge) instead.");
159         }
160 
161         return update(journalArticleResource, false);
162     }
163 
164     /**
165      * Add, update, or merge, the entity. This method also calls the model
166      * listeners to trigger the proper events associated with adding, deleting,
167      * or updating an entity.
168      *
169      * @param        journalArticleResource the entity to add, update, or merge
170      * @param        merge boolean value for whether to merge the entity. The
171      *                default value is false. Setting merge to true is more
172      *                expensive and should only be true when journalArticleResource is
173      *                transient. See LEP-5473 for a detailed discussion of this
174      *                method.
175      * @return        true if the portlet can be displayed via Ajax
176      */
177     public JournalArticleResource update(
178         JournalArticleResource journalArticleResource, boolean merge)
179         throws SystemException {
180         boolean isNew = journalArticleResource.isNew();
181 
182         if (_listeners.length > 0) {
183             for (ModelListener listener : _listeners) {
184                 if (isNew) {
185                     listener.onBeforeCreate(journalArticleResource);
186                 }
187                 else {
188                     listener.onBeforeUpdate(journalArticleResource);
189                 }
190             }
191         }
192 
193         journalArticleResource = updateImpl(journalArticleResource, merge);
194 
195         if (_listeners.length > 0) {
196             for (ModelListener listener : _listeners) {
197                 if (isNew) {
198                     listener.onAfterCreate(journalArticleResource);
199                 }
200                 else {
201                     listener.onAfterUpdate(journalArticleResource);
202                 }
203             }
204         }
205 
206         return journalArticleResource;
207     }
208 
209     public JournalArticleResource updateImpl(
210         com.liferay.portlet.journal.model.JournalArticleResource journalArticleResource,
211         boolean merge) throws SystemException {
212         Session session = null;
213 
214         try {
215             session = openSession();
216 
217             if (merge) {
218                 session.merge(journalArticleResource);
219             }
220             else {
221                 if (journalArticleResource.isNew()) {
222                     session.save(journalArticleResource);
223                 }
224             }
225 
226             session.flush();
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 = (List<JournalArticleResource>)QueryUtil.list(q,
705                         getDialect(), start, end);
706 
707                 if (obc == null) {
708                     Collections.sort(list);
709                 }
710 
711                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
712                     finderClassName, finderMethodName, finderParams,
713                     finderArgs, list);
714 
715                 return list;
716             }
717             catch (Exception e) {
718                 throw processException(e);
719             }
720             finally {
721                 closeSession(session);
722             }
723         }
724         else {
725             return (List<JournalArticleResource>)result;
726         }
727     }
728 
729     public void removeByGroupId(long groupId) throws SystemException {
730         for (JournalArticleResource journalArticleResource : findByGroupId(
731                 groupId)) {
732             remove(journalArticleResource);
733         }
734     }
735 
736     public void removeByG_A(long groupId, String articleId)
737         throws NoSuchArticleResourceException, SystemException {
738         JournalArticleResource journalArticleResource = findByG_A(groupId,
739                 articleId);
740 
741         remove(journalArticleResource);
742     }
743 
744     public void removeAll() throws SystemException {
745         for (JournalArticleResource journalArticleResource : findAll()) {
746             remove(journalArticleResource);
747         }
748     }
749 
750     public int countByGroupId(long groupId) throws SystemException {
751         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
752         String finderClassName = JournalArticleResource.class.getName();
753         String finderMethodName = "countByGroupId";
754         String[] finderParams = new String[] { Long.class.getName() };
755         Object[] finderArgs = new Object[] { new Long(groupId) };
756 
757         Object result = null;
758 
759         if (finderClassNameCacheEnabled) {
760             result = FinderCacheUtil.getResult(finderClassName,
761                     finderMethodName, finderParams, finderArgs, this);
762         }
763 
764         if (result == null) {
765             Session session = null;
766 
767             try {
768                 session = openSession();
769 
770                 StringBuilder query = new StringBuilder();
771 
772                 query.append("SELECT COUNT(*) ");
773                 query.append(
774                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
775 
776                 query.append("groupId = ?");
777 
778                 query.append(" ");
779 
780                 Query q = session.createQuery(query.toString());
781 
782                 QueryPos qPos = QueryPos.getInstance(q);
783 
784                 qPos.add(groupId);
785 
786                 Long count = null;
787 
788                 Iterator<Long> itr = q.list().iterator();
789 
790                 if (itr.hasNext()) {
791                     count = itr.next();
792                 }
793 
794                 if (count == null) {
795                     count = new Long(0);
796                 }
797 
798                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
799                     finderClassName, finderMethodName, finderParams,
800                     finderArgs, count);
801 
802                 return count.intValue();
803             }
804             catch (Exception e) {
805                 throw processException(e);
806             }
807             finally {
808                 closeSession(session);
809             }
810         }
811         else {
812             return ((Long)result).intValue();
813         }
814     }
815 
816     public int countByG_A(long groupId, String articleId)
817         throws SystemException {
818         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
819         String finderClassName = JournalArticleResource.class.getName();
820         String finderMethodName = "countByG_A";
821         String[] finderParams = new String[] {
822                 Long.class.getName(), String.class.getName()
823             };
824         Object[] finderArgs = new Object[] { new Long(groupId), articleId };
825 
826         Object result = null;
827 
828         if (finderClassNameCacheEnabled) {
829             result = FinderCacheUtil.getResult(finderClassName,
830                     finderMethodName, finderParams, finderArgs, this);
831         }
832 
833         if (result == null) {
834             Session session = null;
835 
836             try {
837                 session = openSession();
838 
839                 StringBuilder query = new StringBuilder();
840 
841                 query.append("SELECT COUNT(*) ");
842                 query.append(
843                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
844 
845                 query.append("groupId = ?");
846 
847                 query.append(" AND ");
848 
849                 if (articleId == null) {
850                     query.append("articleId IS NULL");
851                 }
852                 else {
853                     query.append("articleId = ?");
854                 }
855 
856                 query.append(" ");
857 
858                 Query q = session.createQuery(query.toString());
859 
860                 QueryPos qPos = QueryPos.getInstance(q);
861 
862                 qPos.add(groupId);
863 
864                 if (articleId != null) {
865                     qPos.add(articleId);
866                 }
867 
868                 Long count = null;
869 
870                 Iterator<Long> itr = q.list().iterator();
871 
872                 if (itr.hasNext()) {
873                     count = itr.next();
874                 }
875 
876                 if (count == null) {
877                     count = new Long(0);
878                 }
879 
880                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
881                     finderClassName, finderMethodName, finderParams,
882                     finderArgs, count);
883 
884                 return count.intValue();
885             }
886             catch (Exception e) {
887                 throw processException(e);
888             }
889             finally {
890                 closeSession(session);
891             }
892         }
893         else {
894             return ((Long)result).intValue();
895         }
896     }
897 
898     public int countAll() throws SystemException {
899         boolean finderClassNameCacheEnabled = JournalArticleResourceModelImpl.CACHE_ENABLED;
900         String finderClassName = JournalArticleResource.class.getName();
901         String finderMethodName = "countAll";
902         String[] finderParams = new String[] {  };
903         Object[] finderArgs = new Object[] {  };
904 
905         Object result = null;
906 
907         if (finderClassNameCacheEnabled) {
908             result = FinderCacheUtil.getResult(finderClassName,
909                     finderMethodName, finderParams, finderArgs, this);
910         }
911 
912         if (result == null) {
913             Session session = null;
914 
915             try {
916                 session = openSession();
917 
918                 Query q = session.createQuery(
919                         "SELECT COUNT(*) FROM com.liferay.portlet.journal.model.JournalArticleResource");
920 
921                 Long count = null;
922 
923                 Iterator<Long> itr = q.list().iterator();
924 
925                 if (itr.hasNext()) {
926                     count = itr.next();
927                 }
928 
929                 if (count == null) {
930                     count = new Long(0);
931                 }
932 
933                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
934                     finderClassName, finderMethodName, finderParams,
935                     finderArgs, count);
936 
937                 return count.intValue();
938             }
939             catch (Exception e) {
940                 throw processException(e);
941             }
942             finally {
943                 closeSession(session);
944             }
945         }
946         else {
947             return ((Long)result).intValue();
948         }
949     }
950 
951     public void registerListener(ModelListener listener) {
952         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
953 
954         listeners.add(listener);
955 
956         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
957     }
958 
959     public void unregisterListener(ModelListener listener) {
960         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
961 
962         listeners.remove(listener);
963 
964         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
965     }
966 
967     public void afterPropertiesSet() {
968         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
969                     com.liferay.portal.util.PropsUtil.get(
970                         "value.object.listener.com.liferay.portlet.journal.model.JournalArticleResource")));
971 
972         if (listenerClassNames.length > 0) {
973             try {
974                 List<ModelListener> listeners = new ArrayList<ModelListener>();
975 
976                 for (String listenerClassName : listenerClassNames) {
977                     listeners.add((ModelListener)Class.forName(
978                             listenerClassName).newInstance());
979                 }
980 
981                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
982             }
983             catch (Exception e) {
984                 _log.error(e);
985             }
986         }
987     }
988 
989     private static Log _log = LogFactory.getLog(JournalArticleResourcePersistenceImpl.class);
990     private ModelListener[] _listeners = new ModelListener[0];
991 }