1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.journal.NoSuchArticleResourceException;
36  import com.liferay.portlet.journal.model.JournalArticleResource;
37  import com.liferay.portlet.journal.model.impl.JournalArticleResourceImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="JournalArticleResourcePersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class JournalArticleResourcePersistenceImpl extends BasePersistence
58      implements JournalArticleResourcePersistence {
59      public JournalArticleResource create(long resourcePrimKey) {
60          JournalArticleResource journalArticleResource = new JournalArticleResourceImpl();
61          journalArticleResource.setNew(true);
62          journalArticleResource.setPrimaryKey(resourcePrimKey);
63  
64          return journalArticleResource;
65      }
66  
67      public JournalArticleResource remove(long resourcePrimKey)
68          throws NoSuchArticleResourceException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              JournalArticleResource journalArticleResource = (JournalArticleResource)session.get(JournalArticleResourceImpl.class,
75                      new Long(resourcePrimKey));
76  
77              if (journalArticleResource == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn(
80                          "No JournalArticleResource exists with the primary key " +
81                          resourcePrimKey);
82                  }
83  
84                  throw new NoSuchArticleResourceException(
85                      "No JournalArticleResource exists with the primary key " +
86                      resourcePrimKey);
87              }
88  
89              return remove(journalArticleResource);
90          }
91          catch (NoSuchArticleResourceException nsee) {
92              throw nsee;
93          }
94          catch (Exception e) {
95              throw HibernateUtil.processException(e);
96          }
97          finally {
98              closeSession(session);
99          }
100     }
101 
102     public JournalArticleResource remove(
103         JournalArticleResource journalArticleResource)
104         throws SystemException {
105         Session session = null;
106 
107         try {
108             session = openSession();
109             session.delete(journalArticleResource);
110             session.flush();
111 
112             return journalArticleResource;
113         }
114         catch (Exception e) {
115             throw HibernateUtil.processException(e);
116         }
117         finally {
118             closeSession(session);
119             FinderCache.clearCache(JournalArticleResource.class.getName());
120         }
121     }
122 
123     public JournalArticleResource update(
124         com.liferay.portlet.journal.model.JournalArticleResource journalArticleResource)
125         throws SystemException {
126         return update(journalArticleResource, false);
127     }
128 
129     public JournalArticleResource update(
130         com.liferay.portlet.journal.model.JournalArticleResource journalArticleResource,
131         boolean merge) throws SystemException {
132         Session session = null;
133 
134         try {
135             session = openSession();
136 
137             if (merge) {
138                 session.merge(journalArticleResource);
139             }
140             else {
141                 if (journalArticleResource.isNew()) {
142                     session.save(journalArticleResource);
143                 }
144             }
145 
146             session.flush();
147             journalArticleResource.setNew(false);
148 
149             return journalArticleResource;
150         }
151         catch (Exception e) {
152             throw HibernateUtil.processException(e);
153         }
154         finally {
155             closeSession(session);
156             FinderCache.clearCache(JournalArticleResource.class.getName());
157         }
158     }
159 
160     public JournalArticleResource findByPrimaryKey(long resourcePrimKey)
161         throws NoSuchArticleResourceException, SystemException {
162         JournalArticleResource journalArticleResource = fetchByPrimaryKey(resourcePrimKey);
163 
164         if (journalArticleResource == null) {
165             if (_log.isWarnEnabled()) {
166                 _log.warn(
167                     "No JournalArticleResource exists with the primary key " +
168                     resourcePrimKey);
169             }
170 
171             throw new NoSuchArticleResourceException(
172                 "No JournalArticleResource exists with the primary key " +
173                 resourcePrimKey);
174         }
175 
176         return journalArticleResource;
177     }
178 
179     public JournalArticleResource fetchByPrimaryKey(long resourcePrimKey)
180         throws SystemException {
181         Session session = null;
182 
183         try {
184             session = openSession();
185 
186             return (JournalArticleResource)session.get(JournalArticleResourceImpl.class,
187                 new Long(resourcePrimKey));
188         }
189         catch (Exception e) {
190             throw HibernateUtil.processException(e);
191         }
192         finally {
193             closeSession(session);
194         }
195     }
196 
197     public List findByGroupId(long groupId) throws SystemException {
198         String finderClassName = JournalArticleResource.class.getName();
199         String finderMethodName = "findByGroupId";
200         String[] finderParams = new String[] { Long.class.getName() };
201         Object[] finderArgs = new Object[] { new Long(groupId) };
202         Object result = FinderCache.getResult(finderClassName,
203                 finderMethodName, finderParams, finderArgs, getSessionFactory());
204 
205         if (result == null) {
206             Session session = null;
207 
208             try {
209                 session = openSession();
210 
211                 StringMaker query = new StringMaker();
212                 query.append(
213                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
214                 query.append("groupId = ?");
215                 query.append(" ");
216 
217                 Query q = session.createQuery(query.toString());
218                 int queryPos = 0;
219                 q.setLong(queryPos++, groupId);
220 
221                 List list = q.list();
222                 FinderCache.putResult(finderClassName, finderMethodName,
223                     finderParams, finderArgs, list);
224 
225                 return list;
226             }
227             catch (Exception e) {
228                 throw HibernateUtil.processException(e);
229             }
230             finally {
231                 closeSession(session);
232             }
233         }
234         else {
235             return (List)result;
236         }
237     }
238 
239     public List findByGroupId(long groupId, int begin, int end)
240         throws SystemException {
241         return findByGroupId(groupId, begin, end, null);
242     }
243 
244     public List findByGroupId(long groupId, int begin, int end,
245         OrderByComparator obc) throws SystemException {
246         String finderClassName = JournalArticleResource.class.getName();
247         String finderMethodName = "findByGroupId";
248         String[] finderParams = new String[] {
249                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
250                 "com.liferay.portal.kernel.util.OrderByComparator"
251             };
252         Object[] finderArgs = new Object[] {
253                 new Long(groupId), String.valueOf(begin), String.valueOf(end),
254                 String.valueOf(obc)
255             };
256         Object result = FinderCache.getResult(finderClassName,
257                 finderMethodName, finderParams, finderArgs, getSessionFactory());
258 
259         if (result == null) {
260             Session session = null;
261 
262             try {
263                 session = openSession();
264 
265                 StringMaker query = new StringMaker();
266                 query.append(
267                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
268                 query.append("groupId = ?");
269                 query.append(" ");
270 
271                 if (obc != null) {
272                     query.append("ORDER BY ");
273                     query.append(obc.getOrderBy());
274                 }
275 
276                 Query q = session.createQuery(query.toString());
277                 int queryPos = 0;
278                 q.setLong(queryPos++, groupId);
279 
280                 List list = QueryUtil.list(q, getDialect(), begin, end);
281                 FinderCache.putResult(finderClassName, finderMethodName,
282                     finderParams, finderArgs, list);
283 
284                 return list;
285             }
286             catch (Exception e) {
287                 throw HibernateUtil.processException(e);
288             }
289             finally {
290                 closeSession(session);
291             }
292         }
293         else {
294             return (List)result;
295         }
296     }
297 
298     public JournalArticleResource findByGroupId_First(long groupId,
299         OrderByComparator obc)
300         throws NoSuchArticleResourceException, SystemException {
301         List list = findByGroupId(groupId, 0, 1, obc);
302 
303         if (list.size() == 0) {
304             StringMaker msg = new StringMaker();
305             msg.append("No JournalArticleResource exists with the key ");
306             msg.append(StringPool.OPEN_CURLY_BRACE);
307             msg.append("groupId=");
308             msg.append(groupId);
309             msg.append(StringPool.CLOSE_CURLY_BRACE);
310             throw new NoSuchArticleResourceException(msg.toString());
311         }
312         else {
313             return (JournalArticleResource)list.get(0);
314         }
315     }
316 
317     public JournalArticleResource findByGroupId_Last(long groupId,
318         OrderByComparator obc)
319         throws NoSuchArticleResourceException, SystemException {
320         int count = countByGroupId(groupId);
321         List list = findByGroupId(groupId, count - 1, count, obc);
322 
323         if (list.size() == 0) {
324             StringMaker msg = new StringMaker();
325             msg.append("No JournalArticleResource exists with the key ");
326             msg.append(StringPool.OPEN_CURLY_BRACE);
327             msg.append("groupId=");
328             msg.append(groupId);
329             msg.append(StringPool.CLOSE_CURLY_BRACE);
330             throw new NoSuchArticleResourceException(msg.toString());
331         }
332         else {
333             return (JournalArticleResource)list.get(0);
334         }
335     }
336 
337     public JournalArticleResource[] findByGroupId_PrevAndNext(
338         long resourcePrimKey, long groupId, OrderByComparator obc)
339         throws NoSuchArticleResourceException, SystemException {
340         JournalArticleResource journalArticleResource = findByPrimaryKey(resourcePrimKey);
341         int count = countByGroupId(groupId);
342         Session session = null;
343 
344         try {
345             session = openSession();
346 
347             StringMaker query = new StringMaker();
348             query.append(
349                 "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
350             query.append("groupId = ?");
351             query.append(" ");
352 
353             if (obc != null) {
354                 query.append("ORDER BY ");
355                 query.append(obc.getOrderBy());
356             }
357 
358             Query q = session.createQuery(query.toString());
359             int queryPos = 0;
360             q.setLong(queryPos++, groupId);
361 
362             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
363                     journalArticleResource);
364             JournalArticleResource[] array = new JournalArticleResourceImpl[3];
365             array[0] = (JournalArticleResource)objArray[0];
366             array[1] = (JournalArticleResource)objArray[1];
367             array[2] = (JournalArticleResource)objArray[2];
368 
369             return array;
370         }
371         catch (Exception e) {
372             throw HibernateUtil.processException(e);
373         }
374         finally {
375             closeSession(session);
376         }
377     }
378 
379     public JournalArticleResource findByG_A(long groupId, String articleId)
380         throws NoSuchArticleResourceException, SystemException {
381         JournalArticleResource journalArticleResource = fetchByG_A(groupId,
382                 articleId);
383 
384         if (journalArticleResource == null) {
385             StringMaker msg = new StringMaker();
386             msg.append("No JournalArticleResource exists with the key ");
387             msg.append(StringPool.OPEN_CURLY_BRACE);
388             msg.append("groupId=");
389             msg.append(groupId);
390             msg.append(", ");
391             msg.append("articleId=");
392             msg.append(articleId);
393             msg.append(StringPool.CLOSE_CURLY_BRACE);
394 
395             if (_log.isWarnEnabled()) {
396                 _log.warn(msg.toString());
397             }
398 
399             throw new NoSuchArticleResourceException(msg.toString());
400         }
401 
402         return journalArticleResource;
403     }
404 
405     public JournalArticleResource fetchByG_A(long groupId, String articleId)
406         throws SystemException {
407         String finderClassName = JournalArticleResource.class.getName();
408         String finderMethodName = "fetchByG_A";
409         String[] finderParams = new String[] {
410                 Long.class.getName(), String.class.getName()
411             };
412         Object[] finderArgs = new Object[] { new Long(groupId), articleId };
413         Object result = FinderCache.getResult(finderClassName,
414                 finderMethodName, finderParams, finderArgs, getSessionFactory());
415 
416         if (result == null) {
417             Session session = null;
418 
419             try {
420                 session = openSession();
421 
422                 StringMaker query = new StringMaker();
423                 query.append(
424                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
425                 query.append("groupId = ?");
426                 query.append(" AND ");
427 
428                 if (articleId == null) {
429                     query.append("articleId IS NULL");
430                 }
431                 else {
432                     query.append("articleId = ?");
433                 }
434 
435                 query.append(" ");
436 
437                 Query q = session.createQuery(query.toString());
438                 int queryPos = 0;
439                 q.setLong(queryPos++, groupId);
440 
441                 if (articleId != null) {
442                     q.setString(queryPos++, articleId);
443                 }
444 
445                 List list = q.list();
446                 FinderCache.putResult(finderClassName, finderMethodName,
447                     finderParams, finderArgs, list);
448 
449                 if (list.size() == 0) {
450                     return null;
451                 }
452                 else {
453                     return (JournalArticleResource)list.get(0);
454                 }
455             }
456             catch (Exception e) {
457                 throw HibernateUtil.processException(e);
458             }
459             finally {
460                 closeSession(session);
461             }
462         }
463         else {
464             List list = (List)result;
465 
466             if (list.size() == 0) {
467                 return null;
468             }
469             else {
470                 return (JournalArticleResource)list.get(0);
471             }
472         }
473     }
474 
475     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
476         throws SystemException {
477         Session session = null;
478 
479         try {
480             session = openSession();
481 
482             DynamicQuery query = queryInitializer.initialize(session);
483 
484             return query.list();
485         }
486         catch (Exception e) {
487             throw HibernateUtil.processException(e);
488         }
489         finally {
490             closeSession(session);
491         }
492     }
493 
494     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
495         int begin, int end) throws SystemException {
496         Session session = null;
497 
498         try {
499             session = openSession();
500 
501             DynamicQuery query = queryInitializer.initialize(session);
502             query.setLimit(begin, end);
503 
504             return query.list();
505         }
506         catch (Exception e) {
507             throw HibernateUtil.processException(e);
508         }
509         finally {
510             closeSession(session);
511         }
512     }
513 
514     public List findAll() throws SystemException {
515         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
516     }
517 
518     public List findAll(int begin, int end) throws SystemException {
519         return findAll(begin, end, null);
520     }
521 
522     public List findAll(int begin, int end, OrderByComparator obc)
523         throws SystemException {
524         String finderClassName = JournalArticleResource.class.getName();
525         String finderMethodName = "findAll";
526         String[] finderParams = new String[] {
527                 "java.lang.Integer", "java.lang.Integer",
528                 "com.liferay.portal.kernel.util.OrderByComparator"
529             };
530         Object[] finderArgs = new Object[] {
531                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
532             };
533         Object result = FinderCache.getResult(finderClassName,
534                 finderMethodName, finderParams, finderArgs, getSessionFactory());
535 
536         if (result == null) {
537             Session session = null;
538 
539             try {
540                 session = openSession();
541 
542                 StringMaker query = new StringMaker();
543                 query.append(
544                     "FROM com.liferay.portlet.journal.model.JournalArticleResource ");
545 
546                 if (obc != null) {
547                     query.append("ORDER BY ");
548                     query.append(obc.getOrderBy());
549                 }
550 
551                 Query q = session.createQuery(query.toString());
552                 List list = QueryUtil.list(q, getDialect(), begin, end);
553 
554                 if (obc == null) {
555                     Collections.sort(list);
556                 }
557 
558                 FinderCache.putResult(finderClassName, finderMethodName,
559                     finderParams, finderArgs, list);
560 
561                 return list;
562             }
563             catch (Exception e) {
564                 throw HibernateUtil.processException(e);
565             }
566             finally {
567                 closeSession(session);
568             }
569         }
570         else {
571             return (List)result;
572         }
573     }
574 
575     public void removeByGroupId(long groupId) throws SystemException {
576         Iterator itr = findByGroupId(groupId).iterator();
577 
578         while (itr.hasNext()) {
579             JournalArticleResource journalArticleResource = (JournalArticleResource)itr.next();
580             remove(journalArticleResource);
581         }
582     }
583 
584     public void removeByG_A(long groupId, String articleId)
585         throws NoSuchArticleResourceException, SystemException {
586         JournalArticleResource journalArticleResource = findByG_A(groupId,
587                 articleId);
588         remove(journalArticleResource);
589     }
590 
591     public void removeAll() throws SystemException {
592         Iterator itr = findAll().iterator();
593 
594         while (itr.hasNext()) {
595             remove((JournalArticleResource)itr.next());
596         }
597     }
598 
599     public int countByGroupId(long groupId) throws SystemException {
600         String finderClassName = JournalArticleResource.class.getName();
601         String finderMethodName = "countByGroupId";
602         String[] finderParams = new String[] { Long.class.getName() };
603         Object[] finderArgs = new Object[] { new Long(groupId) };
604         Object result = FinderCache.getResult(finderClassName,
605                 finderMethodName, finderParams, finderArgs, getSessionFactory());
606 
607         if (result == null) {
608             Session session = null;
609 
610             try {
611                 session = openSession();
612 
613                 StringMaker query = new StringMaker();
614                 query.append("SELECT COUNT(*) ");
615                 query.append(
616                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
617                 query.append("groupId = ?");
618                 query.append(" ");
619 
620                 Query q = session.createQuery(query.toString());
621                 int queryPos = 0;
622                 q.setLong(queryPos++, groupId);
623 
624                 Long count = null;
625                 Iterator itr = q.list().iterator();
626 
627                 if (itr.hasNext()) {
628                     count = (Long)itr.next();
629                 }
630 
631                 if (count == null) {
632                     count = new Long(0);
633                 }
634 
635                 FinderCache.putResult(finderClassName, finderMethodName,
636                     finderParams, finderArgs, count);
637 
638                 return count.intValue();
639             }
640             catch (Exception e) {
641                 throw HibernateUtil.processException(e);
642             }
643             finally {
644                 closeSession(session);
645             }
646         }
647         else {
648             return ((Long)result).intValue();
649         }
650     }
651 
652     public int countByG_A(long groupId, String articleId)
653         throws SystemException {
654         String finderClassName = JournalArticleResource.class.getName();
655         String finderMethodName = "countByG_A";
656         String[] finderParams = new String[] {
657                 Long.class.getName(), String.class.getName()
658             };
659         Object[] finderArgs = new Object[] { new Long(groupId), articleId };
660         Object result = FinderCache.getResult(finderClassName,
661                 finderMethodName, finderParams, finderArgs, getSessionFactory());
662 
663         if (result == null) {
664             Session session = null;
665 
666             try {
667                 session = openSession();
668 
669                 StringMaker query = new StringMaker();
670                 query.append("SELECT COUNT(*) ");
671                 query.append(
672                     "FROM com.liferay.portlet.journal.model.JournalArticleResource WHERE ");
673                 query.append("groupId = ?");
674                 query.append(" AND ");
675 
676                 if (articleId == null) {
677                     query.append("articleId IS NULL");
678                 }
679                 else {
680                     query.append("articleId = ?");
681                 }
682 
683                 query.append(" ");
684 
685                 Query q = session.createQuery(query.toString());
686                 int queryPos = 0;
687                 q.setLong(queryPos++, groupId);
688 
689                 if (articleId != null) {
690                     q.setString(queryPos++, articleId);
691                 }
692 
693                 Long count = null;
694                 Iterator itr = q.list().iterator();
695 
696                 if (itr.hasNext()) {
697                     count = (Long)itr.next();
698                 }
699 
700                 if (count == null) {
701                     count = new Long(0);
702                 }
703 
704                 FinderCache.putResult(finderClassName, finderMethodName,
705                     finderParams, finderArgs, count);
706 
707                 return count.intValue();
708             }
709             catch (Exception e) {
710                 throw HibernateUtil.processException(e);
711             }
712             finally {
713                 closeSession(session);
714             }
715         }
716         else {
717             return ((Long)result).intValue();
718         }
719     }
720 
721     public int countAll() throws SystemException {
722         String finderClassName = JournalArticleResource.class.getName();
723         String finderMethodName = "countAll";
724         String[] finderParams = new String[] {  };
725         Object[] finderArgs = new Object[] {  };
726         Object result = FinderCache.getResult(finderClassName,
727                 finderMethodName, finderParams, finderArgs, getSessionFactory());
728 
729         if (result == null) {
730             Session session = null;
731 
732             try {
733                 session = openSession();
734 
735                 StringMaker query = new StringMaker();
736                 query.append("SELECT COUNT(*) ");
737                 query.append(
738                     "FROM com.liferay.portlet.journal.model.JournalArticleResource");
739 
740                 Query q = session.createQuery(query.toString());
741                 Long count = null;
742                 Iterator itr = q.list().iterator();
743 
744                 if (itr.hasNext()) {
745                     count = (Long)itr.next();
746                 }
747 
748                 if (count == null) {
749                     count = new Long(0);
750                 }
751 
752                 FinderCache.putResult(finderClassName, finderMethodName,
753                     finderParams, finderArgs, count);
754 
755                 return count.intValue();
756             }
757             catch (Exception e) {
758                 throw HibernateUtil.processException(e);
759             }
760             finally {
761                 closeSession(session);
762             }
763         }
764         else {
765             return ((Long)result).intValue();
766         }
767     }
768 
769     protected void initDao() {
770     }
771 
772     private static Log _log = LogFactory.getLog(JournalArticleResourcePersistenceImpl.class);
773 }