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