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