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