1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.bookmarks.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.bookmarks.NoSuchEntryException;
36  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
37  import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="BookmarksEntryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class BookmarksEntryPersistenceImpl extends BasePersistence
58      implements BookmarksEntryPersistence {
59      public BookmarksEntry create(long entryId) {
60          BookmarksEntry bookmarksEntry = new BookmarksEntryImpl();
61          bookmarksEntry.setNew(true);
62          bookmarksEntry.setPrimaryKey(entryId);
63  
64          return bookmarksEntry;
65      }
66  
67      public BookmarksEntry remove(long entryId)
68          throws NoSuchEntryException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              BookmarksEntry bookmarksEntry = (BookmarksEntry)session.get(BookmarksEntryImpl.class,
75                      new Long(entryId));
76  
77              if (bookmarksEntry == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No BookmarksEntry exists with the primary key " +
80                          entryId);
81                  }
82  
83                  throw new NoSuchEntryException(
84                      "No BookmarksEntry exists with the primary key " + entryId);
85              }
86  
87              return remove(bookmarksEntry);
88          }
89          catch (NoSuchEntryException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw HibernateUtil.processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public BookmarksEntry remove(BookmarksEntry bookmarksEntry)
101         throws SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106             session.delete(bookmarksEntry);
107             session.flush();
108 
109             return bookmarksEntry;
110         }
111         catch (Exception e) {
112             throw HibernateUtil.processException(e);
113         }
114         finally {
115             closeSession(session);
116             FinderCache.clearCache(BookmarksEntry.class.getName());
117         }
118     }
119 
120     public BookmarksEntry update(
121         com.liferay.portlet.bookmarks.model.BookmarksEntry bookmarksEntry)
122         throws SystemException {
123         return update(bookmarksEntry, false);
124     }
125 
126     public BookmarksEntry update(
127         com.liferay.portlet.bookmarks.model.BookmarksEntry bookmarksEntry,
128         boolean merge) throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             if (merge) {
135                 session.merge(bookmarksEntry);
136             }
137             else {
138                 if (bookmarksEntry.isNew()) {
139                     session.save(bookmarksEntry);
140                 }
141             }
142 
143             session.flush();
144             bookmarksEntry.setNew(false);
145 
146             return bookmarksEntry;
147         }
148         catch (Exception e) {
149             throw HibernateUtil.processException(e);
150         }
151         finally {
152             closeSession(session);
153             FinderCache.clearCache(BookmarksEntry.class.getName());
154         }
155     }
156 
157     public BookmarksEntry findByPrimaryKey(long entryId)
158         throws NoSuchEntryException, SystemException {
159         BookmarksEntry bookmarksEntry = fetchByPrimaryKey(entryId);
160 
161         if (bookmarksEntry == null) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn("No BookmarksEntry exists with the primary key " +
164                     entryId);
165             }
166 
167             throw new NoSuchEntryException(
168                 "No BookmarksEntry exists with the primary key " + entryId);
169         }
170 
171         return bookmarksEntry;
172     }
173 
174     public BookmarksEntry fetchByPrimaryKey(long entryId)
175         throws SystemException {
176         Session session = null;
177 
178         try {
179             session = openSession();
180 
181             return (BookmarksEntry)session.get(BookmarksEntryImpl.class,
182                 new Long(entryId));
183         }
184         catch (Exception e) {
185             throw HibernateUtil.processException(e);
186         }
187         finally {
188             closeSession(session);
189         }
190     }
191 
192     public List findByFolderId(long folderId) throws SystemException {
193         String finderClassName = BookmarksEntry.class.getName();
194         String finderMethodName = "findByFolderId";
195         String[] finderParams = new String[] { Long.class.getName() };
196         Object[] finderArgs = new Object[] { new Long(folderId) };
197         Object result = FinderCache.getResult(finderClassName,
198                 finderMethodName, finderParams, finderArgs, getSessionFactory());
199 
200         if (result == null) {
201             Session session = null;
202 
203             try {
204                 session = openSession();
205 
206                 StringMaker query = new StringMaker();
207                 query.append(
208                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
209                 query.append("folderId = ?");
210                 query.append(" ");
211                 query.append("ORDER BY ");
212                 query.append("folderId ASC").append(", ");
213                 query.append("name ASC");
214 
215                 Query q = session.createQuery(query.toString());
216                 int queryPos = 0;
217                 q.setLong(queryPos++, folderId);
218 
219                 List list = q.list();
220                 FinderCache.putResult(finderClassName, finderMethodName,
221                     finderParams, finderArgs, list);
222 
223                 return list;
224             }
225             catch (Exception e) {
226                 throw HibernateUtil.processException(e);
227             }
228             finally {
229                 closeSession(session);
230             }
231         }
232         else {
233             return (List)result;
234         }
235     }
236 
237     public List findByFolderId(long folderId, int begin, int end)
238         throws SystemException {
239         return findByFolderId(folderId, begin, end, null);
240     }
241 
242     public List findByFolderId(long folderId, int begin, int end,
243         OrderByComparator obc) throws SystemException {
244         String finderClassName = BookmarksEntry.class.getName();
245         String finderMethodName = "findByFolderId";
246         String[] finderParams = new String[] {
247                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
248                 "com.liferay.portal.kernel.util.OrderByComparator"
249             };
250         Object[] finderArgs = new Object[] {
251                 new Long(folderId), String.valueOf(begin), String.valueOf(end),
252                 String.valueOf(obc)
253             };
254         Object result = FinderCache.getResult(finderClassName,
255                 finderMethodName, finderParams, finderArgs, getSessionFactory());
256 
257         if (result == null) {
258             Session session = null;
259 
260             try {
261                 session = openSession();
262 
263                 StringMaker query = new StringMaker();
264                 query.append(
265                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
266                 query.append("folderId = ?");
267                 query.append(" ");
268 
269                 if (obc != null) {
270                     query.append("ORDER BY ");
271                     query.append(obc.getOrderBy());
272                 }
273                 else {
274                     query.append("ORDER BY ");
275                     query.append("folderId ASC").append(", ");
276                     query.append("name ASC");
277                 }
278 
279                 Query q = session.createQuery(query.toString());
280                 int queryPos = 0;
281                 q.setLong(queryPos++, folderId);
282 
283                 List list = QueryUtil.list(q, getDialect(), begin, end);
284                 FinderCache.putResult(finderClassName, finderMethodName,
285                     finderParams, finderArgs, list);
286 
287                 return list;
288             }
289             catch (Exception e) {
290                 throw HibernateUtil.processException(e);
291             }
292             finally {
293                 closeSession(session);
294             }
295         }
296         else {
297             return (List)result;
298         }
299     }
300 
301     public BookmarksEntry findByFolderId_First(long folderId,
302         OrderByComparator obc) throws NoSuchEntryException, SystemException {
303         List list = findByFolderId(folderId, 0, 1, obc);
304 
305         if (list.size() == 0) {
306             StringMaker msg = new StringMaker();
307             msg.append("No BookmarksEntry exists with the key ");
308             msg.append(StringPool.OPEN_CURLY_BRACE);
309             msg.append("folderId=");
310             msg.append(folderId);
311             msg.append(StringPool.CLOSE_CURLY_BRACE);
312             throw new NoSuchEntryException(msg.toString());
313         }
314         else {
315             return (BookmarksEntry)list.get(0);
316         }
317     }
318 
319     public BookmarksEntry findByFolderId_Last(long folderId,
320         OrderByComparator obc) throws NoSuchEntryException, SystemException {
321         int count = countByFolderId(folderId);
322         List list = findByFolderId(folderId, count - 1, count, obc);
323 
324         if (list.size() == 0) {
325             StringMaker msg = new StringMaker();
326             msg.append("No BookmarksEntry exists with the key ");
327             msg.append(StringPool.OPEN_CURLY_BRACE);
328             msg.append("folderId=");
329             msg.append(folderId);
330             msg.append(StringPool.CLOSE_CURLY_BRACE);
331             throw new NoSuchEntryException(msg.toString());
332         }
333         else {
334             return (BookmarksEntry)list.get(0);
335         }
336     }
337 
338     public BookmarksEntry[] findByFolderId_PrevAndNext(long entryId,
339         long folderId, OrderByComparator obc)
340         throws NoSuchEntryException, SystemException {
341         BookmarksEntry bookmarksEntry = findByPrimaryKey(entryId);
342         int count = countByFolderId(folderId);
343         Session session = null;
344 
345         try {
346             session = openSession();
347 
348             StringMaker query = new StringMaker();
349             query.append(
350                 "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
351             query.append("folderId = ?");
352             query.append(" ");
353 
354             if (obc != null) {
355                 query.append("ORDER BY ");
356                 query.append(obc.getOrderBy());
357             }
358             else {
359                 query.append("ORDER BY ");
360                 query.append("folderId ASC").append(", ");
361                 query.append("name ASC");
362             }
363 
364             Query q = session.createQuery(query.toString());
365             int queryPos = 0;
366             q.setLong(queryPos++, folderId);
367 
368             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
369                     bookmarksEntry);
370             BookmarksEntry[] array = new BookmarksEntryImpl[3];
371             array[0] = (BookmarksEntry)objArray[0];
372             array[1] = (BookmarksEntry)objArray[1];
373             array[2] = (BookmarksEntry)objArray[2];
374 
375             return array;
376         }
377         catch (Exception e) {
378             throw HibernateUtil.processException(e);
379         }
380         finally {
381             closeSession(session);
382         }
383     }
384 
385     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
386         throws SystemException {
387         Session session = null;
388 
389         try {
390             session = openSession();
391 
392             DynamicQuery query = queryInitializer.initialize(session);
393 
394             return query.list();
395         }
396         catch (Exception e) {
397             throw HibernateUtil.processException(e);
398         }
399         finally {
400             closeSession(session);
401         }
402     }
403 
404     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
405         int begin, int end) throws SystemException {
406         Session session = null;
407 
408         try {
409             session = openSession();
410 
411             DynamicQuery query = queryInitializer.initialize(session);
412             query.setLimit(begin, end);
413 
414             return query.list();
415         }
416         catch (Exception e) {
417             throw HibernateUtil.processException(e);
418         }
419         finally {
420             closeSession(session);
421         }
422     }
423 
424     public List findAll() throws SystemException {
425         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
426     }
427 
428     public List findAll(int begin, int end) throws SystemException {
429         return findAll(begin, end, null);
430     }
431 
432     public List findAll(int begin, int end, OrderByComparator obc)
433         throws SystemException {
434         String finderClassName = BookmarksEntry.class.getName();
435         String finderMethodName = "findAll";
436         String[] finderParams = new String[] {
437                 "java.lang.Integer", "java.lang.Integer",
438                 "com.liferay.portal.kernel.util.OrderByComparator"
439             };
440         Object[] finderArgs = new Object[] {
441                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
442             };
443         Object result = FinderCache.getResult(finderClassName,
444                 finderMethodName, finderParams, finderArgs, getSessionFactory());
445 
446         if (result == null) {
447             Session session = null;
448 
449             try {
450                 session = openSession();
451 
452                 StringMaker query = new StringMaker();
453                 query.append(
454                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry ");
455 
456                 if (obc != null) {
457                     query.append("ORDER BY ");
458                     query.append(obc.getOrderBy());
459                 }
460                 else {
461                     query.append("ORDER BY ");
462                     query.append("folderId ASC").append(", ");
463                     query.append("name ASC");
464                 }
465 
466                 Query q = session.createQuery(query.toString());
467                 List list = QueryUtil.list(q, getDialect(), begin, end);
468 
469                 if (obc == null) {
470                     Collections.sort(list);
471                 }
472 
473                 FinderCache.putResult(finderClassName, finderMethodName,
474                     finderParams, finderArgs, list);
475 
476                 return list;
477             }
478             catch (Exception e) {
479                 throw HibernateUtil.processException(e);
480             }
481             finally {
482                 closeSession(session);
483             }
484         }
485         else {
486             return (List)result;
487         }
488     }
489 
490     public void removeByFolderId(long folderId) throws SystemException {
491         Iterator itr = findByFolderId(folderId).iterator();
492 
493         while (itr.hasNext()) {
494             BookmarksEntry bookmarksEntry = (BookmarksEntry)itr.next();
495             remove(bookmarksEntry);
496         }
497     }
498 
499     public void removeAll() throws SystemException {
500         Iterator itr = findAll().iterator();
501 
502         while (itr.hasNext()) {
503             remove((BookmarksEntry)itr.next());
504         }
505     }
506 
507     public int countByFolderId(long folderId) throws SystemException {
508         String finderClassName = BookmarksEntry.class.getName();
509         String finderMethodName = "countByFolderId";
510         String[] finderParams = new String[] { Long.class.getName() };
511         Object[] finderArgs = new Object[] { new Long(folderId) };
512         Object result = FinderCache.getResult(finderClassName,
513                 finderMethodName, finderParams, finderArgs, getSessionFactory());
514 
515         if (result == null) {
516             Session session = null;
517 
518             try {
519                 session = openSession();
520 
521                 StringMaker query = new StringMaker();
522                 query.append("SELECT COUNT(*) ");
523                 query.append(
524                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry WHERE ");
525                 query.append("folderId = ?");
526                 query.append(" ");
527 
528                 Query q = session.createQuery(query.toString());
529                 int queryPos = 0;
530                 q.setLong(queryPos++, folderId);
531 
532                 Long count = null;
533                 Iterator itr = q.list().iterator();
534 
535                 if (itr.hasNext()) {
536                     count = (Long)itr.next();
537                 }
538 
539                 if (count == null) {
540                     count = new Long(0);
541                 }
542 
543                 FinderCache.putResult(finderClassName, finderMethodName,
544                     finderParams, finderArgs, count);
545 
546                 return count.intValue();
547             }
548             catch (Exception e) {
549                 throw HibernateUtil.processException(e);
550             }
551             finally {
552                 closeSession(session);
553             }
554         }
555         else {
556             return ((Long)result).intValue();
557         }
558     }
559 
560     public int countAll() throws SystemException {
561         String finderClassName = BookmarksEntry.class.getName();
562         String finderMethodName = "countAll";
563         String[] finderParams = new String[] {  };
564         Object[] finderArgs = new Object[] {  };
565         Object result = FinderCache.getResult(finderClassName,
566                 finderMethodName, finderParams, finderArgs, getSessionFactory());
567 
568         if (result == null) {
569             Session session = null;
570 
571             try {
572                 session = openSession();
573 
574                 StringMaker query = new StringMaker();
575                 query.append("SELECT COUNT(*) ");
576                 query.append(
577                     "FROM com.liferay.portlet.bookmarks.model.BookmarksEntry");
578 
579                 Query q = session.createQuery(query.toString());
580                 Long count = null;
581                 Iterator itr = q.list().iterator();
582 
583                 if (itr.hasNext()) {
584                     count = (Long)itr.next();
585                 }
586 
587                 if (count == null) {
588                     count = new Long(0);
589                 }
590 
591                 FinderCache.putResult(finderClassName, finderMethodName,
592                     finderParams, finderArgs, count);
593 
594                 return count.intValue();
595             }
596             catch (Exception e) {
597                 throw HibernateUtil.processException(e);
598             }
599             finally {
600                 closeSession(session);
601             }
602         }
603         else {
604             return ((Long)result).intValue();
605         }
606     }
607 
608     protected void initDao() {
609     }
610 
611     private static Log _log = LogFactory.getLog(BookmarksEntryPersistenceImpl.class);
612 }