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