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