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.BatchSessionUtil;
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 {
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             if (BatchSessionUtil.isEnabled()) {
133                 Object staleObject = session.get(ShoppingCategoryImpl.class,
134                         shoppingCategory.getPrimaryKeyObj());
135 
136                 if (staleObject != null) {
137                     session.evict(staleObject);
138                 }
139             }
140 
141             session.delete(shoppingCategory);
142 
143             session.flush();
144 
145             return shoppingCategory;
146         }
147         catch (Exception e) {
148             throw processException(e);
149         }
150         finally {
151             closeSession(session);
152 
153             FinderCacheUtil.clearCache(ShoppingCategory.class.getName());
154         }
155     }
156 
157     /**
158      * @deprecated Use <code>update(ShoppingCategory shoppingCategory, boolean merge)</code>.
159      */
160     public ShoppingCategory update(ShoppingCategory shoppingCategory)
161         throws SystemException {
162         if (_log.isWarnEnabled()) {
163             _log.warn(
164                 "Using the deprecated update(ShoppingCategory shoppingCategory) method. Use update(ShoppingCategory shoppingCategory, boolean merge) instead.");
165         }
166 
167         return update(shoppingCategory, false);
168     }
169 
170     /**
171      * Add, update, or merge, the entity. This method also calls the model
172      * listeners to trigger the proper events associated with adding, deleting,
173      * or updating an entity.
174      *
175      * @param        shoppingCategory the entity to add, update, or merge
176      * @param        merge boolean value for whether to merge the entity. The
177      *                default value is false. Setting merge to true is more
178      *                expensive and should only be true when shoppingCategory is
179      *                transient. See LEP-5473 for a detailed discussion of this
180      *                method.
181      * @return        true if the portlet can be displayed via Ajax
182      */
183     public ShoppingCategory update(ShoppingCategory shoppingCategory,
184         boolean merge) throws SystemException {
185         boolean isNew = shoppingCategory.isNew();
186 
187         if (_listeners.length > 0) {
188             for (ModelListener listener : _listeners) {
189                 if (isNew) {
190                     listener.onBeforeCreate(shoppingCategory);
191                 }
192                 else {
193                     listener.onBeforeUpdate(shoppingCategory);
194                 }
195             }
196         }
197 
198         shoppingCategory = updateImpl(shoppingCategory, merge);
199 
200         if (_listeners.length > 0) {
201             for (ModelListener listener : _listeners) {
202                 if (isNew) {
203                     listener.onAfterCreate(shoppingCategory);
204                 }
205                 else {
206                     listener.onAfterUpdate(shoppingCategory);
207                 }
208             }
209         }
210 
211         return shoppingCategory;
212     }
213 
214     public ShoppingCategory updateImpl(
215         com.liferay.portlet.shopping.model.ShoppingCategory shoppingCategory,
216         boolean merge) throws SystemException {
217         Session session = null;
218 
219         try {
220             session = openSession();
221 
222             BatchSessionUtil.update(session, shoppingCategory, merge);
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 = null;
890 
891                 if (obc == null) {
892                     list = (List<ShoppingCategory>)QueryUtil.list(q,
893                             getDialect(), start, end, false);
894 
895                     Collections.sort(list);
896                 }
897                 else {
898                     list = (List<ShoppingCategory>)QueryUtil.list(q,
899                             getDialect(), start, end);
900                 }
901 
902                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
903                     finderClassName, finderMethodName, finderParams,
904                     finderArgs, list);
905 
906                 return list;
907             }
908             catch (Exception e) {
909                 throw processException(e);
910             }
911             finally {
912                 closeSession(session);
913             }
914         }
915         else {
916             return (List<ShoppingCategory>)result;
917         }
918     }
919 
920     public void removeByGroupId(long groupId) throws SystemException {
921         for (ShoppingCategory shoppingCategory : findByGroupId(groupId)) {
922             remove(shoppingCategory);
923         }
924     }
925 
926     public void removeByG_P(long groupId, long parentCategoryId)
927         throws SystemException {
928         for (ShoppingCategory shoppingCategory : findByG_P(groupId,
929                 parentCategoryId)) {
930             remove(shoppingCategory);
931         }
932     }
933 
934     public void removeAll() throws SystemException {
935         for (ShoppingCategory shoppingCategory : findAll()) {
936             remove(shoppingCategory);
937         }
938     }
939 
940     public int countByGroupId(long groupId) throws SystemException {
941         boolean finderClassNameCacheEnabled = ShoppingCategoryModelImpl.CACHE_ENABLED;
942         String finderClassName = ShoppingCategory.class.getName();
943         String finderMethodName = "countByGroupId";
944         String[] finderParams = new String[] { Long.class.getName() };
945         Object[] finderArgs = new Object[] { new Long(groupId) };
946 
947         Object result = null;
948 
949         if (finderClassNameCacheEnabled) {
950             result = FinderCacheUtil.getResult(finderClassName,
951                     finderMethodName, finderParams, finderArgs, this);
952         }
953 
954         if (result == null) {
955             Session session = null;
956 
957             try {
958                 session = openSession();
959 
960                 StringBuilder query = new StringBuilder();
961 
962                 query.append("SELECT COUNT(*) ");
963                 query.append(
964                     "FROM com.liferay.portlet.shopping.model.ShoppingCategory WHERE ");
965 
966                 query.append("groupId = ?");
967 
968                 query.append(" ");
969 
970                 Query q = session.createQuery(query.toString());
971 
972                 QueryPos qPos = QueryPos.getInstance(q);
973 
974                 qPos.add(groupId);
975 
976                 Long count = null;
977 
978                 Iterator<Long> itr = q.list().iterator();
979 
980                 if (itr.hasNext()) {
981                     count = itr.next();
982                 }
983 
984                 if (count == null) {
985                     count = new Long(0);
986                 }
987 
988                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
989                     finderClassName, finderMethodName, finderParams,
990                     finderArgs, count);
991 
992                 return count.intValue();
993             }
994             catch (Exception e) {
995                 throw processException(e);
996             }
997             finally {
998                 closeSession(session);
999             }
1000        }
1001        else {
1002            return ((Long)result).intValue();
1003        }
1004    }
1005
1006    public int countByG_P(long groupId, long parentCategoryId)
1007        throws SystemException {
1008        boolean finderClassNameCacheEnabled = ShoppingCategoryModelImpl.CACHE_ENABLED;
1009        String finderClassName = ShoppingCategory.class.getName();
1010        String finderMethodName = "countByG_P";
1011        String[] finderParams = new String[] {
1012                Long.class.getName(), Long.class.getName()
1013            };
1014        Object[] finderArgs = new Object[] {
1015                new Long(groupId), new Long(parentCategoryId)
1016            };
1017
1018        Object result = null;
1019
1020        if (finderClassNameCacheEnabled) {
1021            result = FinderCacheUtil.getResult(finderClassName,
1022                    finderMethodName, finderParams, finderArgs, this);
1023        }
1024
1025        if (result == null) {
1026            Session session = null;
1027
1028            try {
1029                session = openSession();
1030
1031                StringBuilder query = new StringBuilder();
1032
1033                query.append("SELECT COUNT(*) ");
1034                query.append(
1035                    "FROM com.liferay.portlet.shopping.model.ShoppingCategory WHERE ");
1036
1037                query.append("groupId = ?");
1038
1039                query.append(" AND ");
1040
1041                query.append("parentCategoryId = ?");
1042
1043                query.append(" ");
1044
1045                Query q = session.createQuery(query.toString());
1046
1047                QueryPos qPos = QueryPos.getInstance(q);
1048
1049                qPos.add(groupId);
1050
1051                qPos.add(parentCategoryId);
1052
1053                Long count = null;
1054
1055                Iterator<Long> itr = q.list().iterator();
1056
1057                if (itr.hasNext()) {
1058                    count = itr.next();
1059                }
1060
1061                if (count == null) {
1062                    count = new Long(0);
1063                }
1064
1065                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1066                    finderClassName, finderMethodName, finderParams,
1067                    finderArgs, count);
1068
1069                return count.intValue();
1070            }
1071            catch (Exception e) {
1072                throw processException(e);
1073            }
1074            finally {
1075                closeSession(session);
1076            }
1077        }
1078        else {
1079            return ((Long)result).intValue();
1080        }
1081    }
1082
1083    public int countAll() throws SystemException {
1084        boolean finderClassNameCacheEnabled = ShoppingCategoryModelImpl.CACHE_ENABLED;
1085        String finderClassName = ShoppingCategory.class.getName();
1086        String finderMethodName = "countAll";
1087        String[] finderParams = new String[] {  };
1088        Object[] finderArgs = new Object[] {  };
1089
1090        Object result = null;
1091
1092        if (finderClassNameCacheEnabled) {
1093            result = FinderCacheUtil.getResult(finderClassName,
1094                    finderMethodName, finderParams, finderArgs, this);
1095        }
1096
1097        if (result == null) {
1098            Session session = null;
1099
1100            try {
1101                session = openSession();
1102
1103                Query q = session.createQuery(
1104                        "SELECT COUNT(*) FROM com.liferay.portlet.shopping.model.ShoppingCategory");
1105
1106                Long count = null;
1107
1108                Iterator<Long> itr = q.list().iterator();
1109
1110                if (itr.hasNext()) {
1111                    count = itr.next();
1112                }
1113
1114                if (count == null) {
1115                    count = new Long(0);
1116                }
1117
1118                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1119                    finderClassName, finderMethodName, finderParams,
1120                    finderArgs, count);
1121
1122                return count.intValue();
1123            }
1124            catch (Exception e) {
1125                throw processException(e);
1126            }
1127            finally {
1128                closeSession(session);
1129            }
1130        }
1131        else {
1132            return ((Long)result).intValue();
1133        }
1134    }
1135
1136    public void registerListener(ModelListener listener) {
1137        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1138
1139        listeners.add(listener);
1140
1141        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1142    }
1143
1144    public void unregisterListener(ModelListener listener) {
1145        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1146
1147        listeners.remove(listener);
1148
1149        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1150    }
1151
1152    public void afterPropertiesSet() {
1153        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1154                    com.liferay.portal.util.PropsUtil.get(
1155                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCategory")));
1156
1157        if (listenerClassNames.length > 0) {
1158            try {
1159                List<ModelListener> listeners = new ArrayList<ModelListener>();
1160
1161                for (String listenerClassName : listenerClassNames) {
1162                    listeners.add((ModelListener)Class.forName(
1163                            listenerClassName).newInstance());
1164                }
1165
1166                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1167            }
1168            catch (Exception e) {
1169                _log.error(e);
1170            }
1171        }
1172    }
1173
1174    private static Log _log = LogFactory.getLog(ShoppingCategoryPersistenceImpl.class);
1175    private ModelListener[] _listeners = new ModelListener[0];
1176}