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.blogs.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.blogs.NoSuchCategoryException;
36  import com.liferay.portlet.blogs.model.BlogsCategory;
37  import com.liferay.portlet.blogs.model.impl.BlogsCategoryImpl;
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="BlogsCategoryPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class BlogsCategoryPersistenceImpl extends BasePersistence
58      implements BlogsCategoryPersistence {
59      public BlogsCategory create(long categoryId) {
60          BlogsCategory blogsCategory = new BlogsCategoryImpl();
61          blogsCategory.setNew(true);
62          blogsCategory.setPrimaryKey(categoryId);
63  
64          return blogsCategory;
65      }
66  
67      public BlogsCategory remove(long categoryId)
68          throws NoSuchCategoryException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              BlogsCategory blogsCategory = (BlogsCategory)session.get(BlogsCategoryImpl.class,
75                      new Long(categoryId));
76  
77              if (blogsCategory == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No BlogsCategory exists with the primary key " +
80                          categoryId);
81                  }
82  
83                  throw new NoSuchCategoryException(
84                      "No BlogsCategory exists with the primary key " +
85                      categoryId);
86              }
87  
88              return remove(blogsCategory);
89          }
90          catch (NoSuchCategoryException nsee) {
91              throw nsee;
92          }
93          catch (Exception e) {
94              throw HibernateUtil.processException(e);
95          }
96          finally {
97              closeSession(session);
98          }
99      }
100 
101     public BlogsCategory remove(BlogsCategory blogsCategory)
102         throws SystemException {
103         Session session = null;
104 
105         try {
106             session = openSession();
107             session.delete(blogsCategory);
108             session.flush();
109 
110             return blogsCategory;
111         }
112         catch (Exception e) {
113             throw HibernateUtil.processException(e);
114         }
115         finally {
116             closeSession(session);
117             FinderCache.clearCache(BlogsCategory.class.getName());
118         }
119     }
120 
121     public BlogsCategory update(
122         com.liferay.portlet.blogs.model.BlogsCategory blogsCategory)
123         throws SystemException {
124         return update(blogsCategory, false);
125     }
126 
127     public BlogsCategory update(
128         com.liferay.portlet.blogs.model.BlogsCategory blogsCategory,
129         boolean merge) throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             if (merge) {
136                 session.merge(blogsCategory);
137             }
138             else {
139                 if (blogsCategory.isNew()) {
140                     session.save(blogsCategory);
141                 }
142             }
143 
144             session.flush();
145             blogsCategory.setNew(false);
146 
147             return blogsCategory;
148         }
149         catch (Exception e) {
150             throw HibernateUtil.processException(e);
151         }
152         finally {
153             closeSession(session);
154             FinderCache.clearCache(BlogsCategory.class.getName());
155         }
156     }
157 
158     public BlogsCategory findByPrimaryKey(long categoryId)
159         throws NoSuchCategoryException, SystemException {
160         BlogsCategory blogsCategory = fetchByPrimaryKey(categoryId);
161 
162         if (blogsCategory == null) {
163             if (_log.isWarnEnabled()) {
164                 _log.warn("No BlogsCategory exists with the primary key " +
165                     categoryId);
166             }
167 
168             throw new NoSuchCategoryException(
169                 "No BlogsCategory exists with the primary key " + categoryId);
170         }
171 
172         return blogsCategory;
173     }
174 
175     public BlogsCategory fetchByPrimaryKey(long categoryId)
176         throws SystemException {
177         Session session = null;
178 
179         try {
180             session = openSession();
181 
182             return (BlogsCategory)session.get(BlogsCategoryImpl.class,
183                 new Long(categoryId));
184         }
185         catch (Exception e) {
186             throw HibernateUtil.processException(e);
187         }
188         finally {
189             closeSession(session);
190         }
191     }
192 
193     public List findByParentCategoryId(long parentCategoryId)
194         throws SystemException {
195         String finderClassName = BlogsCategory.class.getName();
196         String finderMethodName = "findByParentCategoryId";
197         String[] finderParams = new String[] { Long.class.getName() };
198         Object[] finderArgs = new Object[] { new Long(parentCategoryId) };
199         Object result = FinderCache.getResult(finderClassName,
200                 finderMethodName, finderParams, finderArgs, getSessionFactory());
201 
202         if (result == null) {
203             Session session = null;
204 
205             try {
206                 session = openSession();
207 
208                 StringMaker query = new StringMaker();
209                 query.append(
210                     "FROM com.liferay.portlet.blogs.model.BlogsCategory WHERE ");
211                 query.append("parentCategoryId = ?");
212                 query.append(" ");
213                 query.append("ORDER BY ");
214                 query.append("name ASC");
215 
216                 Query q = session.createQuery(query.toString());
217                 int queryPos = 0;
218                 q.setLong(queryPos++, parentCategoryId);
219 
220                 List list = q.list();
221                 FinderCache.putResult(finderClassName, finderMethodName,
222                     finderParams, finderArgs, list);
223 
224                 return list;
225             }
226             catch (Exception e) {
227                 throw HibernateUtil.processException(e);
228             }
229             finally {
230                 closeSession(session);
231             }
232         }
233         else {
234             return (List)result;
235         }
236     }
237 
238     public List findByParentCategoryId(long parentCategoryId, int begin, int end)
239         throws SystemException {
240         return findByParentCategoryId(parentCategoryId, begin, end, null);
241     }
242 
243     public List findByParentCategoryId(long parentCategoryId, int begin,
244         int end, OrderByComparator obc) throws SystemException {
245         String finderClassName = BlogsCategory.class.getName();
246         String finderMethodName = "findByParentCategoryId";
247         String[] finderParams = new String[] {
248                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
249                 "com.liferay.portal.kernel.util.OrderByComparator"
250             };
251         Object[] finderArgs = new Object[] {
252                 new Long(parentCategoryId), String.valueOf(begin),
253                 String.valueOf(end), String.valueOf(obc)
254             };
255         Object result = FinderCache.getResult(finderClassName,
256                 finderMethodName, finderParams, finderArgs, getSessionFactory());
257 
258         if (result == null) {
259             Session session = null;
260 
261             try {
262                 session = openSession();
263 
264                 StringMaker query = new StringMaker();
265                 query.append(
266                     "FROM com.liferay.portlet.blogs.model.BlogsCategory WHERE ");
267                 query.append("parentCategoryId = ?");
268                 query.append(" ");
269 
270                 if (obc != null) {
271                     query.append("ORDER BY ");
272                     query.append(obc.getOrderBy());
273                 }
274                 else {
275                     query.append("ORDER BY ");
276                     query.append("name ASC");
277                 }
278 
279                 Query q = session.createQuery(query.toString());
280                 int queryPos = 0;
281                 q.setLong(queryPos++, parentCategoryId);
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 BlogsCategory findByParentCategoryId_First(long parentCategoryId,
302         OrderByComparator obc) throws NoSuchCategoryException, SystemException {
303         List list = findByParentCategoryId(parentCategoryId, 0, 1, obc);
304 
305         if (list.size() == 0) {
306             StringMaker msg = new StringMaker();
307             msg.append("No BlogsCategory exists with the key ");
308             msg.append(StringPool.OPEN_CURLY_BRACE);
309             msg.append("parentCategoryId=");
310             msg.append(parentCategoryId);
311             msg.append(StringPool.CLOSE_CURLY_BRACE);
312             throw new NoSuchCategoryException(msg.toString());
313         }
314         else {
315             return (BlogsCategory)list.get(0);
316         }
317     }
318 
319     public BlogsCategory findByParentCategoryId_Last(long parentCategoryId,
320         OrderByComparator obc) throws NoSuchCategoryException, SystemException {
321         int count = countByParentCategoryId(parentCategoryId);
322         List list = findByParentCategoryId(parentCategoryId, count - 1, count,
323                 obc);
324 
325         if (list.size() == 0) {
326             StringMaker msg = new StringMaker();
327             msg.append("No BlogsCategory exists with the key ");
328             msg.append(StringPool.OPEN_CURLY_BRACE);
329             msg.append("parentCategoryId=");
330             msg.append(parentCategoryId);
331             msg.append(StringPool.CLOSE_CURLY_BRACE);
332             throw new NoSuchCategoryException(msg.toString());
333         }
334         else {
335             return (BlogsCategory)list.get(0);
336         }
337     }
338 
339     public BlogsCategory[] findByParentCategoryId_PrevAndNext(long categoryId,
340         long parentCategoryId, OrderByComparator obc)
341         throws NoSuchCategoryException, SystemException {
342         BlogsCategory blogsCategory = findByPrimaryKey(categoryId);
343         int count = countByParentCategoryId(parentCategoryId);
344         Session session = null;
345 
346         try {
347             session = openSession();
348 
349             StringMaker query = new StringMaker();
350             query.append(
351                 "FROM com.liferay.portlet.blogs.model.BlogsCategory WHERE ");
352             query.append("parentCategoryId = ?");
353             query.append(" ");
354 
355             if (obc != null) {
356                 query.append("ORDER BY ");
357                 query.append(obc.getOrderBy());
358             }
359             else {
360                 query.append("ORDER BY ");
361                 query.append("name ASC");
362             }
363 
364             Query q = session.createQuery(query.toString());
365             int queryPos = 0;
366             q.setLong(queryPos++, parentCategoryId);
367 
368             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
369                     blogsCategory);
370             BlogsCategory[] array = new BlogsCategoryImpl[3];
371             array[0] = (BlogsCategory)objArray[0];
372             array[1] = (BlogsCategory)objArray[1];
373             array[2] = (BlogsCategory)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 = BlogsCategory.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.blogs.model.BlogsCategory ");
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("name ASC");
463                 }
464 
465                 Query q = session.createQuery(query.toString());
466                 List list = QueryUtil.list(q, getDialect(), begin, end);
467 
468                 if (obc == null) {
469                     Collections.sort(list);
470                 }
471 
472                 FinderCache.putResult(finderClassName, finderMethodName,
473                     finderParams, finderArgs, list);
474 
475                 return list;
476             }
477             catch (Exception e) {
478                 throw HibernateUtil.processException(e);
479             }
480             finally {
481                 closeSession(session);
482             }
483         }
484         else {
485             return (List)result;
486         }
487     }
488 
489     public void removeByParentCategoryId(long parentCategoryId)
490         throws SystemException {
491         Iterator itr = findByParentCategoryId(parentCategoryId).iterator();
492 
493         while (itr.hasNext()) {
494             BlogsCategory blogsCategory = (BlogsCategory)itr.next();
495             remove(blogsCategory);
496         }
497     }
498 
499     public void removeAll() throws SystemException {
500         Iterator itr = findAll().iterator();
501 
502         while (itr.hasNext()) {
503             remove((BlogsCategory)itr.next());
504         }
505     }
506 
507     public int countByParentCategoryId(long parentCategoryId)
508         throws SystemException {
509         String finderClassName = BlogsCategory.class.getName();
510         String finderMethodName = "countByParentCategoryId";
511         String[] finderParams = new String[] { Long.class.getName() };
512         Object[] finderArgs = new Object[] { new Long(parentCategoryId) };
513         Object result = FinderCache.getResult(finderClassName,
514                 finderMethodName, finderParams, finderArgs, getSessionFactory());
515 
516         if (result == null) {
517             Session session = null;
518 
519             try {
520                 session = openSession();
521 
522                 StringMaker query = new StringMaker();
523                 query.append("SELECT COUNT(*) ");
524                 query.append(
525                     "FROM com.liferay.portlet.blogs.model.BlogsCategory WHERE ");
526                 query.append("parentCategoryId = ?");
527                 query.append(" ");
528 
529                 Query q = session.createQuery(query.toString());
530                 int queryPos = 0;
531                 q.setLong(queryPos++, parentCategoryId);
532 
533                 Long count = null;
534                 Iterator itr = q.list().iterator();
535 
536                 if (itr.hasNext()) {
537                     count = (Long)itr.next();
538                 }
539 
540                 if (count == null) {
541                     count = new Long(0);
542                 }
543 
544                 FinderCache.putResult(finderClassName, finderMethodName,
545                     finderParams, finderArgs, count);
546 
547                 return count.intValue();
548             }
549             catch (Exception e) {
550                 throw HibernateUtil.processException(e);
551             }
552             finally {
553                 closeSession(session);
554             }
555         }
556         else {
557             return ((Long)result).intValue();
558         }
559     }
560 
561     public int countAll() throws SystemException {
562         String finderClassName = BlogsCategory.class.getName();
563         String finderMethodName = "countAll";
564         String[] finderParams = new String[] {  };
565         Object[] finderArgs = new Object[] {  };
566         Object result = FinderCache.getResult(finderClassName,
567                 finderMethodName, finderParams, finderArgs, getSessionFactory());
568 
569         if (result == null) {
570             Session session = null;
571 
572             try {
573                 session = openSession();
574 
575                 StringMaker query = new StringMaker();
576                 query.append("SELECT COUNT(*) ");
577                 query.append(
578                     "FROM com.liferay.portlet.blogs.model.BlogsCategory");
579 
580                 Query q = session.createQuery(query.toString());
581                 Long count = null;
582                 Iterator itr = q.list().iterator();
583 
584                 if (itr.hasNext()) {
585                     count = (Long)itr.next();
586                 }
587 
588                 if (count == null) {
589                     count = new Long(0);
590                 }
591 
592                 FinderCache.putResult(finderClassName, finderMethodName,
593                     finderParams, finderArgs, count);
594 
595                 return count.intValue();
596             }
597             catch (Exception e) {
598                 throw HibernateUtil.processException(e);
599             }
600             finally {
601                 closeSession(session);
602             }
603         }
604         else {
605             return ((Long)result).intValue();
606         }
607     }
608 
609     protected void initDao() {
610     }
611 
612     private static Log _log = LogFactory.getLog(BlogsCategoryPersistenceImpl.class);
613 }