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