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