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