1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.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.GetterUtil;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
35  import com.liferay.portal.model.ModelListener;
36  import com.liferay.portal.service.persistence.BasePersistence;
37  import com.liferay.portal.spring.hibernate.FinderCache;
38  import com.liferay.portal.spring.hibernate.HibernateUtil;
39  import com.liferay.portal.util.PropsUtil;
40  
41  import com.liferay.portlet.bookmarks.NoSuchEntryException;
42  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
43  import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
44  import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryModelImpl;
45  
46  import com.liferay.util.dao.hibernate.QueryUtil;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  import org.hibernate.Query;
52  import org.hibernate.Session;
53  
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.Iterator;
57  import java.util.List;
58  
59  /**
60   * <a href="BookmarksEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   *
64   */
65  public class BookmarksEntryPersistenceImpl extends BasePersistence
66      implements BookmarksEntryPersistence {
67      public BookmarksEntry create(long entryId) {
68          BookmarksEntry bookmarksEntry = new BookmarksEntryImpl();
69  
70          bookmarksEntry.setNew(true);
71          bookmarksEntry.setPrimaryKey(entryId);
72  
73          String uuid = PortalUUIDUtil.generate();
74  
75          bookmarksEntry.setUuid(uuid);
76  
77          return bookmarksEntry;
78      }
79  
80      public BookmarksEntry remove(long entryId)
81          throws NoSuchEntryException, SystemException {
82          Session session = null;
83  
84          try {
85              session = openSession();
86  
87              BookmarksEntry bookmarksEntry = (BookmarksEntry)session.get(BookmarksEntryImpl.class,
88                      new Long(entryId));
89  
90              if (bookmarksEntry == null) {
91                  if (_log.isWarnEnabled()) {
92                      _log.warn("No BookmarksEntry exists with the primary key " +
93                          entryId);
94                  }
95  
96                  throw new NoSuchEntryException(
97                      "No BookmarksEntry exists with the primary key " + entryId);
98              }
99  
100             return remove(bookmarksEntry);
101         }
102         catch (NoSuchEntryException nsee) {
103             throw nsee;
104         }
105         catch (Exception e) {
106             throw HibernateUtil.processException(e);
107         }
108         finally {
109             closeSession(session);
110         }
111     }
112 
113     public BookmarksEntry remove(BookmarksEntry bookmarksEntry)
114         throws SystemException {
115         if (_listeners != null) {
116             for (ModelListener listener : _listeners) {
117                 listener.onBeforeRemove(bookmarksEntry);
118             }
119         }
120 
121         bookmarksEntry = removeImpl(bookmarksEntry);
122 
123         if (_listeners != null) {
124             for (ModelListener listener : _listeners) {
125                 listener.onAfterRemove(bookmarksEntry);
126             }
127         }
128 
129         return bookmarksEntry;
130     }
131 
132     protected BookmarksEntry removeImpl(BookmarksEntry bookmarksEntry)
133         throws SystemException {
134         Session session = null;
135 
136         try {
137             session = openSession();
138 
139             session.delete(bookmarksEntry);
140 
141             session.flush();
142 
143             return bookmarksEntry;
144         }
145         catch (Exception e) {
146             throw HibernateUtil.processException(e);
147         }
148         finally {
149             closeSession(session);
150 
151             FinderCache.clearCache(BookmarksEntry.class.getName());
152         }
153     }
154 
155     /**
156      * @deprecated Use <code>update(BookmarksEntry bookmarksEntry, boolean merge)</code>.
157      */
158     public BookmarksEntry update(BookmarksEntry bookmarksEntry)
159         throws SystemException {
160         if (_log.isWarnEnabled()) {
161             _log.warn(
162                 "Using the deprecated update(BookmarksEntry bookmarksEntry) method. Use update(BookmarksEntry bookmarksEntry, boolean merge) instead.");
163         }
164 
165         return update(bookmarksEntry, false);
166     }
167 
168     /**
169      * Add, update, or merge, the entity. This method also calls the model
170      * listeners to trigger the proper events associated with adding, deleting,
171      * or updating an entity.
172      *
173      * @param        bookmarksEntry the entity to add, update, or merge
174      * @param        merge boolean value for whether to merge the entity. The
175      *                default value is false. Setting merge to true is more
176      *                expensive and should only be true when bookmarksEntry is
177      *                transient. See LEP-5473 for a detailed discussion of this
178      *                method.
179      * @return        true if the portlet can be displayed via Ajax
180      */
181     public BookmarksEntry update(BookmarksEntry bookmarksEntry, boolean merge)
182         throws SystemException {
183         boolean isNew = bookmarksEntry.isNew();
184 
185         if (_listeners != null) {
186             for (ModelListener listener : _listeners) {
187                 if (isNew) {
188                     listener.onBeforeCreate(bookmarksEntry);
189                 }
190                 else {
191                     listener.onBeforeUpdate(bookmarksEntry);
192                 }
193             }
194         }
195 
196         bookmarksEntry = updateImpl(bookmarksEntry, merge);
197 
198         if (_listeners != null) {
199             for (ModelListener listener : _listeners) {
200                 if (isNew) {
201                     listener.onAfterCreate(bookmarksEntry);
202                 }
203                 else {
204                     listener.onAfterUpdate(bookmarksEntry);
205                 }
206             }
207         }
208 
209         return bookmarksEntry;
210     }
211 
212     public BookmarksEntry updateImpl(
213         com.liferay.portlet.bookmarks.model.BookmarksEntry bookmarksEntry,
214         boolean merge) throws SystemException {
215         if (Validator.isNull(bookmarksEntry.getUuid())) {
216             String uuid = PortalUUIDUtil.generate();
217 
218             bookmarksEntry.setUuid(uuid);
219         }
220 
221         Session session = null;
222 
223         try {
224             session = openSession();
225 
226             if (merge) {
227                 session.merge(bookmarksEntry);
228             }
229             else {
230                 if (bookmarksEntry.isNew()) {
231                     session.save(bookmarksEntry);
232                 }
233             }
234 
235             session.flush();
236 
237             bookmarksEntry.setNew(false);
238 
239             return bookmarksEntry;
240         }
241         catch (Exception e) {
242             throw HibernateUtil.processException(e);
243         }
244         finally {
245             closeSession(session);
246 
247             FinderCache.clearCache(BookmarksEntry.class.getName());
248         }
249     }
250 
251     public BookmarksEntry findByPrimaryKey(long entryId)
252         throws NoSuchEntryException, SystemException {
253         BookmarksEntry bookmarksEntry = fetchByPrimaryKey(entryId);
254 
255         if (bookmarksEntry == null) {
256             if (_log.isWarnEnabled()) {
257                 _log.warn("No BookmarksEntry exists with the primary key " +
258                     entryId);
259             }
260 
261             throw new NoSuchEntryException(
262                 "No BookmarksEntry exists with the primary key " + entryId);
263         }
264 
265         return bookmarksEntry;
266     }
267 
268     public BookmarksEntry fetchByPrimaryKey(long entryId)
269         throws SystemException {
270         Session session = null;
271 
272         try {
273             session = openSession();
274 
275             return (BookmarksEntry)session.get(BookmarksEntryImpl.class,
276                 new Long(entryId));
277         }
278         catch (Exception e) {
279             throw HibernateUtil.processException(e);
280         }
281         finally {
282             closeSession(session);
283         }
284     }
285 
286     public List<BookmarksEntry> findByUuid(String uuid)
287         throws SystemException {
288         boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
289         String finderClassName = BookmarksEntry.class.getName();
290         String finderMethodName = "findByUuid";
291         String[] finderParams = new String[] { String.class.getName() };
292         Object[] finderArgs = new Object[] { uuid };
293 
294         Object result = null;
295 
296         if (finderClassNameCacheEnabled) {
297             result = FinderCache.getResult(finderClassName, finderMethodName,
298                     finderParams, finderArgs, getSessionFactory());
299         }
300 
301         if (result == null) {
302             Session session = null;
303 
304             try {
305                 session = openSession();
306 
307                 StringMaker query = new StringMaker();
308 
309                 query.append(
310                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
311 
312                 if (uuid == null) {
313                     query.append("uuid_ IS NULL");
314                 }
315                 else {
316                     query.append("uuid_ = ?");
317                 }
318 
319                 query.append(" ");
320 
321                 query.append("ORDER BY ");
322 
323                 query.append("folderId ASC, ");
324                 query.append("name ASC");
325 
326                 Query q = session.createQuery(query.toString());
327 
328                 int queryPos = 0;
329 
330                 if (uuid != null) {
331                     q.setString(queryPos++, uuid);
332                 }
333 
334                 List<BookmarksEntry> list = q.list();
335 
336                 FinderCache.putResult(finderClassNameCacheEnabled,
337                     finderClassName, finderMethodName, finderParams,
338                     finderArgs, list);
339 
340                 return list;
341             }
342             catch (Exception e) {
343                 throw HibernateUtil.processException(e);
344             }
345             finally {
346                 closeSession(session);
347             }
348         }
349         else {
350             return (List<BookmarksEntry>)result;
351         }
352     }
353 
354     public List<BookmarksEntry> findByUuid(String uuid, int begin, int end)
355         throws SystemException {
356         return findByUuid(uuid, begin, end, null);
357     }
358 
359     public List<BookmarksEntry> findByUuid(String uuid, int begin, int end,
360         OrderByComparator obc) throws SystemException {
361         boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
362         String finderClassName = BookmarksEntry.class.getName();
363         String finderMethodName = "findByUuid";
364         String[] finderParams = new String[] {
365                 String.class.getName(),
366                 
367                 "java.lang.Integer", "java.lang.Integer",
368                 "com.liferay.portal.kernel.util.OrderByComparator"
369             };
370         Object[] finderArgs = new Object[] {
371                 uuid,
372                 
373                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
374             };
375 
376         Object result = null;
377 
378         if (finderClassNameCacheEnabled) {
379             result = FinderCache.getResult(finderClassName, finderMethodName,
380                     finderParams, finderArgs, getSessionFactory());
381         }
382 
383         if (result == null) {
384             Session session = null;
385 
386             try {
387                 session = openSession();
388 
389                 StringMaker query = new StringMaker();
390 
391                 query.append(
392                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
393 
394                 if (uuid == null) {
395                     query.append("uuid_ IS NULL");
396                 }
397                 else {
398                     query.append("uuid_ = ?");
399                 }
400 
401                 query.append(" ");
402 
403                 if (obc != null) {
404                     query.append("ORDER BY ");
405                     query.append(obc.getOrderBy());
406                 }
407 
408                 else {
409                     query.append("ORDER BY ");
410 
411                     query.append("folderId ASC, ");
412                     query.append("name ASC");
413                 }
414 
415                 Query q = session.createQuery(query.toString());
416 
417                 int queryPos = 0;
418 
419                 if (uuid != null) {
420                     q.setString(queryPos++, uuid);
421                 }
422 
423                 List<BookmarksEntry> list = (List<BookmarksEntry>)QueryUtil.list(q,
424                         getDialect(), begin, end);
425 
426                 FinderCache.putResult(finderClassNameCacheEnabled,
427                     finderClassName, finderMethodName, finderParams,
428                     finderArgs, list);
429 
430                 return list;
431             }
432             catch (Exception e) {
433                 throw HibernateUtil.processException(e);
434             }
435             finally {
436                 closeSession(session);
437             }
438         }
439         else {
440             return (List<BookmarksEntry>)result;
441         }
442     }
443 
444     public BookmarksEntry findByUuid_First(String uuid, OrderByComparator obc)
445         throws NoSuchEntryException, SystemException {
446         List<BookmarksEntry> list = findByUuid(uuid, 0, 1, obc);
447 
448         if (list.size() == 0) {
449             StringMaker msg = new StringMaker();
450 
451             msg.append("No BookmarksEntry exists with the key {");
452 
453             msg.append("uuid=" + uuid);
454 
455             msg.append(StringPool.CLOSE_CURLY_BRACE);
456 
457             throw new NoSuchEntryException(msg.toString());
458         }
459         else {
460             return list.get(0);
461         }
462     }
463 
464     public BookmarksEntry findByUuid_Last(String uuid, OrderByComparator obc)
465         throws NoSuchEntryException, SystemException {
466         int count = countByUuid(uuid);
467 
468         List<BookmarksEntry> list = findByUuid(uuid, count - 1, count, obc);
469 
470         if (list.size() == 0) {
471             StringMaker msg = new StringMaker();
472 
473             msg.append("No BookmarksEntry exists with the key {");
474 
475             msg.append("uuid=" + uuid);
476 
477             msg.append(StringPool.CLOSE_CURLY_BRACE);
478 
479             throw new NoSuchEntryException(msg.toString());
480         }
481         else {
482             return list.get(0);
483         }
484     }
485 
486     public BookmarksEntry[] findByUuid_PrevAndNext(long entryId, String uuid,
487         OrderByComparator obc) throws NoSuchEntryException, SystemException {
488         BookmarksEntry bookmarksEntry = findByPrimaryKey(entryId);
489 
490         int count = countByUuid(uuid);
491 
492         Session session = null;
493 
494         try {
495             session = openSession();
496 
497             StringMaker query = new StringMaker();
498 
499             query.append(
500                 "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
501 
502             if (uuid == null) {
503                 query.append("uuid_ IS NULL");
504             }
505             else {
506                 query.append("uuid_ = ?");
507             }
508 
509             query.append(" ");
510 
511             if (obc != null) {
512                 query.append("ORDER BY ");
513                 query.append(obc.getOrderBy());
514             }
515 
516             else {
517                 query.append("ORDER BY ");
518 
519                 query.append("folderId ASC, ");
520                 query.append("name ASC");
521             }
522 
523             Query q = session.createQuery(query.toString());
524 
525             int queryPos = 0;
526 
527             if (uuid != null) {
528                 q.setString(queryPos++, uuid);
529             }
530 
531             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
532                     bookmarksEntry);
533 
534             BookmarksEntry[] array = new BookmarksEntryImpl[3];
535 
536             array[0] = (BookmarksEntry)objArray[0];
537             array[1] = (BookmarksEntry)objArray[1];
538             array[2] = (BookmarksEntry)objArray[2];
539 
540             return array;
541         }
542         catch (Exception e) {
543             throw HibernateUtil.processException(e);
544         }
545         finally {
546             closeSession(session);
547         }
548     }
549 
550     public List<BookmarksEntry> findByFolderId(long folderId)
551         throws SystemException {
552         boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
553         String finderClassName = BookmarksEntry.class.getName();
554         String finderMethodName = "findByFolderId";
555         String[] finderParams = new String[] { Long.class.getName() };
556         Object[] finderArgs = new Object[] { new Long(folderId) };
557 
558         Object result = null;
559 
560         if (finderClassNameCacheEnabled) {
561             result = FinderCache.getResult(finderClassName, finderMethodName,
562                     finderParams, finderArgs, getSessionFactory());
563         }
564 
565         if (result == null) {
566             Session session = null;
567 
568             try {
569                 session = openSession();
570 
571                 StringMaker query = new StringMaker();
572 
573                 query.append(
574                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
575 
576                 query.append("folderId = ?");
577 
578                 query.append(" ");
579 
580                 query.append("ORDER BY ");
581 
582                 query.append("folderId ASC, ");
583                 query.append("name ASC");
584 
585                 Query q = session.createQuery(query.toString());
586 
587                 int queryPos = 0;
588 
589                 q.setLong(queryPos++, folderId);
590 
591                 List<BookmarksEntry> list = q.list();
592 
593                 FinderCache.putResult(finderClassNameCacheEnabled,
594                     finderClassName, finderMethodName, finderParams,
595                     finderArgs, list);
596 
597                 return list;
598             }
599             catch (Exception e) {
600                 throw HibernateUtil.processException(e);
601             }
602             finally {
603                 closeSession(session);
604             }
605         }
606         else {
607             return (List<BookmarksEntry>)result;
608         }
609     }
610 
611     public List<BookmarksEntry> findByFolderId(long folderId, int begin, int end)
612         throws SystemException {
613         return findByFolderId(folderId, begin, end, null);
614     }
615 
616     public List<BookmarksEntry> findByFolderId(long folderId, int begin,
617         int end, OrderByComparator obc) throws SystemException {
618         boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
619         String finderClassName = BookmarksEntry.class.getName();
620         String finderMethodName = "findByFolderId";
621         String[] finderParams = new String[] {
622                 Long.class.getName(),
623                 
624                 "java.lang.Integer", "java.lang.Integer",
625                 "com.liferay.portal.kernel.util.OrderByComparator"
626             };
627         Object[] finderArgs = new Object[] {
628                 new Long(folderId),
629                 
630                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
631             };
632 
633         Object result = null;
634 
635         if (finderClassNameCacheEnabled) {
636             result = FinderCache.getResult(finderClassName, finderMethodName,
637                     finderParams, finderArgs, getSessionFactory());
638         }
639 
640         if (result == null) {
641             Session session = null;
642 
643             try {
644                 session = openSession();
645 
646                 StringMaker query = new StringMaker();
647 
648                 query.append(
649                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
650 
651                 query.append("folderId = ?");
652 
653                 query.append(" ");
654 
655                 if (obc != null) {
656                     query.append("ORDER BY ");
657                     query.append(obc.getOrderBy());
658                 }
659 
660                 else {
661                     query.append("ORDER BY ");
662 
663                     query.append("folderId ASC, ");
664                     query.append("name ASC");
665                 }
666 
667                 Query q = session.createQuery(query.toString());
668 
669                 int queryPos = 0;
670 
671                 q.setLong(queryPos++, folderId);
672 
673                 List<BookmarksEntry> list = (List<BookmarksEntry>)QueryUtil.list(q,
674                         getDialect(), begin, end);
675 
676                 FinderCache.putResult(finderClassNameCacheEnabled,
677                     finderClassName, finderMethodName, finderParams,
678                     finderArgs, list);
679 
680                 return list;
681             }
682             catch (Exception e) {
683                 throw HibernateUtil.processException(e);
684             }
685             finally {
686                 closeSession(session);
687             }
688         }
689         else {
690             return (List<BookmarksEntry>)result;
691         }
692     }
693 
694     public BookmarksEntry findByFolderId_First(long folderId,
695         OrderByComparator obc) throws NoSuchEntryException, SystemException {
696         List<BookmarksEntry> list = findByFolderId(folderId, 0, 1, obc);
697 
698         if (list.size() == 0) {
699             StringMaker msg = new StringMaker();
700 
701             msg.append("No BookmarksEntry exists with the key {");
702 
703             msg.append("folderId=" + folderId);
704 
705             msg.append(StringPool.CLOSE_CURLY_BRACE);
706 
707             throw new NoSuchEntryException(msg.toString());
708         }
709         else {
710             return list.get(0);
711         }
712     }
713 
714     public BookmarksEntry findByFolderId_Last(long folderId,
715         OrderByComparator obc) throws NoSuchEntryException, SystemException {
716         int count = countByFolderId(folderId);
717 
718         List<BookmarksEntry> list = findByFolderId(folderId, count - 1, count,
719                 obc);
720 
721         if (list.size() == 0) {
722             StringMaker msg = new StringMaker();
723 
724             msg.append("No BookmarksEntry exists with the key {");
725 
726             msg.append("folderId=" + folderId);
727 
728             msg.append(StringPool.CLOSE_CURLY_BRACE);
729 
730             throw new NoSuchEntryException(msg.toString());
731         }
732         else {
733             return list.get(0);
734         }
735     }
736 
737     public BookmarksEntry[] findByFolderId_PrevAndNext(long entryId,
738         long folderId, OrderByComparator obc)
739         throws NoSuchEntryException, SystemException {
740         BookmarksEntry bookmarksEntry = findByPrimaryKey(entryId);
741 
742         int count = countByFolderId(folderId);
743 
744         Session session = null;
745 
746         try {
747             session = openSession();
748 
749             StringMaker query = new StringMaker();
750 
751             query.append(
752                 "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
753 
754             query.append("folderId = ?");
755 
756             query.append(" ");
757 
758             if (obc != null) {
759                 query.append("ORDER BY ");
760                 query.append(obc.getOrderBy());
761             }
762 
763             else {
764                 query.append("ORDER BY ");
765 
766                 query.append("folderId ASC, ");
767                 query.append("name ASC");
768             }
769 
770             Query q = session.createQuery(query.toString());
771 
772             int queryPos = 0;
773 
774             q.setLong(queryPos++, folderId);
775 
776             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
777                     bookmarksEntry);
778 
779             BookmarksEntry[] array = new BookmarksEntryImpl[3];
780 
781             array[0] = (BookmarksEntry)objArray[0];
782             array[1] = (BookmarksEntry)objArray[1];
783             array[2] = (BookmarksEntry)objArray[2];
784 
785             return array;
786         }
787         catch (Exception e) {
788             throw HibernateUtil.processException(e);
789         }
790         finally {
791             closeSession(session);
792         }
793     }
794 
795     public List<BookmarksEntry> findWithDynamicQuery(
796         DynamicQueryInitializer queryInitializer) throws SystemException {
797         Session session = null;
798 
799         try {
800             session = openSession();
801 
802             DynamicQuery query = queryInitializer.initialize(session);
803 
804             return query.list();
805         }
806         catch (Exception e) {
807             throw HibernateUtil.processException(e);
808         }
809         finally {
810             closeSession(session);
811         }
812     }
813 
814     public List<BookmarksEntry> findWithDynamicQuery(
815         DynamicQueryInitializer queryInitializer, int begin, int end)
816         throws SystemException {
817         Session session = null;
818 
819         try {
820             session = openSession();
821 
822             DynamicQuery query = queryInitializer.initialize(session);
823 
824             query.setLimit(begin, end);
825 
826             return query.list();
827         }
828         catch (Exception e) {
829             throw HibernateUtil.processException(e);
830         }
831         finally {
832             closeSession(session);
833         }
834     }
835 
836     public List<BookmarksEntry> findAll() throws SystemException {
837         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
838     }
839 
840     public List<BookmarksEntry> findAll(int begin, int end)
841         throws SystemException {
842         return findAll(begin, end, null);
843     }
844 
845     public List<BookmarksEntry> findAll(int begin, int end,
846         OrderByComparator obc) throws SystemException {
847         boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
848         String finderClassName = BookmarksEntry.class.getName();
849         String finderMethodName = "findAll";
850         String[] finderParams = new String[] {
851                 "java.lang.Integer", "java.lang.Integer",
852                 "com.liferay.portal.kernel.util.OrderByComparator"
853             };
854         Object[] finderArgs = new Object[] {
855                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
856             };
857 
858         Object result = null;
859 
860         if (finderClassNameCacheEnabled) {
861             result = FinderCache.getResult(finderClassName, finderMethodName,
862                     finderParams, finderArgs, getSessionFactory());
863         }
864 
865         if (result == null) {
866             Session session = null;
867 
868             try {
869                 session = openSession();
870 
871                 StringMaker query = new StringMaker();
872 
873                 query.append(
874                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry ");
875 
876                 if (obc != null) {
877                     query.append("ORDER BY ");
878                     query.append(obc.getOrderBy());
879                 }
880 
881                 else {
882                     query.append("ORDER BY ");
883 
884                     query.append("folderId ASC, ");
885                     query.append("name ASC");
886                 }
887 
888                 Query q = session.createQuery(query.toString());
889 
890                 List<BookmarksEntry> list = (List<BookmarksEntry>)QueryUtil.list(q,
891                         getDialect(), begin, end);
892 
893                 if (obc == null) {
894                     Collections.sort(list);
895                 }
896 
897                 FinderCache.putResult(finderClassNameCacheEnabled,
898                     finderClassName, finderMethodName, finderParams,
899                     finderArgs, list);
900 
901                 return list;
902             }
903             catch (Exception e) {
904                 throw HibernateUtil.processException(e);
905             }
906             finally {
907                 closeSession(session);
908             }
909         }
910         else {
911             return (List<BookmarksEntry>)result;
912         }
913     }
914 
915     public void removeByUuid(String uuid) throws SystemException {
916         for (BookmarksEntry bookmarksEntry : findByUuid(uuid)) {
917             remove(bookmarksEntry);
918         }
919     }
920 
921     public void removeByFolderId(long folderId) throws SystemException {
922         for (BookmarksEntry bookmarksEntry : findByFolderId(folderId)) {
923             remove(bookmarksEntry);
924         }
925     }
926 
927     public void removeAll() throws SystemException {
928         for (BookmarksEntry bookmarksEntry : findAll()) {
929             remove(bookmarksEntry);
930         }
931     }
932 
933     public int countByUuid(String uuid) throws SystemException {
934         boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
935         String finderClassName = BookmarksEntry.class.getName();
936         String finderMethodName = "countByUuid";
937         String[] finderParams = new String[] { String.class.getName() };
938         Object[] finderArgs = new Object[] { uuid };
939 
940         Object result = null;
941 
942         if (finderClassNameCacheEnabled) {
943             result = FinderCache.getResult(finderClassName, finderMethodName,
944                     finderParams, finderArgs, getSessionFactory());
945         }
946 
947         if (result == null) {
948             Session session = null;
949 
950             try {
951                 session = openSession();
952 
953                 StringMaker query = new StringMaker();
954 
955                 query.append("SELECT COUNT(*) ");
956                 query.append(
957                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
958 
959                 if (uuid == null) {
960                     query.append("uuid_ IS NULL");
961                 }
962                 else {
963                     query.append("uuid_ = ?");
964                 }
965 
966                 query.append(" ");
967 
968                 Query q = session.createQuery(query.toString());
969 
970                 int queryPos = 0;
971 
972                 if (uuid != null) {
973                     q.setString(queryPos++, uuid);
974                 }
975 
976                 Long count = null;
977 
978                 Iterator<Long> itr = q.list().iterator();
979 
980                 if (itr.hasNext()) {
981                     count = itr.next();
982                 }
983 
984                 if (count == null) {
985                     count = new Long(0);
986                 }
987 
988                 FinderCache.putResult(finderClassNameCacheEnabled,
989                     finderClassName, finderMethodName, finderParams,
990                     finderArgs, count);
991 
992                 return count.intValue();
993             }
994             catch (Exception e) {
995                 throw HibernateUtil.processException(e);
996             }
997             finally {
998                 closeSession(session);
999             }
1000        }
1001        else {
1002            return ((Long)result).intValue();
1003        }
1004    }
1005
1006    public int countByFolderId(long folderId) throws SystemException {
1007        boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
1008        String finderClassName = BookmarksEntry.class.getName();
1009        String finderMethodName = "countByFolderId";
1010        String[] finderParams = new String[] { Long.class.getName() };
1011        Object[] finderArgs = new Object[] { new Long(folderId) };
1012
1013        Object result = null;
1014
1015        if (finderClassNameCacheEnabled) {
1016            result = FinderCache.getResult(finderClassName, finderMethodName,
1017                    finderParams, finderArgs, getSessionFactory());
1018        }
1019
1020        if (result == null) {
1021            Session session = null;
1022
1023            try {
1024                session = openSession();
1025
1026                StringMaker query = new StringMaker();
1027
1028                query.append("SELECT COUNT(*) ");
1029                query.append(
1030                    "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
1031
1032                query.append("folderId = ?");
1033
1034                query.append(" ");
1035
1036                Query q = session.createQuery(query.toString());
1037
1038                int queryPos = 0;
1039
1040                q.setLong(queryPos++, folderId);
1041
1042                Long count = null;
1043
1044                Iterator<Long> itr = q.list().iterator();
1045
1046                if (itr.hasNext()) {
1047                    count = itr.next();
1048                }
1049
1050                if (count == null) {
1051                    count = new Long(0);
1052                }
1053
1054                FinderCache.putResult(finderClassNameCacheEnabled,
1055                    finderClassName, finderMethodName, finderParams,
1056                    finderArgs, count);
1057
1058                return count.intValue();
1059            }
1060            catch (Exception e) {
1061                throw HibernateUtil.processException(e);
1062            }
1063            finally {
1064                closeSession(session);
1065            }
1066        }
1067        else {
1068            return ((Long)result).intValue();
1069        }
1070    }
1071
1072    public int countAll() throws SystemException {
1073        boolean finderClassNameCacheEnabled = BookmarksEntryModelImpl.CACHE_ENABLED;
1074        String finderClassName = BookmarksEntry.class.getName();
1075        String finderMethodName = "countAll";
1076        String[] finderParams = new String[] {  };
1077        Object[] finderArgs = new Object[] {  };
1078
1079        Object result = null;
1080
1081        if (finderClassNameCacheEnabled) {
1082            result = FinderCache.getResult(finderClassName, finderMethodName,
1083                    finderParams, finderArgs, getSessionFactory());
1084        }
1085
1086        if (result == null) {
1087            Session session = null;
1088
1089            try {
1090                session = openSession();
1091
1092                Query q = session.createQuery(
1093                        "SELECT COUNT(*) FROM com.liferay.portlet.bookmarks.model.BookmarksEntry");
1094
1095                Long count = null;
1096
1097                Iterator<Long> itr = q.list().iterator();
1098
1099                if (itr.hasNext()) {
1100                    count = itr.next();
1101                }
1102
1103                if (count == null) {
1104                    count = new Long(0);
1105                }
1106
1107                FinderCache.putResult(finderClassNameCacheEnabled,
1108                    finderClassName, finderMethodName, finderParams,
1109                    finderArgs, count);
1110
1111                return count.intValue();
1112            }
1113            catch (Exception e) {
1114                throw HibernateUtil.processException(e);
1115            }
1116            finally {
1117                closeSession(session);
1118            }
1119        }
1120        else {
1121            return ((Long)result).intValue();
1122        }
1123    }
1124
1125    protected void initDao() {
1126        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1127                    PropsUtil.get(
1128                        "value.object.listener.com.liferay.portlet.bookmarks.model.BookmarksEntry")));
1129
1130        if (listenerClassNames.length > 0) {
1131            try {
1132                List<ModelListener> listeners = new ArrayList<ModelListener>();
1133
1134                for (String listenerClassName : listenerClassNames) {
1135                    listeners.add((ModelListener)Class.forName(
1136                            listenerClassName).newInstance());
1137                }
1138
1139                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1140            }
1141            catch (Exception e) {
1142                _log.error(e);
1143            }
1144        }
1145    }
1146
1147    private static Log _log = LogFactory.getLog(BookmarksEntryPersistenceImpl.class);
1148    private ModelListener[] _listeners;
1149}