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.bookmarks.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.bookmarks.NoSuchFolderException;
36  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
37  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
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="BookmarksFolderPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class BookmarksFolderPersistenceImpl extends BasePersistence
58      implements BookmarksFolderPersistence {
59      public BookmarksFolder create(long folderId) {
60          BookmarksFolder bookmarksFolder = new BookmarksFolderImpl();
61          bookmarksFolder.setNew(true);
62          bookmarksFolder.setPrimaryKey(folderId);
63  
64          return bookmarksFolder;
65      }
66  
67      public BookmarksFolder remove(long folderId)
68          throws NoSuchFolderException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              BookmarksFolder bookmarksFolder = (BookmarksFolder)session.get(BookmarksFolderImpl.class,
75                      new Long(folderId));
76  
77              if (bookmarksFolder == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No BookmarksFolder exists with the primary key " +
80                          folderId);
81                  }
82  
83                  throw new NoSuchFolderException(
84                      "No BookmarksFolder exists with the primary key " +
85                      folderId);
86              }
87  
88              return remove(bookmarksFolder);
89          }
90          catch (NoSuchFolderException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw HibernateUtil.processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public BookmarksFolder remove(BookmarksFolder bookmarksFolder)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(bookmarksFolder);
108             session.flush();
109 
110             return bookmarksFolder;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(BookmarksFolder.class.getName());
118         }
119     }
120 
121     public BookmarksFolder update(
122         com.liferay.portlet.bookmarks.model.BookmarksFolder bookmarksFolder)
123         throws SystemException {
124         return update(bookmarksFolder, false);
125     }
126 
127     public BookmarksFolder update(
128         com.liferay.portlet.bookmarks.model.BookmarksFolder bookmarksFolder,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(bookmarksFolder);
137             }
138             else {
139                 if (bookmarksFolder.isNew()) {
140                     session.save(bookmarksFolder);
141                 }
142             }
143 
144             session.flush();
145             bookmarksFolder.setNew(false);
146 
147             return bookmarksFolder;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(BookmarksFolder.class.getName());
155         }
156     }
157 
158     public BookmarksFolder findByPrimaryKey(long folderId)
159         throws NoSuchFolderException, SystemException {
160         BookmarksFolder bookmarksFolder = fetchByPrimaryKey(folderId);
161 
162         if (bookmarksFolder == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No BookmarksFolder exists with the primary key " +
165                     folderId);
166             }
167 
168             throw new NoSuchFolderException(
169                 "No BookmarksFolder exists with the primary key " + folderId);
170         }
171 
172         return bookmarksFolder;
173     }
174 
175     public BookmarksFolder fetchByPrimaryKey(long folderId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (BookmarksFolder)session.get(BookmarksFolderImpl.class,
183                 new Long(folderId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List findByGroupId(long groupId) throws SystemException {
194         String finderClassName = BookmarksFolder.class.getName();
195         String finderMethodName = "findByGroupId";
196         String[] finderParams = new String[] { Long.class.getName() };
197         Object[] finderArgs = new Object[] { new Long(groupId) };
198         Object result = FinderCache.getResult(finderClassName,
199                 finderMethodName, finderParams, finderArgs, getSessionFactory());
200 
201         if (result == null) {
202             Session session = null;
203 
204             try {
205                 session = openSession();
206 
207                 StringMaker query = new StringMaker();
208                 query.append(
209                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
210                 query.append("groupId = ?");
211                 query.append(" ");
212                 query.append("ORDER BY ");
213                 query.append("parentFolderId ASC").append(", ");
214                 query.append("name ASC");
215 
216                 Query q = session.createQuery(query.toString());
217                 int queryPos = 0;
218                 q.setLong(queryPos++, groupId);
219 
220                 List list = q.list();
221                 FinderCache.putResult(finderClassName, finderMethodName,
222                     finderParams, finderArgs, list);
223 
224                 return list;
225             }
226             catch (Exception e) {
227                 throw HibernateUtil.processException(e);
228             }
229             finally {
230                 closeSession(session);
231             }
232         }
233         else {
234             return (List)result;
235         }
236     }
237 
238     public List findByGroupId(long groupId, int begin, int end)
239         throws SystemException {
240         return findByGroupId(groupId, begin, end, null);
241     }
242 
243     public List findByGroupId(long groupId, int begin, int end,
244         OrderByComparator obc) throws SystemException {
245         String finderClassName = BookmarksFolder.class.getName();
246         String finderMethodName = "findByGroupId";
247         String[] finderParams = new String[] {
248                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
249                 "com.liferay.portal.kernel.util.OrderByComparator"
250             };
251         Object[] finderArgs = new Object[] {
252                 new Long(groupId), String.valueOf(begin), String.valueOf(end),
253                 String.valueOf(obc)
254             };
255         Object result = FinderCache.getResult(finderClassName,
256                 finderMethodName, finderParams, finderArgs, getSessionFactory());
257 
258         if (result == null) {
259             Session session = null;
260 
261             try {
262                 session = openSession();
263 
264                 StringMaker query = new StringMaker();
265                 query.append(
266                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
267                 query.append("groupId = ?");
268                 query.append(" ");
269 
270                 if (obc != null) {
271                     query.append("ORDER BY ");
272                     query.append(obc.getOrderBy());
273                 }
274                 else {
275                     query.append("ORDER BY ");
276                     query.append("parentFolderId ASC").append(", ");
277                     query.append("name ASC");
278                 }
279 
280                 Query q = session.createQuery(query.toString());
281                 int queryPos = 0;
282                 q.setLong(queryPos++, groupId);
283 
284                 List list = QueryUtil.list(q, getDialect(), begin, end);
285                 FinderCache.putResult(finderClassName, finderMethodName,
286                     finderParams, finderArgs, list);
287 
288                 return list;
289             }
290             catch (Exception e) {
291                 throw HibernateUtil.processException(e);
292             }
293             finally {
294                 closeSession(session);
295             }
296         }
297         else {
298             return (List)result;
299         }
300     }
301 
302     public BookmarksFolder findByGroupId_First(long groupId,
303         OrderByComparator obc) throws NoSuchFolderException, SystemException {
304         List list = findByGroupId(groupId, 0, 1, obc);
305 
306         if (list.size() == 0) {
307             StringMaker msg = new StringMaker();
308             msg.append("No BookmarksFolder exists with the key ");
309             msg.append(StringPool.OPEN_CURLY_BRACE);
310             msg.append("groupId=");
311             msg.append(groupId);
312             msg.append(StringPool.CLOSE_CURLY_BRACE);
313             throw new NoSuchFolderException(msg.toString());
314         }
315         else {
316             return (BookmarksFolder)list.get(0);
317         }
318     }
319 
320     public BookmarksFolder findByGroupId_Last(long groupId,
321         OrderByComparator obc) throws NoSuchFolderException, SystemException {
322         int count = countByGroupId(groupId);
323         List list = findByGroupId(groupId, count - 1, count, obc);
324 
325         if (list.size() == 0) {
326             StringMaker msg = new StringMaker();
327             msg.append("No BookmarksFolder exists with the key ");
328             msg.append(StringPool.OPEN_CURLY_BRACE);
329             msg.append("groupId=");
330             msg.append(groupId);
331             msg.append(StringPool.CLOSE_CURLY_BRACE);
332             throw new NoSuchFolderException(msg.toString());
333         }
334         else {
335             return (BookmarksFolder)list.get(0);
336         }
337     }
338 
339     public BookmarksFolder[] findByGroupId_PrevAndNext(long folderId,
340         long groupId, OrderByComparator obc)
341         throws NoSuchFolderException, SystemException {
342         BookmarksFolder bookmarksFolder = findByPrimaryKey(folderId);
343         int count = countByGroupId(groupId);
344         Session session = null;
345 
346         try {
347             session = openSession();
348 
349             StringMaker query = new StringMaker();
350             query.append(
351                 "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
352             query.append("groupId = ?");
353             query.append(" ");
354 
355             if (obc != null) {
356                 query.append("ORDER BY ");
357                 query.append(obc.getOrderBy());
358             }
359             else {
360                 query.append("ORDER BY ");
361                 query.append("parentFolderId ASC").append(", ");
362                 query.append("name ASC");
363             }
364 
365             Query q = session.createQuery(query.toString());
366             int queryPos = 0;
367             q.setLong(queryPos++, groupId);
368 
369             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
370                     bookmarksFolder);
371             BookmarksFolder[] array = new BookmarksFolderImpl[3];
372             array[0] = (BookmarksFolder)objArray[0];
373             array[1] = (BookmarksFolder)objArray[1];
374             array[2] = (BookmarksFolder)objArray[2];
375 
376             return array;
377         }
378         catch (Exception e) {
379             throw HibernateUtil.processException(e);
380         }
381         finally {
382             closeSession(session);
383         }
384     }
385 
386     public List findByG_P(long groupId, long parentFolderId)
387         throws SystemException {
388         String finderClassName = BookmarksFolder.class.getName();
389         String finderMethodName = "findByG_P";
390         String[] finderParams = new String[] {
391                 Long.class.getName(), Long.class.getName()
392             };
393         Object[] finderArgs = new Object[] {
394                 new Long(groupId), new Long(parentFolderId)
395             };
396         Object result = FinderCache.getResult(finderClassName,
397                 finderMethodName, finderParams, finderArgs, getSessionFactory());
398 
399         if (result == null) {
400             Session session = null;
401 
402             try {
403                 session = openSession();
404 
405                 StringMaker query = new StringMaker();
406                 query.append(
407                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
408                 query.append("groupId = ?");
409                 query.append(" AND ");
410                 query.append("parentFolderId = ?");
411                 query.append(" ");
412                 query.append("ORDER BY ");
413                 query.append("parentFolderId ASC").append(", ");
414                 query.append("name ASC");
415 
416                 Query q = session.createQuery(query.toString());
417                 int queryPos = 0;
418                 q.setLong(queryPos++, groupId);
419                 q.setLong(queryPos++, parentFolderId);
420 
421                 List list = q.list();
422                 FinderCache.putResult(finderClassName, finderMethodName,
423                     finderParams, finderArgs, list);
424 
425                 return list;
426             }
427             catch (Exception e) {
428                 throw HibernateUtil.processException(e);
429             }
430             finally {
431                 closeSession(session);
432             }
433         }
434         else {
435             return (List)result;
436         }
437     }
438 
439     public List findByG_P(long groupId, long parentFolderId, int begin, int end)
440         throws SystemException {
441         return findByG_P(groupId, parentFolderId, begin, end, null);
442     }
443 
444     public List findByG_P(long groupId, long parentFolderId, int begin,
445         int end, OrderByComparator obc) throws SystemException {
446         String finderClassName = BookmarksFolder.class.getName();
447         String finderMethodName = "findByG_P";
448         String[] finderParams = new String[] {
449                 Long.class.getName(), Long.class.getName(), "java.lang.Integer",
450                 "java.lang.Integer",
451                 "com.liferay.portal.kernel.util.OrderByComparator"
452             };
453         Object[] finderArgs = new Object[] {
454                 new Long(groupId), new Long(parentFolderId),
455                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
456             };
457         Object result = FinderCache.getResult(finderClassName,
458                 finderMethodName, finderParams, finderArgs, getSessionFactory());
459 
460         if (result == null) {
461             Session session = null;
462 
463             try {
464                 session = openSession();
465 
466                 StringMaker query = new StringMaker();
467                 query.append(
468                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
469                 query.append("groupId = ?");
470                 query.append(" AND ");
471                 query.append("parentFolderId = ?");
472                 query.append(" ");
473 
474                 if (obc != null) {
475                     query.append("ORDER BY ");
476                     query.append(obc.getOrderBy());
477                 }
478                 else {
479                     query.append("ORDER BY ");
480                     query.append("parentFolderId ASC").append(", ");
481                     query.append("name ASC");
482                 }
483 
484                 Query q = session.createQuery(query.toString());
485                 int queryPos = 0;
486                 q.setLong(queryPos++, groupId);
487                 q.setLong(queryPos++, parentFolderId);
488 
489                 List list = QueryUtil.list(q, getDialect(), begin, end);
490                 FinderCache.putResult(finderClassName, finderMethodName,
491                     finderParams, finderArgs, list);
492 
493                 return list;
494             }
495             catch (Exception e) {
496                 throw HibernateUtil.processException(e);
497             }
498             finally {
499                 closeSession(session);
500             }
501         }
502         else {
503             return (List)result;
504         }
505     }
506 
507     public BookmarksFolder findByG_P_First(long groupId, long parentFolderId,
508         OrderByComparator obc) throws NoSuchFolderException, SystemException {
509         List list = findByG_P(groupId, parentFolderId, 0, 1, obc);
510 
511         if (list.size() == 0) {
512             StringMaker msg = new StringMaker();
513             msg.append("No BookmarksFolder exists with the key ");
514             msg.append(StringPool.OPEN_CURLY_BRACE);
515             msg.append("groupId=");
516             msg.append(groupId);
517             msg.append(", ");
518             msg.append("parentFolderId=");
519             msg.append(parentFolderId);
520             msg.append(StringPool.CLOSE_CURLY_BRACE);
521             throw new NoSuchFolderException(msg.toString());
522         }
523         else {
524             return (BookmarksFolder)list.get(0);
525         }
526     }
527 
528     public BookmarksFolder findByG_P_Last(long groupId, long parentFolderId,
529         OrderByComparator obc) throws NoSuchFolderException, SystemException {
530         int count = countByG_P(groupId, parentFolderId);
531         List list = findByG_P(groupId, parentFolderId, count - 1, count, obc);
532 
533         if (list.size() == 0) {
534             StringMaker msg = new StringMaker();
535             msg.append("No BookmarksFolder exists with the key ");
536             msg.append(StringPool.OPEN_CURLY_BRACE);
537             msg.append("groupId=");
538             msg.append(groupId);
539             msg.append(", ");
540             msg.append("parentFolderId=");
541             msg.append(parentFolderId);
542             msg.append(StringPool.CLOSE_CURLY_BRACE);
543             throw new NoSuchFolderException(msg.toString());
544         }
545         else {
546             return (BookmarksFolder)list.get(0);
547         }
548     }
549 
550     public BookmarksFolder[] findByG_P_PrevAndNext(long folderId, long groupId,
551         long parentFolderId, OrderByComparator obc)
552         throws NoSuchFolderException, SystemException {
553         BookmarksFolder bookmarksFolder = findByPrimaryKey(folderId);
554         int count = countByG_P(groupId, parentFolderId);
555         Session session = null;
556 
557         try {
558             session = openSession();
559 
560             StringMaker query = new StringMaker();
561             query.append(
562                 "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
563             query.append("groupId = ?");
564             query.append(" AND ");
565             query.append("parentFolderId = ?");
566             query.append(" ");
567 
568             if (obc != null) {
569                 query.append("ORDER BY ");
570                 query.append(obc.getOrderBy());
571             }
572             else {
573                 query.append("ORDER BY ");
574                 query.append("parentFolderId ASC").append(", ");
575                 query.append("name ASC");
576             }
577 
578             Query q = session.createQuery(query.toString());
579             int queryPos = 0;
580             q.setLong(queryPos++, groupId);
581             q.setLong(queryPos++, parentFolderId);
582 
583             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
584                     bookmarksFolder);
585             BookmarksFolder[] array = new BookmarksFolderImpl[3];
586             array[0] = (BookmarksFolder)objArray[0];
587             array[1] = (BookmarksFolder)objArray[1];
588             array[2] = (BookmarksFolder)objArray[2];
589 
590             return array;
591         }
592         catch (Exception e) {
593             throw HibernateUtil.processException(e);
594         }
595         finally {
596             closeSession(session);
597         }
598     }
599 
600     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
601         throws SystemException {
602         Session session = null;
603 
604         try {
605             session = openSession();
606 
607             DynamicQuery query = queryInitializer.initialize(session);
608 
609             return query.list();
610         }
611         catch (Exception e) {
612             throw HibernateUtil.processException(e);
613         }
614         finally {
615             closeSession(session);
616         }
617     }
618 
619     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
620         int begin, int end) throws SystemException {
621         Session session = null;
622 
623         try {
624             session = openSession();
625 
626             DynamicQuery query = queryInitializer.initialize(session);
627             query.setLimit(begin, end);
628 
629             return query.list();
630         }
631         catch (Exception e) {
632             throw HibernateUtil.processException(e);
633         }
634         finally {
635             closeSession(session);
636         }
637     }
638 
639     public List findAll() throws SystemException {
640         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
641     }
642 
643     public List findAll(int begin, int end) throws SystemException {
644         return findAll(begin, end, null);
645     }
646 
647     public List findAll(int begin, int end, OrderByComparator obc)
648         throws SystemException {
649         String finderClassName = BookmarksFolder.class.getName();
650         String finderMethodName = "findAll";
651         String[] finderParams = new String[] {
652                 "java.lang.Integer", "java.lang.Integer",
653                 "com.liferay.portal.kernel.util.OrderByComparator"
654             };
655         Object[] finderArgs = new Object[] {
656                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
657             };
658         Object result = FinderCache.getResult(finderClassName,
659                 finderMethodName, finderParams, finderArgs, getSessionFactory());
660 
661         if (result == null) {
662             Session session = null;
663 
664             try {
665                 session = openSession();
666 
667                 StringMaker query = new StringMaker();
668                 query.append(
669                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder ");
670 
671                 if (obc != null) {
672                     query.append("ORDER BY ");
673                     query.append(obc.getOrderBy());
674                 }
675                 else {
676                     query.append("ORDER BY ");
677                     query.append("parentFolderId ASC").append(", ");
678                     query.append("name ASC");
679                 }
680 
681                 Query q = session.createQuery(query.toString());
682                 List list = QueryUtil.list(q, getDialect(), begin, end);
683 
684                 if (obc == null) {
685                     Collections.sort(list);
686                 }
687 
688                 FinderCache.putResult(finderClassName, finderMethodName,
689                     finderParams, finderArgs, list);
690 
691                 return list;
692             }
693             catch (Exception e) {
694                 throw HibernateUtil.processException(e);
695             }
696             finally {
697                 closeSession(session);
698             }
699         }
700         else {
701             return (List)result;
702         }
703     }
704 
705     public void removeByGroupId(long groupId) throws SystemException {
706         Iterator itr = findByGroupId(groupId).iterator();
707 
708         while (itr.hasNext()) {
709             BookmarksFolder bookmarksFolder = (BookmarksFolder)itr.next();
710             remove(bookmarksFolder);
711         }
712     }
713 
714     public void removeByG_P(long groupId, long parentFolderId)
715         throws SystemException {
716         Iterator itr = findByG_P(groupId, parentFolderId).iterator();
717 
718         while (itr.hasNext()) {
719             BookmarksFolder bookmarksFolder = (BookmarksFolder)itr.next();
720             remove(bookmarksFolder);
721         }
722     }
723 
724     public void removeAll() throws SystemException {
725         Iterator itr = findAll().iterator();
726 
727         while (itr.hasNext()) {
728             remove((BookmarksFolder)itr.next());
729         }
730     }
731 
732     public int countByGroupId(long groupId) throws SystemException {
733         String finderClassName = BookmarksFolder.class.getName();
734         String finderMethodName = "countByGroupId";
735         String[] finderParams = new String[] { Long.class.getName() };
736         Object[] finderArgs = new Object[] { new Long(groupId) };
737         Object result = FinderCache.getResult(finderClassName,
738                 finderMethodName, finderParams, finderArgs, getSessionFactory());
739 
740         if (result == null) {
741             Session session = null;
742 
743             try {
744                 session = openSession();
745 
746                 StringMaker query = new StringMaker();
747                 query.append("SELECT COUNT(*) ");
748                 query.append(
749                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
750                 query.append("groupId = ?");
751                 query.append(" ");
752 
753                 Query q = session.createQuery(query.toString());
754                 int queryPos = 0;
755                 q.setLong(queryPos++, groupId);
756 
757                 Long count = null;
758                 Iterator itr = q.list().iterator();
759 
760                 if (itr.hasNext()) {
761                     count = (Long)itr.next();
762                 }
763 
764                 if (count == null) {
765                     count = new Long(0);
766                 }
767 
768                 FinderCache.putResult(finderClassName, finderMethodName,
769                     finderParams, finderArgs, count);
770 
771                 return count.intValue();
772             }
773             catch (Exception e) {
774                 throw HibernateUtil.processException(e);
775             }
776             finally {
777                 closeSession(session);
778             }
779         }
780         else {
781             return ((Long)result).intValue();
782         }
783     }
784 
785     public int countByG_P(long groupId, long parentFolderId)
786         throws SystemException {
787         String finderClassName = BookmarksFolder.class.getName();
788         String finderMethodName = "countByG_P";
789         String[] finderParams = new String[] {
790                 Long.class.getName(), Long.class.getName()
791             };
792         Object[] finderArgs = new Object[] {
793                 new Long(groupId), new Long(parentFolderId)
794             };
795         Object result = FinderCache.getResult(finderClassName,
796                 finderMethodName, finderParams, finderArgs, getSessionFactory());
797 
798         if (result == null) {
799             Session session = null;
800 
801             try {
802                 session = openSession();
803 
804                 StringMaker query = new StringMaker();
805                 query.append("SELECT COUNT(*) ");
806                 query.append(
807                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder WHERE ");
808                 query.append("groupId = ?");
809                 query.append(" AND ");
810                 query.append("parentFolderId = ?");
811                 query.append(" ");
812 
813                 Query q = session.createQuery(query.toString());
814                 int queryPos = 0;
815                 q.setLong(queryPos++, groupId);
816                 q.setLong(queryPos++, parentFolderId);
817 
818                 Long count = null;
819                 Iterator itr = q.list().iterator();
820 
821                 if (itr.hasNext()) {
822                     count = (Long)itr.next();
823                 }
824 
825                 if (count == null) {
826                     count = new Long(0);
827                 }
828 
829                 FinderCache.putResult(finderClassName, finderMethodName,
830                     finderParams, finderArgs, count);
831 
832                 return count.intValue();
833             }
834             catch (Exception e) {
835                 throw HibernateUtil.processException(e);
836             }
837             finally {
838                 closeSession(session);
839             }
840         }
841         else {
842             return ((Long)result).intValue();
843         }
844     }
845 
846     public int countAll() throws SystemException {
847         String finderClassName = BookmarksFolder.class.getName();
848         String finderMethodName = "countAll";
849         String[] finderParams = new String[] {  };
850         Object[] finderArgs = new Object[] {  };
851         Object result = FinderCache.getResult(finderClassName,
852                 finderMethodName, finderParams, finderArgs, getSessionFactory());
853 
854         if (result == null) {
855             Session session = null;
856 
857             try {
858                 session = openSession();
859 
860                 StringMaker query = new StringMaker();
861                 query.append("SELECT COUNT(*) ");
862                 query.append(
863                     "FROM com.liferay.portlet.bookmarks.model.BookmarksFolder");
864 
865                 Query q = session.createQuery(query.toString());
866                 Long count = null;
867                 Iterator itr = q.list().iterator();
868 
869                 if (itr.hasNext()) {
870                     count = (Long)itr.next();
871                 }
872 
873                 if (count == null) {
874                     count = new Long(0);
875                 }
876 
877                 FinderCache.putResult(finderClassName, finderMethodName,
878                     finderParams, finderArgs, count);
879 
880                 return count.intValue();
881             }
882             catch (Exception e) {
883                 throw HibernateUtil.processException(e);
884             }
885             finally {
886                 closeSession(session);
887             }
888         }
889         else {
890             return ((Long)result).intValue();
891         }
892     }
893 
894     protected void initDao() {
895     }
896 
897     private static Log _log = LogFactory.getLog(BookmarksFolderPersistenceImpl.class);
898 }