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