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.bean.InitializingBean;
27  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.Session;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
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, InitializingBean {
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             session.delete(shoppingCart);
131 
132             session.flush();
133 
134             return shoppingCart;
135         }
136         catch (Exception e) {
137             throw processException(e);
138         }
139         finally {
140             closeSession(session);
141 
142             FinderCacheUtil.clearCache(ShoppingCart.class.getName());
143         }
144     }
145 
146     /**
147      * @deprecated Use <code>update(ShoppingCart shoppingCart, boolean merge)</code>.
148      */
149     public ShoppingCart update(ShoppingCart shoppingCart)
150         throws SystemException {
151         if (_log.isWarnEnabled()) {
152             _log.warn(
153                 "Using the deprecated update(ShoppingCart shoppingCart) method. Use update(ShoppingCart shoppingCart, boolean merge) instead.");
154         }
155 
156         return update(shoppingCart, false);
157     }
158 
159     /**
160      * Add, update, or merge, the entity. This method also calls the model
161      * listeners to trigger the proper events associated with adding, deleting,
162      * or updating an entity.
163      *
164      * @param        shoppingCart the entity to add, update, or merge
165      * @param        merge boolean value for whether to merge the entity. The
166      *                default value is false. Setting merge to true is more
167      *                expensive and should only be true when shoppingCart is
168      *                transient. See LEP-5473 for a detailed discussion of this
169      *                method.
170      * @return        true if the portlet can be displayed via Ajax
171      */
172     public ShoppingCart update(ShoppingCart shoppingCart, boolean merge)
173         throws SystemException {
174         boolean isNew = shoppingCart.isNew();
175 
176         if (_listeners.length > 0) {
177             for (ModelListener listener : _listeners) {
178                 if (isNew) {
179                     listener.onBeforeCreate(shoppingCart);
180                 }
181                 else {
182                     listener.onBeforeUpdate(shoppingCart);
183                 }
184             }
185         }
186 
187         shoppingCart = updateImpl(shoppingCart, merge);
188 
189         if (_listeners.length > 0) {
190             for (ModelListener listener : _listeners) {
191                 if (isNew) {
192                     listener.onAfterCreate(shoppingCart);
193                 }
194                 else {
195                     listener.onAfterUpdate(shoppingCart);
196                 }
197             }
198         }
199 
200         return shoppingCart;
201     }
202 
203     public ShoppingCart updateImpl(
204         com.liferay.portlet.shopping.model.ShoppingCart shoppingCart,
205         boolean merge) throws SystemException {
206         Session session = null;
207 
208         try {
209             session = openSession();
210 
211             if (merge) {
212                 session.merge(shoppingCart);
213             }
214             else {
215                 if (shoppingCart.isNew()) {
216                     session.save(shoppingCart);
217                 }
218             }
219 
220             session.flush();
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 = (List<ShoppingCart>)QueryUtil.list(q,
909                         getDialect(), start, end);
910 
911                 if (obc == null) {
912                     Collections.sort(list);
913                 }
914 
915                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
916                     finderClassName, finderMethodName, finderParams,
917                     finderArgs, list);
918 
919                 return list;
920             }
921             catch (Exception e) {
922                 throw processException(e);
923             }
924             finally {
925                 closeSession(session);
926             }
927         }
928         else {
929             return (List<ShoppingCart>)result;
930         }
931     }
932 
933     public void removeByGroupId(long groupId) throws SystemException {
934         for (ShoppingCart shoppingCart : findByGroupId(groupId)) {
935             remove(shoppingCart);
936         }
937     }
938 
939     public void removeByUserId(long userId) throws SystemException {
940         for (ShoppingCart shoppingCart : findByUserId(userId)) {
941             remove(shoppingCart);
942         }
943     }
944 
945     public void removeByG_U(long groupId, long userId)
946         throws NoSuchCartException, SystemException {
947         ShoppingCart shoppingCart = findByG_U(groupId, userId);
948 
949         remove(shoppingCart);
950     }
951 
952     public void removeAll() throws SystemException {
953         for (ShoppingCart shoppingCart : findAll()) {
954             remove(shoppingCart);
955         }
956     }
957 
958     public int countByGroupId(long groupId) throws SystemException {
959         boolean finderClassNameCacheEnabled = ShoppingCartModelImpl.CACHE_ENABLED;
960         String finderClassName = ShoppingCart.class.getName();
961         String finderMethodName = "countByGroupId";
962         String[] finderParams = new String[] { Long.class.getName() };
963         Object[] finderArgs = new Object[] { new Long(groupId) };
964 
965         Object result = null;
966 
967         if (finderClassNameCacheEnabled) {
968             result = FinderCacheUtil.getResult(finderClassName,
969                     finderMethodName, finderParams, finderArgs, this);
970         }
971 
972         if (result == null) {
973             Session session = null;
974 
975             try {
976                 session = openSession();
977 
978                 StringBuilder query = new StringBuilder();
979 
980                 query.append("SELECT COUNT(*) ");
981                 query.append(
982                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
983 
984                 query.append("groupId = ?");
985 
986                 query.append(" ");
987 
988                 Query q = session.createQuery(query.toString());
989 
990                 QueryPos qPos = QueryPos.getInstance(q);
991 
992                 qPos.add(groupId);
993 
994                 Long count = null;
995 
996                 Iterator<Long> itr = q.list().iterator();
997 
998                 if (itr.hasNext()) {
999                     count = itr.next();
1000                }
1001
1002                if (count == null) {
1003                    count = new Long(0);
1004                }
1005
1006                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1007                    finderClassName, finderMethodName, finderParams,
1008                    finderArgs, count);
1009
1010                return count.intValue();
1011            }
1012            catch (Exception e) {
1013                throw processException(e);
1014            }
1015            finally {
1016                closeSession(session);
1017            }
1018        }
1019        else {
1020            return ((Long)result).intValue();
1021        }
1022    }
1023
1024    public int countByUserId(long userId) throws SystemException {
1025        boolean finderClassNameCacheEnabled = ShoppingCartModelImpl.CACHE_ENABLED;
1026        String finderClassName = ShoppingCart.class.getName();
1027        String finderMethodName = "countByUserId";
1028        String[] finderParams = new String[] { Long.class.getName() };
1029        Object[] finderArgs = new Object[] { new Long(userId) };
1030
1031        Object result = null;
1032
1033        if (finderClassNameCacheEnabled) {
1034            result = FinderCacheUtil.getResult(finderClassName,
1035                    finderMethodName, finderParams, finderArgs, this);
1036        }
1037
1038        if (result == null) {
1039            Session session = null;
1040
1041            try {
1042                session = openSession();
1043
1044                StringBuilder query = new StringBuilder();
1045
1046                query.append("SELECT COUNT(*) ");
1047                query.append(
1048                    "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
1049
1050                query.append("userId = ?");
1051
1052                query.append(" ");
1053
1054                Query q = session.createQuery(query.toString());
1055
1056                QueryPos qPos = QueryPos.getInstance(q);
1057
1058                qPos.add(userId);
1059
1060                Long count = null;
1061
1062                Iterator<Long> itr = q.list().iterator();
1063
1064                if (itr.hasNext()) {
1065                    count = itr.next();
1066                }
1067
1068                if (count == null) {
1069                    count = new Long(0);
1070                }
1071
1072                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1073                    finderClassName, finderMethodName, finderParams,
1074                    finderArgs, count);
1075
1076                return count.intValue();
1077            }
1078            catch (Exception e) {
1079                throw processException(e);
1080            }
1081            finally {
1082                closeSession(session);
1083            }
1084        }
1085        else {
1086            return ((Long)result).intValue();
1087        }
1088    }
1089
1090    public int countByG_U(long groupId, long userId) throws SystemException {
1091        boolean finderClassNameCacheEnabled = ShoppingCartModelImpl.CACHE_ENABLED;
1092        String finderClassName = ShoppingCart.class.getName();
1093        String finderMethodName = "countByG_U";
1094        String[] finderParams = new String[] {
1095                Long.class.getName(), Long.class.getName()
1096            };
1097        Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
1098
1099        Object result = null;
1100
1101        if (finderClassNameCacheEnabled) {
1102            result = FinderCacheUtil.getResult(finderClassName,
1103                    finderMethodName, finderParams, finderArgs, this);
1104        }
1105
1106        if (result == null) {
1107            Session session = null;
1108
1109            try {
1110                session = openSession();
1111
1112                StringBuilder query = new StringBuilder();
1113
1114                query.append("SELECT COUNT(*) ");
1115                query.append(
1116                    "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
1117
1118                query.append("groupId = ?");
1119
1120                query.append(" AND ");
1121
1122                query.append("userId = ?");
1123
1124                query.append(" ");
1125
1126                Query q = session.createQuery(query.toString());
1127
1128                QueryPos qPos = QueryPos.getInstance(q);
1129
1130                qPos.add(groupId);
1131
1132                qPos.add(userId);
1133
1134                Long count = null;
1135
1136                Iterator<Long> itr = q.list().iterator();
1137
1138                if (itr.hasNext()) {
1139                    count = itr.next();
1140                }
1141
1142                if (count == null) {
1143                    count = new Long(0);
1144                }
1145
1146                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1147                    finderClassName, finderMethodName, finderParams,
1148                    finderArgs, count);
1149
1150                return count.intValue();
1151            }
1152            catch (Exception e) {
1153                throw processException(e);
1154            }
1155            finally {
1156                closeSession(session);
1157            }
1158        }
1159        else {
1160            return ((Long)result).intValue();
1161        }
1162    }
1163
1164    public int countAll() throws SystemException {
1165        boolean finderClassNameCacheEnabled = ShoppingCartModelImpl.CACHE_ENABLED;
1166        String finderClassName = ShoppingCart.class.getName();
1167        String finderMethodName = "countAll";
1168        String[] finderParams = new String[] {  };
1169        Object[] finderArgs = new Object[] {  };
1170
1171        Object result = null;
1172
1173        if (finderClassNameCacheEnabled) {
1174            result = FinderCacheUtil.getResult(finderClassName,
1175                    finderMethodName, finderParams, finderArgs, this);
1176        }
1177
1178        if (result == null) {
1179            Session session = null;
1180
1181            try {
1182                session = openSession();
1183
1184                Query q = session.createQuery(
1185                        "SELECT COUNT(*) FROM com.liferay.portlet.shopping.model.ShoppingCart");
1186
1187                Long count = null;
1188
1189                Iterator<Long> itr = q.list().iterator();
1190
1191                if (itr.hasNext()) {
1192                    count = itr.next();
1193                }
1194
1195                if (count == null) {
1196                    count = new Long(0);
1197                }
1198
1199                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1200                    finderClassName, finderMethodName, finderParams,
1201                    finderArgs, count);
1202
1203                return count.intValue();
1204            }
1205            catch (Exception e) {
1206                throw processException(e);
1207            }
1208            finally {
1209                closeSession(session);
1210            }
1211        }
1212        else {
1213            return ((Long)result).intValue();
1214        }
1215    }
1216
1217    public void registerListener(ModelListener listener) {
1218        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1219
1220        listeners.add(listener);
1221
1222        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1223    }
1224
1225    public void unregisterListener(ModelListener listener) {
1226        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1227
1228        listeners.remove(listener);
1229
1230        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1231    }
1232
1233    public void afterPropertiesSet() {
1234        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1235                    com.liferay.portal.util.PropsUtil.get(
1236                        "value.object.listener.com.liferay.portlet.shopping.model.ShoppingCart")));
1237
1238        if (listenerClassNames.length > 0) {
1239            try {
1240                List<ModelListener> listeners = new ArrayList<ModelListener>();
1241
1242                for (String listenerClassName : listenerClassNames) {
1243                    listeners.add((ModelListener)Class.forName(
1244                            listenerClassName).newInstance());
1245                }
1246
1247                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1248            }
1249            catch (Exception e) {
1250                _log.error(e);
1251            }
1252        }
1253    }
1254
1255    private static Log _log = LogFactory.getLog(ShoppingCartPersistenceImpl.class);
1256    private ModelListener[] _listeners = new ModelListener[0];
1257}