1
14
15 package com.liferay.portlet.shopping.service.persistence;
16
17 import com.liferay.portal.NoSuchModelException;
18 import com.liferay.portal.SystemException;
19 import com.liferay.portal.kernel.annotation.BeanReference;
20 import com.liferay.portal.kernel.cache.CacheRegistry;
21 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
22 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
23 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
24 import com.liferay.portal.kernel.dao.orm.FinderPath;
25 import com.liferay.portal.kernel.dao.orm.Query;
26 import com.liferay.portal.kernel.dao.orm.QueryPos;
27 import com.liferay.portal.kernel.dao.orm.QueryUtil;
28 import com.liferay.portal.kernel.dao.orm.Session;
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
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
252 public ShoppingCart update(ShoppingCart shoppingCart)
253 throws SystemException {
254 if (_log.isWarnEnabled()) {
255 _log.warn(
256 "Using the deprecated update(ShoppingCart shoppingCart) method. Use update(ShoppingCart shoppingCart, boolean merge) instead.");
257 }
258
259 return update(shoppingCart, false);
260 }
261
262 public ShoppingCart updateImpl(
263 com.liferay.portlet.shopping.model.ShoppingCart shoppingCart,
264 boolean merge) throws SystemException {
265 shoppingCart = toUnwrappedModel(shoppingCart);
266
267 boolean isNew = shoppingCart.isNew();
268
269 ShoppingCartModelImpl shoppingCartModelImpl = (ShoppingCartModelImpl)shoppingCart;
270
271 Session session = null;
272
273 try {
274 session = openSession();
275
276 BatchSessionUtil.update(session, shoppingCart, merge);
277
278 shoppingCart.setNew(false);
279 }
280 catch (Exception e) {
281 throw processException(e);
282 }
283 finally {
284 closeSession(session);
285 }
286
287 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
288
289 EntityCacheUtil.putResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
290 ShoppingCartImpl.class, shoppingCart.getPrimaryKey(), shoppingCart);
291
292 if (!isNew &&
293 ((shoppingCart.getGroupId() != shoppingCartModelImpl.getOriginalGroupId()) ||
294 (shoppingCart.getUserId() != shoppingCartModelImpl.getOriginalUserId()))) {
295 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_G_U,
296 new Object[] {
297 new Long(shoppingCartModelImpl.getOriginalGroupId()),
298 new Long(shoppingCartModelImpl.getOriginalUserId())
299 });
300 }
301
302 if (isNew ||
303 ((shoppingCart.getGroupId() != shoppingCartModelImpl.getOriginalGroupId()) ||
304 (shoppingCart.getUserId() != shoppingCartModelImpl.getOriginalUserId()))) {
305 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
306 new Object[] {
307 new Long(shoppingCart.getGroupId()),
308 new Long(shoppingCart.getUserId())
309 }, shoppingCart);
310 }
311
312 return shoppingCart;
313 }
314
315 protected ShoppingCart toUnwrappedModel(ShoppingCart shoppingCart) {
316 if (shoppingCart instanceof ShoppingCartImpl) {
317 return shoppingCart;
318 }
319
320 ShoppingCartImpl shoppingCartImpl = new ShoppingCartImpl();
321
322 shoppingCartImpl.setNew(shoppingCart.isNew());
323 shoppingCartImpl.setPrimaryKey(shoppingCart.getPrimaryKey());
324
325 shoppingCartImpl.setCartId(shoppingCart.getCartId());
326 shoppingCartImpl.setGroupId(shoppingCart.getGroupId());
327 shoppingCartImpl.setCompanyId(shoppingCart.getCompanyId());
328 shoppingCartImpl.setUserId(shoppingCart.getUserId());
329 shoppingCartImpl.setUserName(shoppingCart.getUserName());
330 shoppingCartImpl.setCreateDate(shoppingCart.getCreateDate());
331 shoppingCartImpl.setModifiedDate(shoppingCart.getModifiedDate());
332 shoppingCartImpl.setItemIds(shoppingCart.getItemIds());
333 shoppingCartImpl.setCouponCodes(shoppingCart.getCouponCodes());
334 shoppingCartImpl.setAltShipping(shoppingCart.getAltShipping());
335 shoppingCartImpl.setInsure(shoppingCart.isInsure());
336
337 return shoppingCartImpl;
338 }
339
340 public ShoppingCart findByPrimaryKey(Serializable primaryKey)
341 throws NoSuchModelException, SystemException {
342 return findByPrimaryKey(((Long)primaryKey).longValue());
343 }
344
345 public ShoppingCart findByPrimaryKey(long cartId)
346 throws NoSuchCartException, SystemException {
347 ShoppingCart shoppingCart = fetchByPrimaryKey(cartId);
348
349 if (shoppingCart == null) {
350 if (_log.isWarnEnabled()) {
351 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + cartId);
352 }
353
354 throw new NoSuchCartException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
355 cartId);
356 }
357
358 return shoppingCart;
359 }
360
361 public ShoppingCart fetchByPrimaryKey(Serializable primaryKey)
362 throws SystemException {
363 return fetchByPrimaryKey(((Long)primaryKey).longValue());
364 }
365
366 public ShoppingCart fetchByPrimaryKey(long cartId)
367 throws SystemException {
368 ShoppingCart shoppingCart = (ShoppingCart)EntityCacheUtil.getResult(ShoppingCartModelImpl.ENTITY_CACHE_ENABLED,
369 ShoppingCartImpl.class, cartId, this);
370
371 if (shoppingCart == null) {
372 Session session = null;
373
374 try {
375 session = openSession();
376
377 shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
378 new Long(cartId));
379 }
380 catch (Exception e) {
381 throw processException(e);
382 }
383 finally {
384 if (shoppingCart != null) {
385 cacheResult(shoppingCart);
386 }
387
388 closeSession(session);
389 }
390 }
391
392 return shoppingCart;
393 }
394
395 public List<ShoppingCart> findByGroupId(long groupId)
396 throws SystemException {
397 Object[] finderArgs = new Object[] { new Long(groupId) };
398
399 List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
400 finderArgs, this);
401
402 if (list == null) {
403 Session session = null;
404
405 try {
406 session = openSession();
407
408 StringBundler query = new StringBundler(2);
409
410 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
411
412 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
413
414 String sql = query.toString();
415
416 Query q = session.createQuery(sql);
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<ShoppingCart>();
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<ShoppingCart> findByGroupId(long groupId, int start, int end)
445 throws SystemException {
446 return findByGroupId(groupId, start, end, null);
447 }
448
449 public List<ShoppingCart> findByGroupId(long groupId, int start, int end,
450 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<ShoppingCart> list = (List<ShoppingCart>)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 StringBundler query = null;
467
468 if (obc != null) {
469 query = new StringBundler(3 +
470 (obc.getOrderByFields().length * 3));
471 }
472 else {
473 query = new StringBundler(2);
474 }
475
476 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
477
478 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
479
480 if (obc != null) {
481 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
482 }
483
484 String sql = query.toString();
485
486 Query q = session.createQuery(sql);
487
488 QueryPos qPos = QueryPos.getInstance(q);
489
490 qPos.add(groupId);
491
492 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
493 start, end);
494 }
495 catch (Exception e) {
496 throw processException(e);
497 }
498 finally {
499 if (list == null) {
500 list = new ArrayList<ShoppingCart>();
501 }
502
503 cacheResult(list);
504
505 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
506 finderArgs, list);
507
508 closeSession(session);
509 }
510 }
511
512 return list;
513 }
514
515 public ShoppingCart findByGroupId_First(long groupId, OrderByComparator obc)
516 throws NoSuchCartException, SystemException {
517 List<ShoppingCart> list = findByGroupId(groupId, 0, 1, obc);
518
519 if (list.isEmpty()) {
520 StringBundler msg = new StringBundler(4);
521
522 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
523
524 msg.append("groupId=");
525 msg.append(groupId);
526
527 msg.append(StringPool.CLOSE_CURLY_BRACE);
528
529 throw new NoSuchCartException(msg.toString());
530 }
531 else {
532 return list.get(0);
533 }
534 }
535
536 public ShoppingCart findByGroupId_Last(long groupId, OrderByComparator obc)
537 throws NoSuchCartException, SystemException {
538 int count = countByGroupId(groupId);
539
540 List<ShoppingCart> list = findByGroupId(groupId, count - 1, count, obc);
541
542 if (list.isEmpty()) {
543 StringBundler msg = new StringBundler(4);
544
545 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
546
547 msg.append("groupId=");
548 msg.append(groupId);
549
550 msg.append(StringPool.CLOSE_CURLY_BRACE);
551
552 throw new NoSuchCartException(msg.toString());
553 }
554 else {
555 return list.get(0);
556 }
557 }
558
559 public ShoppingCart[] findByGroupId_PrevAndNext(long cartId, long groupId,
560 OrderByComparator obc) throws NoSuchCartException, SystemException {
561 ShoppingCart shoppingCart = findByPrimaryKey(cartId);
562
563 int count = countByGroupId(groupId);
564
565 Session session = null;
566
567 try {
568 session = openSession();
569
570 StringBundler query = null;
571
572 if (obc != null) {
573 query = new StringBundler(3 +
574 (obc.getOrderByFields().length * 3));
575 }
576 else {
577 query = new StringBundler(2);
578 }
579
580 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
581
582 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
583
584 if (obc != null) {
585 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
586 }
587
588 String sql = query.toString();
589
590 Query q = session.createQuery(sql);
591
592 QueryPos qPos = QueryPos.getInstance(q);
593
594 qPos.add(groupId);
595
596 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
597 shoppingCart);
598
599 ShoppingCart[] array = new ShoppingCartImpl[3];
600
601 array[0] = (ShoppingCart)objArray[0];
602 array[1] = (ShoppingCart)objArray[1];
603 array[2] = (ShoppingCart)objArray[2];
604
605 return array;
606 }
607 catch (Exception e) {
608 throw processException(e);
609 }
610 finally {
611 closeSession(session);
612 }
613 }
614
615 public List<ShoppingCart> findByUserId(long userId)
616 throws SystemException {
617 Object[] finderArgs = new Object[] { new Long(userId) };
618
619 List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_USERID,
620 finderArgs, this);
621
622 if (list == null) {
623 Session session = null;
624
625 try {
626 session = openSession();
627
628 StringBundler query = new StringBundler(2);
629
630 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
631
632 query.append(_FINDER_COLUMN_USERID_USERID_2);
633
634 String sql = query.toString();
635
636 Query q = session.createQuery(sql);
637
638 QueryPos qPos = QueryPos.getInstance(q);
639
640 qPos.add(userId);
641
642 list = q.list();
643 }
644 catch (Exception e) {
645 throw processException(e);
646 }
647 finally {
648 if (list == null) {
649 list = new ArrayList<ShoppingCart>();
650 }
651
652 cacheResult(list);
653
654 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_USERID,
655 finderArgs, list);
656
657 closeSession(session);
658 }
659 }
660
661 return list;
662 }
663
664 public List<ShoppingCart> findByUserId(long userId, int start, int end)
665 throws SystemException {
666 return findByUserId(userId, start, end, null);
667 }
668
669 public List<ShoppingCart> findByUserId(long userId, int start, int end,
670 OrderByComparator obc) throws SystemException {
671 Object[] finderArgs = new Object[] {
672 new Long(userId),
673
674 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
675 };
676
677 List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_USERID,
678 finderArgs, this);
679
680 if (list == null) {
681 Session session = null;
682
683 try {
684 session = openSession();
685
686 StringBundler query = null;
687
688 if (obc != null) {
689 query = new StringBundler(3 +
690 (obc.getOrderByFields().length * 3));
691 }
692 else {
693 query = new StringBundler(2);
694 }
695
696 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
697
698 query.append(_FINDER_COLUMN_USERID_USERID_2);
699
700 if (obc != null) {
701 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
702 }
703
704 String sql = query.toString();
705
706 Query q = session.createQuery(sql);
707
708 QueryPos qPos = QueryPos.getInstance(q);
709
710 qPos.add(userId);
711
712 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
713 start, end);
714 }
715 catch (Exception e) {
716 throw processException(e);
717 }
718 finally {
719 if (list == null) {
720 list = new ArrayList<ShoppingCart>();
721 }
722
723 cacheResult(list);
724
725 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_USERID,
726 finderArgs, list);
727
728 closeSession(session);
729 }
730 }
731
732 return list;
733 }
734
735 public ShoppingCart findByUserId_First(long userId, OrderByComparator obc)
736 throws NoSuchCartException, SystemException {
737 List<ShoppingCart> list = findByUserId(userId, 0, 1, obc);
738
739 if (list.isEmpty()) {
740 StringBundler msg = new StringBundler(4);
741
742 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
743
744 msg.append("userId=");
745 msg.append(userId);
746
747 msg.append(StringPool.CLOSE_CURLY_BRACE);
748
749 throw new NoSuchCartException(msg.toString());
750 }
751 else {
752 return list.get(0);
753 }
754 }
755
756 public ShoppingCart findByUserId_Last(long userId, OrderByComparator obc)
757 throws NoSuchCartException, SystemException {
758 int count = countByUserId(userId);
759
760 List<ShoppingCart> list = findByUserId(userId, count - 1, count, obc);
761
762 if (list.isEmpty()) {
763 StringBundler msg = new StringBundler(4);
764
765 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
766
767 msg.append("userId=");
768 msg.append(userId);
769
770 msg.append(StringPool.CLOSE_CURLY_BRACE);
771
772 throw new NoSuchCartException(msg.toString());
773 }
774 else {
775 return list.get(0);
776 }
777 }
778
779 public ShoppingCart[] findByUserId_PrevAndNext(long cartId, long userId,
780 OrderByComparator obc) throws NoSuchCartException, SystemException {
781 ShoppingCart shoppingCart = findByPrimaryKey(cartId);
782
783 int count = countByUserId(userId);
784
785 Session session = null;
786
787 try {
788 session = openSession();
789
790 StringBundler query = null;
791
792 if (obc != null) {
793 query = new StringBundler(3 +
794 (obc.getOrderByFields().length * 3));
795 }
796 else {
797 query = new StringBundler(2);
798 }
799
800 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
801
802 query.append(_FINDER_COLUMN_USERID_USERID_2);
803
804 if (obc != null) {
805 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
806 }
807
808 String sql = query.toString();
809
810 Query q = session.createQuery(sql);
811
812 QueryPos qPos = QueryPos.getInstance(q);
813
814 qPos.add(userId);
815
816 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
817 shoppingCart);
818
819 ShoppingCart[] array = new ShoppingCartImpl[3];
820
821 array[0] = (ShoppingCart)objArray[0];
822 array[1] = (ShoppingCart)objArray[1];
823 array[2] = (ShoppingCart)objArray[2];
824
825 return array;
826 }
827 catch (Exception e) {
828 throw processException(e);
829 }
830 finally {
831 closeSession(session);
832 }
833 }
834
835 public ShoppingCart findByG_U(long groupId, long userId)
836 throws NoSuchCartException, SystemException {
837 ShoppingCart shoppingCart = fetchByG_U(groupId, userId);
838
839 if (shoppingCart == null) {
840 StringBundler msg = new StringBundler(6);
841
842 msg.append(_NO_SUCH_ENTITY_WITH_KEY);
843
844 msg.append("groupId=");
845 msg.append(groupId);
846
847 msg.append(", userId=");
848 msg.append(userId);
849
850 msg.append(StringPool.CLOSE_CURLY_BRACE);
851
852 if (_log.isWarnEnabled()) {
853 _log.warn(msg.toString());
854 }
855
856 throw new NoSuchCartException(msg.toString());
857 }
858
859 return shoppingCart;
860 }
861
862 public ShoppingCart fetchByG_U(long groupId, long userId)
863 throws SystemException {
864 return fetchByG_U(groupId, userId, true);
865 }
866
867 public ShoppingCart fetchByG_U(long groupId, long userId,
868 boolean retrieveFromCache) throws SystemException {
869 Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
870
871 Object result = null;
872
873 if (retrieveFromCache) {
874 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_G_U,
875 finderArgs, this);
876 }
877
878 if (result == null) {
879 Session session = null;
880
881 try {
882 session = openSession();
883
884 StringBundler query = new StringBundler(3);
885
886 query.append(_SQL_SELECT_SHOPPINGCART_WHERE);
887
888 query.append(_FINDER_COLUMN_G_U_GROUPID_2);
889
890 query.append(_FINDER_COLUMN_G_U_USERID_2);
891
892 String sql = query.toString();
893
894 Query q = session.createQuery(sql);
895
896 QueryPos qPos = QueryPos.getInstance(q);
897
898 qPos.add(groupId);
899
900 qPos.add(userId);
901
902 List<ShoppingCart> list = q.list();
903
904 result = list;
905
906 ShoppingCart shoppingCart = null;
907
908 if (list.isEmpty()) {
909 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
910 finderArgs, list);
911 }
912 else {
913 shoppingCart = list.get(0);
914
915 cacheResult(shoppingCart);
916
917 if ((shoppingCart.getGroupId() != groupId) ||
918 (shoppingCart.getUserId() != userId)) {
919 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
920 finderArgs, shoppingCart);
921 }
922 }
923
924 return shoppingCart;
925 }
926 catch (Exception e) {
927 throw processException(e);
928 }
929 finally {
930 if (result == null) {
931 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_G_U,
932 finderArgs, new ArrayList<ShoppingCart>());
933 }
934
935 closeSession(session);
936 }
937 }
938 else {
939 if (result instanceof List<?>) {
940 return null;
941 }
942 else {
943 return (ShoppingCart)result;
944 }
945 }
946 }
947
948 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
949 throws SystemException {
950 Session session = null;
951
952 try {
953 session = openSession();
954
955 dynamicQuery.compile(session);
956
957 return dynamicQuery.list();
958 }
959 catch (Exception e) {
960 throw processException(e);
961 }
962 finally {
963 closeSession(session);
964 }
965 }
966
967 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
968 int start, int end) throws SystemException {
969 Session session = null;
970
971 try {
972 session = openSession();
973
974 dynamicQuery.setLimit(start, end);
975
976 dynamicQuery.compile(session);
977
978 return dynamicQuery.list();
979 }
980 catch (Exception e) {
981 throw processException(e);
982 }
983 finally {
984 closeSession(session);
985 }
986 }
987
988 public List<ShoppingCart> findAll() throws SystemException {
989 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
990 }
991
992 public List<ShoppingCart> findAll(int start, int end)
993 throws SystemException {
994 return findAll(start, end, null);
995 }
996
997 public List<ShoppingCart> findAll(int start, int end, OrderByComparator obc)
998 throws SystemException {
999 Object[] finderArgs = new Object[] {
1000 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1001 };
1002
1003 List<ShoppingCart> list = (List<ShoppingCart>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1004 finderArgs, this);
1005
1006 if (list == null) {
1007 Session session = null;
1008
1009 try {
1010 session = openSession();
1011
1012 StringBundler query = null;
1013 String sql = null;
1014
1015 if (obc != null) {
1016 query = new StringBundler(2 +
1017 (obc.getOrderByFields().length * 3));
1018
1019 query.append(_SQL_SELECT_SHOPPINGCART);
1020
1021 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
1022
1023 sql = query.toString();
1024 }
1025
1026 sql = _SQL_SELECT_SHOPPINGCART;
1027
1028 Query q = session.createQuery(sql);
1029
1030 if (obc == null) {
1031 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1032 start, end, false);
1033
1034 Collections.sort(list);
1035 }
1036 else {
1037 list = (List<ShoppingCart>)QueryUtil.list(q, getDialect(),
1038 start, end);
1039 }
1040 }
1041 catch (Exception e) {
1042 throw processException(e);
1043 }
1044 finally {
1045 if (list == null) {
1046 list = new ArrayList<ShoppingCart>();
1047 }
1048
1049 cacheResult(list);
1050
1051 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1052
1053 closeSession(session);
1054 }
1055 }
1056
1057 return list;
1058 }
1059
1060 public void removeByGroupId(long groupId) throws SystemException {
1061 for (ShoppingCart shoppingCart : findByGroupId(groupId)) {
1062 remove(shoppingCart);
1063 }
1064 }
1065
1066 public void removeByUserId(long userId) throws SystemException {
1067 for (ShoppingCart shoppingCart : findByUserId(userId)) {
1068 remove(shoppingCart);
1069 }
1070 }
1071
1072 public void removeByG_U(long groupId, long userId)
1073 throws NoSuchCartException, SystemException {
1074 ShoppingCart shoppingCart = findByG_U(groupId, userId);
1075
1076 remove(shoppingCart);
1077 }
1078
1079 public void removeAll() throws SystemException {
1080 for (ShoppingCart shoppingCart : findAll()) {
1081 remove(shoppingCart);
1082 }
1083 }
1084
1085 public int countByGroupId(long groupId) throws SystemException {
1086 Object[] finderArgs = new Object[] { new Long(groupId) };
1087
1088 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1089 finderArgs, this);
1090
1091 if (count == null) {
1092 Session session = null;
1093
1094 try {
1095 session = openSession();
1096
1097 StringBundler query = new StringBundler(2);
1098
1099 query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1100
1101 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1102
1103 String sql = query.toString();
1104
1105 Query q = session.createQuery(sql);
1106
1107 QueryPos qPos = QueryPos.getInstance(q);
1108
1109 qPos.add(groupId);
1110
1111 count = (Long)q.uniqueResult();
1112 }
1113 catch (Exception e) {
1114 throw processException(e);
1115 }
1116 finally {
1117 if (count == null) {
1118 count = Long.valueOf(0);
1119 }
1120
1121 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1122 finderArgs, count);
1123
1124 closeSession(session);
1125 }
1126 }
1127
1128 return count.intValue();
1129 }
1130
1131 public int countByUserId(long userId) throws SystemException {
1132 Object[] finderArgs = new Object[] { new Long(userId) };
1133
1134 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_USERID,
1135 finderArgs, this);
1136
1137 if (count == null) {
1138 Session session = null;
1139
1140 try {
1141 session = openSession();
1142
1143 StringBundler query = new StringBundler(2);
1144
1145 query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1146
1147 query.append(_FINDER_COLUMN_USERID_USERID_2);
1148
1149 String sql = query.toString();
1150
1151 Query q = session.createQuery(sql);
1152
1153 QueryPos qPos = QueryPos.getInstance(q);
1154
1155 qPos.add(userId);
1156
1157 count = (Long)q.uniqueResult();
1158 }
1159 catch (Exception e) {
1160 throw processException(e);
1161 }
1162 finally {
1163 if (count == null) {
1164 count = Long.valueOf(0);
1165 }
1166
1167 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_USERID,
1168 finderArgs, count);
1169
1170 closeSession(session);
1171 }
1172 }
1173
1174 return count.intValue();
1175 }
1176
1177 public int countByG_U(long groupId, long userId) throws SystemException {
1178 Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
1179
1180 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_U,
1181 finderArgs, this);
1182
1183 if (count == null) {
1184 Session session = null;
1185
1186 try {
1187 session = openSession();
1188
1189 StringBundler query = new StringBundler(3);
1190
1191 query.append(_SQL_COUNT_SHOPPINGCART_WHERE);
1192
1193 query.append(_FINDER_COLUMN_G_U_GROUPID_2);
1194
1195 query.append(_FINDER_COLUMN_G_U_USERID_2);
1196
1197 String sql = query.toString();
1198
1199 Query q = session.createQuery(sql);
1200
1201 QueryPos qPos = QueryPos.getInstance(q);
1202
1203 qPos.add(groupId);
1204
1205 qPos.add(userId);
1206
1207 count = (Long)q.uniqueResult();
1208 }
1209 catch (Exception e) {
1210 throw processException(e);
1211 }
1212 finally {
1213 if (count == null) {
1214 count = Long.valueOf(0);
1215 }
1216
1217 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_U, finderArgs,
1218 count);
1219
1220 closeSession(session);
1221 }
1222 }
1223
1224 return count.intValue();
1225 }
1226
1227 public int countAll() throws SystemException {
1228 Object[] finderArgs = new Object[0];
1229
1230 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1231 finderArgs, this);
1232
1233 if (count == null) {
1234 Session session = null;
1235
1236 try {
1237 session = openSession();
1238
1239 Query q = session.createQuery(_SQL_COUNT_SHOPPINGCART);
1240
1241 count = (Long)q.uniqueResult();
1242 }
1243 catch (Exception e) {
1244 throw processException(e);
1245 }
1246 finally {
1247 if (count == null) {
1248 count = Long.valueOf(0);
1249 }
1250
1251 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1252 count);
1253
1254 closeSession(session);
1255 }
1256 }
1257
1258 return count.intValue();
1259 }
1260
1261 public void afterPropertiesSet() {
1262 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1263 com.liferay.portal.util.PropsUtil.get(
1264 "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCart")));
1265
1266 if (listenerClassNames.length > 0) {
1267 try {
1268 List<ModelListener<ShoppingCart>> listenersList = new ArrayList<ModelListener<ShoppingCart>>();
1269
1270 for (String listenerClassName : listenerClassNames) {
1271 listenersList.add((ModelListener<ShoppingCart>)Class.forName(
1272 listenerClassName).newInstance());
1273 }
1274
1275 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1276 }
1277 catch (Exception e) {
1278 _log.error(e);
1279 }
1280 }
1281 }
1282
1283 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence")
1284 protected com.liferay.portlet.shopping.service.persistence.ShoppingCartPersistence shoppingCartPersistence;
1285 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence")
1286 protected com.liferay.portlet.shopping.service.persistence.ShoppingCategoryPersistence shoppingCategoryPersistence;
1287 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence")
1288 protected com.liferay.portlet.shopping.service.persistence.ShoppingCouponPersistence shoppingCouponPersistence;
1289 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence")
1290 protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPersistence shoppingItemPersistence;
1291 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence")
1292 protected com.liferay.portlet.shopping.service.persistence.ShoppingItemFieldPersistence shoppingItemFieldPersistence;
1293 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence")
1294 protected com.liferay.portlet.shopping.service.persistence.ShoppingItemPricePersistence shoppingItemPricePersistence;
1295 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence")
1296 protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderPersistence shoppingOrderPersistence;
1297 @BeanReference(name = "com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence")
1298 protected com.liferay.portlet.shopping.service.persistence.ShoppingOrderItemPersistence shoppingOrderItemPersistence;
1299 @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
1300 protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1301 @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
1302 protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1303 private static final String _SQL_SELECT_SHOPPINGCART = "SELECT shoppingCart FROM ShoppingCart shoppingCart";
1304 private static final String _SQL_SELECT_SHOPPINGCART_WHERE = "SELECT shoppingCart FROM ShoppingCart shoppingCart WHERE ";
1305 private static final String _SQL_COUNT_SHOPPINGCART = "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart";
1306 private static final String _SQL_COUNT_SHOPPINGCART_WHERE = "SELECT COUNT(shoppingCart) FROM ShoppingCart shoppingCart WHERE ";
1307 private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "shoppingCart.groupId = ?";
1308 private static final String _FINDER_COLUMN_USERID_USERID_2 = "shoppingCart.userId = ?";
1309 private static final String _FINDER_COLUMN_G_U_GROUPID_2 = "shoppingCart.groupId = ? AND ";
1310 private static final String _FINDER_COLUMN_G_U_USERID_2 = "shoppingCart.userId = ?";
1311 private static final String _ORDER_BY_ENTITY_ALIAS = "shoppingCart.";
1312 private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ShoppingCart exists with the primary key ";
1313 private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ShoppingCart exists with the key {";
1314 private static Log _log = LogFactoryUtil.getLog(ShoppingCartPersistenceImpl.class);
1315}