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