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