1   /**
2    * Copyright (c) 2000-2007 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.DynamicQuery;
27  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.kernel.util.StringMaker;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.service.persistence.BasePersistence;
32  import com.liferay.portal.spring.hibernate.FinderCache;
33  import com.liferay.portal.spring.hibernate.HibernateUtil;
34  
35  import com.liferay.portlet.shopping.NoSuchCartException;
36  import com.liferay.portlet.shopping.model.ShoppingCart;
37  import com.liferay.portlet.shopping.model.impl.ShoppingCartImpl;
38  
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Query;
45  import org.hibernate.Session;
46  
47  import java.util.Collections;
48  import java.util.Iterator;
49  import java.util.List;
50  
51  /**
52   * <a href="ShoppingCartPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class ShoppingCartPersistenceImpl extends BasePersistence
58      implements ShoppingCartPersistence {
59      public ShoppingCart create(long cartId) {
60          ShoppingCart shoppingCart = new ShoppingCartImpl();
61          shoppingCart.setNew(true);
62          shoppingCart.setPrimaryKey(cartId);
63  
64          return shoppingCart;
65      }
66  
67      public ShoppingCart remove(long cartId)
68          throws NoSuchCartException, SystemException {
69          Session session = null;
70  
71          try {
72              session = openSession();
73  
74              ShoppingCart shoppingCart = (ShoppingCart)session.get(ShoppingCartImpl.class,
75                      new Long(cartId));
76  
77              if (shoppingCart == null) {
78                  if (_log.isWarnEnabled()) {
79                      _log.warn("No ShoppingCart exists with the primary key " +
80                          cartId);
81                  }
82  
83                  throw new NoSuchCartException(
84                      "No ShoppingCart exists with the primary key " + cartId);
85              }
86  
87              return remove(shoppingCart);
88          }
89          catch (NoSuchCartException nsee) {
90              throw nsee;
91          }
92          catch (Exception e) {
93              throw HibernateUtil.processException(e);
94          }
95          finally {
96              closeSession(session);
97          }
98      }
99  
100     public ShoppingCart remove(ShoppingCart shoppingCart)
101         throws SystemException {
102         Session session = null;
103 
104         try {
105             session = openSession();
106             session.delete(shoppingCart);
107             session.flush();
108 
109             return shoppingCart;
110         }
111         catch (Exception e) {
112             throw HibernateUtil.processException(e);
113         }
114         finally {
115             closeSession(session);
116             FinderCache.clearCache(ShoppingCart.class.getName());
117         }
118     }
119 
120     public ShoppingCart update(
121         com.liferay.portlet.shopping.model.ShoppingCart shoppingCart)
122         throws SystemException {
123         return update(shoppingCart, false);
124     }
125 
126     public ShoppingCart update(
127         com.liferay.portlet.shopping.model.ShoppingCart shoppingCart,
128         boolean merge) throws SystemException {
129         Session session = null;
130 
131         try {
132             session = openSession();
133 
134             if (merge) {
135                 session.merge(shoppingCart);
136             }
137             else {
138                 if (shoppingCart.isNew()) {
139                     session.save(shoppingCart);
140                 }
141             }
142 
143             session.flush();
144             shoppingCart.setNew(false);
145 
146             return shoppingCart;
147         }
148         catch (Exception e) {
149             throw HibernateUtil.processException(e);
150         }
151         finally {
152             closeSession(session);
153             FinderCache.clearCache(ShoppingCart.class.getName());
154         }
155     }
156 
157     public ShoppingCart findByPrimaryKey(long cartId)
158         throws NoSuchCartException, SystemException {
159         ShoppingCart shoppingCart = fetchByPrimaryKey(cartId);
160 
161         if (shoppingCart == null) {
162             if (_log.isWarnEnabled()) {
163                 _log.warn("No ShoppingCart exists with the primary key " +
164                     cartId);
165             }
166 
167             throw new NoSuchCartException(
168                 "No ShoppingCart exists with the primary key " + cartId);
169         }
170 
171         return shoppingCart;
172     }
173 
174     public ShoppingCart fetchByPrimaryKey(long cartId)
175         throws SystemException {
176         Session session = null;
177 
178         try {
179             session = openSession();
180 
181             return (ShoppingCart)session.get(ShoppingCartImpl.class,
182                 new Long(cartId));
183         }
184         catch (Exception e) {
185             throw HibernateUtil.processException(e);
186         }
187         finally {
188             closeSession(session);
189         }
190     }
191 
192     public List findByGroupId(long groupId) throws SystemException {
193         String finderClassName = ShoppingCart.class.getName();
194         String finderMethodName = "findByGroupId";
195         String[] finderParams = new String[] { Long.class.getName() };
196         Object[] finderArgs = new Object[] { new Long(groupId) };
197         Object result = FinderCache.getResult(finderClassName,
198                 finderMethodName, finderParams, finderArgs, getSessionFactory());
199 
200         if (result == null) {
201             Session session = null;
202 
203             try {
204                 session = openSession();
205 
206                 StringMaker query = new StringMaker();
207                 query.append(
208                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
209                 query.append("groupId = ?");
210                 query.append(" ");
211 
212                 Query q = session.createQuery(query.toString());
213                 int queryPos = 0;
214                 q.setLong(queryPos++, groupId);
215 
216                 List list = q.list();
217                 FinderCache.putResult(finderClassName, finderMethodName,
218                     finderParams, finderArgs, list);
219 
220                 return list;
221             }
222             catch (Exception e) {
223                 throw HibernateUtil.processException(e);
224             }
225             finally {
226                 closeSession(session);
227             }
228         }
229         else {
230             return (List)result;
231         }
232     }
233 
234     public List findByGroupId(long groupId, int begin, int end)
235         throws SystemException {
236         return findByGroupId(groupId, begin, end, null);
237     }
238 
239     public List findByGroupId(long groupId, int begin, int end,
240         OrderByComparator obc) throws SystemException {
241         String finderClassName = ShoppingCart.class.getName();
242         String finderMethodName = "findByGroupId";
243         String[] finderParams = new String[] {
244                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
245                 "com.liferay.portal.kernel.util.OrderByComparator"
246             };
247         Object[] finderArgs = new Object[] {
248                 new Long(groupId), String.valueOf(begin), String.valueOf(end),
249                 String.valueOf(obc)
250             };
251         Object result = FinderCache.getResult(finderClassName,
252                 finderMethodName, finderParams, finderArgs, getSessionFactory());
253 
254         if (result == null) {
255             Session session = null;
256 
257             try {
258                 session = openSession();
259 
260                 StringMaker query = new StringMaker();
261                 query.append(
262                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
263                 query.append("groupId = ?");
264                 query.append(" ");
265 
266                 if (obc != null) {
267                     query.append("ORDER BY ");
268                     query.append(obc.getOrderBy());
269                 }
270 
271                 Query q = session.createQuery(query.toString());
272                 int queryPos = 0;
273                 q.setLong(queryPos++, groupId);
274 
275                 List list = QueryUtil.list(q, getDialect(), begin, end);
276                 FinderCache.putResult(finderClassName, finderMethodName,
277                     finderParams, finderArgs, list);
278 
279                 return list;
280             }
281             catch (Exception e) {
282                 throw HibernateUtil.processException(e);
283             }
284             finally {
285                 closeSession(session);
286             }
287         }
288         else {
289             return (List)result;
290         }
291     }
292 
293     public ShoppingCart findByGroupId_First(long groupId, OrderByComparator obc)
294         throws NoSuchCartException, SystemException {
295         List list = findByGroupId(groupId, 0, 1, obc);
296 
297         if (list.size() == 0) {
298             StringMaker msg = new StringMaker();
299             msg.append("No ShoppingCart exists with the key ");
300             msg.append(StringPool.OPEN_CURLY_BRACE);
301             msg.append("groupId=");
302             msg.append(groupId);
303             msg.append(StringPool.CLOSE_CURLY_BRACE);
304             throw new NoSuchCartException(msg.toString());
305         }
306         else {
307             return (ShoppingCart)list.get(0);
308         }
309     }
310 
311     public ShoppingCart findByGroupId_Last(long groupId, OrderByComparator obc)
312         throws NoSuchCartException, SystemException {
313         int count = countByGroupId(groupId);
314         List list = findByGroupId(groupId, count - 1, count, obc);
315 
316         if (list.size() == 0) {
317             StringMaker msg = new StringMaker();
318             msg.append("No ShoppingCart exists with the key ");
319             msg.append(StringPool.OPEN_CURLY_BRACE);
320             msg.append("groupId=");
321             msg.append(groupId);
322             msg.append(StringPool.CLOSE_CURLY_BRACE);
323             throw new NoSuchCartException(msg.toString());
324         }
325         else {
326             return (ShoppingCart)list.get(0);
327         }
328     }
329 
330     public ShoppingCart[] findByGroupId_PrevAndNext(long cartId, long groupId,
331         OrderByComparator obc) throws NoSuchCartException, SystemException {
332         ShoppingCart shoppingCart = findByPrimaryKey(cartId);
333         int count = countByGroupId(groupId);
334         Session session = null;
335 
336         try {
337             session = openSession();
338 
339             StringMaker query = new StringMaker();
340             query.append(
341                 "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
342             query.append("groupId = ?");
343             query.append(" ");
344 
345             if (obc != null) {
346                 query.append("ORDER BY ");
347                 query.append(obc.getOrderBy());
348             }
349 
350             Query q = session.createQuery(query.toString());
351             int queryPos = 0;
352             q.setLong(queryPos++, groupId);
353 
354             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
355                     shoppingCart);
356             ShoppingCart[] array = new ShoppingCartImpl[3];
357             array[0] = (ShoppingCart)objArray[0];
358             array[1] = (ShoppingCart)objArray[1];
359             array[2] = (ShoppingCart)objArray[2];
360 
361             return array;
362         }
363         catch (Exception e) {
364             throw HibernateUtil.processException(e);
365         }
366         finally {
367             closeSession(session);
368         }
369     }
370 
371     public List findByUserId(long userId) throws SystemException {
372         String finderClassName = ShoppingCart.class.getName();
373         String finderMethodName = "findByUserId";
374         String[] finderParams = new String[] { Long.class.getName() };
375         Object[] finderArgs = new Object[] { new Long(userId) };
376         Object result = FinderCache.getResult(finderClassName,
377                 finderMethodName, finderParams, finderArgs, getSessionFactory());
378 
379         if (result == null) {
380             Session session = null;
381 
382             try {
383                 session = openSession();
384 
385                 StringMaker query = new StringMaker();
386                 query.append(
387                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
388                 query.append("userId = ?");
389                 query.append(" ");
390 
391                 Query q = session.createQuery(query.toString());
392                 int queryPos = 0;
393                 q.setLong(queryPos++, userId);
394 
395                 List list = q.list();
396                 FinderCache.putResult(finderClassName, finderMethodName,
397                     finderParams, finderArgs, list);
398 
399                 return list;
400             }
401             catch (Exception e) {
402                 throw HibernateUtil.processException(e);
403             }
404             finally {
405                 closeSession(session);
406             }
407         }
408         else {
409             return (List)result;
410         }
411     }
412 
413     public List findByUserId(long userId, int begin, int end)
414         throws SystemException {
415         return findByUserId(userId, begin, end, null);
416     }
417 
418     public List findByUserId(long userId, int begin, int end,
419         OrderByComparator obc) throws SystemException {
420         String finderClassName = ShoppingCart.class.getName();
421         String finderMethodName = "findByUserId";
422         String[] finderParams = new String[] {
423                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
424                 "com.liferay.portal.kernel.util.OrderByComparator"
425             };
426         Object[] finderArgs = new Object[] {
427                 new Long(userId), String.valueOf(begin), String.valueOf(end),
428                 String.valueOf(obc)
429             };
430         Object result = FinderCache.getResult(finderClassName,
431                 finderMethodName, finderParams, finderArgs, getSessionFactory());
432 
433         if (result == null) {
434             Session session = null;
435 
436             try {
437                 session = openSession();
438 
439                 StringMaker query = new StringMaker();
440                 query.append(
441                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
442                 query.append("userId = ?");
443                 query.append(" ");
444 
445                 if (obc != null) {
446                     query.append("ORDER BY ");
447                     query.append(obc.getOrderBy());
448                 }
449 
450                 Query q = session.createQuery(query.toString());
451                 int queryPos = 0;
452                 q.setLong(queryPos++, userId);
453 
454                 List list = QueryUtil.list(q, getDialect(), begin, end);
455                 FinderCache.putResult(finderClassName, finderMethodName,
456                     finderParams, finderArgs, list);
457 
458                 return list;
459             }
460             catch (Exception e) {
461                 throw HibernateUtil.processException(e);
462             }
463             finally {
464                 closeSession(session);
465             }
466         }
467         else {
468             return (List)result;
469         }
470     }
471 
472     public ShoppingCart findByUserId_First(long userId, OrderByComparator obc)
473         throws NoSuchCartException, SystemException {
474         List list = findByUserId(userId, 0, 1, obc);
475 
476         if (list.size() == 0) {
477             StringMaker msg = new StringMaker();
478             msg.append("No ShoppingCart exists with the key ");
479             msg.append(StringPool.OPEN_CURLY_BRACE);
480             msg.append("userId=");
481             msg.append(userId);
482             msg.append(StringPool.CLOSE_CURLY_BRACE);
483             throw new NoSuchCartException(msg.toString());
484         }
485         else {
486             return (ShoppingCart)list.get(0);
487         }
488     }
489 
490     public ShoppingCart findByUserId_Last(long userId, OrderByComparator obc)
491         throws NoSuchCartException, SystemException {
492         int count = countByUserId(userId);
493         List list = findByUserId(userId, count - 1, count, obc);
494 
495         if (list.size() == 0) {
496             StringMaker msg = new StringMaker();
497             msg.append("No ShoppingCart exists with the key ");
498             msg.append(StringPool.OPEN_CURLY_BRACE);
499             msg.append("userId=");
500             msg.append(userId);
501             msg.append(StringPool.CLOSE_CURLY_BRACE);
502             throw new NoSuchCartException(msg.toString());
503         }
504         else {
505             return (ShoppingCart)list.get(0);
506         }
507     }
508 
509     public ShoppingCart[] findByUserId_PrevAndNext(long cartId, long userId,
510         OrderByComparator obc) throws NoSuchCartException, SystemException {
511         ShoppingCart shoppingCart = findByPrimaryKey(cartId);
512         int count = countByUserId(userId);
513         Session session = null;
514 
515         try {
516             session = openSession();
517 
518             StringMaker query = new StringMaker();
519             query.append(
520                 "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
521             query.append("userId = ?");
522             query.append(" ");
523 
524             if (obc != null) {
525                 query.append("ORDER BY ");
526                 query.append(obc.getOrderBy());
527             }
528 
529             Query q = session.createQuery(query.toString());
530             int queryPos = 0;
531             q.setLong(queryPos++, userId);
532 
533             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
534                     shoppingCart);
535             ShoppingCart[] array = new ShoppingCartImpl[3];
536             array[0] = (ShoppingCart)objArray[0];
537             array[1] = (ShoppingCart)objArray[1];
538             array[2] = (ShoppingCart)objArray[2];
539 
540             return array;
541         }
542         catch (Exception e) {
543             throw HibernateUtil.processException(e);
544         }
545         finally {
546             closeSession(session);
547         }
548     }
549 
550     public ShoppingCart findByG_U(long groupId, long userId)
551         throws NoSuchCartException, SystemException {
552         ShoppingCart shoppingCart = fetchByG_U(groupId, userId);
553 
554         if (shoppingCart == null) {
555             StringMaker msg = new StringMaker();
556             msg.append("No ShoppingCart exists with the key ");
557             msg.append(StringPool.OPEN_CURLY_BRACE);
558             msg.append("groupId=");
559             msg.append(groupId);
560             msg.append(", ");
561             msg.append("userId=");
562             msg.append(userId);
563             msg.append(StringPool.CLOSE_CURLY_BRACE);
564 
565             if (_log.isWarnEnabled()) {
566                 _log.warn(msg.toString());
567             }
568 
569             throw new NoSuchCartException(msg.toString());
570         }
571 
572         return shoppingCart;
573     }
574 
575     public ShoppingCart fetchByG_U(long groupId, long userId)
576         throws SystemException {
577         String finderClassName = ShoppingCart.class.getName();
578         String finderMethodName = "fetchByG_U";
579         String[] finderParams = new String[] {
580                 Long.class.getName(), Long.class.getName()
581             };
582         Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
583         Object result = FinderCache.getResult(finderClassName,
584                 finderMethodName, finderParams, finderArgs, getSessionFactory());
585 
586         if (result == null) {
587             Session session = null;
588 
589             try {
590                 session = openSession();
591 
592                 StringMaker query = new StringMaker();
593                 query.append(
594                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
595                 query.append("groupId = ?");
596                 query.append(" AND ");
597                 query.append("userId = ?");
598                 query.append(" ");
599 
600                 Query q = session.createQuery(query.toString());
601                 int queryPos = 0;
602                 q.setLong(queryPos++, groupId);
603                 q.setLong(queryPos++, userId);
604 
605                 List list = q.list();
606                 FinderCache.putResult(finderClassName, finderMethodName,
607                     finderParams, finderArgs, list);
608 
609                 if (list.size() == 0) {
610                     return null;
611                 }
612                 else {
613                     return (ShoppingCart)list.get(0);
614                 }
615             }
616             catch (Exception e) {
617                 throw HibernateUtil.processException(e);
618             }
619             finally {
620                 closeSession(session);
621             }
622         }
623         else {
624             List list = (List)result;
625 
626             if (list.size() == 0) {
627                 return null;
628             }
629             else {
630                 return (ShoppingCart)list.get(0);
631             }
632         }
633     }
634 
635     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
636         throws SystemException {
637         Session session = null;
638 
639         try {
640             session = openSession();
641 
642             DynamicQuery query = queryInitializer.initialize(session);
643 
644             return query.list();
645         }
646         catch (Exception e) {
647             throw HibernateUtil.processException(e);
648         }
649         finally {
650             closeSession(session);
651         }
652     }
653 
654     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
655         int begin, int end) throws SystemException {
656         Session session = null;
657 
658         try {
659             session = openSession();
660 
661             DynamicQuery query = queryInitializer.initialize(session);
662             query.setLimit(begin, end);
663 
664             return query.list();
665         }
666         catch (Exception e) {
667             throw HibernateUtil.processException(e);
668         }
669         finally {
670             closeSession(session);
671         }
672     }
673 
674     public List findAll() throws SystemException {
675         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
676     }
677 
678     public List findAll(int begin, int end) throws SystemException {
679         return findAll(begin, end, null);
680     }
681 
682     public List findAll(int begin, int end, OrderByComparator obc)
683         throws SystemException {
684         String finderClassName = ShoppingCart.class.getName();
685         String finderMethodName = "findAll";
686         String[] finderParams = new String[] {
687                 "java.lang.Integer", "java.lang.Integer",
688                 "com.liferay.portal.kernel.util.OrderByComparator"
689             };
690         Object[] finderArgs = new Object[] {
691                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
692             };
693         Object result = FinderCache.getResult(finderClassName,
694                 finderMethodName, finderParams, finderArgs, getSessionFactory());
695 
696         if (result == null) {
697             Session session = null;
698 
699             try {
700                 session = openSession();
701 
702                 StringMaker query = new StringMaker();
703                 query.append(
704                     "FROM com.liferay.portlet.shopping.model.ShoppingCart ");
705 
706                 if (obc != null) {
707                     query.append("ORDER BY ");
708                     query.append(obc.getOrderBy());
709                 }
710 
711                 Query q = session.createQuery(query.toString());
712                 List list = QueryUtil.list(q, getDialect(), begin, end);
713 
714                 if (obc == null) {
715                     Collections.sort(list);
716                 }
717 
718                 FinderCache.putResult(finderClassName, finderMethodName,
719                     finderParams, finderArgs, list);
720 
721                 return list;
722             }
723             catch (Exception e) {
724                 throw HibernateUtil.processException(e);
725             }
726             finally {
727                 closeSession(session);
728             }
729         }
730         else {
731             return (List)result;
732         }
733     }
734 
735     public void removeByGroupId(long groupId) throws SystemException {
736         Iterator itr = findByGroupId(groupId).iterator();
737 
738         while (itr.hasNext()) {
739             ShoppingCart shoppingCart = (ShoppingCart)itr.next();
740             remove(shoppingCart);
741         }
742     }
743 
744     public void removeByUserId(long userId) throws SystemException {
745         Iterator itr = findByUserId(userId).iterator();
746 
747         while (itr.hasNext()) {
748             ShoppingCart shoppingCart = (ShoppingCart)itr.next();
749             remove(shoppingCart);
750         }
751     }
752 
753     public void removeByG_U(long groupId, long userId)
754         throws NoSuchCartException, SystemException {
755         ShoppingCart shoppingCart = findByG_U(groupId, userId);
756         remove(shoppingCart);
757     }
758 
759     public void removeAll() throws SystemException {
760         Iterator itr = findAll().iterator();
761 
762         while (itr.hasNext()) {
763             remove((ShoppingCart)itr.next());
764         }
765     }
766 
767     public int countByGroupId(long groupId) throws SystemException {
768         String finderClassName = ShoppingCart.class.getName();
769         String finderMethodName = "countByGroupId";
770         String[] finderParams = new String[] { Long.class.getName() };
771         Object[] finderArgs = new Object[] { new Long(groupId) };
772         Object result = FinderCache.getResult(finderClassName,
773                 finderMethodName, finderParams, finderArgs, getSessionFactory());
774 
775         if (result == null) {
776             Session session = null;
777 
778             try {
779                 session = openSession();
780 
781                 StringMaker query = new StringMaker();
782                 query.append("SELECT COUNT(*) ");
783                 query.append(
784                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
785                 query.append("groupId = ?");
786                 query.append(" ");
787 
788                 Query q = session.createQuery(query.toString());
789                 int queryPos = 0;
790                 q.setLong(queryPos++, groupId);
791 
792                 Long count = null;
793                 Iterator itr = q.list().iterator();
794 
795                 if (itr.hasNext()) {
796                     count = (Long)itr.next();
797                 }
798 
799                 if (count == null) {
800                     count = new Long(0);
801                 }
802 
803                 FinderCache.putResult(finderClassName, finderMethodName,
804                     finderParams, finderArgs, count);
805 
806                 return count.intValue();
807             }
808             catch (Exception e) {
809                 throw HibernateUtil.processException(e);
810             }
811             finally {
812                 closeSession(session);
813             }
814         }
815         else {
816             return ((Long)result).intValue();
817         }
818     }
819 
820     public int countByUserId(long userId) throws SystemException {
821         String finderClassName = ShoppingCart.class.getName();
822         String finderMethodName = "countByUserId";
823         String[] finderParams = new String[] { Long.class.getName() };
824         Object[] finderArgs = new Object[] { new Long(userId) };
825         Object result = FinderCache.getResult(finderClassName,
826                 finderMethodName, finderParams, finderArgs, getSessionFactory());
827 
828         if (result == null) {
829             Session session = null;
830 
831             try {
832                 session = openSession();
833 
834                 StringMaker query = new StringMaker();
835                 query.append("SELECT COUNT(*) ");
836                 query.append(
837                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
838                 query.append("userId = ?");
839                 query.append(" ");
840 
841                 Query q = session.createQuery(query.toString());
842                 int queryPos = 0;
843                 q.setLong(queryPos++, userId);
844 
845                 Long count = null;
846                 Iterator itr = q.list().iterator();
847 
848                 if (itr.hasNext()) {
849                     count = (Long)itr.next();
850                 }
851 
852                 if (count == null) {
853                     count = new Long(0);
854                 }
855 
856                 FinderCache.putResult(finderClassName, finderMethodName,
857                     finderParams, finderArgs, count);
858 
859                 return count.intValue();
860             }
861             catch (Exception e) {
862                 throw HibernateUtil.processException(e);
863             }
864             finally {
865                 closeSession(session);
866             }
867         }
868         else {
869             return ((Long)result).intValue();
870         }
871     }
872 
873     public int countByG_U(long groupId, long userId) throws SystemException {
874         String finderClassName = ShoppingCart.class.getName();
875         String finderMethodName = "countByG_U";
876         String[] finderParams = new String[] {
877                 Long.class.getName(), Long.class.getName()
878             };
879         Object[] finderArgs = new Object[] { new Long(groupId), new Long(userId) };
880         Object result = FinderCache.getResult(finderClassName,
881                 finderMethodName, finderParams, finderArgs, getSessionFactory());
882 
883         if (result == null) {
884             Session session = null;
885 
886             try {
887                 session = openSession();
888 
889                 StringMaker query = new StringMaker();
890                 query.append("SELECT COUNT(*) ");
891                 query.append(
892                     "FROM com.liferay.portlet.shopping.model.ShoppingCart WHERE ");
893                 query.append("groupId = ?");
894                 query.append(" AND ");
895                 query.append("userId = ?");
896                 query.append(" ");
897 
898                 Query q = session.createQuery(query.toString());
899                 int queryPos = 0;
900                 q.setLong(queryPos++, groupId);
901                 q.setLong(queryPos++, userId);
902 
903                 Long count = null;
904                 Iterator itr = q.list().iterator();
905 
906                 if (itr.hasNext()) {
907                     count = (Long)itr.next();
908                 }
909 
910                 if (count == null) {
911                     count = new Long(0);
912                 }
913 
914                 FinderCache.putResult(finderClassName, finderMethodName,
915                     finderParams, finderArgs, count);
916 
917                 return count.intValue();
918             }
919             catch (Exception e) {
920                 throw HibernateUtil.processException(e);
921             }
922             finally {
923                 closeSession(session);
924             }
925         }
926         else {
927             return ((Long)result).intValue();
928         }
929     }
930 
931     public int countAll() throws SystemException {
932         String finderClassName = ShoppingCart.class.getName();
933         String finderMethodName = "countAll";
934         String[] finderParams = new String[] {  };
935         Object[] finderArgs = new Object[] {  };
936         Object result = FinderCache.getResult(finderClassName,
937                 finderMethodName, finderParams, finderArgs, getSessionFactory());
938 
939         if (result == null) {
940             Session session = null;
941 
942             try {
943                 session = openSession();
944 
945                 StringMaker query = new StringMaker();
946                 query.append("SELECT COUNT(*) ");
947                 query.append(
948                     "FROM com.liferay.portlet.shopping.model.ShoppingCart");
949 
950                 Query q = session.createQuery(query.toString());
951                 Long count = null;
952                 Iterator itr = q.list().iterator();
953 
954                 if (itr.hasNext()) {
955                     count = (Long)itr.next();
956                 }
957 
958                 if (count == null) {
959                     count = new Long(0);
960                 }
961 
962                 FinderCache.putResult(finderClassName, finderMethodName,
963                     finderParams, finderArgs, count);
964 
965                 return count.intValue();
966             }
967             catch (Exception e) {
968                 throw HibernateUtil.processException(e);
969             }
970             finally {
971                 closeSession(session);
972             }
973         }
974         else {
975             return ((Long)result).intValue();
976         }
977     }
978 
979     protected void initDao() {
980     }
981 
982     private static Log _log = LogFactory.getLog(ShoppingCartPersistenceImpl.class);
983 }