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