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