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