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