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